From 9c403753e2158fb69e2a7bb72a529e6ac19f3aca Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 5 Jun 2018 10:11:43 -0700 Subject: [PATCH] refactor(ivy): misc minor fixes in the JIT compiler (#24308) PR Close #24308 --- packages/core/src/render3/jit/module.ts | 52 ++++++++++++++++--------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/packages/core/src/render3/jit/module.ts b/packages/core/src/render3/jit/module.ts index 769d0f3413..2ebbac9c13 100644 --- a/packages/core/src/render3/jit/module.ts +++ b/packages/core/src/render3/jit/module.ts @@ -29,34 +29,47 @@ export function compileNgModule(type: Type, ngModule: NgModule): void { // Compute transitiveCompileScope const transitiveCompileScope = { - directives: [] as any[], - pipes: [] as any[], + directives: new Set(), + pipes: new Set(), + modules: new Set(), }; function addExportsFrom(module: Type& {ngModuleDef: NgModuleDef}): void { - module.ngModuleDef.exports.forEach((exp: any) => { - if (isNgModule(exp)) { - addExportsFrom(exp); - } else if (exp.ngPipeDef) { - transitiveCompileScope.pipes.push(exp); - } else { - transitiveCompileScope.directives.push(exp); - } - }); + if (!transitiveCompileScope.modules.has(module)) { + module.ngModuleDef.exports.forEach((exp: any) => { + if (isNgModule(exp)) { + addExportsFrom(exp); + } else if (exp.ngPipeDef) { + transitiveCompileScope.pipes.add(exp); + } else { + transitiveCompileScope.directives.add(exp); + } + }); + } } - flatten([(ngModule.imports || EMPTY_ARRAY), (ngModule.exports || EMPTY_ARRAY)]) - .filter(importExport => isNgModule(importExport)) - .forEach(mod => addExportsFrom(mod)); + flatten([ + (ngModule.imports || EMPTY_ARRAY), (ngModule.exports || EMPTY_ARRAY) + ]).forEach(importExport => { + const maybeModule = expandModuleWithProviders(importExport); + if (isNgModule(maybeModule)) { + addExportsFrom(maybeModule); + } + }); flatten(ngModule.declarations || EMPTY_ARRAY).forEach(decl => { if (decl.ngPipeDef) { - transitiveCompileScope.pipes.push(decl); + transitiveCompileScope.pipes.add(decl); + } else if (decl.ngDirectiveDef) { + transitiveCompileScope.directives.add(decl); } else if (decl.ngComponentDef) { - transitiveCompileScope.directives.push(decl); + transitiveCompileScope.directives.add(decl); patchComponentWithScope(decl, type as any); } else { - transitiveCompileScope.directives.push(decl); + // A component that has not been compiled yet because the template is being fetched + // we need to store a reference to the module to update the selector scope after + // the component gets compiled + transitiveCompileScope.directives.add(decl); decl.ngSelectorScope = type; } }); @@ -77,7 +90,10 @@ export function compileNgModule(type: Type, ngModule: NgModule): void { }; const res = compileR3NgModule(meta); def = jitExpression(res.expression, angularCoreEnv, `ng://${type.name}/ngModuleDef.js`); - def.transitiveCompileScope = transitiveCompileScope; + def.transitiveCompileScope = { + directives: Array.from(transitiveCompileScope.directives), + pipes: Array.from(transitiveCompileScope.pipes), + }; } return def; },