8000 bug #52508 [AssetMapper] Fix jsdelivr import parsing with no imported… · jschaedl/symfony@3128c60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3128c60

Browse files
bug symfony#52508 [AssetMapper] Fix jsdelivr import parsing with no imported value (weaverryan)
This PR was merged into the 6.4 branch. Discussion ---------- [AssetMapper] Fix jsdelivr import parsing with no imported value | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#52467 | License | MIT Another new import syntax found by `@tacman`! I think this one is kind of a bug in the library - a module environment should not `import 'foo'` (i.e. import a module and not actually use any value), but our job is just to find these, not judge them ;). FYI - I have an issue on jsdelivr proposing that they create an API endpoint to expose this info, so we don't need to parse it. They agree and already were thinking about this - jsdelivr/jsdelivr#18538 - so this may be something helpful in the future. Cheers! Commits ------- dc1b27d [AssetMapper] Adding import regex support for not importing any value
2 parents 278b203 + dc1b27d commit 3128c60

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class JsDelivrEsmResolver implements PackageResolverInterface
2626
public const URL_PATTERN_DIST = self::URL_PATTERN_DIST_CSS.'/+esm';
2727
public const URL_PATTERN_ENTRYPOINT = 'https://data.jsdelivr.com/v1/packages/npm/%s@%s/entrypoints';
2828

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

3131
private HttpClientInterface $httpClient;
3232

@@ -221,9 +221,9 @@ private function fetchPackageRequirementsFromImports(string $content): array
221221
// imports from jsdelivr follow a predictable format
222222
preg_match_all(self::IMPORT_REGEX, $content, $matches);
223223
$dependencies = [];
224-
foreach ($matches[1] as $index => $packageName) {
225-
$version = $matches[2][$index] ?: null;
226-
$packageName .= $matches[3][$index]; // add the path if any
224+
foreach ($matches[2] as $index => $packageName) {
225+
$version = $matches[3][$index] ?: null;
226+
$packageName .= $matches[4][$index]; // add the path if any
227227

228228
$dependencies[] = new PackageRequireOptions($packageName, $version);
229229
}
@@ -239,10 +239,11 @@ private function fetchPackageRequirementsFromImports(string $content): array
239239
private function makeImportsBare(string $content, array &$dependencies): string
240240
{
241241
$content = preg_replace_callback(self::IMPORT_REGEX, function ($matches) use (&$dependencies) {
242-
$packageName = $matches[1].$matches[3]; // add the path if any
242+
$packageName = $matches[2].$matches[4]; // add the path if any
243243
$dependencies[] = $packageName;
244244

245-
return sprintf('from"%s"', $packageName);
245+
// replace the "/npm/package@version/+esm" with "package@version"
246+
return str_replace($matches[1], sprintf('"%s"', $packageName), $matches[0]);
246247
}, $content);
247248

248249
// source maps are not also downloaded - so remove the sourceMappingURL

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,12 @@ public function testImportRegex(string $subject, array $expectedPackages)
463463
$expectedVersions[] = $packageData[1];
464464
}
465465
$actualNames = [];
466-
foreach ($matches[1] as $i => $name) {
467-
$actualNames[] = $name.$matches[3][$i];
466+
foreach ($matches[2] as $i => $name) {
467+
$actualNames[] = $name.$matches[4][$i];
468468
}
469469

470470
$this->assertSame($expectedNames, $actualNames);
471-
$this->assertSame($expectedVersions, $matches[2]);
471+
$this->assertSame($expectedVersions, $matches[3]);
472472
}
473473

474474
public static function provideImportRegex(): iterable
@@ -529,6 +529,26 @@ public static function provideImportRegex(): iterable
529529
['prosemirror-state/php/strings/sprintf', '1.4.3'],
530530
],
531531
];
532+
533+
yield 'import without importing a value' => [
534+
'import "/npm/jquery@3.7.1/+esm";',
535+
[
536+
['jquery', '3.7.1'],
537+
],
538+
];
539+
540+
yield 'multiple imports and exports with and without values' => [
541+
'import"/npm/jquery@3.7.1/+esm";import e from"/npm/datatables.net-bs5@1.13.7/+esm";export{default}from"/npm/datatables.net-bs5@1.13.7/+esm";import"/npm/datatables.net-select@1.7.0/+esm";
542+
/*! Bootstrap 5 styling wrapper for Select
543+
* © SpryMedia Ltd - datatables.net/license
544+
*/',
545+
[
546+
['jquery', '3.7.1'],
547+
['datatables.net-bs5', '1.13.7'],
548+
['datatables.net-bs5', '1.13.7'],
549+
['datatables.net-select', '1.7.0'],
550+
],
551+
];
532552
}
533553

534554
private static function createRemoteEntry(string $importName, string $version, ImportMapType $type = ImportMapType::JS, string $packageSpecifier = null): ImportMapEntry

0 commit comments

Comments
 (0)
0