2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-08-30 18:07:40 -07:00
|
|
|
/**
|
|
|
|
* @module
|
|
|
|
* @description
|
|
|
|
* Entry point for all APIs of the compiler package.
|
|
|
|
*
|
|
|
|
* <div class="callout is-critical">
|
|
|
|
* <header>Unstable APIs</header>
|
|
|
|
* <p>
|
|
|
|
* All compiler apis are currently considered experimental and private!
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* We expect the APIs in this package to keep on changing. Do not rely on them.
|
|
|
|
* </p>
|
|
|
|
* </div>
|
|
|
|
*/
|
|
|
|
export * from './schema_registry_mock';
|
|
|
|
export * from './directive_resolver_mock';
|
|
|
|
export * from './ng_module_resolver_mock';
|
|
|
|
export * from './pipe_resolver_mock';
|
2016-07-18 03:50:31 -07:00
|
|
|
|
2017-04-26 09:24:42 -07:00
|
|
|
import {createPlatformFactory, ModuleWithComponentFactories, Injectable, CompilerOptions, COMPILER_OPTIONS, CompilerFactory, ComponentFactory, NgModuleFactory, Injector, NgModule, Component, Directive, Pipe, Type, PlatformRef, ɵstringify} from '@angular/core';
|
2017-02-17 12:55:55 -08:00
|
|
|
import {MetadataOverride, ɵTestingCompilerFactory as TestingCompilerFactory, ɵTestingCompiler as TestingCompiler} from '@angular/core/testing';
|
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
|
|
|
import {platformCoreDynamic, JitCompiler, DirectiveResolver, NgModuleResolver, PipeResolver, CompileMetadataResolver, CompileReflector} from '@angular/compiler';
|
2016-08-30 18:07:40 -07:00
|
|
|
import {MockDirectiveResolver} from './directive_resolver_mock';
|
|
|
|
import {MockNgModuleResolver} from './ng_module_resolver_mock';
|
|
|
|
import {MockPipeResolver} from './pipe_resolver_mock';
|
|
|
|
import {MetadataOverrider} from './metadata_overrider';
|
2016-07-18 03:50:31 -07:00
|
|
|
|
2016-07-28 04:54:49 -07:00
|
|
|
@Injectable()
|
|
|
|
export class TestingCompilerFactoryImpl implements TestingCompilerFactory {
|
|
|
|
constructor(private _compilerFactory: CompilerFactory) {}
|
|
|
|
|
|
|
|
createTestingCompiler(options: CompilerOptions[]): TestingCompiler {
|
2016-11-14 17:37:47 -08:00
|
|
|
const compiler = <JitCompiler>this._compilerFactory.createCompiler(options);
|
2016-07-28 04:54:49 -07:00
|
|
|
return new TestingCompilerImpl(
|
|
|
|
compiler, compiler.injector.get(MockDirectiveResolver),
|
2017-04-26 09:24:42 -07:00
|
|
|
compiler.injector.get(MockPipeResolver), compiler.injector.get(MockNgModuleResolver),
|
|
|
|
compiler.injector.get(CompileMetadataResolver));
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TestingCompilerImpl implements TestingCompiler {
|
|
|
|
private _overrider = new MetadataOverrider();
|
|
|
|
constructor(
|
2016-11-14 17:37:47 -08:00
|
|
|
private _compiler: JitCompiler, private _directiveResolver: MockDirectiveResolver,
|
2017-04-26 09:24:42 -07:00
|
|
|
private _pipeResolver: MockPipeResolver, private _moduleResolver: MockNgModuleResolver,
|
|
|
|
private _metadataResolver: CompileMetadataResolver) {}
|
2016-07-28 04:54:49 -07:00
|
|
|
get injector(): Injector { return this._compiler.injector; }
|
2016-08-19 13:51:45 -07:00
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
compileModuleSync<T>(moduleType: Type<T>): NgModuleFactory<T> {
|
2016-07-28 04:54:49 -07:00
|
|
|
return this._compiler.compileModuleSync(moduleType);
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
compileModuleAsync<T>(moduleType: Type<T>): Promise<NgModuleFactory<T>> {
|
2016-07-28 04:54:49 -07:00
|
|
|
return this._compiler.compileModuleAsync(moduleType);
|
|
|
|
}
|
2016-08-10 18:21:28 -07:00
|
|
|
compileModuleAndAllComponentsSync<T>(moduleType: Type<T>): ModuleWithComponentFactories<T> {
|
2016-07-28 04:54:49 -07:00
|
|
|
return this._compiler.compileModuleAndAllComponentsSync(moduleType);
|
|
|
|
}
|
|
|
|
|
2016-08-10 18:21:28 -07:00
|
|
|
compileModuleAndAllComponentsAsync<T>(moduleType: Type<T>):
|
2016-07-28 04:54:49 -07:00
|
|
|
Promise<ModuleWithComponentFactories<T>> {
|
|
|
|
return this._compiler.compileModuleAndAllComponentsAsync(moduleType);
|
|
|
|
}
|
|
|
|
|
2016-11-02 22:38:00 +00:00
|
|
|
getNgContentSelectors(component: Type<any>): string[] {
|
|
|
|
return this._compiler.getNgContentSelectors(component);
|
|
|
|
}
|
|
|
|
|
2017-04-26 09:24:42 -07:00
|
|
|
getComponentFactory<T>(component: Type<T>): ComponentFactory<T> {
|
|
|
|
return this._compiler.getComponentFactory(component);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkOverrideAllowed(type: Type<any>) {
|
|
|
|
if (this._compiler.hasAotSummary(type)) {
|
|
|
|
throw new Error(`${ɵstringify(type)} was AOT compiled, so its metadata cannot be changed.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 09:44:20 -07:00
|
|
|
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void {
|
2017-04-26 09:24:42 -07:00
|
|
|
this.checkOverrideAllowed(ngModule);
|
2016-07-28 04:54:49 -07:00
|
|
|
const oldMetadata = this._moduleResolver.resolve(ngModule, false);
|
|
|
|
this._moduleResolver.setNgModule(
|
2016-09-12 19:14:17 -07:00
|
|
|
ngModule, this._overrider.overrideMetadata(NgModule, oldMetadata, override));
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
2016-09-12 09:44:20 -07:00
|
|
|
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void {
|
2017-04-26 09:24:42 -07:00
|
|
|
this.checkOverrideAllowed(directive);
|
2016-07-28 04:54:49 -07:00
|
|
|
const oldMetadata = this._directiveResolver.resolve(directive, false);
|
|
|
|
this._directiveResolver.setDirective(
|
2017-03-24 09:59:58 -07:00
|
|
|
directive, this._overrider.overrideMetadata(Directive, oldMetadata !, override));
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
2016-09-12 09:44:20 -07:00
|
|
|
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void {
|
2017-04-26 09:24:42 -07:00
|
|
|
this.checkOverrideAllowed(component);
|
2016-07-28 04:54:49 -07:00
|
|
|
const oldMetadata = this._directiveResolver.resolve(component, false);
|
|
|
|
this._directiveResolver.setDirective(
|
2017-03-24 09:59:58 -07:00
|
|
|
component, this._overrider.overrideMetadata(Component, oldMetadata !, override));
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
2016-09-12 09:44:20 -07:00
|
|
|
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void {
|
2017-04-26 09:24:42 -07:00
|
|
|
this.checkOverrideAllowed(pipe);
|
2016-07-28 04:54:49 -07:00
|
|
|
const oldMetadata = this._pipeResolver.resolve(pipe, false);
|
2016-09-12 09:44:20 -07:00
|
|
|
this._pipeResolver.setPipe(pipe, this._overrider.overrideMetadata(Pipe, oldMetadata, override));
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
2017-04-26 09:24:42 -07:00
|
|
|
loadAotSummaries(summaries: () => any[]) { this._compiler.loadAotSummaries(summaries); }
|
2016-07-28 04:54:49 -07:00
|
|
|
clearCache(): void { this._compiler.clearCache(); }
|
2016-08-10 18:21:28 -07:00
|
|
|
clearCacheFor(type: Type<any>) { this._compiler.clearCacheFor(type); }
|
2016-07-28 04:54:49 -07:00
|
|
|
}
|
2016-07-18 03:50:31 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Platform for dynamic tests
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-08-10 18:21:28 -07:00
|
|
|
export const platformCoreDynamicTesting: (extraProviders?: any[]) => PlatformRef =
|
2016-07-28 04:54:49 -07:00
|
|
|
createPlatformFactory(platformCoreDynamic, 'coreDynamicTesting', [
|
|
|
|
{
|
2016-08-04 11:31:58 -07:00
|
|
|
provide: COMPILER_OPTIONS,
|
2016-07-28 04:54:49 -07:00
|
|
|
useValue: {
|
|
|
|
providers: [
|
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: MockPipeResolver, deps: [Injector, CompileReflector]},
|
2016-12-22 10:17:41 -08:00
|
|
|
{provide: PipeResolver, useExisting: MockPipeResolver},
|
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: MockDirectiveResolver, deps: [Injector, CompileReflector]},
|
2016-12-22 10:17:41 -08:00
|
|
|
{provide: DirectiveResolver, useExisting: MockDirectiveResolver},
|
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: MockNgModuleResolver, deps: [Injector, CompileReflector]},
|
2016-12-22 10:17:41 -08:00
|
|
|
{provide: NgModuleResolver, useExisting: MockNgModuleResolver},
|
2016-07-28 04:54:49 -07:00
|
|
|
]
|
|
|
|
},
|
|
|
|
multi: true
|
|
|
|
},
|
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: TestingCompilerFactory,
|
|
|
|
useClass: TestingCompilerFactoryImpl,
|
|
|
|
deps: [CompilerFactory]
|
|
|
|
}
|
2016-07-28 04:54:49 -07:00
|
|
|
]);
|