2019-02-11 11:12:57 +01:00
|
|
|
|
2016-10-19 21:41:04 +01:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-10-19 21:41:04 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-03-22 09:42:52 +00:00
|
|
|
import {IInjectorService} from '../../src/common/src/angular1';
|
2016-10-19 21:41:04 +01:00
|
|
|
|
|
|
|
// We have to do a little dance to get the ng1 injector into the module injector.
|
|
|
|
// We store the ng1 injector so that the provider in the module injector can access it
|
|
|
|
// Then we "get" the ng1 injector from the module injector, which triggers the provider to read
|
|
|
|
// the stored injector and release the reference to it.
|
2019-02-11 11:12:57 +01:00
|
|
|
let tempInjectorRef: IInjectorService|null = null;
|
|
|
|
export function setTempInjectorRef(injector: IInjectorService) {
|
2016-10-19 21:41:04 +01:00
|
|
|
tempInjectorRef = injector;
|
|
|
|
}
|
|
|
|
export function injectorFactory() {
|
2017-07-20 19:06:13 +03:00
|
|
|
if (!tempInjectorRef) {
|
|
|
|
throw new Error('Trying to get the AngularJS injector before it being set.');
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:12:57 +01:00
|
|
|
const injector: IInjectorService = tempInjectorRef;
|
2016-10-19 21:41:04 +01:00
|
|
|
tempInjectorRef = null; // clear the value to prevent memory leaks
|
|
|
|
return injector;
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:12:57 +01:00
|
|
|
export function rootScopeFactory(i: IInjectorService) {
|
2016-10-19 21:41:04 +01:00
|
|
|
return i.get('$rootScope');
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:12:57 +01:00
|
|
|
export function compileFactory(i: IInjectorService) {
|
2016-10-19 21:41:04 +01:00
|
|
|
return i.get('$compile');
|
|
|
|
}
|
|
|
|
|
2019-02-11 11:12:57 +01:00
|
|
|
export function parseFactory(i: IInjectorService) {
|
2016-10-19 21:41:04 +01:00
|
|
|
return i.get('$parse');
|
|
|
|
}
|
|
|
|
|
|
|
|
export const angular1Providers = [
|
|
|
|
// We must use exported named functions for the ng2 factories to keep the compiler happy:
|
|
|
|
// > Metadata collected contains an error that will be reported at runtime:
|
|
|
|
// > Function calls are not supported.
|
|
|
|
// > Consider replacing the function or lambda with a reference to an exported function
|
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
2017-08-03 12:33:29 -07:00
|
|
|
{provide: '$injector', useFactory: injectorFactory, deps: []},
|
2016-10-19 21:41:04 +01:00
|
|
|
{provide: '$rootScope', useFactory: rootScopeFactory, deps: ['$injector']},
|
|
|
|
{provide: '$compile', useFactory: compileFactory, deps: ['$injector']},
|
|
|
|
{provide: '$parse', useFactory: parseFactory, deps: ['$injector']}
|
|
|
|
];
|