Skip to content

[AssetMapper] Fix URL pattern when importing es-module-shims #53003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,17 @@ 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
],
],
[
'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' => [],
Expand Down