8000 Rendering default polyfill if es-module-shims requested by not in imp… · symfony/symfony@0fd142f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fd142f

Browse files
committed
Rendering default polyfill if es-module-shims requested by not in importmap
1 parent cdd71d6 commit 0fd142f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Symfony/Component/AssetMapper/ImportMap/ImportMapRenderer.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
class ImportMapRenderer
2323
{
24+
private const DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL = 'https://ga.jspm.io/npm:es-module-shims@1.8.0/dist/es-module-shims.js';
25+
2426
public function __construct(
2527
private readonly ImportMapManager $importMapManager,
2628
private readonly ?Packages $assetPackages = null,
@@ -84,7 +86,12 @@ public function render(string|array $entryPoint, array $attributes = []): string
8486
HTML;
8587

8688
if (false !== $this->polyfillImportName && null === $polyFillPath) {
87-
throw new \InvalidArgumentException(sprintf('The JavaScript module polyfill was not found in your import map. Either disable the polyfill or run "php bin/console importmap:require "%s"" to install it.', $this->polyfillImportName));
89+
if ('es-module-shims' !== $this->polyfillImportName) {
90+
throw new \InvalidArgumentException(sprintf('The JavaScript module polyfill was not found in your import map. Either disable the polyfill or run "php bin/console importmap:require "%s"" to install it.', $this->polyfillImportName));
91+
}
92+
93+
// a fallback for the default polyfill in case it's not in the importmap
94+
$polyFillPath = self::DEFAULT_ES_MODULE_SHIMS_POLYFILL_URL;
8895
}
8996

9097
if ($polyFillPath) {

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapRendererTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,23 @@ public function testNoPolyfill()
9797
$this->assertStringNotContainsString('https://ga.jspm.io/npm:es-module-shims', $renderer->render([]));
9898
}
9999

100+
public function testDefaultPolyfillUsedIfNotInImportmap()
101+
{
102+
$importMapManager = $this->createMock(ImportMapManager::class);
103+
$importMapManager->expects($this->once())
104+
->method('getImportMapData')
105+
->with(['app'])
106+
->willReturn([]);
107+
108+
$renderer = new ImportMapRenderer(
109+
$importMapManager,
110+
$this->createMock(Packages::class),
111+
polyfillImportName: 'es-module-shims',
112+
);
113+
$html = $renderer->render(['app']);
114+
$this->assertStringContainsString('<script async src="https://ga.jspm.io/npm:es-module-shims@', $html);
115+
}
116+
100117
public function testCustomScriptAttributes()
101118
{
102119
$renderer = new ImportMapRenderer($this->createBasicImportMapManager(), null, 'UTF-8', 'es-module-shims', [

0 commit comments

Comments
 (0)
0