feat(compiler): mark @NgModules in provider lists for identification at runtime (#22005)

All of the providers in a module get compiled into a module definition in the
factory file. Some of these providers are for the actual module types, as those
are available for injection in Angular. For tree-shakeable tokens, the runtime
needs to be able to distinguish which modules are present in an injector.

This change adds a NodeFlag which tags those module providers for later
identification.

PR Close #22005
This commit is contained in:
Alex Rickabaugh
2018-02-02 09:31:58 -08:00
committed by Miško Hevery
parent 647b8595d0
commit 2d5e7d1b52
7 changed files with 28 additions and 10 deletions

View File

@ -36,8 +36,12 @@ export function moduleProvideDef(
export function moduleDef(providers: NgModuleProviderDef[]): NgModuleDefinition {
const providersByKey: {[key: string]: NgModuleProviderDef} = {};
const modules = [];
for (let i = 0; i < providers.length; i++) {
const provider = providers[i];
if (provider.flags & NodeFlags.TypeNgModule) {
modules.push(provider.token);
}
provider.index = i;
providersByKey[tokenKey(provider.token)] = provider;
}
@ -45,7 +49,8 @@ export function moduleDef(providers: NgModuleProviderDef[]): NgModuleDefinition
// Will be filled later...
factory: null,
providersByKey,
providers
providers,
modules,
};
}

View File

@ -480,6 +480,7 @@ class NgModuleRef_ implements NgModuleData, InternalNgModuleRef<any> {
private _destroyed: boolean = false;
/** @internal */
_providers: any[];
_modules: any[];
readonly injector: Injector = this;

View File

@ -42,6 +42,7 @@ export interface Definition<DF extends DefinitionFactory<any>> { factory: DF|nul
export interface NgModuleDefinition extends Definition<NgModuleDefinitionFactory> {
providers: NgModuleProviderDef[];
providersByKey: {[tokenKey: string]: NgModuleProviderDef};
modules: any[];
}
export interface NgModuleDefinitionFactory extends DefinitionFactory<NgModuleDefinition> {}
@ -192,6 +193,7 @@ export const enum NodeFlags {
TypeViewQuery = 1 << 27,
StaticQuery = 1 << 28,
DynamicQuery = 1 << 29,
TypeNgModule = 1 << 30,
CatQuery = TypeContentQuery | TypeViewQuery,
// mutually exclusive values...
@ -293,6 +295,11 @@ export const enum DepFlags {
Value = 2 << 2,
}
export interface InjectableDef {
scope: any;
factory: () => any;
}
export interface TextDef { prefix: string; }
export interface QueryDef {