build: add moduleName to ngFactory sourcefiles (#29385)

PR Close #29385
This commit is contained in:
Alex Eagle 2019-03-18 09:52:50 -07:00 committed by Matias Niemelä
parent ae4a86e3b5
commit 86aba1e8f3
2 changed files with 43 additions and 4 deletions

View File

@ -369,10 +369,15 @@ 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;
if (this.options.module === ts.ModuleKind.AMD || this.options.module === ts.ModuleKind.UMD) {
if (this.context.amdModuleName) {
const moduleName = this.context.amdModuleName(sf);
if (moduleName) sf.moduleName = moduleName;
} else if (/node_modules/.test(genFile.genFileUrl)) {
// If we are generating an ngModule file under node_modules, we know the right module name
// We don't need the host to supply a function in this case.
sf.moduleName = stripNodeModulesPrefix(genFile.genFileUrl.replace(EXT, ''));
}
}
this.generatedSourceFiles.set(genFile.genFileUrl, {
sourceFile: sf,

View File

@ -213,6 +213,40 @@ describe('NgCompilerHost', () => {
});
});
describe('addGeneratedFile', () => {
function generate(path: string, files: {}) {
codeGenerator.findGeneratedFileNames.and.returnValue([`${path}.ngfactory.ts`]);
codeGenerator.generateFile.and.returnValue(
new compiler.GeneratedFile(`${path}.ts`, `${path}.ngfactory.ts`, []));
const host = createHost({
files,
options: {
basePath: '/tmp',
moduleResolution: ts.ModuleResolutionKind.NodeJs,
// Request UMD, which should get default module names
module: ts.ModuleKind.UMD
},
});
return host.getSourceFile(`${path}.ngfactory.ts`, ts.ScriptTarget.Latest);
}
it('should include a moduleName when the file is in node_modules', () => {
const genSf = generate(
'/tmp/node_modules/@angular/core/core',
{'tmp': {'node_modules': {'@angular': {'core': {'core.ts': `// some content`}}}}});
expect(genSf.moduleName).toBe('@angular/core/core.ngfactory');
});
it('should not get tripped on nested node_modules', () => {
const genSf = generate('/tmp/node_modules/lib1/node_modules/lib2/thing', {
'tmp': {
'node_modules': {'lib1': {'node_modules': {'lib2': {'thing.ts': `// some content`}}}}
}
});
expect(genSf.moduleName).toBe('lib2/thing.ngfactory');
});
});
describe('getSourceFile', () => {
it('should cache source files by name', () => {
const host = createHost({files: {'tmp': {'src': {'index.ts': ``}}}});