Skip to content

[AssetMapper] Add "=alias" syntax to importmap:require #50445

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
May 26, 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 @@ -56,6 +56,14 @@ protected function configure(): void
<info>php %command.full_name% lodash --preload</info>
<info>php %command.full_name% "lodash@^4.15"</info>

You can also require specific paths of a package:

<info>php %command.full_name% "chart.js/auto"</info>

Or download one package/file, but alias its name in your import map:

<info>php %command.full_name% "vue/dist/vue.esm-bundler.js=vue"</info>

The <info>preload</info> option will set the <info>preload</info> option in the importmap,
which will tell the browser to preload the package. This should be used for all
critical packages that are needed on page load.
Expand Down Expand Up @@ -114,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$parts['version'] ?? null,
$input->getOption('download'),
$input->getOption('preload'),
null,
$parts['alias'] ?? $parts['package'],
isset($parts['registry']) && $parts['registry'] ? $parts['registry'] : null,
$path,
);
Expand Down
14 changes: 11 additions & 3 deletions src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,18 @@ public function update(): array
*/
public static function parsePackageName(string $packageName): ?array
{
// https://regex101.com/r/d99BEc/1
$regex = '/(?:(?P<registry>[^:\n]+):)?((?P<package>@?[^@\n]+))(?:@(?P<version>[^\s\n]+))?/';
// https://regex101.com/r/MDz0bN/1
$regex = '/(?:(?P<registry>[^:\n]+):)?((?P<package>@?[^=@\n]+))(?:@(?P<version>[^=\s\n]+))?(?:=(?P<alias>[^\s\n]+))?/';

return preg_match($regex, $packageName, $matches) ? $matches : null;
if (!preg_match($regex, $packageName, $matches)) {
return null;
}

if (isset($matches['version']) && '' === $matches['version']) {
unset($matches['version']);
}

return $matches;
}

private function buildImportMapJson(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,17 @@ public function testUpdate()
public function testParsePackageName(string $packageName, array $expectedReturn)
{
$parsed = ImportMapManager::parsePackageName($packageName);
// remove integer keys - they're noise
$this->assertIsArray($parsed);

if (\is_array($parsed)) {
$parsed = array_filter($parsed, function ($key) {
return !\is_int($key);
}, \ARRAY_FILTER_USE_KEY);
}
// remove integer keys - they're noise
$parsed = array_filter($parsed, fn ($key) => !\is_int($key), \ARRAY_FILTER_USE_KEY);
$this->assertEquals($expectedReturn, $parsed);

$parsedWithAlias = ImportMapManager::parsePackageName($packageName.'=some_alias');
$this->assertIsArray($parsedWithAlias);
$parsedWithAlias = array_filter($parsedWithAlias, fn ($key) => !\is_int($key), \ARRAY_FILTER_USE_KEY);
$expectedReturnWithAlias = $expectedReturn + ['alias' => 'some_alias'];
$this->assertEquals($expectedReturnWithAlias, $parsedWithAlias, 'Asserting with alias');
}

public static function getPackageNameTests(): iterable
Expand Down Expand Up @@ -442,7 +445,7 @@ public static function getPackageNameTests(): iterable
],
];

yield 'namespaced_package_with_registry' => [
yield 'namespaced_package_with_registry_no_version' => [
'npm:@hotwired/stimulus',
[
'package' => '@hotwired/stimulus',
Expand Down