Files
angular/packages/compiler/src/identifiers.ts
Alex Rickabaugh 235a235fab feat: change @Injectable() to support tree-shakeable tokens (#22005)
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
2018-02-12 14:34:59 -08:00

134 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 {CompileTokenMetadata} from './compile_metadata';
import {CompileReflector} from './compile_reflector';
import * as o from './output/output_ast';
const CORE = '@angular/core';
export class Identifiers {
static ANALYZE_FOR_ENTRY_COMPONENTS: o.ExternalReference = {
name: 'ANALYZE_FOR_ENTRY_COMPONENTS',
moduleName: CORE,
};
static ElementRef: o.ExternalReference = {name: 'ElementRef', moduleName: CORE};
static NgModuleRef: o.ExternalReference = {name: 'NgModuleRef', moduleName: CORE};
static ViewContainerRef: o.ExternalReference = {name: 'ViewContainerRef', moduleName: CORE};
static ChangeDetectorRef: o.ExternalReference = {
name: 'ChangeDetectorRef',
moduleName: CORE,
};
static QueryList: o.ExternalReference = {name: 'QueryList', moduleName: CORE};
static TemplateRef: o.ExternalReference = {name: 'TemplateRef', moduleName: CORE};
static CodegenComponentFactoryResolver: o.ExternalReference = {
name: 'ɵCodegenComponentFactoryResolver',
moduleName: CORE,
};
static ComponentFactoryResolver: o.ExternalReference = {
name: 'ComponentFactoryResolver',
moduleName: CORE,
};
static ComponentFactory: o.ExternalReference = {name: 'ComponentFactory', moduleName: CORE};
static ComponentRef: o.ExternalReference = {name: 'ComponentRef', moduleName: CORE};
static NgModuleFactory: o.ExternalReference = {name: 'NgModuleFactory', moduleName: CORE};
static createModuleFactory: o.ExternalReference = {
name: 'ɵcmf',
moduleName: CORE,
};
static moduleDef: o.ExternalReference = {
name: 'ɵmod',
moduleName: CORE,
};
static moduleProviderDef: o.ExternalReference = {
name: 'ɵmpd',
moduleName: CORE,
};
static RegisterModuleFactoryFn: o.ExternalReference = {
name: 'ɵregisterModuleFactory',
moduleName: CORE,
};
static inject: o.ExternalReference = {name: 'inject', moduleName: CORE};
static Injector: o.ExternalReference = {name: 'Injector', moduleName: CORE};
static defineInjectable: o.ExternalReference = {name: 'defineInjectable', moduleName: CORE};
static ViewEncapsulation: o.ExternalReference = {
name: 'ViewEncapsulation',
moduleName: CORE,
};
static ChangeDetectionStrategy: o.ExternalReference = {
name: 'ChangeDetectionStrategy',
moduleName: CORE,
};
static SecurityContext: o.ExternalReference = {
name: 'SecurityContext',
moduleName: CORE,
};
static LOCALE_ID: o.ExternalReference = {name: 'LOCALE_ID', moduleName: CORE};
static TRANSLATIONS_FORMAT: o.ExternalReference = {
name: 'TRANSLATIONS_FORMAT',
moduleName: CORE,
};
static inlineInterpolate: o.ExternalReference = {
name: 'ɵinlineInterpolate',
moduleName: CORE,
};
static interpolate: o.ExternalReference = {name: 'ɵinterpolate', moduleName: CORE};
static EMPTY_ARRAY: o.ExternalReference = {name: 'ɵEMPTY_ARRAY', moduleName: CORE};
static EMPTY_MAP: o.ExternalReference = {name: 'ɵEMPTY_MAP', moduleName: CORE};
static Renderer: o.ExternalReference = {name: 'Renderer', moduleName: CORE};
static viewDef: o.ExternalReference = {name: 'ɵvid', moduleName: CORE};
static elementDef: o.ExternalReference = {name: 'ɵeld', moduleName: CORE};
static anchorDef: o.ExternalReference = {name: 'ɵand', moduleName: CORE};
static textDef: o.ExternalReference = {name: 'ɵted', moduleName: CORE};
static directiveDef: o.ExternalReference = {name: 'ɵdid', moduleName: CORE};
static providerDef: o.ExternalReference = {name: 'ɵprd', moduleName: CORE};
static queryDef: o.ExternalReference = {name: 'ɵqud', moduleName: CORE};
static pureArrayDef: o.ExternalReference = {name: 'ɵpad', moduleName: CORE};
static pureObjectDef: o.ExternalReference = {name: 'ɵpod', moduleName: CORE};
static purePipeDef: o.ExternalReference = {name: 'ɵppd', moduleName: CORE};
static pipeDef: o.ExternalReference = {name: 'ɵpid', moduleName: CORE};
static nodeValue: o.ExternalReference = {name: 'ɵnov', moduleName: CORE};
static ngContentDef: o.ExternalReference = {name: 'ɵncd', moduleName: CORE};
static unwrapValue: o.ExternalReference = {name: 'ɵunv', moduleName: CORE};
static createRendererType2: o.ExternalReference = {name: 'ɵcrt', moduleName: CORE};
// type only
static RendererType2: o.ExternalReference = {
name: 'RendererType2',
moduleName: CORE,
};
// type only
static ViewDefinition: o.ExternalReference = {
name: 'ɵViewDefinition',
moduleName: CORE,
};
static createComponentFactory: o.ExternalReference = {name: 'ɵccf', moduleName: CORE};
}
export function createTokenForReference(reference: any): CompileTokenMetadata {
return {identifier: {reference: reference}};
}
export function createTokenForExternalReference(
reflector: CompileReflector, reference: o.ExternalReference): CompileTokenMetadata {
return createTokenForReference(reflector.resolveExternalReference(reference));
}