Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec6338f

Browse files
committed
feature#50445 [AssetMapper] Add "=alias" syntax to importmap:require (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 TODOI'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 thatsymfony/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
2 parents68f8ad9 +27ecdb4 commitec6338f

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
publicstaticfunctionparsePackageName(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-
returnpreg_match($regex,$packageName,$matches) ?$matches :null;
121+
if (!preg_match($regex,$packageName,$matches)) {
122+
returnnull;
123+
}
124+
125+
if (isset($matches['version']) &&'' ===$matches['version']) {
126+
unset($matches['version']);
127+
}
128+
129+
return$matches;
122130
}
123131

124132
privatefunctionbuildImportMapJson():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
publicfunctiontestParsePackageName(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
publicstaticfunctiongetPackageNameTests():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)

[8]ページ先頭

©2009-2025 Movatter.jp