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
[AssetMapper] Fix eager imports are not deduplicated
  • Loading branch information
smnandre authored and fabpot committed Nov 25, 2023
commit e76b24b3c1178d79dec60afcddc08bdb7e2a1162
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy