Skip to content

Commit 345bdd3

Browse files
alxhubIgorMinar
authored andcommitted
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
1 parent d70a7f3 commit 345bdd3

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

‎packages/compiler-cli/src/ngtsc/shims/src/factory_generator.ts‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,23 @@ export class FactoryGenerator implements ShimGenerator {
4747
// Grab the symbol name.
4848
.map(decl => decl.name !.text);
4949

50-
// For each symbol name, generate a constant export of the corresponding NgFactory.
51-
// This will encompass a lot of symbols which don't need factories, but that's okay
52-
// because it won't miss any that do.
53-
const varLines = symbolNames.map(
54-
name => `export const ${name}NgFactory = new i0.ɵNgModuleFactory(${name});`);
55-
const sourceText = [
56-
// This might be incorrect if the current package being compiled is Angular core, but it's
57-
// okay to leave in at type checking time. TypeScript can handle this reference via its path
58-
// mapping, but downstream bundlers can't. If the current package is core itself, this will be
59-
// replaced in the factory transformer before emit.
60-
`import * as i0 from '@angular/core';`,
61-
`import {${symbolNames.join(', ')}} from '${relativePathToSource}';`,
62-
...varLines,
63-
].join('\n');
50+
let sourceText = '';
51+
if (symbolNames.length > 0) {
52+
// For each symbol name, generate a constant export of the corresponding NgFactory.
53+
// This will encompass a lot of symbols which don't need factories, but that's okay
54+
// because it won't miss any that do.
55+
const varLines = symbolNames.map(
56+
name => `export const ${name}NgFactory = new i0.ɵNgModuleFactory(${name});`);
57+
sourceText = [
58+
// This might be incorrect if the current package being compiled is Angular core, but it's
59+
// okay to leave in at type checking time. TypeScript can handle this reference via its path
60+
// mapping, but downstream bundlers can't. If the current package is core itself, this will
61+
// be replaced in the factory transformer before emit.
62+
`import * as i0 from '@angular/core';`,
63+
`import {${symbolNames.join(', ')}} from '${relativePathToSource}';`,
64+
...varLines,
65+
].join('\n');
66+
}
6467
return ts.createSourceFile(
6568
genFilePath, sourceText, original.languageVersion, true, ts.ScriptKind.TS);
6669
}

0 commit comments

Comments
 (0)