@@ -217,25 +217,36 @@ private function findAsset(string $path): ?MappedAsset
217
217
return $ this ->assetMapper ->getAssetFromSourcePath ($ this ->importMapConfigReader ->convertPathToFilesystemPath ($ path ));
218
218
}
219
219
220
+ /**
221
+ * Finds recursively all the non-lazy modules imported by an asset.
222
+ *
223
+ * @return array<string> The array of deduplicated import names
224
+ */
220
225
private function findEagerImports (MappedAsset $ asset ): array
221
226
{
222
227
$ dependencies = [];
223
- foreach ($ asset ->getJavaScriptImports () as $ javaScriptImport ) {
224
- if ($ javaScriptImport ->isLazy ) {
225
- continue ;
226
- }
228
+ $ queue = [$ asset ];
227
229
228
- $ dependencies [] = $ javaScriptImport ->importName ;
230
+ while ($ asset = array_shift ($ queue )) {
231
+ foreach ($ asset ->getJavaScriptImports () as $ javaScriptImport ) {
232
+ if ($ javaScriptImport ->isLazy ) {
233
+ continue ;
234
+ }
235
+ if (isset ($ dependencies [$ javaScriptImport ->importName ])) {
236
+ continue ;
237
+ }
238
+ $ dependencies [$ javaScriptImport ->importName ] = true ;
229
239
230
- // Follow its imports!
231
- if (!$ nextAsset = $ this ->assetMapper ->getAsset ($ javaScriptImport ->assetLogicalPath )) {
232
- // should not happen at this point, unless something added a bogus JavaScriptImport to this asset
233
- throw new LogicException (sprintf ('Cannot find imported JavaScript asset "%s" in asset mapper. ' , $ javaScriptImport ->assetLogicalPath ));
240
+ // Follow its imports!
241
+ if (!$ javaScriptAsset = $ this ->assetMapper ->getAsset ($ javaScriptImport ->assetLogicalPath )) {
242
+ // should not happen at this point, unless something added a bogus JavaScriptImport to this asset
243
+ throw new LogicException (sprintf ('Cannot find JavaScript asset "%s" (imported in "%s") in asset mapper. ' , $ javaScriptImport ->assetLogicalPath , $ asset ->logicalPath ));
244
+ }
245
+ $ queue [] = $ javaScriptAsset ;
234
246
}
235
- $ dependencies = array_merge ($ dependencies , $ this ->findEagerImports ($ nextAsset ));
236
247
}
237
248
238
- return $ dependencies ;
249
+ return array_keys ( $ dependencies) ;
239
250
}
240
251
241
252
private function createMissingImportMapAssetException (ImportMapEntry $ entry ): \InvalidArgumentException
0 commit comments