fix(bazel): produce named AMD modules for codegen (#20547)

fixes #19422

Signed-off-by: Alex Eagle <alexeagle@google.com>

PR Close #20547
This commit is contained in:
Alex Eagle
2017-11-14 11:29:16 -08:00
committed by Miško Hevery
parent a53a040071
commit 6e83204238
6 changed files with 28 additions and 17 deletions

View File

@ -9,7 +9,7 @@
"typescript": ">=2.4.2 <2.6"
},
"dependencies": {
"@bazel/typescript": "0.3.x",
"@bazel/typescript": "0.3.2",
"@types/node": "6.0.84"
},
"repository": {

View File

@ -164,11 +164,19 @@ export function compile({allowNonHermeticReads, allDepsCompiledWithBazel = true,
}
return origBazelHostFileExist.call(bazelHost, fileName);
};
const origBazelHostShouldNameModule = bazelHost.shouldNameModule.bind(bazelHost);
bazelHost.shouldNameModule = (fileName: string) =>
origBazelHostShouldNameModule(fileName) || NGC_GEN_FILES.test(fileName);
const ngHost = ng.createCompilerHost({options: compilerOpts, tsHost: bazelHost});
ngHost.fileNameToModuleName = (importedFilePath: string, containingFilePath: string) =>
relativeToRootDirs(importedFilePath, compilerOpts.rootDirs).replace(EXT, '');
ngHost.fileNameToModuleName = (importedFilePath: string, containingFilePath: string) => {
if ((compilerOpts.module === ts.ModuleKind.UMD || compilerOpts.module === ts.ModuleKind.AMD) &&
ngHost.amdModuleName) {
return ngHost.amdModuleName({ fileName: importedFilePath } as ts.SourceFile);
}
return relativeToRootDirs(importedFilePath, compilerOpts.rootDirs).replace(EXT, '');
};
ngHost.toSummaryFileName = (fileName: string, referringSrcFileName: string) =>
ngHost.fileNameToModuleName(fileName, referringSrcFileName);
if (allDepsCompiledWithBazel) {

View File

@ -192,6 +192,13 @@ export interface CompilerHost extends ts.CompilerHost {
* cause a diagnostics diagnostic error or an exception to be thrown.
*/
readResource?(fileName: string): Promise<string>|string;
/**
* Produce an AMD module name for the source file. Used in Bazel.
*
* An AMD module can have an arbitrary name, so that it is require'd by name
* rather than by path. See http://requirejs.org/docs/whyamd.html#namedmodules
*/
amdModuleName?(sf: ts.SourceFile): string|undefined;
}
export enum EmitFlags {

View File

@ -303,6 +303,11 @@ export class TsCompilerAotCompilerTypeCheckHostAdapter implements ts.CompilerHos
/* emitSourceMaps */ false);
const sf = ts.createSourceFile(
genFile.genFileUrl, sourceText, this.options.target || ts.ScriptTarget.Latest);
if ((this.options.module === ts.ModuleKind.AMD || this.options.module === ts.ModuleKind.UMD) &&
this.context.amdModuleName) {
const moduleName = this.context.amdModuleName(sf);
if (moduleName) sf.moduleName = moduleName;
}
this.generatedSourceFiles.set(genFile.genFileUrl, {
sourceFile: sf,
emitCtx: context, externalReferences,