perf: switch angular to use StaticInjector instead of ReflectiveInjector

This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.

Code savings for HelloWorld using Closure:

Reflective: bundle.js:  105,864(34,190 gzip)
    Static: bundle.js:  154,889(33,555 gzip)
                            645( 2%)

BREAKING CHANGE:

`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.

Example:
Before:
```
[
  MyClass,
  {provide: ClassA, useClass: SubClassA}
]

```

After:
```
[
  {provide: MyClass, deps: [Dep1,...]},
  {provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```

NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.

Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.

DEPRECATION:

- `ReflectiveInjector` is now deprecated as it will be remove. Use
  `Injector.create` as a replacement.

closes #18496
This commit is contained in:
Miško Hevery
2017-08-03 12:33:29 -07:00
committed by Victor Berchet
parent d9d00bd9b5
commit fcadbf4bf6
94 changed files with 380 additions and 279 deletions

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ReflectiveInjector} from '@angular/core';
import {Injector} from '@angular/core';
import {IterableDiffers} from '@angular/core/src/change_detection/differs/iterable_differs';
import {SpyIterableDifferFactory} from '../../spies';
@ -50,7 +50,7 @@ export function main() {
describe('.extend()', () => {
it('should throw if calling extend when creating root injector', () => {
const injector = ReflectiveInjector.resolveAndCreate([IterableDiffers.extend([])]);
const injector = Injector.create([IterableDiffers.extend([])]);
expect(() => injector.get(IterableDiffers))
.toThrowError(/Cannot extend IterableDiffers without a parent injector/);
@ -58,9 +58,8 @@ export function main() {
it('should extend di-inherited differs', () => {
const parent = new IterableDiffers([factory1]);
const injector =
ReflectiveInjector.resolveAndCreate([{provide: IterableDiffers, useValue: parent}]);
const childInjector = injector.resolveAndCreateChild([IterableDiffers.extend([factory2])]);
const injector = Injector.create([{provide: IterableDiffers, useValue: parent}]);
const childInjector = Injector.create([IterableDiffers.extend([factory2])], injector);
expect(injector.get(IterableDiffers).factories).toEqual([factory1]);
expect(childInjector.get(IterableDiffers).factories).toEqual([factory2, factory1]);

View File

@ -7,7 +7,7 @@
*/
import {CommonModule} from '@angular/common';
import {Compiler, ComponentFactory, ErrorHandler, EventEmitter, Host, Inject, Injectable, InjectionToken, Injector, NO_ERRORS_SCHEMA, NgModule, NgModuleRef, OnDestroy, ReflectiveInjector, SkipSelf} from '@angular/core';
import {Compiler, ComponentFactory, ErrorHandler, EventEmitter, Host, Inject, Injectable, InjectionToken, Injector, NO_ERRORS_SCHEMA, NgModule, NgModuleRef, OnDestroy, SkipSelf} from '@angular/core';
import {ChangeDetectionStrategy, ChangeDetectorRef, PipeTransform} from '@angular/core/src/change_detection/change_detection';
import {getDebugContext} from '@angular/core/src/errors';
import {ComponentFactoryResolver} from '@angular/core/src/linker/component_factory_resolver';
@ -1850,8 +1850,7 @@ class DynamicViewport {
const myService = new MyService();
myService.greeting = 'dynamic greet';
this.injector = ReflectiveInjector.resolveAndCreate(
[{provide: MyService, useValue: myService}], vc.injector);
this.injector = Injector.create([{provide: MyService, useValue: myService}], vc.injector);
this.componentFactory =
componentFactoryResolver.resolveComponentFactory(ChildCompUsingService) !;
}

View File

@ -163,13 +163,19 @@ export function main() {
// root elements
expect(() => createAndGetRootNodes(compViewDef(nodes)))
.toThrowError('No provider for Dep!');
.toThrowError(
'StaticInjectorError[Dep]: \n' +
' StaticInjectorError[Dep]: \n' +
' NullInjectorError: No provider for Dep!');
// non root elements
expect(
() => createAndGetRootNodes(compViewDef(
[elementDef(NodeFlags.None, null !, null !, 4, 'span')].concat(nodes))))
.toThrowError('No provider for Dep!');
.toThrowError(
'StaticInjectorError[Dep]: \n' +
' StaticInjectorError[Dep]: \n' +
' NullInjectorError: No provider for Dep!');
});
it('should inject from a parent element in a parent view', () => {
@ -191,7 +197,10 @@ export function main() {
elementDef(NodeFlags.None, null !, null !, 1, 'span'),
directiveDef(NodeFlags.None, null !, 0, SomeService, ['nonExistingDep'])
])))
.toThrowError('No provider for nonExistingDep!');
.toThrowError(
'StaticInjectorError[nonExistingDep]: \n' +
' StaticInjectorError[nonExistingDep]: \n' +
' NullInjectorError: No provider for nonExistingDep!');
});
it('should use null for optional missing dependencies', () => {