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 4 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
26 changes: 21 additions & 5 deletions src/PackageJsonSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Author

Choose a reason for hiding this comment

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

I can clean this comment if needed

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure to read it correctly to be fair 😅

$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:')) {
Expand All @@ -163,6 +174,7 @@ private function resolveImportMapPackages($phpPackage): array

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

continue;
Expand Down Expand Up @@ -239,7 +251,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 @@ -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',
Expand Down
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
29 changes: 23 additions & 6 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 @@ -399,11 +404,16 @@ 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';
$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)));

$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 Down
Loading