fix(ivy): properly rewrite imports in generated factory shims (#27998)
Generated factory shims can import from @angular/core. However, we have special logic in place to rewrite self-imports when generating code for @angular/core. This commit leverages the new standalone ImportRewriter interface to properly rewrite imports in generated factory shims. Before this fix, a generated factory file for core would look like: ```typescript import * as i0 from './r3_symbols'; export var ApplicationModuleNgFactory = new ɵNgModuleFactory(...); ``` This is invalid, as ɵNgModuleFactory is just NgModuleFactory when imported via r3_symbols. FW-881 #resolve PR Close #27998
This commit is contained in:

committed by
Andrew Kushnir

parent
3cf1b62722
commit
6003145422
@ -905,6 +905,29 @@ describe('ngtsc behavioral tests', () => {
|
||||
expect(emptyFactory).toContain(`export var ɵNonEmptyModule = true;`);
|
||||
});
|
||||
|
||||
it('should generate correct imports in factory stubs when compiling @angular/core', () => {
|
||||
env.tsconfig({'allowEmptyCodegenFiles': true});
|
||||
|
||||
env.write('test.ts', `
|
||||
import {NgModule} from '@angular/core';
|
||||
|
||||
@NgModule({})
|
||||
export class TestModule {}
|
||||
`);
|
||||
|
||||
// Trick the compiler into thinking it's compiling @angular/core.
|
||||
env.write('r3_symbols.ts', 'export const ITS_JUST_ANGULAR = true;');
|
||||
|
||||
env.driveMain();
|
||||
|
||||
const factoryContents = env.getContents('test.ngfactory.js');
|
||||
expect(normalize(factoryContents)).toBe(normalize(`
|
||||
import * as i0 from "./r3_symbols";
|
||||
import { TestModule } from './test';
|
||||
export var TestModuleNgFactory = new i0.NgModuleFactory(TestModule);
|
||||
`));
|
||||
});
|
||||
|
||||
it('should generate a summary stub for decorated classes in the input file only', () => {
|
||||
env.tsconfig({'allowEmptyCodegenFiles': true});
|
||||
|
||||
@ -1635,3 +1658,7 @@ function expectTokenAtPosition<T extends ts.Node>(
|
||||
expect(guard(node)).toBe(true);
|
||||
return node as T;
|
||||
}
|
||||
|
||||
function normalize(input: string): string {
|
||||
return input.replace(/\s+/g, ' ').trim();
|
||||
}
|
Reference in New Issue
Block a user