From 42d42871538e33285cca83d5a9e85f2c6ebf4401 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Tue, 10 Jul 2018 09:59:29 -0700 Subject: [PATCH] fix(ivy): ngInjectorDef should copy full imports/exports nodes (#24862) @NgModule()s get compiled to two fields: ngModuleDef and ngInjectorDef. Both fields contain imports, as both selector scopes and injectors have the concept of composed units of configuration. Previously these fields were generated by static resolution of imports and exports in metadata. Support for ModuleWithProviders requires they be generated differently. ngModuleDef's imports/exports are generated as resolved lists of types, whereas ngInjectorDef's imports should reflect the raw expressions that the developer wrote in the metadata. This change modifies the NgModule handler and properly copies raw nodes for the imports and exports into the ngInjectorDef. PR Close #24862 --- .../src/ngtsc/annotations/src/ng_module.ts | 11 +++++++++-- packages/compiler-cli/test/ngtsc/ngtsc_spec.ts | 6 +++++- 2 files changed, 14 insertions(+), 3 deletions(-) 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');