refactor(compiler-cli): make IncrementalBuild strategy configurable (#37339)

Commit 24b2f1da2b introduced an `NgCompiler` which operates on a
`ts.Program` independently of the `NgtscProgram`. The NgCompiler got its
`IncrementalDriver` (for incremental reuse of Angular compilation results)
by looking at a monkey-patched property on the `ts.Program`.

This monkey-patching operation causes problems with the Angular indexer
(specifically, it seems to cause the indexer to retain too much of prior
programs, resulting in OOM issues). To work around this, `IncrementalDriver`
reuse is now handled by a dedicated `IncrementalBuildStrategy`. One
implementation of this interface is used by the `NgtscProgram` to perform
the old-style reuse, relying on the previous instance of `NgtscProgram`
instead of monkey-patching. Only for `NgTscPlugin` is the monkey-patching
strategy used, as the plugin sits behind an interface which only provides
access to the `ts.Program`, not a prior instance of the plugin.

PR Close #37339
This commit is contained in:
Alex Rickabaugh
2020-05-28 16:08:52 -07:00
committed by Misko Hevery
parent 472bedd3ea
commit cde5cced69
10 changed files with 126 additions and 49 deletions

View File

@ -10,6 +10,7 @@
import {CompilerOptions} from '@angular/compiler-cli';
import {NgCompiler, NgCompilerHost} from '@angular/compiler-cli/src/ngtsc/core';
import {AbsoluteFsPath} from '@angular/compiler-cli/src/ngtsc/file_system';
import {PatchedProgramIncrementalBuildStrategy} from '@angular/compiler-cli/src/ngtsc/incremental';
import {TypeCheckingProgramStrategy, UpdateMode} from '@angular/compiler-cli/src/ngtsc/typecheck';
import * as ts from 'typescript/lib/tsserverlibrary';
@ -31,7 +32,9 @@ export class Compiler {
);
this.strategy = createTypeCheckingProgramStrategy(project);
this.lastKnownProgram = this.strategy.getProgram();
this.compiler = new NgCompiler(ngCompilerHost, options, this.lastKnownProgram, this.strategy);
this.compiler = new NgCompiler(
ngCompilerHost, options, this.lastKnownProgram, this.strategy,
new PatchedProgramIncrementalBuildStrategy());
}
setCompilerOptions(options: CompilerOptions) {
@ -43,8 +46,9 @@ export class Compiler {
const ngCompilerHost =
NgCompilerHost.wrap(this.tsCompilerHost, inputFiles, this.options, this.lastKnownProgram);
const program = this.strategy.getProgram();
this.compiler =
new NgCompiler(ngCompilerHost, this.options, program, this.strategy, this.lastKnownProgram);
this.compiler = new NgCompiler(
ngCompilerHost, this.options, program, this.strategy,
new PatchedProgramIncrementalBuildStrategy(), this.lastKnownProgram);
try {
// This is the only way to force the compiler to update the typecheck file
// in the program. We have to do try-catch because the compiler immediately