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 3d2a3eb149..f82c2db834 100644 --- a/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts +++ b/packages/compiler-cli/src/ngtsc/annotations/src/ng_module.ts @@ -158,10 +158,8 @@ export class NgModuleDecoratorHandler implements DecoratorHandler { expect(jsContents).toContain('i0.ɵdefineNgModule({ type: TestModule, bootstrap: [TestCmp] });'); expect(jsContents) .toContain('/*@__PURE__*/ i0.ɵsetNgModuleScope(TestModule, { declarations: [TestCmp] });'); + expect(jsContents) + .toContain( + 'i0.defineInjector({ factory: ' + + 'function TestModule_Factory(t) { return new (t || TestModule)(); } });'); const dtsContents = env.getContents('test.d.ts'); expect(dtsContents) @@ -528,8 +532,8 @@ describe('ngtsc behavioral tests', () => { expect(jsContents) .toContain( 'i0.defineInjector({ factory: function TestModule_Factory(t) ' + - '{ return new (t || TestModule)(); }, providers: [], ' + - 'imports: [[OtherModule, RouterModule.forRoot()],\n OtherModule,\n RouterModule] });'); + '{ return new (t || TestModule)(); }, imports: [[OtherModule, RouterModule.forRoot()],' + + '\n OtherModule,\n RouterModule] });'); }); it('should compile NgModules with services without errors', () => { diff --git a/packages/compiler/src/render3/r3_module_compiler.ts b/packages/compiler/src/render3/r3_module_compiler.ts index 51c78d2f97..ff5673ffed 100644 --- a/packages/compiler/src/render3/r3_module_compiler.ts +++ b/packages/compiler/src/render3/r3_module_compiler.ts @@ -190,7 +190,7 @@ export interface R3InjectorMetadata { name: string; type: o.Expression; deps: R3DependencyMetadata[]|null; - providers: o.Expression; + providers: o.Expression|null; imports: o.Expression[]; } @@ -201,11 +201,19 @@ export function compileInjector(meta: R3InjectorMetadata): R3InjectorDef { deps: meta.deps, injectFn: R3.inject, }); - const expression = o.importExpr(R3.defineInjector).callFn([mapToMapExpression({ + const definitionMap = { factory: result.factory, - providers: meta.providers, - imports: o.literalArr(meta.imports), - })]); + } as{factory: o.Expression, providers: o.Expression, imports: o.Expression}; + + if (meta.providers !== null) { + definitionMap.providers = meta.providers; + } + + if (meta.imports.length > 0) { + definitionMap.imports = o.literalArr(meta.imports); + } + + const expression = o.importExpr(R3.defineInjector).callFn([mapToMapExpression(definitionMap)]); const type = new o.ExpressionType(o.importExpr(R3.InjectorDef, [new o.ExpressionType(meta.type)])); return {expression, type, statements: result.statements};