refactor(core): rename ngModuleDef to ɵmod (#33142)
Module defs are not considered public API, so the property that contains them should be prefixed with Angular's marker for "private" ('ɵ') to discourage apps from relying on def APIs directly. This commit adds the prefix and shortens the name from ngModuleDef to mod. This is because property names cannot be minified by Uglify without turning on property mangling (which most apps have turned off) and are thus size-sensitive. PR Close #33142
This commit is contained in:

committed by
Miško Hevery

parent
d62eff7316
commit
fc93dafab1
@ -224,7 +224,7 @@ export {
|
||||
NG_COMP_DEF as ɵNG_COMP_DEF,
|
||||
NG_DIR_DEF as ɵNG_DIR_DEF,
|
||||
NG_PIPE_DEF as ɵNG_PIPE_DEF,
|
||||
NG_MODULE_DEF as ɵNG_MODULE_DEF,
|
||||
NG_MOD_DEF as ɵNG_MOD_DEF,
|
||||
NG_BASE_DEF as ɵNG_BASE_DEF
|
||||
} from './render3/fields';
|
||||
|
||||
|
@ -39,14 +39,14 @@ function assertSameOrNotExisting(id: string, type: Type<any>| null, incoming: Ty
|
||||
}
|
||||
|
||||
export function registerNgModuleType(ngModuleType: NgModuleType) {
|
||||
if (ngModuleType.ngModuleDef.id !== null) {
|
||||
const id = ngModuleType.ngModuleDef.id;
|
||||
if (ngModuleType.ɵmod.id !== null) {
|
||||
const id = ngModuleType.ɵmod.id;
|
||||
const existing = modules.get(id) as NgModuleType | null;
|
||||
assertSameOrNotExisting(id, existing, ngModuleType);
|
||||
modules.set(id, ngModuleType);
|
||||
}
|
||||
|
||||
let imports = ngModuleType.ngModuleDef.imports;
|
||||
let imports = ngModuleType.ɵmod.imports;
|
||||
if (imports instanceof Function) {
|
||||
imports = imports();
|
||||
}
|
||||
|
@ -29,8 +29,7 @@ export function assertComponentType(
|
||||
|
||||
export function assertNgModuleType(
|
||||
actual: any,
|
||||
msg: string =
|
||||
'Type passed in is not NgModuleType, it does not have \'ngModuleDef\' property.') {
|
||||
msg: string = 'Type passed in is not NgModuleType, it does not have \'ɵmod\' property.') {
|
||||
if (!getNgModuleDef(actual)) {
|
||||
throwError(msg);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ import {initNgDevMode} from '../util/ng_dev_mode';
|
||||
import {stringify} from '../util/stringify';
|
||||
|
||||
import {EMPTY_ARRAY, EMPTY_OBJ} from './empty';
|
||||
import {NG_BASE_DEF, NG_COMP_DEF, NG_DIR_DEF, NG_FACTORY_DEF, NG_LOCALE_ID_DEF, NG_MODULE_DEF, NG_PIPE_DEF} from './fields';
|
||||
import {NG_BASE_DEF, NG_COMP_DEF, NG_DIR_DEF, NG_FACTORY_DEF, NG_LOCALE_ID_DEF, NG_MOD_DEF, NG_PIPE_DEF} from './fields';
|
||||
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DirectiveDef, DirectiveDefFeature, DirectiveType, DirectiveTypesOrFactory, FactoryFn, HostBindingsFunction, PipeDef, PipeType, PipeTypesOrFactory, ViewQueriesFunction, ɵɵBaseDef} from './interfaces/definition';
|
||||
import {TAttributes} from './interfaces/node';
|
||||
// while SelectorFlags is unused here, it's required so that types don't get resolved lazily
|
||||
@ -768,9 +768,9 @@ export function getFactoryDef<T>(type: any, throwNotFound?: boolean): FactoryFn<
|
||||
export function getNgModuleDef<T>(type: any, throwNotFound: true): NgModuleDef<T>;
|
||||
export function getNgModuleDef<T>(type: any): NgModuleDef<T>|null;
|
||||
export function getNgModuleDef<T>(type: any, throwNotFound?: boolean): NgModuleDef<T>|null {
|
||||
const ngModuleDef = type[NG_MODULE_DEF] || null;
|
||||
const ngModuleDef = type[NG_MOD_DEF] || null;
|
||||
if (!ngModuleDef && throwNotFound === true) {
|
||||
throw new Error(`Type ${stringify(type)} does not have 'ngModuleDef' property.`);
|
||||
throw new Error(`Type ${stringify(type)} does not have 'ɵmod' property.`);
|
||||
}
|
||||
return ngModuleDef;
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import {getClosureSafeProperty} from '../util/property';
|
||||
export const NG_COMP_DEF = getClosureSafeProperty({ɵcmp: getClosureSafeProperty});
|
||||
export const NG_DIR_DEF = getClosureSafeProperty({ɵdir: getClosureSafeProperty});
|
||||
export const NG_PIPE_DEF = getClosureSafeProperty({ɵpipe: getClosureSafeProperty});
|
||||
export const NG_MODULE_DEF = getClosureSafeProperty({ngModuleDef: getClosureSafeProperty});
|
||||
export const NG_MOD_DEF = getClosureSafeProperty({ɵmod: getClosureSafeProperty});
|
||||
export const NG_LOCALE_ID_DEF = getClosureSafeProperty({ngLocaleIdDef: getClosureSafeProperty});
|
||||
export const NG_BASE_DEF = getClosureSafeProperty({ngBaseDef: getClosureSafeProperty});
|
||||
export const NG_FACTORY_DEF = getClosureSafeProperty({ɵfac: getClosureSafeProperty});
|
||||
|
@ -16,7 +16,7 @@ import {ModuleWithProviders, NgModule, NgModuleDef, NgModuleTransitiveScopes} fr
|
||||
import {deepForEach, flatten} from '../../util/array_utils';
|
||||
import {assertDefined} from '../../util/assert';
|
||||
import {getComponentDef, getDirectiveDef, getNgModuleDef, getPipeDef} from '../definition';
|
||||
import {NG_COMP_DEF, NG_DIR_DEF, NG_MODULE_DEF, NG_PIPE_DEF} from '../fields';
|
||||
import {NG_COMP_DEF, NG_DIR_DEF, NG_MOD_DEF, NG_PIPE_DEF} from '../fields';
|
||||
import {ComponentDef} from '../interfaces/definition';
|
||||
import {NgModuleType} from '../ng_module_ref';
|
||||
import {maybeUnwrapFn, stringifyForError} from '../util/misc_utils';
|
||||
@ -93,7 +93,7 @@ export function compileNgModule(moduleType: Type<any>, ngModule: NgModule = {}):
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles and adds the `ngModuleDef` and `ngInjectorDef` properties to the module class.
|
||||
* Compiles and adds the `ɵmod` and `ngInjectorDef` properties to the module class.
|
||||
*
|
||||
* It's possible to compile a module via this API which will allow duplicate declarations in its
|
||||
* root.
|
||||
@ -105,7 +105,7 @@ export function compileNgModuleDefs(
|
||||
ngDevMode && assertDefined(ngModule, 'Required value ngModule');
|
||||
const declarations: Type<any>[] = flatten(ngModule.declarations || EMPTY_ARRAY);
|
||||
let ngModuleDef: any = null;
|
||||
Object.defineProperty(moduleType, NG_MODULE_DEF, {
|
||||
Object.defineProperty(moduleType, NG_MOD_DEF, {
|
||||
configurable: true,
|
||||
get: () => {
|
||||
if (ngModuleDef === null) {
|
||||
@ -115,7 +115,7 @@ export function compileNgModuleDefs(
|
||||
throw new Error(`'${stringifyForError(moduleType)}' module can't import itself`);
|
||||
}
|
||||
ngModuleDef = getCompilerFacade().compileNgModule(
|
||||
angularCoreEnv, `ng:///${moduleType.name}/ngModuleDef.js`, {
|
||||
angularCoreEnv, `ng:///${moduleType.name}/ɵmod.js`, {
|
||||
type: moduleType,
|
||||
bootstrap: flatten(ngModule.bootstrap || EMPTY_ARRAY).map(resolveForwardRef),
|
||||
declarations: declarations.map(resolveForwardRef),
|
||||
@ -429,7 +429,7 @@ export function transitiveScopesFor<T>(
|
||||
moduleType: Type<T>,
|
||||
processNgModuleFn?: (ngModule: NgModuleType) => void): NgModuleTransitiveScopes {
|
||||
if (!isNgModule(moduleType)) {
|
||||
throw new Error(`${moduleType.name} does not have an ngModuleDef`);
|
||||
throw new Error(`${moduleType.name} does not have a module def (ɵmod property)`);
|
||||
}
|
||||
const def = getNgModuleDef(moduleType) !;
|
||||
|
||||
@ -465,11 +465,11 @@ export function transitiveScopesFor<T>(
|
||||
maybeUnwrapFn(def.imports).forEach(<I>(imported: Type<I>) => {
|
||||
const importedType = imported as Type<I>& {
|
||||
// If imported is an @NgModule:
|
||||
ngModuleDef?: NgModuleDef<I>;
|
||||
ɵmod?: NgModuleDef<I>;
|
||||
};
|
||||
|
||||
if (!isNgModule<I>(importedType)) {
|
||||
throw new Error(`Importing ${importedType.name} which does not have an ngModuleDef`);
|
||||
throw new Error(`Importing ${importedType.name} which does not have a ɵmod property`);
|
||||
}
|
||||
|
||||
if (processNgModuleFn) {
|
||||
@ -488,7 +488,7 @@ export function transitiveScopesFor<T>(
|
||||
// Components, Directives, NgModules, and Pipes can all be exported.
|
||||
ɵcmp?: any;
|
||||
ɵdir?: any;
|
||||
ngModuleDef?: NgModuleDef<E>;
|
||||
ɵmod?: NgModuleDef<E>;
|
||||
ɵpipe?: any;
|
||||
};
|
||||
|
||||
@ -528,6 +528,6 @@ function isModuleWithProviders(value: any): value is ModuleWithProviders<{}> {
|
||||
return (value as{ngModule?: any}).ngModule !== undefined;
|
||||
}
|
||||
|
||||
function isNgModule<T>(value: Type<T>): value is Type<T>&{ngModuleDef: NgModuleDef<T>} {
|
||||
function isNgModule<T>(value: Type<T>): value is Type<T>&{ɵmod: NgModuleDef<T>} {
|
||||
return !!getNgModuleDef(value);
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import {getNgLocaleIdDef, getNgModuleDef} from './definition';
|
||||
import {setLocaleId} from './i18n';
|
||||
import {maybeUnwrapFn} from './util/misc_utils';
|
||||
|
||||
export interface NgModuleType<T = any> extends Type<T> { ngModuleDef: NgModuleDef<T>; }
|
||||
export interface NgModuleType<T = any> extends Type<T> { ɵmod: NgModuleDef<T>; }
|
||||
|
||||
const COMPONENT_FACTORY_RESOLVER: StaticProvider = {
|
||||
provide: viewEngine_ComponentFactoryResolver,
|
||||
|
@ -335,14 +335,14 @@ function declareTests(config?: {useJit: boolean}) {
|
||||
.it('should register a module even if not importing the .ngfactory file or calling create()',
|
||||
() => {
|
||||
class ChildModule {
|
||||
static ngModuleDef = defineNgModule({
|
||||
static ɵmod = defineNgModule({
|
||||
type: ChildModule,
|
||||
id: 'child',
|
||||
});
|
||||
}
|
||||
|
||||
class Module {
|
||||
static ngModuleDef = defineNgModule({
|
||||
static ɵmod = defineNgModule({
|
||||
type: Module,
|
||||
id: 'test',
|
||||
imports: [ChildModule],
|
||||
|
@ -156,7 +156,7 @@ ivyEnabled && describe('render3 jit', () => {
|
||||
class Module {
|
||||
}
|
||||
|
||||
const moduleDef: NgModuleDef<Module> = (Module as any).ngModuleDef;
|
||||
const moduleDef: NgModuleDef<Module> = (Module as any).ɵmod;
|
||||
expect(moduleDef).toBeDefined();
|
||||
if (!Array.isArray(moduleDef.declarations)) {
|
||||
return fail('Expected an array');
|
||||
@ -208,7 +208,7 @@ ivyEnabled && describe('render3 jit', () => {
|
||||
class Module {
|
||||
}
|
||||
|
||||
const moduleDef: NgModuleDef<Module> = (Module as any).ngModuleDef;
|
||||
const moduleDef: NgModuleDef<Module> = (Module as any).ɵmod;
|
||||
// directive defs are still null, since no directives were in that component
|
||||
expect(cmpDef.directiveDefs).toBeNull();
|
||||
});
|
||||
|
@ -1134,7 +1134,7 @@ describe('providers', () => {
|
||||
{provide: String, useValue: 'From module injector'}
|
||||
]
|
||||
});
|
||||
static ngModuleDef: NgModuleDef<any> = { bootstrap: [] } as any;
|
||||
static ɵmod: NgModuleDef<any> = { bootstrap: [] } as any;
|
||||
}
|
||||
const myAppModuleFactory = new NgModuleFactory(MyAppModule);
|
||||
const ngModuleRef = myAppModuleFactory.create(null);
|
||||
|
@ -553,7 +553,7 @@ describe('TestBed', () => {
|
||||
|
||||
// This is an AOT compiled module which declares (but does not export) SomeComponent.
|
||||
class ModuleClass {
|
||||
static ngModuleDef = defineNgModule({
|
||||
static ɵmod = defineNgModule({
|
||||
type: ModuleClass,
|
||||
declarations: [SomeComponent],
|
||||
});
|
||||
|
@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {ApplicationInitStatus, COMPILER_OPTIONS, Compiler, Component, Directive, Injector, LOCALE_ID, ModuleWithComponentFactories, ModuleWithProviders, NgModule, NgModuleFactory, NgZone, Pipe, PlatformRef, Provider, Type, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵDirectiveDef as DirectiveDef, ɵNG_COMP_DEF as NG_COMP_DEF, ɵNG_DIR_DEF as NG_DIR_DEF, ɵNG_INJECTOR_DEF as NG_INJECTOR_DEF, ɵNG_MODULE_DEF as NG_MODULE_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵsetLocaleId as setLocaleId, ɵtransitiveScopesFor as transitiveScopesFor, ɵɵInjectableDef as InjectableDef} from '@angular/core';
|
||||
import {ApplicationInitStatus, COMPILER_OPTIONS, Compiler, Component, Directive, Injector, LOCALE_ID, ModuleWithComponentFactories, ModuleWithProviders, NgModule, NgModuleFactory, NgZone, Pipe, PlatformRef, Provider, Type, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID, ɵDirectiveDef as DirectiveDef, ɵNG_COMP_DEF as NG_COMP_DEF, ɵNG_DIR_DEF as NG_DIR_DEF, ɵNG_INJECTOR_DEF as NG_INJECTOR_DEF, ɵNG_MOD_DEF as NG_MOD_DEF, ɵNG_PIPE_DEF as NG_PIPE_DEF, ɵNgModuleFactory as R3NgModuleFactory, ɵNgModuleTransitiveScopes as NgModuleTransitiveScopes, ɵNgModuleType as NgModuleType, ɵRender3ComponentFactory as ComponentFactory, ɵRender3NgModuleRef as NgModuleRef, ɵcompileComponent as compileComponent, ɵcompileDirective as compileDirective, ɵcompileNgModuleDefs as compileNgModuleDefs, ɵcompilePipe as compilePipe, ɵgetInjectableDef as getInjectableDef, ɵpatchComponentDefWithScope as patchComponentDefWithScope, ɵsetLocaleId as setLocaleId, ɵtransitiveScopesFor as transitiveScopesFor, ɵɵInjectableDef as InjectableDef} from '@angular/core';
|
||||
import {ModuleRegistrationMap, getRegisteredModulesState, restoreRegisteredModulesState} from '../../src/linker/ng_module_factory_registration';
|
||||
|
||||
import {clearResolutionOfComponentResourcesQueue, isComponentDefPendingResolution, resolveComponentResources, restoreComponentResolutionQueue} from '../../src/metadata/resource_loading';
|
||||
@ -130,7 +130,7 @@ export class R3TestBedCompiler {
|
||||
|
||||
this.recompileNgModule(ngModule);
|
||||
|
||||
// At this point, the module has a valid .ngModuleDef, but the override may have introduced
|
||||
// At this point, the module has a valid module def (ɵmod), but the override may have introduced
|
||||
// new declarations or imported modules. Ingest any possible new types and add them to the
|
||||
// current queue.
|
||||
this.queueTypesFromModulesArray([ngModule]);
|
||||
@ -285,7 +285,7 @@ export class R3TestBedCompiler {
|
||||
* @internal
|
||||
*/
|
||||
_getComponentFactories(moduleType: NgModuleType): ComponentFactory<any>[] {
|
||||
return maybeUnwrapFn(moduleType.ngModuleDef.declarations).reduce((factories, declaration) => {
|
||||
return maybeUnwrapFn(moduleType.ɵmod.declarations).reduce((factories, declaration) => {
|
||||
const componentDef = (declaration as any).ɵcmp;
|
||||
componentDef && factories.push(new ComponentFactory(componentDef, this.testModuleRef !));
|
||||
return factories;
|
||||
@ -380,7 +380,7 @@ export class R3TestBedCompiler {
|
||||
}
|
||||
|
||||
// Apply provider overrides to imported modules recursively
|
||||
const moduleDef: any = (moduleType as any)[NG_MODULE_DEF];
|
||||
const moduleDef: any = (moduleType as any)[NG_MOD_DEF];
|
||||
for (const importType of moduleDef.imports) {
|
||||
this.applyProviderOverridesToModule(importType);
|
||||
}
|
||||
@ -409,7 +409,7 @@ export class R3TestBedCompiler {
|
||||
throw new Error(`Unable to resolve metadata for NgModule: ${ngModule.name}`);
|
||||
}
|
||||
// Cache the initial ngModuleDef as it will be overwritten.
|
||||
this.maybeStoreNgDef(NG_MODULE_DEF, ngModule);
|
||||
this.maybeStoreNgDef(NG_MOD_DEF, ngModule);
|
||||
this.maybeStoreNgDef(NG_INJECTOR_DEF, ngModule);
|
||||
|
||||
compileNgModuleDefs(ngModule as NgModuleType<any>, metadata);
|
||||
@ -469,7 +469,7 @@ export class R3TestBedCompiler {
|
||||
if (Array.isArray(value)) {
|
||||
this.queueTypesFromModulesArray(value);
|
||||
} else if (hasNgModuleDef(value)) {
|
||||
const def = value.ngModuleDef;
|
||||
const def = value.ɵmod;
|
||||
// Look through declarations, imports, and exports, and queue everything found there.
|
||||
this.queueTypeArray(maybeUnwrapFn(def.declarations), value);
|
||||
this.queueTypesFromModulesArray(maybeUnwrapFn(def.imports));
|
||||
@ -682,7 +682,7 @@ function initResolvers(): Resolvers {
|
||||
}
|
||||
|
||||
function hasNgModuleDef<T>(value: Type<T>): value is NgModuleType<T> {
|
||||
return value.hasOwnProperty('ngModuleDef');
|
||||
return value.hasOwnProperty('ɵmod');
|
||||
}
|
||||
|
||||
function maybeUnwrapFn<T>(maybeFn: (() => T) | T): T {
|
||||
|
Reference in New Issue
Block a user