
This commit bundles 3 important changes, with the goal of enabling tree-shaking of services which are never injected. Ordinarily, this tree-shaking is prevented by the existence of a hard dependency on the service by the module in which it is declared. Firstly, @Injectable() is modified to accept a 'scope' parameter, which points to an @NgModule(). This reverses the dependency edge, permitting the module to not depend on the service which it "provides". Secondly, the runtime is modified to understand the new relationship created above. When a module receives a request to inject a token, and cannot find that token in its list of providers, it will then look at the token for a special ngInjectableDef field which indicates which module the token is scoped to. If that module happens to be in the injector, it will behave as if the token itself was in the injector to begin with. Thirdly, the compiler is modified to read the @Injectable() metadata and to generate the special ngInjectableDef field as part of TS compilation, using the PartialModules system. Additionally, this commit adds several unit and integration tests of various flavors to test this change. PR Close #22005
99 lines
5.0 KiB
TypeScript
99 lines
5.0 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
import {CompileReflector, ExternalReference, Identifiers, getUrlScheme, syntaxError} from '@angular/compiler';
|
|
import {ANALYZE_FOR_ENTRY_COMPONENTS, ChangeDetectionStrategy, ChangeDetectorRef, Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, ElementRef, Injector, LOCALE_ID, NgModuleFactory, NgModuleRef, QueryList, Renderer, SecurityContext, TRANSLATIONS_FORMAT, TemplateRef, ViewContainerRef, ViewEncapsulation, ɵCodegenComponentFactoryResolver, ɵEMPTY_ARRAY, ɵEMPTY_MAP, ɵReflectionCapabilities as ReflectionCapabilities, ɵand, ɵccf, ɵcmf, ɵcrt, ɵdid, ɵeld, ɵinlineInterpolate, ɵinterpolate, ɵmod, ɵmpd, ɵncd, ɵnov, ɵpad, ɵpid, ɵpod, ɵppd, ɵprd, ɵqud, ɵregisterModuleFactory, ɵstringify as stringify, ɵted, ɵunv, ɵvid} from '@angular/core';
|
|
|
|
export const MODULE_SUFFIX = '';
|
|
const builtinExternalReferences = createBuiltinExternalReferencesMap();
|
|
|
|
export class JitReflector implements CompileReflector {
|
|
private reflectionCapabilities: ReflectionCapabilities;
|
|
private builtinExternalReferences = new Map<ExternalReference, any>();
|
|
constructor() { this.reflectionCapabilities = new ReflectionCapabilities(); }
|
|
componentModuleUrl(type: any, cmpMetadata: Component): string {
|
|
const moduleId = cmpMetadata.moduleId;
|
|
|
|
if (typeof moduleId === 'string') {
|
|
const scheme = getUrlScheme(moduleId);
|
|
return scheme ? moduleId : `package:${moduleId}${MODULE_SUFFIX}`;
|
|
} else if (moduleId !== null && moduleId !== void 0) {
|
|
throw syntaxError(
|
|
`moduleId should be a string in "${stringify(type)}". See https://goo.gl/wIDDiL for more information.\n` +
|
|
`If you're using Webpack you should inline the template and the styles, see https://goo.gl/X2J8zc.`);
|
|
}
|
|
|
|
return `./${stringify(type)}`;
|
|
}
|
|
parameters(typeOrFunc: /*Type*/ any): any[][] {
|
|
return this.reflectionCapabilities.parameters(typeOrFunc);
|
|
}
|
|
tryAnnotations(typeOrFunc: /*Type*/ any): any[] { return this.annotations(typeOrFunc); }
|
|
annotations(typeOrFunc: /*Type*/ any): any[] {
|
|
return this.reflectionCapabilities.annotations(typeOrFunc);
|
|
}
|
|
propMetadata(typeOrFunc: /*Type*/ any): {[key: string]: any[]} {
|
|
return this.reflectionCapabilities.propMetadata(typeOrFunc);
|
|
}
|
|
hasLifecycleHook(type: any, lcProperty: string): boolean {
|
|
return this.reflectionCapabilities.hasLifecycleHook(type, lcProperty);
|
|
}
|
|
guards(type: any): {[key: string]: any} { return this.reflectionCapabilities.guards(type); }
|
|
resolveExternalReference(ref: ExternalReference): any {
|
|
return builtinExternalReferences.get(ref) || ref.runtime;
|
|
}
|
|
}
|
|
|
|
|
|
function createBuiltinExternalReferencesMap() {
|
|
const map = new Map<ExternalReference, any>();
|
|
map.set(Identifiers.ANALYZE_FOR_ENTRY_COMPONENTS, ANALYZE_FOR_ENTRY_COMPONENTS);
|
|
map.set(Identifiers.ElementRef, ElementRef);
|
|
map.set(Identifiers.NgModuleRef, NgModuleRef);
|
|
map.set(Identifiers.ViewContainerRef, ViewContainerRef);
|
|
map.set(Identifiers.ChangeDetectorRef, ChangeDetectorRef);
|
|
map.set(Identifiers.QueryList, QueryList);
|
|
map.set(Identifiers.TemplateRef, TemplateRef);
|
|
map.set(Identifiers.CodegenComponentFactoryResolver, ɵCodegenComponentFactoryResolver);
|
|
map.set(Identifiers.ComponentFactoryResolver, ComponentFactoryResolver);
|
|
map.set(Identifiers.ComponentFactory, ComponentFactory);
|
|
map.set(Identifiers.ComponentRef, ComponentRef);
|
|
map.set(Identifiers.NgModuleFactory, NgModuleFactory);
|
|
map.set(Identifiers.createModuleFactory, ɵcmf);
|
|
map.set(Identifiers.moduleDef, ɵmod);
|
|
map.set(Identifiers.moduleProviderDef, ɵmpd);
|
|
map.set(Identifiers.RegisterModuleFactoryFn, ɵregisterModuleFactory);
|
|
map.set(Identifiers.Injector, Injector);
|
|
map.set(Identifiers.ViewEncapsulation, ViewEncapsulation);
|
|
map.set(Identifiers.ChangeDetectionStrategy, ChangeDetectionStrategy);
|
|
map.set(Identifiers.SecurityContext, SecurityContext);
|
|
map.set(Identifiers.LOCALE_ID, LOCALE_ID);
|
|
map.set(Identifiers.TRANSLATIONS_FORMAT, TRANSLATIONS_FORMAT);
|
|
map.set(Identifiers.inlineInterpolate, ɵinlineInterpolate);
|
|
map.set(Identifiers.interpolate, ɵinterpolate);
|
|
map.set(Identifiers.EMPTY_ARRAY, ɵEMPTY_ARRAY);
|
|
map.set(Identifiers.EMPTY_MAP, ɵEMPTY_MAP);
|
|
map.set(Identifiers.Renderer, Renderer);
|
|
map.set(Identifiers.viewDef, ɵvid);
|
|
map.set(Identifiers.elementDef, ɵeld);
|
|
map.set(Identifiers.anchorDef, ɵand);
|
|
map.set(Identifiers.textDef, ɵted);
|
|
map.set(Identifiers.directiveDef, ɵdid);
|
|
map.set(Identifiers.providerDef, ɵprd);
|
|
map.set(Identifiers.queryDef, ɵqud);
|
|
map.set(Identifiers.pureArrayDef, ɵpad);
|
|
map.set(Identifiers.pureObjectDef, ɵpod);
|
|
map.set(Identifiers.purePipeDef, ɵppd);
|
|
map.set(Identifiers.pipeDef, ɵpid);
|
|
map.set(Identifiers.nodeValue, ɵnov);
|
|
map.set(Identifiers.ngContentDef, ɵncd);
|
|
map.set(Identifiers.unwrapValue, ɵunv);
|
|
map.set(Identifiers.createRendererType2, ɵcrt);
|
|
map.set(Identifiers.createComponentFactory, ɵccf);
|
|
return map;
|
|
} |