From 7219639ff3bedf8c9bbe616811e6865a89f2ea6b Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 30 Jan 2019 08:30:53 -0800 Subject: [PATCH] fix(compiler-cli): base synthetic filepaths on input filepath (#28453) This change is needed to work in google3, where file paths in the ts.Program must always be absolute. PR Close #28453 --- .../ngtsc/synthetic_files_compiler_host.ts | 2 +- packages/compiler-cli/src/ngtsc/tsc_plugin.ts | 31 +++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/synthetic_files_compiler_host.ts b/packages/compiler-cli/src/ngtsc/synthetic_files_compiler_host.ts index 2a799c4aca..7354b1ee6c 100644 --- a/packages/compiler-cli/src/ngtsc/synthetic_files_compiler_host.ts +++ b/packages/compiler-cli/src/ngtsc/synthetic_files_compiler_host.ts @@ -30,7 +30,7 @@ export class SyntheticFilesCompilerHost implements PluginCompilerHost { private rootFiles: string[], private delegate: ts.CompilerHost, generatedFiles: (rootFiles: string[]) => { [fileName: string]: (host: ts.CompilerHost) => ts.SourceFile | undefined - }, ) { + }) { // Allow ngtsc to contribute in-memory synthetic files, which will be loaded // as if they existed on disk as action inputs. const angularGeneratedFiles = generatedFiles !(rootFiles); diff --git a/packages/compiler-cli/src/ngtsc/tsc_plugin.ts b/packages/compiler-cli/src/ngtsc/tsc_plugin.ts index 3aa127a2b2..282dcc4804 100644 --- a/packages/compiler-cli/src/ngtsc/tsc_plugin.ts +++ b/packages/compiler-cli/src/ngtsc/tsc_plugin.ts @@ -11,7 +11,9 @@ import * as ts from 'typescript'; import {SyntheticFilesCompilerHost} from './synthetic_files_compiler_host'; -// Copied from tsc_wrapped/plugin_api.ts to avoid a runtime dependency on that package +// Copied from tsc_wrapped/plugin_api.ts to avoid a runtime dependency on the +// @bazel/typescript package - it would be strange for non-Bazel users of +// Angular to fetch that package. function createProxy(delegate: T): T { const proxy = Object.create(null); for (const k of Object.keys(delegate)) { @@ -23,6 +25,20 @@ function createProxy(delegate: T): T { export class NgTscPlugin implements TscPlugin { constructor(private angularCompilerOptions: unknown) {} + wrapHost(inputFiles: string[], compilerHost: ts.CompilerHost) { + return new SyntheticFilesCompilerHost(inputFiles, compilerHost, (rootFiles: string[]) => { + // For demo purposes, assume that the first .ts rootFile is the only + // one that needs ngfactory.js/d.ts back-compat files produced. + const tsInputs = rootFiles.filter(f => f.endsWith('.ts') && !f.endsWith('.d.ts')); + const factoryPath: string = tsInputs[0].replace(/\.ts/, '.ngfactory.ts'); + + return { + factoryPath: (host: ts.CompilerHost) => + ts.createSourceFile(factoryPath, 'contents', ts.ScriptTarget.ES5), + }; + }); + } + wrap(program: ts.Program, config: {}, host: ts.CompilerHost) { const proxy = createProxy(program); proxy.getSemanticDiagnostics = (sourceFile: ts.SourceFile) => { @@ -38,7 +54,7 @@ export class NgTscPlugin implements TscPlugin { category: ts.DiagnosticCategory.Error, code: 12345, // source is the name of the plugin. - source: 'Angular', + source: 'ngtsc', }; result.push(fake); } @@ -64,15 +80,4 @@ export class NgTscPlugin implements TscPlugin { }]; return {afterDeclarations}; } - - wrapHost(inputFiles: string[], compilerHost: ts.CompilerHost) { - return new SyntheticFilesCompilerHost(inputFiles, compilerHost, this.generatedFiles); - } - - generatedFiles(rootFiles: string[]) { - return { - 'file-1.ts': (host: ts.CompilerHost) => - ts.createSourceFile('file-1.ts', 'contents', ts.ScriptTarget.ES5), - }; - } }