Skip to content

Commit

Permalink
refactor(compiler-cli): ng module injector compilation in local mode
Browse files Browse the repository at this point in the history
In local mode the compiler combines the raw imports and exports and pass them to the injector definition as the imports field. It is not possible to filter out ng modules at compile time though, and it will be done in runtime.
  • Loading branch information
pmvald committed Jul 16, 2023
1 parent 55d412c commit e7b6395
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {createModuleWithProvidersResolver, isResolvedModuleWithProviders} from '

export interface NgModuleAnalysis {
mod: R3NgModuleMetadata;
inj: Omit<R3InjectorMetadata, 'imports'>;
inj: R3InjectorMetadata;
fac: R3FactoryMetadata;
classMetadata: R3ClassMetadata|null;
declarations: Reference<ClassDeclaration>[];
Expand Down Expand Up @@ -437,12 +437,23 @@ export class NgModuleDecoratorHandler implements
}
}

const injectorMetadata: Omit<R3InjectorMetadata, 'imports'> = {
const injectorMetadata: R3InjectorMetadata = {
name,
type,
providers: wrappedProviders,
imports: [],
};

if (this.compilationMode === CompilationMode.LOCAL) {
if (rawImports !== null && ts.isArrayLiteralExpression(rawImports) && rawImports.elements) {
injectorMetadata.imports.push(...rawImports.elements.map(n => new WrappedNodeExpr(n)));
}

if (rawExports !== null && ts.isArrayLiteralExpression(rawExports) && rawExports.elements) {
injectorMetadata.imports.push(...rawExports.elements.map(n => new WrappedNodeExpr(n)));
}
}

const factoryMetadata: R3FactoryMetadata = {
name,
type,
Expand Down Expand Up @@ -705,7 +716,6 @@ export class NgModuleDecoratorHandler implements
const factoryFn = compileNgFactoryDefField(fac);
const ngInjectorDef = compileInjector({
...inj,
imports: [],
});
const ngModuleDef = compileNgModule(mod);
const statements = ngModuleDef.statements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
"imports_exports.js"
]
}
],
"compilationModeFilter": [
"full compile",
"local compile"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class AModule {}
AModule.ɵfac = function AModule_Factory(t) { return new (t || AModule)(); };
AModule.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: AModule });
AModule.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [A1Component, A2Component] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AModule, [{
type: NgModule,
args: [{ declarations: [A1Component, A2Component], exports: [A1Component, A2Component] }]
}], null, null); })();

export class BModule {}
BModule.ɵfac = function BModule_Factory(t) { return new (t || BModule)(); };
BModule.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: BModule });
BModule.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [AModule] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BModule, [{
type: NgModule,
args: [{ declarations: [B1Component, B2Component], exports: [AModule] }]
}], null, null); })();

export class AppModule {}
AppModule.ɵfac = function AppModule_Factory(t) { return new (t || AppModule)(); };
AppModule.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: AppModule });
AppModule.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [BModule] });
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(AppModule, [{
type: NgModule,
args: [{ imports: [BModule] }]
}], null, null); })();

0 comments on commit e7b6395

Please sign in to comment.