From 6f54bc25c3bd643f700df71267a964ca4d78fa74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Tue, 21 May 2024 15:53:11 +0200 Subject: [PATCH 1/2] [AssetMapper] Allow to define entrypoint in importmap.php --- src/PackageJsonSynchronizer.php | 15 ++++++++--- .../symfony/new-package/assets/package.json | 5 ++-- tests/PackageJsonSynchronizerTest.php | 25 +++++++++++++------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/PackageJsonSynchronizer.php b/src/PackageJsonSynchronizer.php index 698894c5b..a3049c754 100644 --- a/src/PackageJsonSynchronizer.php +++ b/src/PackageJsonSynchronizer.php @@ -147,7 +147,7 @@ private function resolveImportMapPackages($phpPackage): array } $dependencies = []; - + $entrypoints = $packageJson->read()['symfony']['entrypoints'] ?? []; foreach ($packageJson->read()['symfony']['importmap'] ?? [] as $importMapName => $constraintConfig) { if (\is_array($constraintConfig)) { $constraint = $constraintConfig['version'] ?? []; @@ -163,6 +163,7 @@ private function resolveImportMapPackages($phpPackage): array $dependencies[$importMapName] = [ 'path' => $path, + 'entrypoint' => \in_array($importMapName, $entrypoints, true), ]; continue; @@ -239,7 +240,7 @@ private function shouldUpdateConstraint(string $existingConstraint, string $cons } /** - * @param array $importMapEntries + * @param array $importMapEntries */ private function updateImportMap(array $importMapEntries): void { @@ -267,8 +268,14 @@ private function updateImportMap(array $importMapEntries): void $this->io->writeError(sprintf('Updating package %s from %s to %s.', $name, $version, $versionConstraint)); } + $arguments = []; + if (isset($importMapEntry['entrypoint']) && $importMapEntry['entrypoint'] === true) { + $arguments[] = '--entrypoint'; + } + if (isset($importMapEntry['path'])) { - $arguments = [$name, '--path='.$importMapEntry['path']]; + $arguments[] = $name; + $arguments[] = '--path='.$importMapEntry['path']; $this->scriptExecutor->execute( 'symfony-cmd', 'importmap:require', @@ -283,7 +290,7 @@ private function updateImportMap(array $importMapEntries): void if ($importMapEntry['package'] !== $name) { $packageName .= '='.$name; } - $arguments = [$packageName]; + $arguments[] = $packageName; $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..b67245910 100644 --- a/tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json +++ b/tests/Fixtures/packageJson/vendor/symfony/new-package/assets/package.json @@ -9,12 +9,13 @@ } } }, - "entrypoints": ["admin.js"], + "entrypoints": ["admin.js", "@symfony/new-package/entry.js"], "importmap": { "@hotcake/foo": "^1.9.0", "@symfony/new-package": { "version": "path:%PACKAGE%/dist/loader.js" - } + }, + "@symfony/new-package/entry.js": "path:%PACKAGE%/entry.js" } }, "peerDependencies": { diff --git a/tests/PackageJsonSynchronizerTest.php b/tests/PackageJsonSynchronizerTest.php index 7e6b27c0d..e1f047f6f 100644 --- a/tests/PackageJsonSynchronizerTest.php +++ b/tests/PackageJsonSynchronizerTest.php @@ -189,7 +189,7 @@ public function testSynchronizeNewPackage() ], ], ], - 'entrypoints' => ['admin.js'], + 'entrypoints' => ['admin.js', '@symfony/new-package/entry.js'], ], json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true) ); @@ -323,11 +323,14 @@ 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'; + + $this->scriptExecutor->expects($this->exactly(3)) ->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', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]] ); $this->synchronizer->synchronize([ @@ -382,7 +385,7 @@ public function testSynchronizeAssetMapperNewPackage() ], ], ], - 'entrypoints' => ['admin.js'], + 'entrypoints' => ['admin.js', '@symfony/new-package/entry.js'], ], json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true) ); @@ -399,11 +402,14 @@ 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'; + + $this->scriptExecutor->expects($this->exactly(3)) ->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', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]] ); $this->synchronizer->synchronize([ @@ -425,10 +431,13 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied() 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', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]] ); $this->synchronizer->synchronize([ From 14189260e2513403467d8897604ae85578713219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Tue, 21 May 2024 16:00:08 +0200 Subject: [PATCH 2/2] Apply fabbot patch --- src/PackageJsonSynchronizer.php | 2 +- tests/PackageJsonSynchronizerTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PackageJsonSynchronizer.php b/src/PackageJsonSynchronizer.php index a3049c754..23594e840 100644 --- a/src/PackageJsonSynchronizer.php +++ b/src/PackageJsonSynchronizer.php @@ -269,7 +269,7 @@ private function updateImportMap(array $importMapEntries): void } $arguments = []; - if (isset($importMapEntry['entrypoint']) && $importMapEntry['entrypoint'] === true) { + if (isset($importMapEntry['entrypoint']) && true === $importMapEntry['entrypoint']) { $arguments[] = '--entrypoint'; } diff --git a/tests/PackageJsonSynchronizerTest.php b/tests/PackageJsonSynchronizerTest.php index e1f047f6f..ad5b32e3c 100644 --- a/tests/PackageJsonSynchronizerTest.php +++ b/tests/PackageJsonSynchronizerTest.php @@ -330,7 +330,7 @@ public function testSynchronizeAssetMapperNewPackage() ->withConsecutive( ['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']], ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]], - ['symfony-cmd', 'importmap:require', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]] + ['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]] ); $this->synchronizer->synchronize([ @@ -409,7 +409,7 @@ public function testSynchronizeAssetMapperUpgradesPackageIfNeeded() ->withConsecutive( ['symfony-cmd', 'importmap:require', ['@hotcake/foo@^1.9.0']], ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]], - ['symfony-cmd', 'importmap:require', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]] + ['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]] ); $this->synchronizer->synchronize([ @@ -437,7 +437,7 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied() ->method('execute') ->withConsecutive( ['symfony-cmd', 'importmap:require', ['@symfony/new-package', '--path='.$fileModulePath]], - ['symfony-cmd', 'importmap:require', ['--entrypoint','@symfony/new-package/entry.js', '--path='.$entrypointPath]] + ['symfony-cmd', 'importmap:require', ['--entrypoint', '@symfony/new-package/entry.js', '--path='.$entrypointPath]] ); $this->synchronizer->synchronize([