Skip to content

Commit 2e6eea4

Browse files
clydinKeen Yee Liau
authored andcommitted
refactor(@ngtools/webpack): use async/await where possible
1 parent 4c5ed59 commit 2e6eea4

File tree

1 file changed

+134
-144
lines changed

1 file changed

+134
-144
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 134 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -346,81 +346,72 @@ export class AngularCompilerPlugin {
346346
});
347347
}
348348

349-
private _createOrUpdateProgram() {
350-
return Promise.resolve()
351-
.then(() => {
352-
// Get the root files from the ts config.
353-
// When a new root name (like a lazy route) is added, it won't be available from
354-
// following imports on the existing files, so we need to get the new list of root files.
355-
const config = readConfiguration(this._tsConfigPath);
356-
this._rootNames = config.rootNames.concat(...this._singleFileIncludes);
357-
358-
// Update the forked type checker with all changed compilation files.
359-
// This includes templates, that also need to be reloaded on the type checker.
360-
if (this._forkTypeChecker && this._typeCheckerProcess && !this._firstRun) {
361-
this._updateForkedTypeChecker(this._rootNames, this._getChangedCompilationFiles());
362-
}
349+
private async _createOrUpdateProgram() {
350+
// Get the root files from the ts config.
351+
// When a new root name (like a lazy route) is added, it won't be available from
352+
// following imports on the existing files, so we need to get the new list of root files.
353+
const config = readConfiguration(this._tsConfigPath);
354+
this._rootNames = config.rootNames.concat(...this._singleFileIncludes);
363355

364-
// Use an identity function as all our paths are absolute already.
365-
this._moduleResolutionCache = ts.createModuleResolutionCache(this._basePath, x => x);
356+
// Update the forked type checker with all changed compilation files.
357+
// This includes templates, that also need to be reloaded on the type checker.
358+
if (this._forkTypeChecker && this._typeCheckerProcess && !this._firstRun) {
359+
this._updateForkedTypeChecker(this._rootNames, this._getChangedCompilationFiles());
360+
}
366361

367-
const tsProgram = this._getTsProgram();
368-
const oldFiles = new Set(tsProgram ?
369-
tsProgram.getSourceFiles().map(sf => sf.fileName)
370-
: [],
371-
);
362+
// Use an identity function as all our paths are absolute already.
363+
this._moduleResolutionCache = ts.createModuleResolutionCache(this._basePath, x => x);
372364

373-
if (this._JitMode) {
374-
// Create the TypeScript program.
375-
time('AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram');
376-
this._program = ts.createProgram(
377-
this._rootNames,
378-
this._compilerOptions,
379-
this._compilerHost,
380-
tsProgram,
381-
);
382-
timeEnd('AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram');
365+
const tsProgram = this._getTsProgram();
366+
const oldFiles = new Set(tsProgram ?
367+
tsProgram.getSourceFiles().map(sf => sf.fileName)
368+
: [],
369+
);
383370

384-
const newFiles = this._program.getSourceFiles().filter(sf => !oldFiles.has(sf.fileName));
385-
for (const newFile of newFiles) {
386-
this._compilerHost.invalidate(newFile.fileName);
387-
}
371+
if (this._JitMode) {
372+
// Create the TypeScript program.
373+
time('AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram');
374+
this._program = ts.createProgram(
375+
this._rootNames,
376+
this._compilerOptions,
377+
this._compilerHost,
378+
tsProgram,
379+
);
380+
timeEnd('AngularCompilerPlugin._createOrUpdateProgram.ts.createProgram');
388381

389-
return Promise.resolve();
390-
} else {
391-
time('AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram');
392-
// Create the Angular program.
393-
this._program = createProgram({
394-
rootNames: this._rootNames,
395-
options: this._compilerOptions,
396-
host: this._compilerHost,
397-
oldProgram: this._program as Program,
398-
});
399-
timeEnd('AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram');
382+
const newFiles = this._program.getSourceFiles().filter(sf => !oldFiles.has(sf.fileName));
383+
for (const newFile of newFiles) {
384+
this._compilerHost.invalidate(newFile.fileName);
385+
}
386+
} else {
387+
time('AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram');
388+
// Create the Angular program.
389+
this._program = createProgram({
390+
rootNames: this._rootNames,
391+
options: this._compilerOptions,
392+
host: this._compilerHost,
393+
oldProgram: this._program as Program,
394+
});
395+
timeEnd('AngularCompilerPlugin._createOrUpdateProgram.ng.createProgram');
400396

401-
time('AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync');
397+
time('AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync');
398+
await this._program.loadNgStructureAsync();
399+
timeEnd('AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync');
402400

403-
return this._program.loadNgStructureAsync()
404-
.then(() => {
405-
timeEnd('AngularCompilerPlugin._createOrUpdateProgram.ng.loadNgStructureAsync');
401+
const newFiles = this._program.getTsProgram()
402+
.getSourceFiles().filter(sf => !oldFiles.has(sf.fileName));
403+
for (const newFile of newFiles) {
404+
this._compilerHost.invalidate(newFile.fileName);
405+
}
406+
}
406407

407-
const newFiles = (this._program as Program).getTsProgram()
408-
.getSourceFiles().filter(sf => !oldFiles.has(sf.fileName));
409-
for (const newFile of newFiles) {
410-
this._compilerHost.invalidate(newFile.fileName);
411-
}
412-
});
413-
}
414-
})
415-
.then(() => {
416-
// If there's still no entryModule try to resolve from mainPath.
417-
if (!this._entryModule && this._mainPath) {
418-
time('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
419-
this._entryModule = resolveEntryModuleFromMain(
420-
this._mainPath, this._compilerHost, this._getTsProgram() as ts.Program);
421-
timeEnd('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
422-
}
423-
});
408+
// If there's still no entryModule try to resolve from mainPath.
409+
if (!this._entryModule && this._mainPath) {
410+
time('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
411+
this._entryModule = resolveEntryModuleFromMain(
412+
this._mainPath, this._compilerHost, this._getTsProgram() as ts.Program);
413+
timeEnd('AngularCompilerPlugin._make.resolveEntryModuleFromMain');
414+
}
424415
}
425416

426417
private _getLazyRoutesFromNgtools() {
@@ -734,7 +725,10 @@ export class AngularCompilerPlugin {
734725
compiler.hooks.watchClose.tap('angular-compiler', () => this._killForkedTypeChecker());
735726

736727
// Remake the plugin on each compilation.
737-
compiler.hooks.make.tapPromise('angular-compiler', compilation => this._make(compilation));
728+
compiler.hooks.make.tapPromise(
729+
'angular-compiler',
730+
compilation => this._donePromise = this._make(compilation),
731+
);
738732
compiler.hooks.invalid.tap('angular-compiler', () => this._firstRun = false);
739733
compiler.hooks.afterEmit.tap('angular-compiler', compilation => {
740734
// tslint:disable-next-line:no-any
@@ -802,16 +796,15 @@ export class AngularCompilerPlugin {
802796
// Update the resource loader with the new webpack compilation.
803797
this._resourceLoader.update(compilation);
804798

805-
return this._donePromise = Promise.resolve()
806-
.then(() => this._update())
807-
.then(() => {
808-
this.pushCompilationErrors(compilation);
809-
timeEnd('AngularCompilerPlugin._make');
810-
}, err => {
811-
compilation.errors.push(err);
812-
this.pushCompilationErrors(compilation);
813-
timeEnd('AngularCompilerPlugin._make');
814-
});
799+
try {
800+
await this._update();
801+
this.pushCompilationErrors(compilation);
802+
} catch (err) {
803+
compilation.errors.push(err);
804+
this.pushCompilationErrors(compilation);
805+
}
806+
807+
timeEnd('AngularCompilerPlugin._make');
815808
}
816809

817810
private pushCompilationErrors(compilation: compilation.Compilation) {
@@ -868,7 +861,7 @@ export class AngularCompilerPlugin {
868861
}
869862
}
870863

871-
private _update() {
864+
private async _update() {
872865
time('AngularCompilerPlugin._update');
873866
// We only want to update on TS and template changes, but all kinds of files are on this
874867
// list, like package.json and .ngsummary.json files.
@@ -879,75 +872,72 @@ export class AngularCompilerPlugin {
879872
return Promise.resolve();
880873
}
881874

882-
return Promise.resolve()
883-
// Make a new program and load the Angular structure.
884-
.then(() => this._createOrUpdateProgram())
885-
.then(() => {
886-
if (this.entryModule) {
887-
// Try to find lazy routes if we have an entry module.
888-
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
889-
// and other things that we might miss using the (faster) findLazyRoutesInAst.
890-
// Lazy routes modules will be read with compilerHost and added to the changed files.
891-
if (this._ngCompilerSupportsNewApi) {
892-
this._processLazyRoutes(this._listLazyRoutesFromProgram());
893-
} else if (this._firstRun) {
894-
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
895-
} else {
896-
const changedTsFiles = this._getChangedTsFiles();
897-
if (changedTsFiles.length > 0) {
898-
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
899-
}
900-
}
901-
if (this._options.additionalLazyModules) {
902-
this._processLazyRoutes(this._options.additionalLazyModules);
903-
}
904-
}
905-
})
906-
.then(() => {
907-
// Emit and report errors.
908-
909-
// We now have the final list of changed TS files.
910-
// Go through each changed file and add transforms as needed.
911-
const sourceFiles = this._getChangedTsFiles()
912-
.map((fileName) => (this._getTsProgram() as ts.Program).getSourceFile(fileName))
913-
// At this point we shouldn't need to filter out undefined files, because any ts file
914-
// that changed should be emitted.
915-
// But due to hostReplacementPaths there can be files (the environment files)
916-
// that changed but aren't part of the compilation, specially on `ng test`.
917-
// So we ignore missing source files files here.
918-
// hostReplacementPaths needs to be fixed anyway to take care of the following issue.
919-
// https://github.com/angular/angular-cli/issues/7305#issuecomment-332150230
920-
.filter((x) => !!x) as ts.SourceFile[];
921-
922-
// Emit files.
923-
time('AngularCompilerPlugin._update._emit');
924-
const { emitResult, diagnostics } = this._emit(sourceFiles);
925-
timeEnd('AngularCompilerPlugin._update._emit');
926-
927-
// Report diagnostics.
928-
const errors = diagnostics
929-
.filter((diag) => diag.category === ts.DiagnosticCategory.Error);
930-
const warnings = diagnostics
931-
.filter((diag) => diag.category === ts.DiagnosticCategory.Warning);
932-
933-
if (errors.length > 0) {
934-
const message = formatDiagnostics(errors);
935-
this._errors.push(new Error(message));
875+
// Make a new program and load the Angular structure.
876+
await this._createOrUpdateProgram();
877+
878+
if (this.entryModule) {
879+
// Try to find lazy routes if we have an entry module.
880+
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
881+
// and other things that we might miss using the (faster) findLazyRoutesInAst.
882+
// Lazy routes modules will be read with compilerHost and added to the changed files.
883+
if (this._ngCompilerSupportsNewApi) {
884+
this._processLazyRoutes(this._listLazyRoutesFromProgram());
885+
} else if (this._firstRun) {
886+
this._processLazyRoutes(this._getLazyRoutesFromNgtools());
887+
} else {
888+
const changedTsFiles = this._getChangedTsFiles();
889+
if (changedTsFiles.length > 0) {
890+
this._processLazyRoutes(this._findLazyRoutesInAst(changedTsFiles));
936891
}
892+
}
893+
if (this._options.additionalLazyModules) {
894+
this._processLazyRoutes(this._options.additionalLazyModules);
895+
}
896+
}
937897

938-
if (warnings.length > 0) {
939-
const message = formatDiagnostics(warnings);
940-
this._warnings.push(message);
941-
}
898+
// Emit and report errors.
899+
900+
// We now have the final list of changed TS files.
901+
// Go through each changed file and add transforms as needed.
902+
const sourceFiles = this._getChangedTsFiles()
903+
.map((fileName) => (this._getTsProgram() as ts.Program).getSourceFile(fileName))
904+
// At this point we shouldn't need to filter out undefined files, because any ts file
905+
// that changed should be emitted.
906+
// But due to hostReplacementPaths there can be files (the environment files)
907+
// that changed but aren't part of the compilation, specially on `ng test`.
908+
// So we ignore missing source files files here.
909+
// hostReplacementPaths needs to be fixed anyway to take care of the following issue.
910+
// https://github.com/angular/angular-cli/issues/7305#issuecomment-332150230
911+
.filter((x) => !!x) as ts.SourceFile[];
912+
913+
// Emit files.
914+
time('AngularCompilerPlugin._update._emit');
915+
const { emitResult, diagnostics } = this._emit(sourceFiles);
916+
timeEnd('AngularCompilerPlugin._update._emit');
917+
918+
// Report diagnostics.
919+
const errors = diagnostics
920+
.filter((diag) => diag.category === ts.DiagnosticCategory.Error);
921+
const warnings = diagnostics
922+
.filter((diag) => diag.category === ts.DiagnosticCategory.Warning);
923+
924+
if (errors.length > 0) {
925+
const message = formatDiagnostics(errors);
926+
this._errors.push(new Error(message));
927+
}
942928

943-
this._emitSkipped = !emitResult || emitResult.emitSkipped;
929+
if (warnings.length > 0) {
930+
const message = formatDiagnostics(warnings);
931+
this._warnings.push(message);
932+
}
944933

945-
// Reset changed files on successful compilation.
946-
if (!this._emitSkipped && this._errors.length === 0) {
947-
this._compilerHost.resetChangedFileTracker();
948-
}
949-
timeEnd('AngularCompilerPlugin._update');
950-
});
934+
this._emitSkipped = !emitResult || emitResult.emitSkipped;
935+
936+
// Reset changed files on successful compilation.
937+
if (!this._emitSkipped && this._errors.length === 0) {
938+
this._compilerHost.resetChangedFileTracker();
939+
}
940+
timeEnd('AngularCompilerPlugin._update');
951941
}
952942

953943
writeI18nOutFile() {

0 commit comments

Comments
 (0)