Skip to content

Commit 0cc5f9c

Browse files
committed
[AssetMapper] Moving the polyfill into importmap.php for convenience
1 parent 8ac7385 commit 0cc5f9c

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,8 @@ private function addAssetMapperSection(ArrayNodeDefinition $rootNode, callable $
924924
->defaultValue('%kernel.project_dir%/importmap.php')
925925
->end()
926926
->scalarNode('importmap_polyfill')
927-
->info('URL of the ES Module Polyfill to use, false to disable. Defaults to using a CDN URL.')
928-
->defaultValue(null)
927+
->info('The importmap name that will be used to load the polyfill. Set to false to disable.')
928+
->defaultValue('es-module-shims')
929929
->end()
930930
->arrayNode('importmap_script_attributes')
931931
->info('Key-value pair of attributes to add to script tags output for the importmap.')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use Symfony\Component\Asset\PackageInterface;
3434
use Symfony\Component\AssetMapper\AssetMapper;
3535
use Symfony\Component\AssetMapper\Compiler\AssetCompilerInterface;
36-
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
3736
use Symfony\Component\BrowserKit\AbstractBrowser;
3837
use Symfony\Component\Cache\Adapter\AdapterInterface;
3938
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -1378,7 +1377,7 @@ private function registerAssetMapperConfiguration(array $config, ContainerBuilde
13781377

13791378
$container
13801379
->getDefinition('asset_mapper.importmap.renderer')
1381-
->replaceArgument(3, $config['importmap_polyfill'] ?? ImportMapManager::POLYFILL_URL)
1380+
->replaceArgument(3, $config['importmap_polyfill'])
13821381
->replaceArgument(4, $config['importmap_script_attributes'])
13831382
;
13841383
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function testAssetMapperCanBeEnabled()
132132
'missing_import_mode' => 'warn',
133133
'extensions' => [],
134134
'importmap_path' => '%kernel.project_dir%/importmap.php',
135-
'importmap_polyfill' => null,
135+
'importmap_polyfill' => 'es-module-shims',
136136
'vendor_dir' => '%kernel.project_dir%/assets/vendor',
137137
'importmap_script_attributes' => [],
138138
];
@@ -668,7 +668,7 @@ protected static function getBundleDefaultConfig()
668668
'missing_import_mode' => 'warn',
669669
'extensions' => [],
670670
'importmap_path' => '%kernel.project_dir%/importmap.php',
671-
'importmap_polyfill' => null,
671+
'importmap_polyfill' => 'es-module-shims',
672672
'vendor_dir' => '%kernel.project_dir%/assets/vendor',
673673
'importmap_script_attributes' => [],
674674
],

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
class ImportMapManager
2626
{
27-
public const POLYFILL_URL = 'https://ga.jspm.io/npm:es-module-shims@1.7.2/dist/es-module-shims.js';
2827
public const IMPORT_MAP_CACHE_FILENAME = 'importmap.json';
2928
public const ENTRYPOINT_CACHE_FILENAME_PATTERN = 'entrypoint.%s.json';
3029

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525
private readonly ImportMapManager $importMapManager,
2626
private readonly ?Packages $assetPackages = null,
2727
private readonly string $charset = 'UTF-8',
28-
private readonly string|false $polyfillUrl = ImportMapManager::POLYFILL_URL,
28+
private readonly string|false $polyfillImportName = false,
2929
private readonly array $scriptAttributes = [],
3030
) {
3131
}
@@ -38,6 +38,7 @@ public function render(string|array $entryPoint, array $attributes = []): string
3838
$importMap = [];
3939
$modulePreloads = [];
4040
$cssLinks = [];
41+
$polyFillPath = null;
4142
foreach ($importMapData as $importName => $data) {
4243
$path = $data['path'];
4344

@@ -46,6 +47,12 @@ public function render(string|array $entryPoint, array $attributes = []): string
4647
$path = $this->assetPackages->getUrl(ltrim($path, '/'));
4748
}
4849

50+
// if this represents the polyfill, hide it from the import map
51+
if ($importName === $this->polyfillImportName) {
52+
$polyFillPath = $path;
53+
continue;
54+
}
55+
4956
$preload = $data['preload'] ?? false;
5057
if ('css' !== $data['type']) {
5158
$importMap[$importName] = $path;
@@ -76,8 +83,12 @@ public function render(string|array $entryPoint, array $attributes = []): string
7683
</script>
7784
HTML;
7885

79-
if ($this->polyfillUrl) {
80-
$url = $this->escapeAttributeValue($this->polyfillUrl);
86+
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));
88+
}
89+
90+
if ($polyFillPath) {
91+
$url = $this->escapeAttributeValue($polyFillPath);
8192

8293
$output .= <<<HTML
8394

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function testBasicRender()
4747
'path' => 'https://cdn.example.com/assets/remote-d1g35t.js',
4848
'type' => 'js',
4949
],
50+
'es-module-shim' => [
51+
'path' => 'https://ga.jspm.io/npm:es-module-shims',
52+
'type' => 'js',
53+
],
5054
]);
5155

5256
$assetPackages = $this->createMock(Packages::class);
@@ -61,11 +65,14 @@ public function testBasicRender()
6165
return '/subdirectory/'.$path;
6266
});
6367

64-
$renderer = new ImportMapRenderer($importMapManager, $assetPackages);
68+
$renderer = new ImportMapRenderer($importMapManager, $assetPackages, polyfillImportName: 'es-module-shim');
6569
$html = $renderer->render(['app']);
6670

6771
$this->assertStringContainsString('<script type="importmap">', $html);
68-
$this->assertStringContainsString('https://ga.jspm.io/npm:es-module-shims', $html);
72+
// polyfill is rendered as a normal script tag
73+
$this->assertStringContainsString('<script async src="https://ga.jspm.io/npm:es-module-shims"></script>', $html);
74+
// and is hidden from the import map
75+
$this->assertStringNotContainsString('"es-module-shim"', $html);
6976
$this->assertStringContainsString('import \'app\';', $html);
7077

7178
// preloaded js file

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy