Skip to content

Commit

Permalink
feature #50445 [AssetMapper] Add "=alias" syntax to importmap:require…
Browse files Browse the repository at this point in the history
… (weaverryan)

This PR was merged into the 6.3 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | yes-ish
| Deprecations? | no
| Tickets       | None
| License       | MIT
| Doc PR        | Still TODO

I'm doing final testing against all of the UX packages. There is one edge-case where we need to require a specific package path - `vue/dist/vue.esm-bundler.js` - but alias it (i.e. make its key) set to something else - `vue` - in `importmap.php`. We need the command to support this so that symfony/flex#975 can use it when installing UX packages.

Hopefully the last thing I come across - I've done a LOT of testing at this point.

Thanks!

Commits
-------

27ecdb4 [AssetMapper] Adding an "alias" syntax to importmap:require
  • Loading branch information
fabpot committed May 26, 2023
2 parents 68f8ad9 + 27ecdb4 commit ec6338f
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 ec6338f

Please sign in to comment.