From 37c5a264214811382bc918bdcac61edd3375a502 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Wed, 6 Mar 2019 14:26:56 -0800 Subject: [PATCH] perf(ivy): switch ngtsc to use single-file emit (#29147) In the TypeScript compiler API, emit() can be performed either on a single ts.SourceFile or on the entire ts.Program simultaneously. ngtsc previously used whole-program emit, which was convenient to use while spinning up the project but has a significant drawback: it causes a type checking operation to occur for the whole program, including .d.ts files. In large Bazel environments (such as Google's codebase), an ngtsc invocation can have a few .ts files and thousands of .d.ts inputs. This unwanted type checking is therefore a significant drain on performance. This commit switches ngtsc to emit each .ts file individually, avoiding the unwanted type checking. PR Close #29147 --- packages/compiler-cli/src/ngtsc/program.ts | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/program.ts b/packages/compiler-cli/src/ngtsc/program.ts index 22a667ef87..50d1b9eb7f 100644 --- a/packages/compiler-cli/src/ngtsc/program.ts +++ b/packages/compiler-cli/src/ngtsc/program.ts @@ -293,19 +293,28 @@ export class NgtscProgram implements api.Program { beforeTransforms.push(...customTransforms.beforeTs); } + const emitResults: ts.EmitResult[] = []; + for (const targetSourceFile of this.tsProgram.getSourceFiles()) { + if (targetSourceFile.isDeclarationFile) { + continue; + } + + emitResults.push(emitCallback({ + targetSourceFile, + program: this.tsProgram, + host: this.host, + options: this.options, + emitOnlyDtsFiles: false, writeFile, + customTransformers: { + before: beforeTransforms, + after: customTransforms && customTransforms.afterTs, + afterDeclarations: afterDeclarationsTransforms, + }, + })); + } + // Run the emit, including a custom transformer that will downlevel the Ivy decorators in code. - const emitResult = emitCallback({ - program: this.tsProgram, - host: this.host, - options: this.options, - emitOnlyDtsFiles: false, writeFile, - customTransformers: { - before: beforeTransforms, - after: customTransforms && customTransforms.afterTs, - afterDeclarations: afterDeclarationsTransforms, - }, - }); - return emitResult; + return ((opts && opts.mergeEmitResultsCallback) || mergeEmitResults)(emitResults); } private compileTypeCheckProgram(ctx: TypeCheckContext): ReadonlyArray {