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
This commit is contained in:
Alex Eagle 2019-01-30 08:30:53 -08:00 committed by Matias Niemelä
parent f2621dbb37
commit 7219639ff3
2 changed files with 19 additions and 14 deletions

View File

@ -30,7 +30,7 @@ export class SyntheticFilesCompilerHost implements PluginCompilerHost {
private rootFiles: string[], private delegate: ts.CompilerHost, private rootFiles: string[], private delegate: ts.CompilerHost,
generatedFiles: (rootFiles: string[]) => { generatedFiles: (rootFiles: string[]) => {
[fileName: string]: (host: ts.CompilerHost) => ts.SourceFile | undefined [fileName: string]: (host: ts.CompilerHost) => ts.SourceFile | undefined
}, ) { }) {
// Allow ngtsc to contribute in-memory synthetic files, which will be loaded // Allow ngtsc to contribute in-memory synthetic files, which will be loaded
// as if they existed on disk as action inputs. // as if they existed on disk as action inputs.
const angularGeneratedFiles = generatedFiles !(rootFiles); const angularGeneratedFiles = generatedFiles !(rootFiles);

View File

@ -11,7 +11,9 @@ import * as ts from 'typescript';
import {SyntheticFilesCompilerHost} from './synthetic_files_compiler_host'; 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<T>(delegate: T): T { function createProxy<T>(delegate: T): T {
const proxy = Object.create(null); const proxy = Object.create(null);
for (const k of Object.keys(delegate)) { for (const k of Object.keys(delegate)) {
@ -23,6 +25,20 @@ function createProxy<T>(delegate: T): T {
export class NgTscPlugin implements TscPlugin { export class NgTscPlugin implements TscPlugin {
constructor(private angularCompilerOptions: unknown) {} 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) { wrap(program: ts.Program, config: {}, host: ts.CompilerHost) {
const proxy = createProxy(program); const proxy = createProxy(program);
proxy.getSemanticDiagnostics = (sourceFile: ts.SourceFile) => { proxy.getSemanticDiagnostics = (sourceFile: ts.SourceFile) => {
@ -38,7 +54,7 @@ export class NgTscPlugin implements TscPlugin {
category: ts.DiagnosticCategory.Error, category: ts.DiagnosticCategory.Error,
code: 12345, code: 12345,
// source is the name of the plugin. // source is the name of the plugin.
source: 'Angular', source: 'ngtsc',
}; };
result.push(fake); result.push(fake);
} }
@ -64,15 +80,4 @@ export class NgTscPlugin implements TscPlugin {
}]; }];
return {afterDeclarations}; 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),
};
}
} }