Skip to content

Commit

Permalink
refactor(core): implement runtime logic to compute component dependen…
Browse files Browse the repository at this point in the history
…cies in local compilation mode

The runtime `ɵɵsetNgModuleScope` is modified to accept raw scope info as passed to it in local compilation mode. The runtime further registers the ng-module in the deps tracker. Then the runtime `ɵɵgetComponentDepsFactory` is implemented to use the deps tracker to get the component dependencies which leads to a valid and working Angular code.
  • Loading branch information
pmvald committed Aug 9, 2023
1 parent 542b415 commit 0358802
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
23 changes: 18 additions & 5 deletions packages/core/src/render3/interfaces/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,21 +517,34 @@ export type PipeTypeList =
export const unusedValueExportToPlacateAjd = 1;

/**
* NgModule scope info as provided by NgModule decorator.
* NgModule scope info as provided by AoT compiler
*
* In full compilation Ivy resolved all the "module with providers" and forward refs the whole array
* if at least one element is forward refed. So we end up with type `Type<any>[]|(() =>
* Type<any>[])`.
*
* In local mode the compiler passes the raw info as they are to the runtime functions as it is not
* possible to resolve them any further due to limited info at compile time. So we end up with type
* `RawScopeInfoFromDecorator[]`.
*/
export interface NgModuleScopeInfoFromDecorator {
/** List of components, directives, and pipes declared by this module. */
declarations?: Type<any>[]|(() => Type<any>[]);
declarations?: Type<any>[]|(() => Type<any>[])|RawScopeInfoFromDecorator[];

/** List of modules or `ModuleWithProviders` imported by this module. */
imports?: Type<any>[]|(() => Type<any>[]);
/** List of modules or `ModuleWithProviders` or standalone components imported by this module. */
imports?: Type<any>[]|(() => Type<any>[])|RawScopeInfoFromDecorator[];

/**
* List of modules, `ModuleWithProviders`, components, directives, or pipes exported by this
* module.
*/
exports?: Type<any>[]|(() => Type<any>[]);
exports?: Type<any>[]|(() => Type<any>[])|RawScopeInfoFromDecorator[];
}

/**
* The array element type passed to:
* - NgModule's annotation imports/exports/declarations fields
* - standalone component annotation imports field
*/
export type RawScopeInfoFromDecorator =
Type<any>|ModuleWithProviders<any>|(() => Type<any>)|(() => ModuleWithProviders<any>);
12 changes: 6 additions & 6 deletions packages/core/src/render3/local_compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Type} from '../interface/type';

import {DependencyTypeList, RawScopeInfoFromDecorator} from './interfaces/definition';
import {depsTracker} from './deps_tracker/deps_tracker';
import {ComponentType, DependencyTypeList, RawScopeInfoFromDecorator} from './interfaces/definition';

export function ɵɵgetComponentDepsFactory(
type: Type<any>, rawImports?: RawScopeInfoFromDecorator): () => DependencyTypeList {
// TODO(pmvald): Implement this runtime using deps tracker.
return () => [];
type: ComponentType<any>, rawImports?: RawScopeInfoFromDecorator[]): () => DependencyTypeList {
return () => {
return depsTracker.getComponentDependencies(type, rawImports).dependencies;
};
}
30 changes: 26 additions & 4 deletions packages/core/src/render3/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
* found in the LICENSE file at https://angular.io/license
*/

import {isForwardRef, resolveForwardRef} from '../di/forward_ref';
import {Type} from '../interface/type';
import {noSideEffects} from '../util/closure';
import {EMPTY_ARRAY} from '../util/empty';

import {extractDefListOrFactory, getNgModuleDef} from './definition';
import {ComponentDef, ComponentType, NgModuleScopeInfoFromDecorator} from './interfaces/definition';
import {depsTracker} from './deps_tracker/deps_tracker';
import {ComponentDef, ComponentType, NgModuleScopeInfoFromDecorator, RawScopeInfoFromDecorator} from './interfaces/definition';
import {isModuleWithProviders} from './jit/util';

/**
* Generated next to NgModules to monkey-patch directive and pipe references onto a component's
Expand Down Expand Up @@ -43,8 +46,27 @@ export function ɵɵsetComponentScope(
export function ɵɵsetNgModuleScope(type: any, scope: NgModuleScopeInfoFromDecorator): unknown {
return noSideEffects(() => {
const ngModuleDef = getNgModuleDef(type, true);
ngModuleDef.declarations = scope.declarations || EMPTY_ARRAY;
ngModuleDef.imports = scope.imports || EMPTY_ARRAY;
ngModuleDef.exports = scope.exports || EMPTY_ARRAY;
ngModuleDef.declarations = convertToTypeArray(scope.declarations || EMPTY_ARRAY);
ngModuleDef.imports = convertToTypeArray(scope.imports || EMPTY_ARRAY);
ngModuleDef.exports = convertToTypeArray(scope.exports || EMPTY_ARRAY);

depsTracker.registerNgModule(type, scope);
});
}

function convertToTypeArray(values: Type<any>[]|(() => Type<any>[])|
RawScopeInfoFromDecorator[]): Type<any>[]|(() => Type<any>[]) {
if (typeof values === 'function') {
return values;
}

if (values.some(isForwardRef)) {
return () => values.map(resolveForwardRef).map(maybeUnwrapModuleWithProviders);
} else {
return values.map(maybeUnwrapModuleWithProviders);
}
}

function maybeUnwrapModuleWithProviders(value: any): Type<any> {
return isModuleWithProviders(value) ? value.ngModule : value as Type<any>;
}

0 comments on commit 0358802

Please sign in to comment.