Skip to content

Commit

Permalink
build(tsc-wrapped): use tsickleCompilerHost for initial file load
Browse files Browse the repository at this point in the history
In order for tsickle's new support for input source maps to work, the tsickleCompilerHost must be used for the initial load of source files, since that's when the inline source maps are read and stripped.
  • Loading branch information
LucasSloan authored and SamVerschueren committed Mar 18, 2017
1 parent 92a8875 commit 7788af0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 23 deletions.
44 changes: 21 additions & 23 deletions tools/@angular/tsc-wrapped/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ export function main(
parsed.fileNames.push(name);
}

const program = createProgram(host);
const tsickleCompilerHostOptions: tsickle.Options = {
googmodule: false,
untyped: true,
convertIndexImportShorthand:
ngOptions.target === ts.ScriptTarget.ES2015, // This covers ES6 too
};

const tsickleHost: tsickle.TsickleHost = {
shouldSkipTsickleProcessing: (fileName) => /\.d\.ts$/.test(fileName),
pathToModuleName: (context, importPath) => '',
shouldIgnoreWarningsForPath: (filePath) => false,
fileNameToModuleId: (fileName) => fileName,
};

const tsickleCompilerHost =
new tsickle.TsickleCompilerHost(host, ngOptions, tsickleCompilerHostOptions, tsickleHost);

const program = createProgram(tsickleCompilerHost);

const errors = program.getOptionsDiagnostics();
check(errors);

Expand All @@ -97,43 +115,23 @@ export function main(
if (diagnostics) console.time('NG codegen');
return codegen(ngOptions, cliOptions, program, host).then(() => {
if (diagnostics) console.timeEnd('NG codegen');
let definitionsHost = host;
let definitionsHost: ts.CompilerHost = tsickleCompilerHost;
if (!ngOptions.skipMetadataEmit) {
definitionsHost = new MetadataWriterHost(host, ngOptions);
definitionsHost = new MetadataWriterHost(tsickleCompilerHost, ngOptions);
}
// Create a new program since codegen files were created after making the old program
let programWithCodegen = createProgram(definitionsHost, program);
tsc.typeCheck(host, programWithCodegen);

let preprocessHost = host;
let programForJsEmit = programWithCodegen;


const tsickleCompilerHostOptions: tsickle.Options = {
googmodule: false,
untyped: true,
convertIndexImportShorthand:
ngOptions.target === ts.ScriptTarget.ES2015, // This covers ES6 too
};

const tsickleHost: tsickle.TsickleHost = {
shouldSkipTsickleProcessing: (fileName) => /\.d\.ts$/.test(fileName),
pathToModuleName: (context, importPath) => '',
shouldIgnoreWarningsForPath: (filePath) => false,
fileNameToModuleId: (fileName) => fileName,
};

const tsickleCompilerHost = new tsickle.TsickleCompilerHost(
preprocessHost, ngOptions, tsickleCompilerHostOptions, tsickleHost);

if (ngOptions.annotationsAs !== 'decorators') {
if (diagnostics) console.time('NG downlevel');
tsickleCompilerHost.reconfigureForRun(programForJsEmit, tsickle.Pass.DECORATOR_DOWNLEVEL);
// A program can be re-used only once; save the programWithCodegen to be reused by
// metadataWriter
programForJsEmit = createProgram(tsickleCompilerHost);
check(tsickleCompilerHost.diagnostics);
preprocessHost = tsickleCompilerHost;
if (diagnostics) console.timeEnd('NG downlevel');
}

Expand Down
31 changes: 31 additions & 0 deletions tools/@angular/tsc-wrapped/test/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,35 @@ describe('tsc-wrapped', () => {
})
.catch(e => done.fail(e));
});

it('should accept input source maps', (done) => {
write('tsconfig.json', `{
"compilerOptions": {
"experimentalDecorators": true,
"types": [],
"outDir": "built",
"declaration": true,
"moduleResolution": "node",
"target": "es2015",
"sourceMap": true
},
"angularCompilerOptions": {
"annotateForClosureCompiler": true
},
"files": ["test.ts"]
}`);
// Provide a file called test.ts that has an inline source map
// which says that it was transpiled from a file other_test.ts
// with exactly the same content.
write('test.ts', `const x = 3;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm90aGVyX3Rlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsTUFBTSxFQUFFLEVBQUUsQ0FBQyIsImZpbGUiOiIuLi90ZXN0LnRzIiwic291cmNlUm9vdCI6IiJ9`);

main(basePath, {basePath})
.then(() => {
const out = readOut('js.map');
expect(out).toContain('"sources":["other_test.ts"]');
done();
})
.catch(e => done.fail(e));
});
});

0 comments on commit 7788af0

Please sign in to comment.