Skip to content

Commit

Permalink
[AssetMapper] Adding an "alias" syntax to importmap:require
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed May 26, 2023
1 parent 68f8ad9 commit 27ecdb4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
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
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
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

0 comments on commit 27ecdb4

Please sign in to comment.