Skip to content

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

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

Open
wants to merge 7 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
38 changes: 29 additions & 9 deletions src/PackageJsonSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function removeObsoletePackageJsonLinks(): bool

foreach (['dependencies' => $jsDependencies, 'devDependencies' => $jsDevDependencies] as $key => $packages) {
foreach ($packages as $name => $version) {
if ('@' !== $name[0] || 0 !== strpos($version, 'file:'.$this->vendorDir.'/') || false === strpos($version, '/assets')) {
if ('@' !== $name[0] || !str_starts_with($version, 'file:'.$this->vendorDir.'/') || !str_contains($version, '/assets')) {
continue;
}
if (file_exists($this->rootDir.'/'.substr($version, 5).'/package.json')) {
Expand Down Expand Up @@ -149,20 +149,36 @@ 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)) {
// Matches string constraint, like "^3.0" or "path:%PACKAGE%/script.js"
$constraint = $constraintConfig;
$package = $importMapName;
$entrypoint = false;
} elseif (\is_array($constraintConfig)) {
// Matches array constraint, like {"version":"^3.0"} or {"version":"path:%PACKAGE%/script.js","entrypoint":true}
// Note that non-path assets can't be entrypoint
$constraint = $constraintConfig['version'] ?? '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't read the whole PR, but before your changes, it was $constraint = $constraintConfig['version'] ?? [].
I believe that's on purpose, $constraint must be a string IINW

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, it must be a string.

$package = $constraintConfig['package'] ?? $importMapName;
$entrypoint = $constraintConfig['entrypoint'] ?? false;
} else {
throw new \InvalidArgumentException(\sprintf('Invalid constraint config for key "%s": "%s" given, array or string expected.', $importMapName, var_export($constraintConfig, true)));
}

if (0 === strpos($constraint, 'path:')) {
// When "$constraintConfig" matches one of the following cases:
// - "entrypoint:%PACKAGE%/script.js"
// - {"version": "entrypoint:%PACKAGE%/script.js"}
if (str_starts_with($constraint, 'entrypoint:')) {
$entrypoint = true;
$constraint = str_replace('entrypoint:', 'path:', $constraint);
}

if (str_starts_with($constraint, 'path:')) {
$path = substr($constraint, 5);
$path = str_replace('%PACKAGE%', \dirname($packageJson->getPath()), $path);

$dependencies[$importMapName] = [
'path' => $path,
'entrypoint' => $entrypoint,
];

continue;
Expand Down Expand Up @@ -239,7 +255,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 All @@ -264,11 +280,15 @@ private function updateImportMap(array $importMapEntries): void
continue;
}

$this->io->writeError(sprintf('Updating package <comment>%s</> from <info>%s</> to <info>%s</>.', $name, $version, $versionConstraint));
$this->io->writeError(\sprintf('Updating package <comment>%s</> from <info>%s</> to <info>%s</>.', $name, $version, $versionConstraint));
}

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',
Expand All @@ -293,7 +313,7 @@ private function updateImportMap(array $importMapEntries): void
continue;
}

throw new \InvalidArgumentException(sprintf('Invalid importmap entry: "%s".', var_export($importMapEntry, true)));
throw new \InvalidArgumentException(\sprintf('Invalid importmap entry: "%s".', var_export($importMapEntry, true)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"symfony": {
"importmap": {
"@symfony/test": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
48 changes: 40 additions & 8 deletions tests/PackageJsonSynchronizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,16 @@ 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';
$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([
Expand Down Expand Up @@ -396,14 +401,19 @@ public function testSynchronizeAssetMapperUpgradesPackageIfNeeded()
'version' => '1.8.0',
],
];
file_put_contents($this->tempDir.'/importmap.php', sprintf('<?php return %s;', var_export($importMap, true)));
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';
$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([
Expand All @@ -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('<?php return %s;', var_export($importMap, true)));
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', ['@symfony/new-package/entry.js', '--path='.$entrypointPath, '--entrypoint']],
);

$this->synchronizer->synchronize([
Expand All @@ -438,4 +455,19 @@ public function testSynchronizeAssetMapperSkipsUpgradeIfAlreadySatisfied()
],
]);
}

public function testExceptionWhenInvalidImportMapConstraint()
{
file_put_contents($this->tempDir.'/importmap.php', '<?php return [];');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid constraint config for key "@symfony/test": "true" given, array or string expected.');

$this->synchronizer->synchronize([
[
'name' => 'symfony/importmap-invalid-constraint-package',
'keywords' => ['symfony-ux'],
],
]);
}
}