Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AssetMapper] Allow to define entrypoint in importmap.php #1026

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/PackageJsonSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? [];
Expand All @@ -163,6 +163,7 @@ private function resolveImportMapPackages($phpPackage): array

$dependencies[$importMapName] = [
'path' => $path,
'entrypoint' => \in_array($importMapName, $entrypoints, true),
];

continue;
Expand Down Expand Up @@ -239,7 +240,7 @@ private function shouldUpdateConstraint(string $existingConstraint, string $cons
}

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

$arguments = [];
if (isset($importMapEntry['entrypoint']) && true === $importMapEntry['entrypoint']) {
$arguments[] = '--entrypoint';
}

if (isset($importMapEntry['path'])) {
$arguments = [$name, '--path='.$importMapEntry['path']];
$arguments[] = $name;
$arguments[] = '--path='.$importMapEntry['path'];
$this->scriptExecutor->execute(
'symfony-cmd',
'importmap:require',
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
25 changes: 17 additions & 8 deletions tests/PackageJsonSynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down Expand Up @@ -323,11 +323,14 @@ public function testSynchronizeAssetMapperNewPackage()
file_put_contents($this->tempDir.'/importmap.php', '<?php return [];');

$fileModulePath = $this->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([
Expand Down Expand Up @@ -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)
);
Expand All @@ -399,11 +402,14 @@ public function testSynchronizeAssetMapperUpgradesPackageIfNeeded()
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));

$fileModulePath = $this->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([
Expand All @@ -425,10 +431,13 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));

$fileModulePath = $this->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([
Expand Down
Loading