@@ -47,6 +47,7 @@ import {
47
47
createProgram ,
48
48
createCompilerHost ,
49
49
formatDiagnostics ,
50
+ readConfiguration ,
50
51
} from './ngtools_api' ;
51
52
import { findAstNodes } from './transformers/ast_helpers' ;
52
53
@@ -72,8 +73,6 @@ export interface AngularCompilerPluginOptions {
72
73
platform ?: PLATFORM ;
73
74
74
75
// Use tsconfig to include path globs.
75
- exclude ?: string | string [ ] ;
76
- include ?: string [ ] ;
77
76
compilerOptions ?: ts . CompilerOptions ;
78
77
}
79
78
@@ -86,13 +85,11 @@ export class AngularCompilerPlugin implements Tapable {
86
85
private _options : AngularCompilerPluginOptions ;
87
86
88
87
// TS compilation.
89
- private _compilerOptions : ts . CompilerOptions ;
90
- private _angularCompilerOptions : CompilerOptions ;
91
- private _tsFilenames : string [ ] ;
88
+ private _compilerOptions : CompilerOptions ;
89
+ private _rootNames : string [ ] ;
92
90
private _program : ( ts . Program | Program ) ;
93
- private _compilerHost : WebpackCompilerHost ;
91
+ private _compilerHost : WebpackCompilerHost & CompilerHost ;
94
92
private _moduleResolutionCache : ts . ModuleResolutionCache ;
95
- private _angularCompilerHost : WebpackCompilerHost & CompilerHost ;
96
93
private _resourceLoader : WebpackResourceLoader ;
97
94
// Contains `moduleImportPath#exportName` => `fullModulePath`.
98
95
private _lazyRoutes : LazyRouteMap = Object . create ( null ) ;
@@ -186,39 +183,11 @@ export class AngularCompilerPlugin implements Tapable {
186
183
) ;
187
184
}
188
185
189
- // Default exclude to **/*.spec.ts files.
190
- if ( ! options . hasOwnProperty ( 'exclude' ) ) {
191
- options [ 'exclude' ] = [ '**/*.spec.ts' ] ;
192
- }
193
-
194
- // Add custom excludes to default TypeScript excludes.
195
- if ( options . hasOwnProperty ( 'exclude' ) ) {
196
- // If the tsconfig doesn't contain any excludes, we must add the default ones before adding
197
- // any extra ones (otherwise we'd include all of these which can cause unexpected errors).
198
- // This is the same logic as present in TypeScript.
199
- if ( ! tsConfigJson . exclude ) {
200
- tsConfigJson [ 'exclude' ] = [ 'node_modules' , 'bower_components' , 'jspm_packages' ] ;
201
- if ( tsConfigJson . compilerOptions && tsConfigJson . compilerOptions . outDir ) {
202
- tsConfigJson . exclude . push ( tsConfigJson . compilerOptions . outDir ) ;
203
- }
204
- }
205
-
206
- // Join our custom excludes with the existing ones.
207
- tsConfigJson . exclude = tsConfigJson . exclude . concat ( options . exclude ) ;
208
- }
209
-
210
- // Add extra includes.
211
- if ( options . hasOwnProperty ( 'include' ) && Array . isArray ( options . include ) ) {
212
- tsConfigJson . include = tsConfigJson . include || [ ] ;
213
- tsConfigJson . include . push ( ...options . include ) ;
214
- }
215
-
216
186
// Parse the tsconfig contents.
217
- const tsConfig = ts . parseJsonConfigFileContent (
218
- tsConfigJson , ts . sys , basePath , undefined , this . _tsConfigPath ) ;
187
+ const config = readConfiguration ( this . _tsConfigPath , tsConfigJson . compilerOptions ) ;
219
188
220
- this . _tsFilenames = tsConfig . fileNames ;
221
- this . _compilerOptions = tsConfig . options ;
189
+ this . _rootNames = config . rootNames ;
190
+ this . _compilerOptions = config . options ;
222
191
223
192
// Overwrite outDir so we can find generated files next to their .ts origin in compilerHost.
224
193
this . _compilerOptions . outDir = '' ;
@@ -250,55 +219,54 @@ export class AngularCompilerPlugin implements Tapable {
250
219
// to the webpack dependency tree and rebuilds triggered by file edits.
251
220
this . _compilerOptions . noEmitOnError = false ;
252
221
253
- // Compose Angular Compiler Options.
254
- this . _angularCompilerOptions = Object . assign (
255
- this . _compilerOptions ,
256
- tsConfig . raw [ 'angularCompilerOptions' ] ,
257
- { basePath }
258
- ) ;
259
-
260
222
// Set JIT (no code generation) or AOT mode.
261
223
if ( options . skipCodeGeneration !== undefined ) {
262
224
this . _JitMode = options . skipCodeGeneration ;
263
225
}
264
226
265
227
// Process i18n options.
266
228
if ( options . hasOwnProperty ( 'i18nInFile' ) ) {
267
- this . _angularCompilerOptions . i18nInFile = options . i18nInFile ;
229
+ this . _compilerOptions . i18nInFile = options . i18nInFile ;
268
230
}
269
231
if ( options . hasOwnProperty ( 'i18nInFormat' ) ) {
270
- this . _angularCompilerOptions . i18nInFormat = options . i18nInFormat ;
232
+ this . _compilerOptions . i18nInFormat = options . i18nInFormat ;
271
233
}
272
234
if ( options . hasOwnProperty ( 'i18nOutFile' ) ) {
273
- this . _angularCompilerOptions . i18nOutFile = options . i18nOutFile ;
235
+ this . _compilerOptions . i18nOutFile = options . i18nOutFile ;
274
236
}
275
237
if ( options . hasOwnProperty ( 'i18nOutFormat' ) ) {
276
- this . _angularCompilerOptions . i18nOutFormat = options . i18nOutFormat ;
238
+ this . _compilerOptions . i18nOutFormat = options . i18nOutFormat ;
277
239
}
278
240
if ( options . hasOwnProperty ( 'locale' ) && options . locale ) {
279
- this . _angularCompilerOptions . i18nInLocale = this . _validateLocale ( options . locale ) ;
241
+ this . _compilerOptions . i18nInLocale = this . _validateLocale ( options . locale ) ;
280
242
}
281
243
if ( options . hasOwnProperty ( 'missingTranslation' ) ) {
282
- this . _angularCompilerOptions . i18nInMissingTranslations =
244
+ this . _compilerOptions . i18nInMissingTranslations =
283
245
options . missingTranslation as 'error' | 'warning' | 'ignore' ;
284
246
}
285
247
286
248
// Use entryModule if available in options, otherwise resolve it from mainPath after program
287
249
// creation.
288
250
if ( this . _options . entryModule ) {
289
251
this . _entryModule = this . _options . entryModule ;
290
- } else if ( this . _angularCompilerOptions . entryModule ) {
252
+ } else if ( this . _compilerOptions . entryModule ) {
291
253
this . _entryModule = path . resolve ( this . _basePath ,
292
- this . _angularCompilerOptions . entryModule ) ;
254
+ this . _compilerOptions . entryModule ) ;
293
255
}
294
256
295
257
// Create the webpack compiler host.
296
- this . _compilerHost = new WebpackCompilerHost ( this . _compilerOptions , this . _basePath ) ;
297
- this . _compilerHost . enableCaching ( ) ;
258
+ const webpackCompilerHost = new WebpackCompilerHost ( this . _compilerOptions , this . _basePath ) ;
259
+ webpackCompilerHost . enableCaching ( ) ;
298
260
299
261
// Create and set a new WebpackResourceLoader.
300
262
this . _resourceLoader = new WebpackResourceLoader ( ) ;
301
- this . _compilerHost . setResourceLoader ( this . _resourceLoader ) ;
263
+ webpackCompilerHost . setResourceLoader ( this . _resourceLoader ) ;
264
+
265
+ // Use the WebpackCompilerHost with a resource loader to create an AngularCompilerHost.
266
+ this . _compilerHost = createCompilerHost ( {
267
+ options : this . _compilerOptions ,
268
+ tsHost : webpackCompilerHost
269
+ } ) as CompilerHost & WebpackCompilerHost ;
302
270
303
271
// Override some files in the FileSystem.
304
272
if ( this . _options . hostOverrideFileSystem ) {
@@ -342,28 +310,38 @@ export class AngularCompilerPlugin implements Tapable {
342
310
private _createOrUpdateProgram ( ) {
343
311
return Promise . resolve ( )
344
312
. then ( ( ) => {
345
- const changedTsFiles = this . _getChangedTsFiles ( ) ;
346
-
347
- changedTsFiles . forEach ( ( file ) => {
348
- if ( ! this . _tsFilenames . includes ( file ) ) {
313
+ // Get the root files from the ts config.
314
+ // When a new root name (like a lazy route) is added, it won't be available from
315
+ // following imports on the existing files, so we need to get the new list of root files.
316
+ this . _rootNames = readConfiguration ( this . _tsConfigPath ) . rootNames ;
317
+
318
+ // *************************************************************************************
319
+ // REMOVE THIS WITH UPDATED NG5 SCHEMATICS
320
+ // Doing this is EXTREMELY wrong and we have to tell users to update their package.json
321
+ // instead.
322
+ // `src/tsconfig.spec.json` needs to be updated with `"include": [ "**/*.ts" ]`
323
+ // *************************************************************************************
324
+ this . _getChangedTsFiles ( ) . forEach ( ( file ) => {
325
+ if ( ! this . _rootNames . includes ( file ) ) {
349
326
// TODO: figure out if action is needed for files that were removed from the
350
327
// compilation.
351
- this . _tsFilenames . push ( file ) ;
328
+ this . _rootNames . push ( file ) ;
352
329
}
353
330
} ) ;
354
331
355
- // Update the forked type checker.
332
+ // Update the forked type checker with all changed compilation files.
333
+ // This includes templates, that also need to be reloaded on the type checker.
356
334
if ( this . _forkTypeChecker && ! this . _firstRun ) {
357
- this . _updateForkedTypeChecker ( changedTsFiles ) ;
335
+ this . _updateForkedTypeChecker ( this . _rootNames , this . _getChangedCompilationFiles ( ) ) ;
358
336
}
359
337
360
338
if ( this . _JitMode ) {
361
339
// Create the TypeScript program.
362
340
time ( 'AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram' ) ;
363
341
this . _program = ts . createProgram (
364
- this . _tsFilenames ,
365
- this . _angularCompilerOptions ,
366
- this . _angularCompilerHost ,
342
+ this . _rootNames ,
343
+ this . _compilerOptions ,
344
+ this . _compilerHost ,
367
345
this . _program as ts . Program
368
346
) ;
369
347
timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram' ) ;
@@ -372,26 +350,19 @@ export class AngularCompilerPlugin implements Tapable {
372
350
} else {
373
351
time ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram' ) ;
374
352
// Create the Angular program.
375
- try {
376
- this . _program = createProgram ( {
377
- rootNames : this . _tsFilenames ,
378
- options : this . _angularCompilerOptions ,
379
- host : this . _angularCompilerHost ,
380
- oldProgram : this . _program as Program
353
+ this . _program = createProgram ( {
354
+ rootNames : this . _rootNames ,
355
+ options : this . _compilerOptions ,
356
+ host : this . _compilerHost ,
357
+ oldProgram : this . _program as Program
358
+ } ) ;
359
+ timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram' ) ;
360
+
361
+ time ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ;
362
+ return this . _program . loadNgStructureAsync ( )
363
+ . then ( ( ) => {
364
+ timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ;
381
365
} ) ;
382
- timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram' ) ;
383
-
384
- time ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ;
385
- return this . _program . loadNgStructureAsync ( )
386
- . then ( ( ) => {
387
- timeEnd ( 'AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync' ) ;
388
- } ) ;
389
- } catch ( e ) {
390
- // TODO: remove this when the issue is addressed.
391
- // Temporary workaround for https://github.com/angular/angular/issues/19951
392
- this . _program = undefined ;
393
- throw e ;
394
- }
395
366
}
396
367
} )
397
368
. then ( ( ) => {
@@ -412,7 +383,7 @@ export class AngularCompilerPlugin implements Tapable {
412
383
const result = __NGTOOLS_PRIVATE_API_2 . listLazyRoutes ( {
413
384
program : this . _getTsProgram ( ) ,
414
385
host : this . _compilerHost ,
415
- angularCompilerOptions : Object . assign ( { } , this . _angularCompilerOptions , {
386
+ angularCompilerOptions : Object . assign ( { } , this . _compilerOptions , {
416
387
// genDir seems to still be needed in @angular \compiler-cli\src\compiler_host.js:226.
417
388
genDir : ''
418
389
} ) ,
@@ -506,10 +477,8 @@ export class AngularCompilerPlugin implements Tapable {
506
477
) ;
507
478
}
508
479
} else {
509
- // Found a new route, add it to the map and read it into the compiler host .
480
+ // Found a new route, add it to the map.
510
481
this . _lazyRoutes [ moduleKey ] = modulePath ;
511
- this . _angularCompilerHost . readFile ( lazyRouteTSFile ) ;
512
- this . _angularCompilerHost . invalidate ( lazyRouteTSFile ) ;
513
482
}
514
483
} ) ;
515
484
}
@@ -545,7 +514,7 @@ export class AngularCompilerPlugin implements Tapable {
545
514
546
515
this . _typeCheckerProcess = fork ( path . resolve ( __dirname , typeCheckerFile ) , [ ] , forkOptions ) ;
547
516
this . _typeCheckerProcess . send ( new InitMessage ( this . _compilerOptions , this . _basePath ,
548
- this . _JitMode , this . _tsFilenames ) ) ;
517
+ this . _JitMode , this . _rootNames ) ) ;
549
518
550
519
// Cleanup.
551
520
const killTypeCheckerProcess = ( ) => {
@@ -557,8 +526,8 @@ export class AngularCompilerPlugin implements Tapable {
557
526
process . once ( 'uncaughtException' , killTypeCheckerProcess ) ;
558
527
}
559
528
560
- private _updateForkedTypeChecker ( changedTsFiles : string [ ] ) {
561
- this . _typeCheckerProcess . send ( new UpdateMessage ( changedTsFiles ) ) ;
529
+ private _updateForkedTypeChecker ( rootNames : string [ ] , changedCompilationFiles : string [ ] ) {
530
+ this . _typeCheckerProcess . send ( new UpdateMessage ( rootNames , changedCompilationFiles ) ) ;
562
531
}
563
532
564
533
@@ -682,22 +651,12 @@ export class AngularCompilerPlugin implements Tapable {
682
651
// Update the resource loader with the new webpack compilation.
683
652
this . _resourceLoader . update ( compilation ) ;
684
653
654
+ // Create a new process for the type checker on the second build if there isn't one yet.
655
+ if ( this . _forkTypeChecker && ! this . _firstRun && ! this . _typeCheckerProcess ) {
656
+ this . _createForkedTypeChecker ( ) ;
657
+ }
658
+
685
659
this . _donePromise = Promise . resolve ( )
686
- . then ( ( ) => {
687
- // Create a new process for the type checker.
688
- if ( this . _forkTypeChecker && ! this . _firstRun && ! this . _typeCheckerProcess ) {
689
- this . _createForkedTypeChecker ( ) ;
690
- }
691
- } )
692
- . then ( ( ) => {
693
- if ( this . _firstRun ) {
694
- // Use the WebpackResourceLoader with a resource loader to create an AngularCompilerHost.
695
- this . _angularCompilerHost = createCompilerHost ( {
696
- options : this . _angularCompilerOptions ,
697
- tsHost : this . _compilerHost
698
- } ) as CompilerHost & WebpackCompilerHost ;
699
- }
700
- } )
701
660
. then ( ( ) => this . _update ( ) )
702
661
. then ( ( ) => {
703
662
timeEnd ( 'AngularCompilerPlugin._make' ) ;
@@ -731,18 +690,14 @@ export class AngularCompilerPlugin implements Tapable {
731
690
const changedTsFiles = this . _getChangedTsFiles ( ) ;
732
691
if ( this . _ngCompilerSupportsNewApi ) {
733
692
this . _processLazyRoutes ( this . _listLazyRoutesFromProgram ( ) ) ;
734
- // TODO: remove this when the issue is addressed.
735
- // Fix for a bug in compiler where the program needs to be updated after
736
- // _listLazyRoutesFromProgram is called.
737
- return this . _createOrUpdateProgram ( ) ;
738
693
} else if ( this . _firstRun ) {
739
694
this . _processLazyRoutes ( this . _getLazyRoutesFromNgtools ( ) ) ;
740
695
} else if ( changedTsFiles . length > 0 ) {
741
696
this . _processLazyRoutes ( this . _findLazyRoutesInAst ( changedTsFiles ) ) ;
742
697
}
743
698
} )
744
699
. then ( ( ) => {
745
- // Build transforms, emit and report errorsn .
700
+ // Build transforms, emit and report errors .
746
701
747
702
// We now have the final list of changed TS files.
748
703
// Go through each changed file and add transforms as needed.
@@ -772,11 +727,11 @@ export class AngularCompilerPlugin implements Tapable {
772
727
}
773
728
774
729
// If we have a locale, auto import the locale data file.
775
- if ( this . _angularCompilerOptions . i18nInLocale ) {
730
+ if ( this . _compilerOptions . i18nInLocale ) {
776
731
transformOps . push ( ...registerLocaleData (
777
732
sf ,
778
733
this . entryModule ,
779
- this . _angularCompilerOptions . i18nInLocale
734
+ this . _compilerOptions . i18nInLocale
780
735
) ) ;
781
736
}
782
737
} else if ( this . _platform === PLATFORM . Server ) {
@@ -846,7 +801,7 @@ export class AngularCompilerPlugin implements Tapable {
846
801
}
847
802
848
803
// Write the extracted messages to disk.
849
- const i18nOutFilePath = path . resolve ( this . _basePath , this . _angularCompilerOptions . i18nOutFile ) ;
804
+ const i18nOutFilePath = path . resolve ( this . _basePath , this . _compilerOptions . i18nOutFile ) ;
850
805
const i18nOutFileContent = this . _compilerHost . readFile ( i18nOutFilePath ) ;
851
806
if ( i18nOutFileContent ) {
852
807
_recursiveMkDir ( path . dirname ( i18nOutFilePath ) )
@@ -994,17 +949,14 @@ export class AngularCompilerPlugin implements Tapable {
994
949
995
950
if ( ! hasErrors ( allDiagnostics ) ) {
996
951
time ( 'AngularCompilerPlugin._emit.ng.emit' ) ;
997
- const extractI18n = ! ! this . _angularCompilerOptions . i18nOutFile ;
952
+ const extractI18n = ! ! this . _compilerOptions . i18nOutFile ;
998
953
const emitFlags = extractI18n ? EmitFlags . I18nBundle : EmitFlags . Default ;
999
954
emitResult = angularProgram . emit ( { emitFlags, customTransformers } ) ;
1000
955
allDiagnostics . push ( ...emitResult . diagnostics ) ;
1001
956
if ( extractI18n ) {
1002
957
this . writeI18nOutFile ( ) ;
1003
958
}
1004
959
timeEnd ( 'AngularCompilerPlugin._emit.ng.emit' ) ;
1005
- } else {
1006
- // Throw away the old program if there was an error.
1007
- this . _program = undefined ;
1008
960
}
1009
961
}
1010
962
} catch ( e ) {
0 commit comments