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
This commit is contained in:
Alex Rickabaugh
2018-02-02 10:33:48 -08:00
committed by Miško Hevery
parent 2d5e7d1b52
commit 235a235fab
58 changed files with 1753 additions and 228 deletions

View File

@ -14,8 +14,8 @@
export interface Inject { token: any; }
export const createInject = makeMetadataFactory<Inject>('Inject', (token: any) => ({token}));
export const createInjectionToken =
makeMetadataFactory<object>('InjectionToken', (desc: string) => ({_desc: desc}));
export const createInjectionToken = makeMetadataFactory<object>(
'InjectionToken', (desc: string) => ({_desc: desc, ngInjectableDef: undefined}));
export interface Attribute { attributeName?: string; }
export const createAttribute =
@ -126,7 +126,16 @@ export interface ModuleWithProviders {
ngModule: Type;
providers?: Provider[];
}
export interface Injectable {
scope?: Type|any;
useClass?: Type|any;
useExisting?: Type|any;
useValue?: any;
useFactory?: Type|any;
deps?: Array<Type|any[]>;
}
export const createInjectable =
makeMetadataFactory('Injectable', (injectable: Injectable = {}) => injectable);
export interface SchemaMetadata { name: string; }
export const CUSTOM_ELEMENTS_SCHEMA: SchemaMetadata = {
@ -138,7 +147,6 @@ export const NO_ERRORS_SCHEMA: SchemaMetadata = {
};
export const createOptional = makeMetadataFactory('Optional');
export const createInjectable = makeMetadataFactory('Injectable');
export const createSelf = makeMetadataFactory('Self');
export const createSkipSelf = makeMetadataFactory('SkipSelf');
export const createHost = makeMetadataFactory('Host');
@ -205,7 +213,18 @@ export const enum DepFlags {
None = 0,
SkipSelf = 1 << 0,
Optional = 1 << 1,
Value = 2 << 2,
Self = 1 << 2,
Value = 1 << 3,
}
/** Injection flags for DI. */
export const enum InjectFlags {
Default = 0,
/** Skip the node that is requesting injection. */
SkipSelf = 1 << 0,
/** Don't descend into ancestors of the node requesting injection. */
Self = 1 << 1,
}
export const enum ArgumentType {Inline = 0, Dynamic = 1}