diff --git a/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php b/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php index 032ec892c9d0c..dc60593067f28 100644 --- a/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php +++ b/src/Symfony/Component/AssetMapper/ImportMap/Resolver/JsDelivrEsmResolver.php @@ -26,7 +26,7 @@ final class JsDelivrEsmResolver implements PackageResolverInterface public const URL_PATTERN_DIST = self::URL_PATTERN_DIST_CSS.'/+esm'; public const URL_PATTERN_ENTRYPOINT = 'https://data.jsdelivr.com/v1/packages/npm/%s@%s/entrypoints'; - public const IMPORT_REGEX = '{from"/npm/((?:@[^/]+/)?[^@]+)@([^/]+)/\+esm"}'; + public const IMPORT_REGEX = '{from"/npm/((?:@[^/]+/)?[^@]+)@([^/]+)((?:/[^/]+)*?)/\+esm"}'; private HttpClientInterface $httpClient; @@ -223,6 +223,7 @@ private function fetchPackageRequirementsFromImports(string $content): array $dependencies = []; foreach ($matches[1] as $index => $packageName) { $version = $matches[2][$index]; + $packageName .= $matches[3][$index]; // add the path if any $dependencies[] = new PackageRequireOptions($packageName, $version); } @@ -238,7 +239,7 @@ private function fetchPackageRequirementsFromImports(string $content): array private function makeImportsBare(string $content, array &$dependencies): string { $content = preg_replace_callback(self::IMPORT_REGEX, function ($matches) use (&$dependencies) { - $packageName = $matches[1]; + $packageName = $matches[1].$matches[3]; // add the path if any $dependencies[] = $packageName; return sprintf('from"%s"', $packageName); diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php index 204a971d9fde1..14ec9f14fcd10 100644 --- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php +++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/Resolver/JsDelivrEsmResolverTest.php @@ -393,6 +393,24 @@ public static function provideDownloadPackagesTests() ], ]; + yield 'make imports point to file and relative' => [ + [ + 'twig' => self::createRemoteEntry('twig', version: '1.16.0'), + ], + [ + [ + 'url' => '/twig@1.16.0/+esm', + 'body' => 'import e from"/npm/locutus@2.0.16/php/strings/sprintf/+esm";console.log()', + ], + ], + [ + 'twig' => [ + 'content' => 'import e from"locutus/php/strings/sprintf";console.log()', + 'dependencies' => ['locutus/php/strings/sprintf'], + ], + ], + ]; + yield 'js sourcemap is removed' => [ [ '@chart.js/auto' => self::createRemoteEntry('chart.js/auto', version: '1.2.3'), @@ -444,7 +462,12 @@ public function testImportRegex(string $subject, array $expectedPackages) $expectedNames[] = $packageData[0]; $expectedVersions[] = $packageData[1]; } - $this->assertSame($expectedNames, $matches[1]); + $actualNames = []; + foreach ($matches[1] as $i => $name) { + $actualNames[] = $name.$matches[3][$i]; + } + + $this->assertSame($expectedNames, $actualNames); $this->assertSame($expectedVersions, $matches[2]); } @@ -482,6 +505,14 @@ public static function provideImportRegex(): iterable ['datatables.net', '2.1.1'], // for the export syntax ], ]; + + yield 'import statements with paths' => [ + 'import e from"/npm/locutus@2.0.16/php/strings/sprintf/+esm";import t from"/npm/locutus@2.0.16/php/strings/vsprintf/+esm"', + [ + ['locutus/php/strings/sprintf', '2.0.16'], + ['locutus/php/strings/vsprintf', '2.0.16'], + ], + ]; } private static function createRemoteEntry(string $importName, string $version, ImportMapType $type = ImportMapType::JS, string $packageSpecifier = null): ImportMapEntry