@@ -18,8 +18,6 @@ import {
18
18
} from './virtual_file_system_decorator' ;
19
19
import { resolveEntryModuleFromMain } from './entry_resolver' ;
20
20
import {
21
- TransformOperation ,
22
- makeTransform ,
23
21
replaceBootstrap ,
24
22
exportNgFactory ,
25
23
exportLazyModuleMap ,
@@ -41,15 +39,14 @@ import {
41
39
CompilerOptions ,
42
40
CompilerHost ,
43
41
Diagnostics ,
44
- CustomTransformers ,
45
42
EmitFlags ,
46
43
LazyRoute ,
47
44
createProgram ,
48
45
createCompilerHost ,
49
46
formatDiagnostics ,
50
47
readConfiguration ,
51
48
} from './ngtools_api' ;
52
- import { findAstNodes } from './transformers/ast_helpers' ;
49
+ import { collectDeepNodes } from './transformers/ast_helpers' ;
53
50
54
51
55
52
/**
@@ -95,8 +92,9 @@ export class AngularCompilerPlugin implements Tapable {
95
92
private _lazyRoutes : LazyRouteMap = Object . create ( null ) ;
96
93
private _tsConfigPath : string ;
97
94
private _entryModule : string ;
95
+ private _mainPath : string | undefined ;
98
96
private _basePath : string ;
99
- private _transformMap : Map < string , TransformOperation [ ] > = new Map ( ) ;
97
+ private _transformers : ts . TransformerFactory < ts . SourceFile > [ ] = [ ] ;
100
98
private _platform : PLATFORM ;
101
99
private _JitMode = false ;
102
100
private _emitSkipped = true ;
@@ -128,6 +126,9 @@ export class AngularCompilerPlugin implements Tapable {
128
126
get options ( ) { return this . _options ; }
129
127
get done ( ) { return this . _donePromise ; }
130
128
get entryModule ( ) {
129
+ if ( ! this . _entryModule ) {
130
+ return undefined ;
131
+ }
131
132
const splitted = this . _entryModule . split ( '#' ) ;
132
133
const path = splitted [ 0 ] ;
133
134
const className = splitted [ 1 ] || 'default' ;
@@ -157,6 +158,7 @@ export class AngularCompilerPlugin implements Tapable {
157
158
basePath = path . resolve ( process . cwd ( ) , options . basePath ) ;
158
159
}
159
160
161
+ // TODO: check if we can get this from readConfiguration
160
162
this . _basePath = basePath ;
161
163
162
164
// Parse the tsconfig contents.
@@ -224,15 +226,6 @@ export class AngularCompilerPlugin implements Tapable {
224
226
options . missingTranslation as 'error' | 'warning' | 'ignore' ;
225
227
}
226
228
227
- // Use entryModule if available in options, otherwise resolve it from mainPath after program
228
- // creation.
229
- if ( this . _options . entryModule ) {
230
- this . _entryModule = this . _options . entryModule ;
231
- } else if ( this . _compilerOptions . entryModule ) {
232
- this . _entryModule = path . resolve ( this . _basePath ,
233
- this . _compilerOptions . entryModule ) ;
234
- }
235
-
236
229
// Create the webpack compiler host.
237
230
const webpackCompilerHost = new WebpackCompilerHost ( this . _compilerOptions , this . _basePath ) ;
238
231
webpackCompilerHost . enableCaching ( ) ;
@@ -266,8 +259,26 @@ export class AngularCompilerPlugin implements Tapable {
266
259
// Use an identity function as all our paths are absolute already.
267
260
this . _moduleResolutionCache = ts . createModuleResolutionCache ( this . _basePath , x => x ) ;
268
261
262
+ // Resolve mainPath if provided.
263
+ if ( options . mainPath ) {
264
+ this . _mainPath = this . _compilerHost . resolve ( options . mainPath ) ;
265
+ }
266
+
267
+ // Use entryModule if available in options, otherwise resolve it from mainPath after program
268
+ // creation.
269
+ if ( this . _options . entryModule ) {
270
+ this . _entryModule = this . _options . entryModule ;
271
+ } else if ( this . _compilerOptions . entryModule ) {
272
+ this . _entryModule = path . resolve ( this . _basePath ,
273
+ this . _compilerOptions . entryModule ) ;
274
+ }
275
+
269
276
// Set platform.
270
277
this . _platform = options . platform || PLATFORM . Browser ;
278
+
279
+ // Make transformers.
280
+ this . _makeTransformers ( ) ;
281
+
271
282
timeEnd ( 'AngularCompilerPlugin._setupOptions' ) ;
272
283
}
273
284
@@ -332,11 +343,10 @@ export class AngularCompilerPlugin implements Tapable {
332
343
} )
333
344
. then ( ( ) => {
334
345
// If there's still no entryModule try to resolve from mainPath.
335
- if ( ! this . _entryModule && this . _options . mainPath ) {
346
+ if ( ! this . _entryModule && this . _mainPath ) {
336
347
time ( 'AngularCompilerPlugin._make.resolveEntryModuleFromMain' ) ;
337
- const mainPath = path . resolve ( this . _basePath , this . _options . mainPath ) ;
338
348
this . _entryModule = resolveEntryModuleFromMain (
339
- mainPath , this . _compilerHost , this . _getTsProgram ( ) ) ;
349
+ this . _mainPath , this . _compilerHost , this . _getTsProgram ( ) ) ;
340
350
timeEnd ( 'AngularCompilerPlugin._make.resolveEntryModuleFromMain' ) ;
341
351
}
342
352
} ) ;
@@ -633,6 +643,39 @@ export class AngularCompilerPlugin implements Tapable {
633
643
} ) ;
634
644
}
635
645
646
+ private _makeTransformers ( ) {
647
+
648
+ // TODO use compilerhost.denormalize when #8210 is merged.
649
+ const isAppPath = ( fileName : string ) =>
650
+ this . _rootNames . includes ( fileName . replace ( / \/ / g, path . sep ) ) ;
651
+ const isMainPath = ( fileName : string ) => fileName === this . _mainPath ;
652
+ const getEntryModule = ( ) => this . entryModule ;
653
+ const getLazyRoutes = ( ) => this . _lazyRoutes ;
654
+
655
+ if ( this . _JitMode ) {
656
+ // Replace resources in JIT.
657
+ this . _transformers . push ( replaceResources ( isAppPath ) ) ;
658
+ }
659
+
660
+ if ( this . _platform === PLATFORM . Browser ) {
661
+ if ( ! this . _JitMode ) {
662
+ // Replace bootstrap in browser AOT.
663
+ this . _transformers . push ( replaceBootstrap ( isAppPath , getEntryModule ) ) ;
664
+ }
665
+
666
+ // If we have a locale, auto import the locale data file.
667
+ if ( this . _compilerOptions . i18nInLocale ) {
668
+ this . _transformers . push ( registerLocaleData ( isAppPath , getEntryModule ,
669
+ this . _compilerOptions . i18nInLocale ) ) ;
670
+ }
671
+ } else if ( this . _platform === PLATFORM . Server ) {
672
+ this . _transformers . push ( exportLazyModuleMap ( isMainPath , getLazyRoutes ) ) ;
673
+ if ( ! this . _JitMode ) {
674
+ this . _transformers . push ( exportNgFactory ( isMainPath , getEntryModule ) ) ;
675
+ }
676
+ }
677
+ }
678
+
636
679
private _update ( ) {
637
680
time ( 'AngularCompilerPlugin._update' ) ;
638
681
// We only want to update on TS and template changes, but all kinds of files are on this
@@ -662,7 +705,7 @@ export class AngularCompilerPlugin implements Tapable {
662
705
}
663
706
} )
664
707
. then ( ( ) => {
665
- // Build transforms, emit and report errors.
708
+ // Emit and report errors.
666
709
667
710
// We now have the final list of changed TS files.
668
711
// Go through each changed file and add transforms as needed.
@@ -677,56 +720,9 @@ export class AngularCompilerPlugin implements Tapable {
677
720
return sourceFile ;
678
721
} ) ;
679
722
680
- time ( 'AngularCompilerPlugin._update.transformOps' ) ;
681
- sourceFiles . forEach ( ( sf ) => {
682
- const fileName = this . _compilerHost . resolve ( sf . fileName ) ;
683
- let transformOps = [ ] ;
684
-
685
- if ( this . _JitMode ) {
686
- transformOps . push ( ...replaceResources ( sf ) ) ;
687
- }
688
-
689
- if ( this . _platform === PLATFORM . Browser ) {
690
- if ( ! this . _JitMode ) {
691
- transformOps . push ( ...replaceBootstrap ( sf , this . entryModule ) ) ;
692
- }
693
-
694
- // If we have a locale, auto import the locale data file.
695
- if ( this . _compilerOptions . i18nInLocale ) {
696
- transformOps . push ( ...registerLocaleData (
697
- sf ,
698
- this . entryModule ,
699
- this . _compilerOptions . i18nInLocale
700
- ) ) ;
701
- }
702
- } else if ( this . _platform === PLATFORM . Server ) {
703
- if ( fileName === this . _compilerHost . resolve ( this . _options . mainPath ) ) {
704
- transformOps . push ( ...exportLazyModuleMap ( sf , this . _lazyRoutes ) ) ;
705
- if ( ! this . _JitMode ) {
706
- transformOps . push ( ...exportNgFactory ( sf , this . entryModule ) ) ;
707
- }
708
- }
709
- }
710
-
711
- // We need to keep a map of transforms for each file, to reapply on each update.
712
- this . _transformMap . set ( fileName , transformOps ) ;
713
- } ) ;
714
-
715
- const transformOps : TransformOperation [ ] = [ ] ;
716
- for ( let fileTransformOps of this . _transformMap . values ( ) ) {
717
- transformOps . push ( ...fileTransformOps ) ;
718
- }
719
- timeEnd ( 'AngularCompilerPlugin._update.transformOps' ) ;
720
-
721
- time ( 'AngularCompilerPlugin._update.makeTransform' ) ;
722
- const transformers : CustomTransformers = {
723
- beforeTs : transformOps . length > 0 ? [ makeTransform ( transformOps ) ] : [ ]
724
- } ;
725
- timeEnd ( 'AngularCompilerPlugin._update.makeTransform' ) ;
726
-
727
723
// Emit files.
728
724
time ( 'AngularCompilerPlugin._update._emit' ) ;
729
- const { emitResult, diagnostics } = this . _emit ( sourceFiles , transformers ) ;
725
+ const { emitResult, diagnostics } = this . _emit ( sourceFiles ) ;
730
726
timeEnd ( 'AngularCompilerPlugin._update._emit' ) ;
731
727
732
728
// Report diagnostics.
@@ -824,7 +820,7 @@ export class AngularCompilerPlugin implements Tapable {
824
820
const host = this . _compilerHost ;
825
821
const cache = this . _moduleResolutionCache ;
826
822
827
- const esImports = findAstNodes < ts . ImportDeclaration > ( null , sourceFile ,
823
+ const esImports = collectDeepNodes < ts . ImportDeclaration > ( sourceFile ,
828
824
ts . SyntaxKind . ImportDeclaration )
829
825
. map ( decl => {
830
826
const moduleName = ( decl . moduleSpecifier as ts . StringLiteral ) . text ;
@@ -851,10 +847,7 @@ export class AngularCompilerPlugin implements Tapable {
851
847
// This code mostly comes from `performCompilation` in `@angular/compiler-cli`.
852
848
// It skips the program creation because we need to use `loadNgStructureAsync()`,
853
849
// and uses CustomTransformers.
854
- private _emit (
855
- sourceFiles : ts . SourceFile [ ] ,
856
- customTransformers : ts . CustomTransformers & CustomTransformers
857
- ) {
850
+ private _emit ( sourceFiles : ts . SourceFile [ ] ) {
858
851
time ( 'AngularCompilerPlugin._emit' ) ;
859
852
const program = this . _program ;
860
853
const allDiagnostics : Diagnostics = [ ] ;
@@ -881,7 +874,7 @@ export class AngularCompilerPlugin implements Tapable {
881
874
const timeLabel = `AngularCompilerPlugin._emit.ts+${ sf . fileName } +.emit` ;
882
875
time ( timeLabel ) ;
883
876
emitResult = tsProgram . emit ( sf , undefined , undefined , undefined ,
884
- { before : customTransformers . beforeTs }
877
+ { before : this . _transformers }
885
878
) ;
886
879
allDiagnostics . push ( ...emitResult . diagnostics ) ;
887
880
timeEnd ( timeLabel ) ;
@@ -916,7 +909,11 @@ export class AngularCompilerPlugin implements Tapable {
916
909
time ( 'AngularCompilerPlugin._emit.ng.emit' ) ;
917
910
const extractI18n = ! ! this . _compilerOptions . i18nOutFile ;
918
911
const emitFlags = extractI18n ? EmitFlags . I18nBundle : EmitFlags . Default ;
919
- emitResult = angularProgram . emit ( { emitFlags, customTransformers } ) ;
912
+ emitResult = angularProgram . emit ( {
913
+ emitFlags, customTransformers : {
914
+ beforeTs : this . _transformers
915
+ }
916
+ } ) ;
920
917
allDiagnostics . push ( ...emitResult . diagnostics ) ;
921
918
if ( extractI18n ) {
922
919
this . writeI18nOutFile ( ) ;
0 commit comments