@@ -41,10 +41,11 @@ import {
41
41
CompilerHost ,
42
42
Diagnostics ,
43
43
CustomTransformers ,
44
+ EmitFlags ,
45
+ LazyRoute ,
44
46
createProgram ,
45
47
createCompilerHost ,
46
48
formatDiagnostics ,
47
- EmitFlags ,
48
49
} from './ngtools_api' ;
49
50
import { findAstNodes } from './transformers/ast_helpers' ;
50
51
@@ -87,7 +88,7 @@ export class AngularCompilerPlugin implements Tapable {
87
88
private _compilerOptions : ts . CompilerOptions ;
88
89
private _angularCompilerOptions : CompilerOptions ;
89
90
private _tsFilenames : string [ ] ;
90
- private _program : ts . Program | Program ;
91
+ private _program : ( ts . Program | Program ) ;
91
92
private _compilerHost : WebpackCompilerHost ;
92
93
private _moduleResolutionCache : ts . ModuleResolutionCache ;
93
94
private _angularCompilerHost : WebpackCompilerHost & CompilerHost ;
@@ -111,6 +112,14 @@ export class AngularCompilerPlugin implements Tapable {
111
112
private _forkTypeChecker = true ;
112
113
private _typeCheckerProcess : ChildProcess ;
113
114
115
+ private get _ngCompilerSupportsNewApi ( ) {
116
+ if ( this . _JitMode ) {
117
+ return false ;
118
+ } else {
119
+ return ! ! ( this . _program as Program ) . listLazyRoutes ;
120
+ }
121
+ }
122
+
114
123
constructor ( options : AngularCompilerPluginOptions ) {
115
124
CompilerCliIsSupported ( ) ;
116
125
this . _options = Object . assign ( { } , options ) ;
@@ -364,8 +373,10 @@ export class AngularCompilerPlugin implements Tapable {
364
373
timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram' ) ;
365
374
366
375
time ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ;
367
- return this . _program . loadNgStructureAsync ( ) . then ( ( ) =>
368
- timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ) ;
376
+ return this . _program . loadNgStructureAsync ( )
377
+ . then ( ( ) => {
378
+ timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ;
379
+ } ) ;
369
380
}
370
381
}
371
382
@@ -409,6 +420,32 @@ export class AngularCompilerPlugin implements Tapable {
409
420
return result ;
410
421
}
411
422
423
+ private _listLazyRoutesFromProgram ( ) : LazyRouteMap {
424
+ const ngProgram = this . _program as Program ;
425
+ if ( ! ngProgram . listLazyRoutes ) {
426
+ throw new Error ( '_listLazyRoutesFromProgram was called with an old program.' ) ;
427
+ }
428
+
429
+ const lazyRoutes = ngProgram . listLazyRoutes ( ) ;
430
+
431
+ return lazyRoutes . reduce (
432
+ ( acc : LazyRouteMap , curr : LazyRoute ) => {
433
+ const ref = curr . route ;
434
+ if ( ref in acc && acc [ ref ] !== curr . referencedModule . filePath ) {
435
+ throw new Error (
436
+ + `Duplicated path in loadChildren detected: "${ ref } " is used in 2 loadChildren, `
437
+ + `but they point to different modules "(${ acc [ ref ] } and `
438
+ + `"${ curr . referencedModule . filePath } "). Webpack cannot distinguish on context and `
439
+ + 'would fail to load the proper one.'
440
+ ) ;
441
+ }
442
+ acc [ ref ] = curr . referencedModule . filePath ;
443
+ return acc ;
444
+ } ,
445
+ { } as LazyRouteMap
446
+ ) ;
447
+ }
448
+
412
449
// Process the lazy routes discovered, adding then to _lazyRoutes.
413
450
// TODO: find a way to remove lazy routes that don't exist anymore.
414
451
// This will require a registry of known references to a lazy route, removing it when no
@@ -666,6 +703,10 @@ export class AngularCompilerPlugin implements Tapable {
666
703
667
704
return Promise . resolve ( )
668
705
. then ( ( ) => {
706
+ if ( this . _ngCompilerSupportsNewApi ) {
707
+ return ;
708
+ }
709
+
669
710
// Try to find lazy routes.
670
711
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
671
712
// and other things that we might miss using the (faster) findLazyRoutesInAst.
@@ -684,6 +725,12 @@ export class AngularCompilerPlugin implements Tapable {
684
725
return this . _createOrUpdateProgram ( ) ;
685
726
}
686
727
} )
728
+ . then ( ( ) => {
729
+ if ( this . _ngCompilerSupportsNewApi ) {
730
+ // TODO: keep this when the new ngCompiler supports the new lazy routes API.
731
+ this . _lazyRoutes = this . _listLazyRoutesFromProgram ( ) ;
732
+ }
733
+ } )
687
734
. then ( ( ) => {
688
735
// Build transforms, emit and report errors if there are changes or it's the first run.
689
736
if ( changedFiles . length > 0 || this . _firstRun ) {
0 commit comments