diff --git a/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php b/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php index b8d5b14d0eee5..082e790be399f 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php @@ -30,6 +30,8 @@ final class JsDelivrEsmResolver implements PackageResolverInterface public const IMPORT_REGEX = '#(?:import\s*(?:\w+,)?(?:(?:\{[^}]*\}|\w+|\*\s*as\s+\w+)\s*\bfrom\s*)?|export\s*(?:\{[^}]*\}|\*)\s*from\s*)("/npm/((?:@[^/]+/)?[^@]+?)(?:@([^/]+))?((?:/[^/]+)*?)/\+esm")#'; + private const ES_MODULE_SHIMS = 'es-module-shims'; + private HttpClientInterface $httpClient; public function __construct( @@ -78,7 +80,7 @@ public function resolvePackages(array $packagesToRequire): array throw new RuntimeException(sprintf('Unable to find the latest version for package "%s" - try specifying the version manually.', $packageName)); } - $pattern = str_ends_with($filePath, '.css') ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST; + $pattern = $this->resolveUrlPattern($packageName, $filePath); $requiredPackages[$i][1] = $this->httpClient->request('GET', sprintf($pattern, $packageName, $version, $filePath)); $requiredPackages[$i][4] = $version; @@ -169,7 +171,11 @@ public function downloadPackages(array $importMapEntries, callable $progressCall throw new \InvalidArgumentException(sprintf('The entry "%s" is not a remote package.', $entry->importName)); } - $pattern = ImportMapType::CSS === $entry->type ? self::URL_PATTERN_DIST_CSS : self::URL_PATTERN_DIST; + $pattern = $this->resolveUrlPattern( + $entry->getPackageName(), + $entry->getPackagePathString(), + $entry->type, + ); $url = sprintf($pattern, $entry->getPackageName(), $entry->version, $entry->getPackagePathString()); $responses[$package] = [$this->httpClient->request('GET', $url), $entry]; @@ -326,4 +332,19 @@ private function makeImportsBare(string $content, array &$dependencies, array &$ return preg_replace('{/\*# sourceMappingURL=[^ ]*+ \*/}', '', $content); } + + /** + * Determine the URL pattern to be used by the HTTP Client. + */ + private function resolveUrlPattern(string $packageName, string $path, ImportMapType $type = null): string + { + // The URL for the es-module-shims polyfill package uses the CSS pattern to + // prevent a syntax error in the browser console, so check the package name + // as part of the condition. + if (self::ES_MODULE_SHIMS === $packageName || str_ends_with($path, '.css') || ImportMapType::CSS === $type) { + return self::URL_PATTERN_DIST_CSS; + } + + return self::URL_PATTERN_DIST; + } } diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php index c47cb4890ad24..50eb55228b338 100644 --- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php @@ -439,9 +439,9 @@ public static function provideDownloadPackagesTests() ], [ [ - 'url' => '/es-module-shims@1.8.2/+esm', + 'url' => '/es-module-shims@1.8.2', 'body' => <<<'EOF' -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}; +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; //# sourceMappingURL=/sm/ef3916de598f421a779ba0e69af94655b2043095cde2410cc01893452d893338.map EOF ], @@ -449,7 +449,7 @@ public static function provideDownloadPackagesTests() [ 'es-module-shims' => [ 'content' => <<<'EOF' -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}; +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; EOF, 'dependencies' => [], 'extraFiles' => [], 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