diff --git a/src/PackageJsonSynchronizer.php b/src/PackageJsonSynchronizer.php index 698894c5b..74ec41e11 100644 --- a/src/PackageJsonSynchronizer.php +++ b/src/PackageJsonSynchronizer.php @@ -149,12 +149,23 @@ private function resolveImportMapPackages($phpPackage): array $dependencies = []; foreach ($packageJson->read()['symfony']['importmap'] ?? [] as $importMapName => $constraintConfig) { - if (\is_array($constraintConfig)) { - $constraint = $constraintConfig['version'] ?? []; - $package = $constraintConfig['package'] ?? $importMapName; - } else { + if (\is_string($constraintConfig)) { + // Case "jquery": "^3.0" | "@mybundle/script.js": "path:%PACKAGE%/script.js" $constraint = $constraintConfig; $package = $importMapName; + $entrypoint = false; + } else { + // Case "jquery": {"version": "^3.0"} | "@mybundle/script.js": {"version": "path:%PACKAGE%/script.js", "entrypoint": true} + // Note that non-path assets can't be entrypoint + $constraint = $constraintConfig['version'] ?? ''; + $package = $constraintConfig['package'] ?? $importMapName; + $entrypoint = $constraintConfig['entrypoint'] ?? false; + } + + // Case "@mybundle/script.js": "entrypoint:%PACKAGE%/script.js" | "@mybundle/script.js": {"version": "entrypoint:%PACKAGE%/script.js"} + if (0 === strpos($constraint, 'entrypoint:')) { + $entrypoint = true; + $constraint = str_replace('entrypoint:', 'path:', $constraint); } if (0 === strpos($constraint, 'path:')) { @@ -163,6 +174,7 @@ private function resolveImportMapPackages($phpPackage): array $dependencies[$importMapName] = [ 'path' => $path, + 'entrypoint' => $entrypoint, ]; continue; @@ -239,7 +251,7 @@ private function shouldUpdateConstraint(string $existingConstraint, string $cons } /** - * @param array $importMapEntries + * @param array $importMapEntries */ private function updateImportMap(array $importMapEntries): void { @@ -269,6 +281,10 @@ private function updateImportMap(array $importMapEntries): void if (isset($importMapEntry['path'])) { $arguments = [$name, '--path='.$importMapEntry['path']]; + if (isset($importMapEntry['entrypoint']) && true === $importMapEntry['entrypoint']) { + $arguments[] = '--entrypoint'; + } + $this->scriptExecutor->execute( 'symfony-cmd', 'importmap:require', diff --git a/tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json b/tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json index e4003ea6c..419528379 100644 --- a/tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json +++ b/tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json @@ -14,6 +14,11 @@ "@hotcake/foo": "^1.9.0", "@symfony/new-package": { "version": "path:%PACKAGE%/dist/loader.js" + }, + "@symfony/new-package/entry.js": "entrypoint:%PACKAGE%/entry.js", + "@symfony/new-package/entry2.js": { + "version": "path:%PACKAGE%/entry2.js", + "entrypoint": true } } }, diff --git a/tests/PackageJsonSynchronizerTest.php b/tests/PackageJsonSynchronizerTest.php index 7e6b27c0d..98a750d6d 100644 --- a/tests/PackageJsonSynchronizerTest.php +++ b/tests/PackageJsonSynchronizerTest.php @@ -323,11 +323,16 @@ public function testSynchronizeAssetMapperNewPackage() file_put_contents($this->tempDir.'/importmap.php', 'tempDir.'/vendor/symfony/new-package/assets/dist/loader.js'; - $this->scriptExecutor->expects($this->exactly(2)) + $entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js'; + $secondEntrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry2.js'; + + $this->scriptExecutor->expects($this->exactly(4)) ->method('execute') ->withConsecutive( ['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']], - ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]] + ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]], + ['symfony-cmd', 'importmap:require', ['@symfony/new-package/entry.js', '--path='.$entrypointPath, '--entrypoint']], + ['symfony-cmd', 'importmap:require', ['@symfony/new-package/entry2.js', '--path='.$secondEntrypointPath, '--entrypoint']], ); $this->synchronizer->synchronize([ @@ -399,11 +404,16 @@ public function testSynchronizeAssetMapperUpgradesPackageIfNeeded() file_put_contents($this->tempDir.'/importmap.php', sprintf('tempDir.'/vendor/symfony/new-package/assets/dist/loader.js'; - $this->scriptExecutor->expects($this->exactly(2)) + $entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js'; + $secondEntrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry2.js'; + + $this->scriptExecutor->expects($this->exactly(4)) ->method('execute') ->withConsecutive( ['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']], - ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]] + ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]], + ['symfony-cmd', 'importmap:require', ['@symfony/new-package/entry.js', '--path='.$entrypointPath, '--entrypoint']], + ['symfony-cmd', 'importmap:require', ['@symfony/new-package/entry2.js', '--path='.$secondEntrypointPath, '--entrypoint']] ); $this->synchronizer->synchronize([ @@ -421,14 +431,21 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied() // constraint in package.json is ^1.9.0 'version' => '1.9.1', ], + '@symfony/new-package/entry2.js' => [ + 'path' => './vendor/symfony/new-package/assets/entry2.js', + 'entrypoint' => true, + ] ]; file_put_contents($this->tempDir.'/importmap.php', sprintf('tempDir.'/vendor/symfony/new-package/assets/dist/loader.js'; - $this->scriptExecutor->expects($this->once()) + $entrypointPath = $this->tempDir.'/vendor/symfony/new-package/assets/entry.js'; + + $this->scriptExecutor->expects($this->exactly(2)) ->method('execute') ->withConsecutive( - ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]] + ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]], + ['symfony-cmd', 'importmap:require', ['@symfony/new-package/entry.js', '--path='.$entrypointPath, '--entrypoint']], ); $this->synchronizer->synchronize([