Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
[AssetMapper] Adding import-parsing case where import contains a path
  • Loading branch information
weaverryan committed Oct 23, 2023
commit b9e9dce7be642be110ea021a1de7b71445bd638a
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '/[email protected]/+esm',
'body' => 'import e from"/npm/[email protected]/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'),
Expand Down Expand Up @@ -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]);
}

Expand Down Expand Up @@ -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/[email protected]/php/strings/sprintf/+esm";import t from"/npm/[email protected]/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
Expand Down