Skip to content

Commit 27ecdb4

Browse files
committed
[AssetMapper] Adding an "alias" syntax to importmap:require
1 parent 68f8ad9 commit 27ecdb4

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ protected function configure(): void
5656
<info>php %command.full_name% lodash --preload</info>
5757
<info>php %command.full_name% "lodash@^4.15"</info>
5858
59+
You can also require specific paths of a package:
60+
61+
<info>php %command.full_name% "chart.js/auto"</info>
62+
63+
Or download one package/file, but alias its name in your import map:
64+
65+
<info>php %command.full_name% "vue/dist/vue.esm-bundler.js=vue"</info>
66+
5967
The <info>preload</info> option will set the <info>preload</info> option in the importmap,
6068
which will tell the browser to preload the package. This should be used for all
6169
critical packages that are needed on page load.
@@ -114,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114122
$parts['version'] ?? null,
115123
$input->getOption('download'),
116124
$input->getOption('preload'),
117-
null,
125+
$parts['alias'] ?? $parts['package'],
118126
isset($parts['registry']) && $parts['registry'] ? $parts['registry'] : null,
119127
$path,
120128
);

src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,18 @@ public function update(): array
115115
*/
116116
public static function parsePackageName(string $packageName): ?array
117117
{
118-
// https://regex101.com/r/d99BEc/1
119-
$regex = '/(?:(?P<registry>[^:\n]+):)?((?P<package>@?[^@\n]+))(?:@(?P<version>[^\s\n]+))?/';
118+
// https://regex101.com/r/MDz0bN/1
119+
$regex = '/(?:(?P<registry>[^:\n]+):)?((?P<package>@?[^=@\n]+))(?:@(?P<version>[^=\s\n]+))?(?:=(?P<alias>[^\s\n]+))?/';
120120

121-
return preg_match($regex, $packageName, $matches) ? $matches : null;
121+
if (!preg_match($regex, $packageName, $matches)) {
122+
return null;
123+
}
124+
125+
if (isset($matches['version']) && '' === $matches['version']) {
126+
unset($matches['version']);
127+
}
128+
129+
return $matches;
122130
}
123131

124132
private function buildImportMapJson(): void

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,17 @@ public function testUpdate()
379379
public function testParsePackageName(string $packageName, array $expectedReturn)
380380
{
381381
$parsed = ImportMapManager::parsePackageName($packageName);
382-
// remove integer keys - they're noise
382+
$this->assertIsArray($parsed);
383383

384-
if (\is_array($parsed)) {
385-
$parsed = array_filter($parsed, function ($key) {
386-
return !\is_int($key);
387-
}, \ARRAY_FILTER_USE_KEY);
388-
}
384+
// remove integer keys - they're noise
385+
$parsed = array_filter($parsed, fn ($key) => !\is_int($key), \ARRAY_FILTER_USE_KEY);
389386
$this->assertEquals($expectedReturn, $parsed);
387+
388+
$parsedWithAlias = ImportMapManager::parsePackageName($packageName.'=some_alias');
389+
$this->assertIsArray($parsedWithAlias);
390+
$parsedWithAlias = array_filter($parsedWithAlias, fn ($key) => !\is_int($key), \ARRAY_FILTER_USE_KEY);
391+
$expectedReturnWithAlias = $expectedReturn + ['alias' => 'some_alias'];
392+
$this->assertEquals($expectedReturnWithAlias, $parsedWithAlias, 'Asserting with alias');
390393
}
391394

392395
public static function getPackageNameTests(): iterable
@@ -442,7 +445,7 @@ public static function getPackageNameTests(): iterable
442445
],
443446
];
444447

445-
yield 'namespaced_package_with_registry' => [
448+
yield 'namespaced_package_with_registry_no_version' => [
446449
'npm:@hotwired/stimulus',
447450
[
448451
'package' => '@hotwired/stimulus',

0 commit comments

Comments
 (0)