@@ -30,6 +30,8 @@ final class JsDelivrEsmResolver implements PackageResolverInterface
30
30
31
31
public const IMPORT_REGEX = '#(?:import\s*(?:\w+,)?(?:(?:\{[^}]*\}|\w+|\*\s*as\s+\w+)\s*\bfrom\s*)?|export\s*(?:\{[^}]*\}|\*)\s*from\s*)("/npm/((?:@[^/]+/)?[^@]+?)(?:@([^/]+))?((?:/[^/]+)*?)/\+esm")# ' ;
32
32
33
+ public const ES_MODULE_SHIMS = 'es-module-shims ' ;
34
+
33
35
private HttpClientInterface $ httpClient ;
34
36
35
37
public function __construct (
@@ -78,7 +80,7 @@ public function resolvePackages(array $packagesToRequire): array
78
80
throw new RuntimeException (sprintf ('Unable to find the latest version for package "%s" - try specifying the version manually. ' , $ packageName ));
79
81
}
80
82
81
- $ pattern = str_ends_with ( $ filePath , ' .css ' ) ? self :: URL_PATTERN_DIST_CSS : self :: URL_PATTERN_DIST ;
83
+ $ pattern = $ this -> resolveUrlPattern ( $ packageName , $ filePath ) ;
82
84
$ requiredPackages [$ i ][1 ] = $ this ->httpClient ->request ('GET ' , sprintf ($ pattern , $ packageName , $ version , $ filePath ));
83
85
$ requiredPackages [$ i ][4 ] = $ version ;
84
86
@@ -169,7 +171,11 @@ public function downloadPackages(array $importMapEntries, callable $progressCall
169
171
throw new \InvalidArgumentException (sprintf ('The entry "%s" is not a remote package. ' , $ entry ->importName ));
170
172
}
171
173
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
+ );
173
179
$ url = sprintf ($ pattern , $ entry ->getPackageName (), $ entry ->version , $ entry ->getPackagePathString ());
174
180
175
181
$ responses [$ package ] = [$ this ->httpClient ->request ('GET ' , $ url ), $ entry ];
@@ -326,4 +332,22 @@ private function makeImportsBare(string $content, array &$dependencies, array &$
326
332
327
333
return preg_replace ('{/\*# sourceMappingURL=[^ ]*+ \*/} ' , '' , $ content );
328
334
}
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.
343
+ if (self ::ES_MODULE_SHIMS === $ packageName ) {
344
+ return self ::URL_PATTERN_DIST_CSS ;
345
+ }
346
+
347
+ if (str_ends_with ($ path , '.css ' ) || ($ type && ImportMapType::CSS === $ type )) {
348
+ return self ::URL_PATTERN_DIST_CSS ;
349
+ }
350
+
351
+ return self ::URL_PATTERN_DIST ;
352
+ }
329
353
}
0 commit comments