Skip to content

[AssetMapper] Fix eager imports are not deduplicated #52702

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

Merged
merged 1 commit into from
Nov 25, 2023
Merged
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
33 changes: 22 additions & 11 deletions src/Symfony/Component/AssetMapper/ImportMap/ImportMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,36 @@ private function findAsset(string $path): ?MappedAsset
return $this->assetMapper->getAssetFromSourcePath($this->importMapConfigReader->convertPathToFilesystemPath($path));
}

/**
* Finds recursively all the non-lazy modules imported by an asset.
*
* @return array<string> The array of deduplicated import names
*/
private function findEagerImports(MappedAsset $asset): array
{
$dependencies = [];
foreach ($asset->getJavaScriptImports() as $javaScriptImport) {
if ($javaScriptImport->isLazy) {
continue;
}
$queue = [$asset];

$dependencies[] = $javaScriptImport->importName;
while ($asset = array_shift($queue)) {
foreach ($asset->getJavaScriptImports() as $javaScriptImport) {
if ($javaScriptImport->isLazy) {
continue;
}
if (isset($dependencies[$javaScriptImport->importName])) {
continue;
}
$dependencies[$javaScriptImport->importName] = true;

// Follow its imports!
if (!$nextAsset = $this->assetMapper->getAsset($javaScriptImport->assetLogicalPath)) {
// should not happen at this point, unless something added a bogus JavaScriptImport to this asset
throw new LogicException(sprintf('Cannot find imported JavaScript asset "%s" in asset mapper.', $javaScriptImport->assetLogicalPath));
// Follow its imports!
if (!$javaScriptAsset = $this->assetMapper->getAsset($javaScriptImport->assetLogicalPath)) {
// should not happen at this point, unless something added a bogus JavaScriptImport to this asset
throw new LogicException(sprintf('Cannot find JavaScript asset "%s" (imported in "%s") in asset mapper.', $javaScriptImport->assetLogicalPath, $asset->logicalPath));
}
$queue[] = $javaScriptAsset;
}
$dependencies = array_merge($dependencies, $this->findEagerImports($nextAsset));
}

return $dependencies;
return array_keys($dependencies);
}

private function createMissingImportMapAssetException(ImportMapEntry $entry): \InvalidArgumentException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,25 @@ public function getEagerEntrypointImportsTests(): iterable
['/assets/imports_simple.js', '/assets/simple.js'],
[$simpleAsset, $importsSimpleAsset],
];

$importsSimpleAsset2 = new MappedAsset(
'imports_simple2.js',
'/path/to/imports_simple2.js',
publicPathWithoutDigest: '/assets/imports_simple2.js',
javaScriptImports: [new JavaScriptImport('/assets/simple.js', assetLogicalPath: $simpleAsset->logicalPath, assetSourcePath: $simpleAsset->sourcePath, isLazy: false)]
);
yield 'an entry recursive dependencies are deduplicated' => [
new MappedAsset(
'app.js',
publicPath: '/assets/app.js',
javaScriptImports: [
new JavaScriptImport('/assets/imports_simple.js', assetLogicalPath: $importsSimpleAsset->logicalPath, assetSourcePath: $importsSimpleAsset->sourcePath, isLazy: false),
new JavaScriptImport('/assets/imports_simple2.js', assetLogicalPath: $importsSimpleAsset2->logicalPath, assetSourcePath: $importsSimpleAsset2->sourcePath, isLazy: false),
]
),
['/assets/imports_simple.js', '/assets/imports_simple2.js', '/assets/simple.js'],
[$simpleAsset, $importsSimpleAsset, $importsSimpleAsset2],
];
}

public function testFindEagerEntrypointImportsUsesCacheFile()
Expand Down