diff --git a/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php b/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php
index 9c6031e6cf43..636a03b706d7 100644
--- a/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php
+++ b/src/Symfony/Component/AssetMapper/Command/ImportMapRequireCommand.php
@@ -56,6 +56,14 @@ protected function configure(): void
php %command.full_name% lodash --preload
php %command.full_name% "lodash@^4.15"
+You can also require specific paths of a package:
+
+ php %command.full_name% "chart.js/auto"
+
+Or download one package/file, but alias its name in your import map:
+
+ php %command.full_name% "vue/dist/vue.esm-bundler.js=vue"
+
The preload option will set the preload 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.
@@ -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,
);
diff --git a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php
index 620b2a2210e4..849273b24762 100644
--- a/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php
+++ b/src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php
@@ -115,10 +115,18 @@ public function update(): array
*/
public static function parsePackageName(string $packageName): ?array
{
- // https://regex101.com/r/d99BEc/1
- $regex = '/(?:(?P[^:\n]+):)?((?P@?[^@\n]+))(?:@(?P[^\s\n]+))?/';
+ // https://regex101.com/r/MDz0bN/1
+ $regex = '/(?:(?P[^:\n]+):)?((?P@?[^=@\n]+))(?:@(?P[^=\s\n]+))?(?:=(?P[^\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
diff --git a/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php b/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php
index ae5450602f88..c947d334909e 100644
--- a/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php
+++ b/src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php
@@ -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
@@ -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',