8000 [AssetMapper] Moving the polyfill into importmap.php for convenience · symfony/symfony@311a2fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 311a2fc

Browse files
committed
[AssetMapper] Moving the polyfill into importmap.php for convenience
1 parent fa651b1 commit 311a2fc

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

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

+2-2
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

+1-2
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/Component/AssetMapper/ImportMap/ImportMapManager.php

-1
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

+14-3
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< 8000 /td>+
$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

0 commit comments

Comments
 (0)
0