Skip to content

Commit 5534989

Browse files
committed
bug symfony#53003 [AssetMapper] Fix URL pattern when importing es-module-shims (hashbanged)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [AssetMapper] Fix URL pattern when importing es-module-shims This fixes a JS console syntax error when importing the `es-module-shims` polyfills package | Q | A | ------------- | --- | Branch? | 6.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix symfony#52783 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT Requiring `es-module-shims` using AssetMapper causes a trailing `export {t as default};` in the downloaded file. The recommendation in symfony#52783 is to be explicit about checking if the package is the shims/polyfill, and, if so, to use the CSS URL pattern when building the HTTP request for the package. This PR adds a new method for handling this case, and it centralizes the logic for determining if the URL pattern should be `URL_PATTERN_DIST`|`URL_PATTERN_DIST_CSS` <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 1472137 [AssetMapper] Fix URL pattern when importing es-module-shims
2 parents 024ad06 + 1472137 commit 5534989

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class JsDelivrEsmResolver implements PackageResolverInterface
3030

3131
public const IMPORT_REGEX = '#(?:import\s*(?:\w+,)?(?:(?:\{[^}]*\}|\w+|\*\s*as\s+\w+)\s*\bfrom\s*)?|export\s*(?:\{[^}]*\}|\*)\s*from\s*)("/npm/((?:@[^/]+/)?[^@]+?)(?:@([^/]+))?((?:/[^/]+)*?)/\+esm")#';
3232

33+
private const ES_MODULE_SHIMS = 'es-module-shims';
34+
3335
private HttpClientInterface $httpClient;
3436

3537
public function __construct(
@@ -78,7 +80,7 @@ public function resolvePackages(array $packagesToRequire): array
7880
throw new RuntimeException(sprintf('Unable to find the latest version for package "%s" - try specifying the version manually.', $packageName));
7981
}
8082

81-
$pattern = str_ends_with($filePath, '.css') ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST;
83+
$pattern = $this->resolveUrlPattern($packageName, $filePath);
8284
$requiredPackages[$i][1] = $this->httpClient->request('GET', sprintf($pattern, $packageName, $version, $filePath));
8385
$requiredPackages[$i][4] = $version;
8486

@@ -169,7 +171,11 @@ public function downloadPackages(array $importMapEntries, callable $progressCall
169171
throw new \InvalidArgumentException(sprintf('The entry "%s" is not a remote package.', $entry->importName));
170172
}
171173

172-
$pattern = ImportMapType::CSS === $entry->type ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST;
174+
$pattern = $this->resolveUrlPattern(
175+
$entry->getPackageName(),
176+
$entry->getPackagePathString(),
177+
$entry->type,
178+
);
173179
$url = sprintf($pattern, $entry->getPackageName(), $entry->version, $entry->getPackagePathString());
174180

175181
$responses[$package] = [$this->httpClient->request('GET', $url), $entry];
@@ -326,4 +332,19 @@ private function makeImportsBare(string $content, array &$dependencies, array &$
326332

327333
return preg_replace('{/\*# sourceMappingURL=[^ ]*+ \*/}', '', $content);
328334
}
335+
336+
/**
337+
* Determine the URL pattern to be used by the HTTP Client.
338+
*/
339+
private function resolveUrlPattern(string $packageName, string $path, ImportMapType $type = null): string
340+
{
341+
// The URL for the es-module-shims polyfill package uses the CSS pattern to
342+
// prevent a syntax error in the browser console, so check the package name
343+
// as part of the condition.
344+
if (self::ES_MODULE_SHIMS === $packageName || str_ends_with($path, '.css') || ImportMapType::CSS === $type) {
345+
return self::URL_PATTERN_DIST_CSS;
346+
}
347+
348+
return self::URL_PATTERN_DIST;
349+
}
329350
}

src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,17 @@ public static function provideDownloadPackagesTests()
439439
],
440440
[
441441
[
442-
'url' => '/es-module-shims@1.8.2/+esm',
442+
'url' => '/es-module-shims@1.8.2',
443443
'body' => <<<'EOF'
444-
const je="\n//# sourceURL=",Ue="\n//# sourceMappingURL=",Me=/^(text|application)\/(x-)?javascript(;|$)/,_e=/^(application)\/wasm(;|$)/,Ie=/^(text|application)\/json(;|$)/,Re=/^(text|application)\/css(;|$)/,Te=/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;export{t as default};
444+
const je="\n//# sourceURL=",Ue="\n//# sourceMappingURL=",Me=/^(text|application)\/(x-)?javascript(;|$)/,_e=/^(application)\/wasm(;|$)/,Ie=/^(text|application)\/json(;|$)/,Re=/^(text|application)\/css(;|$)/,Te=/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;
445445
//# sourceMappingURL=/sm/ef3916de598f421a779ba0e69af94655b2043095cde2410cc01893452d893338.map
446446
EOF
447447
],
448448
],
449449
[
450450
'es-module-shims' => [
451451
'content' => <<<'EOF'
452-
const je="\n//# sourceURL=",Ue="\n//# sourceMappingURL=",Me=/^(text|application)\/(x-)?javascript(;|$)/,_e=/^(application)\/wasm(;|$)/,Ie=/^(text|application)\/json(;|$)/,Re=/^(text|application)\/css(;|$)/,Te=/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;export{t as default};
452+
const je="\n//# sourceURL=",Ue="\n//# sourceMappingURL=",Me=/^(text|application)\/(x-)?javascript(;|$)/,_e=/^(application)\/wasm(;|$)/,Ie=/^(text|application)\/json(;|$)/,Re=/^(text|application)\/css(;|$)/,Te=/url\(\s*(?:(["'])((?:\\.|[^\n\\"'])+)\1|((?:\\.|[^\s,"'()\\])+))\s*\)/g;
453453
EOF,
454454
'dependencies' => [],
455455
'extraFiles' => [],

0 commit comments

Comments
 (0)