@@ -11,7 +11,7 @@ import * as ts from 'typescript';
11
11
import { ModuleResolver } from '../../imports' ;
12
12
import { PartialEvaluator } from '../../partial_evaluator' ;
13
13
14
- import { scanForRouteEntryPoints } from './lazy' ;
14
+ import { scanForCandidateTransitiveModules , scanForRouteEntryPoints } from './lazy' ;
15
15
import { RouterEntryPointManager , entryPointKeyFor } from './route' ;
16
16
17
17
export interface NgModuleRawRouteData {
@@ -48,18 +48,53 @@ export class NgModuleRouteAnalyzer {
48
48
} ) ;
49
49
}
50
50
51
- listLazyRoutes ( ) : LazyRoute [ ] {
51
+ listLazyRoutes ( entryModuleKey ?: string | undefined ) : LazyRoute [ ] {
52
+ if ( ( entryModuleKey !== undefined ) && ! this . modules . has ( entryModuleKey ) ) {
53
+ throw new Error ( `Failed to list lazy routes: Unknown module '${ entryModuleKey } '.` ) ;
54
+ }
55
+
52
56
const routes : LazyRoute [ ] = [ ] ;
53
- for ( const key of Array . from ( this . modules . keys ( ) ) ) {
57
+ const scannedModuleKeys = new Set < string > ( ) ;
58
+ const pendingModuleKeys = entryModuleKey ? [ entryModuleKey ] : Array . from ( this . modules . keys ( ) ) ;
59
+
60
+ // When listing lazy routes for a specific entry module, we need to recursively extract
61
+ // "transitive" routes from imported/exported modules. This is not necessary when listing all
62
+ // lazy routes, because all analyzed modules will be scanned anyway.
63
+ const scanRecursively = entryModuleKey !== undefined ;
64
+
65
+ while ( pendingModuleKeys . length > 0 ) {
66
+ const key = pendingModuleKeys . pop ( ) ! ;
67
+
68
+ if ( scannedModuleKeys . has ( key ) ) {
69
+ continue ;
70
+ } else {
71
+ scannedModuleKeys . add ( key ) ;
72
+ }
73
+
54
74
const data = this . modules . get ( key ) ! ;
55
75
const entryPoints = scanForRouteEntryPoints (
56
76
data . sourceFile , data . moduleName , data , this . entryPointManager , this . evaluator ) ;
77
+
57
78
routes . push ( ...entryPoints . map ( entryPoint => ( {
58
79
route : entryPoint . loadChildren ,
59
80
module : entryPoint . from ,
60
81
referencedModule : entryPoint . resolvedTo ,
61
82
} ) ) ) ;
83
+
84
+ if ( scanRecursively ) {
85
+ pendingModuleKeys . push (
86
+ ...[
87
+ // Scan the retrieved lazy route entry points.
88
+ ...entryPoints . map (
89
+ ( { resolvedTo} ) => entryPointKeyFor ( resolvedTo . filePath , resolvedTo . moduleName ) ) ,
90
+ // Scan the current module's imported modules.
91
+ ...scanForCandidateTransitiveModules ( data . imports , this . evaluator ) ,
92
+ // Scan the current module's exported modules.
93
+ ...scanForCandidateTransitiveModules ( data . exports , this . evaluator ) ,
94
+ ] . filter ( key => this . modules . has ( key ) ) ) ;
95
+ }
62
96
}
97
+
63
98
return routes ;
64
99
}
65
100
}
0 commit comments