Skip to content

Commit b7b72e2

Browse files
committed
Use new key/keword instead use $.symfony.entrypoints in package.json
Now, it allow "@mybundle/script.js": "entrypoint:%PACKAGE%/script.js" or "@mybundle/script.js": {"version": "entrypoint:%PACKAGE%/script.js"} or "@mybundle/script.js": {"version": "path:%PACKAGE%/script.js", "entrypoint": true}
1 parent 1418926 commit b7b72e2

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/PackageJsonSynchronizer.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,24 @@ private function resolveImportMapPackages($phpPackage): array
147147
}
148148

149149
$dependencies = [];
150-
$entrypoints = $packageJson->read()['symfony']['entrypoints'] ?? [];
151150
foreach ($packageJson->read()['symfony']['importmap'] ?? [] as $importMapName => $constraintConfig) {
152-
if (\is_array($constraintConfig)) {
153-
$constraint = $constraintConfig['version'] ?? [];
154-
$package = $constraintConfig['package'] ?? $importMapName;
155-
} else {
151+
if (\is_string($constraintConfig)) {
152+
// Case "jquery": "^3.0" | "@mybundle/script.js": "path:%PACKAGE%/script.js"
156153
$constraint = $constraintConfig;
157154
$package = $importMapName;
155+
$entrypoint = false;
156+
} else {
157+
// Case "jquery": {"version": "^3.0"} | "@mybundle/script.js": {"version": "path:%PACKAGE%/script.js", "entrypoint": true}
158+
// Note that non-path assets can't be entrypoint
159+
$constraint = $constraintConfig['version'] ?? '';
160+
$package = $constraintConfig['package'] ?? $importMapName;
161+
$entrypoint = $constraintConfig['entrypoint'] ?? false;
162+
}
163+
164+
// Case "@mybundle/script.js": "entrypoint:%PACKAGE%/script.js" | "@mybundle/script.js": {"version": "entrypoint:%PACKAGE%/script.js"}
165+
if (0 === strpos($constraint, 'entrypoint:')) {
166+
$entrypoint = true;
167+
$constraint = str_replace('entrypoint:', 'path:', $constraint);
158168
}
159169

160170
if (0 === strpos($constraint, 'path:')) {
@@ -163,7 +173,7 @@ private function resolveImportMapPackages($phpPackage): array
163173

164174
$dependencies[$importMapName] = [
165175
'path' => $path,
166-
'entrypoint' => \in_array($importMapName, $entrypoints, true),
176+
'entrypoint' => $entrypoint,
167177
];
168178

169179
continue;
@@ -269,11 +279,11 @@ private function updateImportMap(array $importMapEntries): void
269279
}
270280

271281
$arguments = [];
272-
if (isset($importMapEntry['entrypoint']) && true === $importMapEntry['entrypoint']) {
273-
$arguments[] = '--entrypoint';
274-
}
275-
276282
if (isset($importMapEntry['path'])) {
283+
if (isset($importMapEntry['entrypoint']) && true === $importMapEntry['entrypoint']) {
284+
$arguments[] = '--entrypoint';
285+
}
286+
277287
$arguments[] = $name;
278288
$arguments[] = '--path='.$importMapEntry['path'];
279289
$this->scriptExecutor->execute(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
}
1010
}
1111
},
12-
"entrypoints": ["admin.js", "@symfony/new-package/entry.js"],
12+
"entrypoints": ["admin.js"],
1313
"importmap": {
1414
"@hotcake/foo": "^1.9.0",
1515
"@symfony/new-package": {
1616
"version": "path:%PACKAGE%/dist/loader.js"
1717
},
18-
"@symfony/new-package/entry.js": "path:%PACKAGE%/entry.js"
18+
"@symfony/new-package/entry.js": "entrypoint:%PACKAGE%/entry.js",
19+
"@symfony/new-package/entry2.js": {
20+
"version": "path:%PACKAGE%/entry2.js",
21+
"entrypoint": true
22+
}
1923
}
2024
},
2125
"peerDependencies": {

tests/PackageJsonSynchronizerTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function testSynchronizeNewPackage()
189189
],
190190
],
191191
],
192-
'entrypoints' => ['admin.js', '@symfony/new-package/entry.js'],
192+
'entrypoints' => ['admin.js'],
193193
],
194194
json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true)
195195
);
@@ -324,13 +324,15 @@ public function testSynchronizeAssetMapperNewPackage()
324324

325325
$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
326326
$entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js';
327+
$secondEntrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry2.js';
327328

328-
$this->scriptExecutor->expects($this->exactly(3))
329+
$this->scriptExecutor->expects($this->exactly(4))
329330
->method('execute')
330331
->withConsecutive(
331332
['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']],
332333
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]],
333-
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]]
334+
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]],
335+
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry2.js', '--path='.$secondEntrypointPath]]
334336
);
335337

336338
$this->synchronizer->synchronize([
@@ -385,7 +387,7 @@ public function testSynchronizeAssetMapperNewPackage()
385387
],
386388
],
387389
],
388-
'entrypoints' => ['admin.js', '@symfony/new-package/entry.js'],
390+
'entrypoints' => ['admin.js'],
389391
],
390392
json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true)
391393
);
@@ -403,13 +405,15 @@ public function testSynchronizeAssetMapperUpgradesPackageIfNeeded()
403405

404406
$fileModulePath = $this->tempDir.'/vendor/symfony/new-package/assets/dist/loader.js';
405407
$entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js';
408+
$secondEntrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry2.js';
406409

407-
$this->scriptExecutor->expects($this->exactly(3))
410+
$this->scriptExecutor->expects($this->exactly(4))
408411
->method('execute')
409412
->withConsecutive(
410413
['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']],
411414
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]],
412-
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]]
415+
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]],
416+
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry2.js', '--path='.$secondEntrypointPath]]
413417
);
414418

415419
$this->synchronizer->synchronize([
@@ -427,6 +431,10 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
427431
// constraint in package.json is ^1.9.0
428432
'version' => '1.9.1',
429433
],
434+
'@symfony/new-package/entry2.js' => [
435+
'path' => './vendor/symfony/new-package/assets/entry2.js',
436+
'entrypoint' => true,
437+
]
430438
];
431439
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));
432440

@@ -437,7 +445,7 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
437445
->method('execute')
438446
->withConsecutive(
439447
['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]],
440-
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]]
448+
['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]],
441449
);
442450

443451
$this->synchronizer->synchronize([

0 commit comments

Comments
 (0)