fix(ivy): properly tree-shake away StaticInjector (#30219)
Ivy uses R3Injector, but we are currently pulling in both the StaticInjector (View Engine injector) and the R3Injector when running with Ivy. This commit adds an ivy switch so calling Injector.create() pulls in the correct implementation of the injector depending on whether you are using VE or Ivy. This saves us about 3KB in the bundle. PR Close #30219
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
|
||||
import {Inject, InjectionToken, Injector, Optional, Self, SkipSelf, forwardRef} from '@angular/core';
|
||||
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
||||
import {ivyEnabled, modifiedInIvy} from '@angular/private/testing';
|
||||
|
||||
import {stringify} from '../../src/util/stringify';
|
||||
|
||||
@ -86,7 +87,7 @@ function factoryFn(a: any){}
|
||||
{provide: 'provider10', useValue: 1}
|
||||
];
|
||||
|
||||
describe(`StaticInjector`, () => {
|
||||
modifiedInIvy('Ivy uses R3Injector').describe(`StaticInjector`, () => {
|
||||
|
||||
it('should instantiate a class without dependencies', () => {
|
||||
const injector = Injector.create([Engine.PROVIDER]);
|
||||
@ -413,9 +414,11 @@ function factoryFn(a: any){}
|
||||
[{provide: Car, useFactory: (e: Engine) => new Car(e), deps: [[Engine, new Self()]]}],
|
||||
parent);
|
||||
|
||||
const injectorName = ivyEnabled ? `R3Injector` : `StaticInjector`;
|
||||
|
||||
expect(() => child.get(Car))
|
||||
.toThrowError(
|
||||
`StaticInjectorError[${stringify(Car)} -> ${stringify(Engine)}]: \n` +
|
||||
`${injectorName}Error[${stringify(Car)} -> ${stringify(Engine)}]: \n` +
|
||||
' NullInjectorError: No provider for Engine!');
|
||||
});
|
||||
});
|
||||
@ -472,8 +475,11 @@ function factoryFn(a: any){}
|
||||
|
||||
describe('displayName', () => {
|
||||
it('should work', () => {
|
||||
const ivyError = `R3Injector[Engine, BrokenEngine, InjectionToken INJECTOR]`;
|
||||
const viewEngineError =
|
||||
`StaticInjector[Injector, InjectionToken INJECTOR, Engine, BrokenEngine]`;
|
||||
expect(Injector.create([Engine.PROVIDER, {provide: BrokenEngine, useValue: null}]).toString())
|
||||
.toEqual('StaticInjector[Injector, InjectionToken INJECTOR, Engine, BrokenEngine]');
|
||||
.toEqual(ivyEnabled ? ivyError : viewEngineError);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user