Skip to content

Commit 6f54bc2

Browse files
committed
[AssetMapper] Allow to define entrypoint in importmap.php
1 parent 4dc1191 commit 6f54bc2

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

src/PackageJsonSynchronizer.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private function resolveImportMapPackages($phpPackage): array
147147
}
148148

149149
$dependencies = [];
150-
150+
$entrypoints = $packageJson->read()['symfony']['entrypoints'] ?? [];
151151
foreach ($packageJson->read()['symfony']['importmap'] ?? [] as $importMapName => $constraintConfig) {
152152
if (\is_array($constraintConfig)) {
153153
$constraint = $constraintConfig['version'] ?? [];
@@ -163,6 +163,7 @@ private function resolveImportMapPackages($phpPackage): array
163163

164164
$dependencies[$importMapName] = [
165165
'path' => $path,
166+
'entrypoint' => \in_array($importMapName, $entrypoints, true),
166167
];
167168

168169
continue;
@@ -239,7 +240,7 @@ private function shouldUpdateConstraint(string $existingConstraint, string $cons
239240
}
240241

241242
/**
242-
* @param array<string, array{path?: string, package?: string, version?: string}> $importMapEntries
243+
* @param array<string, array{path?: string, package?: string, version?: string, entrypoint?: bool}> $importMapEntries
243244
*/
244245
private function updateImportMap(array $importMapEntries): void
245246
{
@@ -267,8 +268,14 @@ private function updateImportMap(array $importMapEntries): void
267268
$this->io->writeError(sprintf('Updating package <comment>%s</> from <info>%s</> to <info>%s</>.', $name, $version, $versionConstraint));
268269
}
269270

271+
$arguments = [];
272+
if (isset($importMapEntry['entrypoint']) && $importMapEntry['entrypoint'] === true) {
273+
$arguments[] = '--entrypoint';
274+
}
275+
270276
if (isset($importMapEntry['path'])) {
271-
$arguments = [$name, '--path='.$importMapEntry['path']];
277+
$arguments[] = $name;
278+
$arguments[] = '--path='.$importMapEntry['path'];
272279
$this->scriptExecutor->execute(
273280
'symfony-cmd',
274281
'importmap:require',
@@ -283,7 +290,7 @@ private function updateImportMap(array $importMapEntries): void
283290
if ($importMapEntry['package'] !== $name) {
284291
$packageName .= '='.$name;
285292
}
286-
$arguments = [$packageName];
293+
$arguments[] = $packageName;
287294
$this->scriptExecutor->execute(
288295
'symfony-cmd',
289296
'importmap:require',

tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
}
1010
}
1111
},
12-
"entrypoints": ["admin.js"],
12+
"entrypoints": ["admin.js", "@symfony/new-package/entry.js"],
1313
"importmap": {
1414
"@hotcake/foo": "^1.9.0",
1515
"@symfony/new-package": {
1616
"version": "path:%PACKAGE%/dist/loader.js"
17-
}
17+
},
18+
"@symfony/new-package/entry.js": "path:%PACKAGE%/entry.js"
1819
}
1920
},
2021
"peerDependencies": {

tests/PackageJsonSynchronizerTest.php

+17-8
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function testSynchronizeNewPackage()
189189
],
190190
],
191191
],
192-
'entrypoints' => ['admin.js'],
192+
'entrypoints' => ['admin.js', '@symfony/new-package/entry.js'],
193193
],
194194
json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true)
195195
);
@@ -323,11 +323,14 @@ public function testSynchronizeAssetMapperNewPackage()
323323
file_put_contents($this->tempDir.'/importmap.php', '<?php return [];');
324324

325325
$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
326-
$this->scriptExecutor->expects($this->exactly(2))
326+
$entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js';
327+
328+
$this->scriptExecutor->expects($this->exactly(3))
327329
->method('execute')
328330
->withConsecutive(
329331
['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']],
330-
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]]
332+
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]],
333+
['symfony-cmd', 'importmap:require', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]]
331334
);
332335

333336
$this->synchronizer->synchronize([
@@ -382,7 +385,7 @@ public function testSynchronizeAssetMapperNewPackage()
382385
],
383386
],
384387
],
385-
'entrypoints' => ['admin.js'],
388+
'entrypoints' => ['admin.js', '@symfony/new-package/entry.js'],
386389
],
387390
json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true)
388391
);
@@ -399,11 +402,14 @@ public function testSynchronizeAssetMapperUpgradesPackageIfNeeded()
399402
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));
400403

401404
$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
402-
$this->scriptExecutor->expects($this->exactly(2))
405+
$entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js';
406+
407+
$this->scriptExecutor->expects($this->exactly(3))
403408
->method('execute')
404409
->withConsecutive(
405410
['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']],
406-
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]]
411+
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]],
412+
['symfony-cmd', 'importmap:require', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]]
407413
);
408414

409415
$this->synchronizer->synchronize([
@@ -425,10 +431,13 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
425431
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));
426432

427433
$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
428-
$this->scriptExecutor->expects($this->once())
434+
$entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js';
435+
436+
$this->scriptExecutor->expects($this->exactly(2))
429437
->method('execute')
430438
->withConsecutive(
431-
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]]
439+
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]],
440+
['symfony-cmd', 'importmap:require', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]]
432441
);
433442

434443
$this->synchronizer->synchronize([

0 commit comments

Comments
 (0)