diff --git a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts index aac8f1bfef..bd4153ca20 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts @@ -96,12 +96,19 @@ export class NgModuleDecoratorHandler implements DecoratorHandler[] = []; + if (ngModule.has('imports')) { + injectorImports.push(new WrappedNodeExpr(ngModule.get('imports') !)); + } + if (ngModule.has('exports')) { + injectorImports.push(new WrappedNodeExpr(ngModule.get('exports') !)); + } + const ngInjectorDef: R3InjectorMetadata = { name: node.name !.text, type: new WrappedNodeExpr(node.name !), deps: getConstructorDependencies(node, this.reflector, this.isCore), providers, - imports: new LiteralArrayExpr( - [...imports, ...exports].map(imp => referenceToExpression(imp, context))), + imports: new LiteralArrayExpr(injectorImports), }; return { diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index 0d5fc832b5..eb5e1623b0 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -236,7 +236,7 @@ describe('ngtsc behavioral tests', () => { .toContain( `TestModule.ngInjectorDef = i0.defineInjector({ factory: ` + `function TestModule_Factory() { return new TestModule(); }, providers: [{ provide: ` + - `Token, useValue: 'test' }], imports: [OtherModule] });`); + `Token, useValue: 'test' }], imports: [[OtherModule]] });`); const dtsContents = getContents('test.d.ts'); expect(dtsContents) @@ -366,6 +366,10 @@ describe('ngtsc behavioral tests', () => { const exitCode = main(['-p', basePath], errorSpy); expect(errorSpy).not.toHaveBeenCalled(); expect(exitCode).toBe(0); + + const jsContents = getContents('test.js'); + expect(jsContents).toContain('imports: [[RouterModule.forRoot()]]'); + const dtsContents = getContents('test.d.ts'); expect(dtsContents).toContain(`import * as i1 from 'router';`); expect(dtsContents).toContain('i0.ɵNgModuleDef');