Skip to content

Commit 1bd1889

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
refactor(@ngtools/webpack): cleanup/optimize emit logic
1 parent 747b751 commit 1bd1889

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,6 @@ export class AngularCompilerPlugin {
327327
return this._JitMode ? this._program as ts.Program : (this._program as Program).getTsProgram();
328328
}
329329

330-
private _getChangedTsFiles() {
331-
return this._compilerHost.getChangedFilePaths()
332-
.filter(k => (k.endsWith('.ts') || k.endsWith('.tsx')) && !k.endsWith('.d.ts'))
333-
.filter(k => this._compilerHost.fileExists(k));
334-
}
335-
336330
updateChangedFileExtensions(extension: string) {
337331
if (extension) {
338332
this._changedFileExtensions.add(extension);
@@ -873,35 +867,16 @@ export class AngularCompilerPlugin {
873867
// Make a new program and load the Angular structure.
874868
await this._createOrUpdateProgram();
875869

876-
// Try to find lazy routes if we have an entry module.
877-
// We need to run the `listLazyRoutes` the first time because it also navigates libraries
878-
// and other things that we might miss using the (faster) findLazyRoutesInAst.
879-
// Lazy routes modules will be read with compilerHost and added to the changed files.
870+
// Find lazy routes
880871
const lazyRouteMap: LazyRouteMap = {
881-
... (this._entryModule || !this._JitMode ? this._listLazyRoutesFromProgram() : {}),
872+
...this._listLazyRoutesFromProgram(),
882873
...this._options.additionalLazyModules,
883874
};
884-
885875
this._processLazyRoutes(lazyRouteMap);
886876

887-
// Emit and report errors.
888-
889-
// We now have the final list of changed TS files.
890-
// Go through each changed file and add transforms as needed.
891-
const sourceFiles = this._getChangedTsFiles()
892-
.map((fileName) => (this._getTsProgram() as ts.Program).getSourceFile(fileName))
893-
// At this point we shouldn't need to filter out undefined files, because any ts file
894-
// that changed should be emitted.
895-
// But due to hostReplacementPaths there can be files (the environment files)
896-
// that changed but aren't part of the compilation, specially on `ng test`.
897-
// So we ignore missing source files files here.
898-
// hostReplacementPaths needs to be fixed anyway to take care of the following issue.
899-
// https://github.com/angular/angular-cli/issues/7305#issuecomment-332150230
900-
.filter((x) => !!x) as ts.SourceFile[];
901-
902877
// Emit files.
903878
time('AngularCompilerPlugin._update._emit');
904-
const { emitResult, diagnostics } = this._emit(sourceFiles);
879+
const { emitResult, diagnostics } = this._emit();
905880
timeEnd('AngularCompilerPlugin._update._emit');
906881

907882
// Report diagnostics.
@@ -1042,7 +1017,7 @@ export class AngularCompilerPlugin {
10421017
// This code mostly comes from `performCompilation` in `@angular/compiler-cli`.
10431018
// It skips the program creation because we need to use `loadNgStructureAsync()`,
10441019
// and uses CustomTransformers.
1045-
private _emit(sourceFiles: ts.SourceFile[]) {
1020+
private _emit() {
10461021
time('AngularCompilerPlugin._emit');
10471022
const program = this._program;
10481023
const allDiagnostics: Array<ts.Diagnostic | Diagnostic> = [];
@@ -1053,19 +1028,33 @@ export class AngularCompilerPlugin {
10531028
try {
10541029
if (this._JitMode) {
10551030
const tsProgram = program as ts.Program;
1031+
const changedTsFiles = new Set<string>();
10561032

10571033
if (this._firstRun) {
10581034
// Check parameter diagnostics.
10591035
time('AngularCompilerPlugin._emit.ts.getOptionsDiagnostics');
10601036
allDiagnostics.push(...tsProgram.getOptionsDiagnostics());
10611037
timeEnd('AngularCompilerPlugin._emit.ts.getOptionsDiagnostics');
1038+
} else {
1039+
// generate a list of changed files for emit
1040+
// not needed on first run since a full program emit is required
1041+
for (const changedFile of this._compilerHost.getChangedFilePaths()) {
1042+
if (!changedFile.endsWith('.ts') && !changedFile.endsWith('.tsx')) {
1043+
continue;
1044+
}
1045+
// existing type definitions are not emitted
1046+
if (changedFile.endsWith('.d.ts')) {
1047+
continue;
1048+
}
1049+
changedTsFiles.add(changedFile);
1050+
}
10621051
}
10631052

10641053
allDiagnostics.push(...gatherDiagnostics(tsProgram, this._JitMode,
10651054
'AngularCompilerPlugin._emit.ts', diagMode));
10661055

10671056
if (!hasErrors(allDiagnostics)) {
1068-
if (this._firstRun || sourceFiles.length > 20) {
1057+
if (this._firstRun || changedTsFiles.size > 20) {
10691058
emitResult = tsProgram.emit(
10701059
undefined,
10711060
undefined,
@@ -1075,15 +1064,20 @@ export class AngularCompilerPlugin {
10751064
);
10761065
allDiagnostics.push(...emitResult.diagnostics);
10771066
} else {
1078-
sourceFiles.forEach((sf) => {
1079-
const timeLabel = `AngularCompilerPlugin._emit.ts+${sf.fileName}+.emit`;
1067+
for (const changedFile of changedTsFiles) {
1068+
const sourceFile = tsProgram.getSourceFile(changedFile);
1069+
if (!sourceFile) {
1070+
continue;
1071+
}
1072+
1073+
const timeLabel = `AngularCompilerPlugin._emit.ts+${sourceFile.fileName}+.emit`;
10801074
time(timeLabel);
1081-
emitResult = tsProgram.emit(sf, undefined, undefined, undefined,
1075+
emitResult = tsProgram.emit(sourceFile, undefined, undefined, undefined,
10821076
{ before: this._transformers },
10831077
);
10841078
allDiagnostics.push(...emitResult.diagnostics);
10851079
timeEnd(timeLabel);
1086-
});
1080+
}
10871081
}
10881082
}
10891083
} else {

0 commit comments

Comments
 (0)