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 2 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'] ?? [];
Copy link
Member

Choose a reason for hiding this comment

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

Took some time to refresh my memory and re-read all this code 😅

So .. nope : this is not related to importmap entrypoints, but to stimulus bundle + controllers.json file. Thus should not be used here :)

Should not be used here

Copy link
Member

Choose a reason for hiding this comment

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

On a pure technical level, we may want to use another key, and definitively one nested under the importmap key.

The [symfony][entrypoints] is used by flex and is or has been used by stimulus bundle / stimulus bridge / webpack encore bundle.

foreach ($packageJson->read()['symfony']['entrypoints'] ?? [] as $entrypoint => $filename) {
if (!isset($newControllersJson['entrypoints'][$entrypoint])) {
$newControllersJson['entrypoints'][$entrypoint] = $filename;
}
}
}

Meaning it would have different meaning, and behaviour, wether someone uses Webpack or AssetMapper.

Maybe there is something to do in the symfony[importmap][ PACKAGE_NAME ] key.

Currently we allow here two shapes

$importMapName =>  [
    'package' => 'foobar',
    'version' => 'foobar',
]

and

$importmapName => 'path:foobar'

As seen in my other comment, the first one is out of scope (entrypoints cannot have version).

But I suppose you could here allow another array syntax, like

$importMapName =>  [
    'package' => 'path:foobarr',
    'entrypoiit' => true,
]

Or something like replace path: with entrypoint:

$importMapName => 'entrypoint:foobar',

(that would give the path and specifu an entrypointà)

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),
];
Copy link
Member

@smnandre smnandre Dec 14, 2024

Choose a reason for hiding this comment

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

(update) collapsed my comment here: it was not constructive

By definition, a dependency cannot be an importmap entrypoint :)


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';
}
Copy link
Member

Choose a reason for hiding this comment

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

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

This must be updated (moved in the code -- see below) as it will potentially generate an incorrect state.

Entrypoints cannot be remote packages.

In ImportMapGenerator

        if ($rootImportEntries->get($entryName)->isRemotePackage()) {
            throw new \InvalidArgumentException(\sprintf('The entrypoint "%s" is a remote package and cannot be used as an entrypoint.', $entryName));
        }

Remote package = package with a version

In ImportMapEntry.php

    public function isRemotePackage(): bool
    {
        return null !== $this->version;
    }

Version and path are exclusive properties

In ImportMapConfigReader.php

                if (isset($data['version'])) {
                    throw new RuntimeException(\sprintf('The importmap entry "%s" cannot have both a "path" and "version" option.', $importName));
                }

Package have either version or path

In ImportMapConfigReader.php

            if (null === $version) {
                throw new RuntimeException(\sprintf('The importmap entry "%s" must have either a "path" or "version" option.', $importName));
            }

Tip

So entrypoints must be local. And this must be moved line 277 (inside the if (isset($importMapEntry['path'])) {)


Another question: should we allow overriding existing user entrypoints? Do we want to allow that ?


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