fix(ivy): generate empty ngfactory files if needed (#27470)

Previously the ngfactory shim generator in ngtsc would always write two
imports in the factory file shims:

1) an import to @angular/core
2) an import to the base file

If the base file has no exports, import #2 would be empty. This turns out
to cause issues downstream.

This commit changes the generated shim so if there are no exports in the
base file, the generated shim is empty too.

PR Close #27470
This commit is contained in:
Alex Rickabaugh 2018-12-04 14:44:36 -08:00 committed by Igor Minar
parent d70a7f3ac9
commit 345bdd3db0
1 changed files with 17 additions and 14 deletions

View File

@ -47,20 +47,23 @@ export class FactoryGenerator implements ShimGenerator {
// Grab the symbol name. // Grab the symbol name.
.map(decl => decl.name !.text); .map(decl => decl.name !.text);
// For each symbol name, generate a constant export of the corresponding NgFactory. let sourceText = '';
// This will encompass a lot of symbols which don't need factories, but that's okay if (symbolNames.length > 0) {
// because it won't miss any that do. // For each symbol name, generate a constant export of the corresponding NgFactory.
const varLines = symbolNames.map( // This will encompass a lot of symbols which don't need factories, but that's okay
name => `export const ${name}NgFactory = new i0.ɵNgModuleFactory(${name});`); // because it won't miss any that do.
const sourceText = [ const varLines = symbolNames.map(
// This might be incorrect if the current package being compiled is Angular core, but it's name => `export const ${name}NgFactory = new i0.ɵNgModuleFactory(${name});`);
// okay to leave in at type checking time. TypeScript can handle this reference via its path sourceText = [
// mapping, but downstream bundlers can't. If the current package is core itself, this will be // This might be incorrect if the current package being compiled is Angular core, but it's
// replaced in the factory transformer before emit. // okay to leave in at type checking time. TypeScript can handle this reference via its path
`import * as i0 from '@angular/core';`, // mapping, but downstream bundlers can't. If the current package is core itself, this will
`import {${symbolNames.join(', ')}} from '${relativePathToSource}';`, // be replaced in the factory transformer before emit.
...varLines, `import * as i0 from '@angular/core';`,
].join('\n'); `import {${symbolNames.join(', ')}} from '${relativePathToSource}';`,
...varLines,
].join('\n');
}
return ts.createSourceFile( return ts.createSourceFile(
genFilePath, sourceText, original.languageVersion, true, ts.ScriptKind.TS); genFilePath, sourceText, original.languageVersion, true, ts.ScriptKind.TS);
} }