2016-06-23 12:47:54 -04: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 21:07:40 -04: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 06:50:31 -04:00
|
|
|
|
2016-09-12 22:14:17 -04:00
|
|
|
import {createPlatformFactory, ModuleWithComponentFactories, Injectable, CompilerOptions, COMPILER_OPTIONS, CompilerFactory, NgModuleFactory, Injector, NgModule, Component, Directive, Pipe, Type, PlatformRef} from '@angular/core';
|
2016-07-28 07:54:49 -04:00
|
|
|
import {MetadataOverride} from '@angular/core/testing';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {TestingCompilerFactory, TestingCompiler} from './private_import_core';
|
2016-11-14 20:37:47 -05:00
|
|
|
import {platformCoreDynamic, JitCompiler, DirectiveResolver, NgModuleResolver, PipeResolver} from '@angular/compiler';
|
2016-08-30 21:07:40 -04: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 06:50:31 -04:00
|
|
|
|
2016-07-28 07:54:49 -04:00
|
|
|
@Injectable()
|
|
|
|
export class TestingCompilerFactoryImpl implements TestingCompilerFactory {
|
|
|
|
constructor(private _compilerFactory: CompilerFactory) {}
|
|
|
|
|
|
|
|
createTestingCompiler(options: CompilerOptions[]): TestingCompiler {
|
2016-11-14 20:37:47 -05:00
|
|
|
const compiler = <JitCompiler>this._compilerFactory.createCompiler(options);
|
2016-07-28 07:54:49 -04:00
|
|
|
return new TestingCompilerImpl(
|
|
|
|
compiler, compiler.injector.get(MockDirectiveResolver),
|
|
|
|
compiler.injector.get(MockPipeResolver), compiler.injector.get(MockNgModuleResolver));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class TestingCompilerImpl implements TestingCompiler {
|
|
|
|
private _overrider = new MetadataOverrider();
|
|
|
|
constructor(
|
2016-11-14 20:37:47 -05:00
|
|
|
private _compiler: JitCompiler, private _directiveResolver: MockDirectiveResolver,
|
2016-07-28 07:54:49 -04:00
|
|
|
private _pipeResolver: MockPipeResolver, private _moduleResolver: MockNgModuleResolver) {}
|
|
|
|
get injector(): Injector { return this._compiler.injector; }
|
2016-08-19 16:51:45 -04:00
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
compileModuleSync<T>(moduleType: Type<T>): NgModuleFactory<T> {
|
2016-07-28 07:54:49 -04:00
|
|
|
return this._compiler.compileModuleSync(moduleType);
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
compileModuleAsync<T>(moduleType: Type<T>): Promise<NgModuleFactory<T>> {
|
2016-07-28 07:54:49 -04:00
|
|
|
return this._compiler.compileModuleAsync(moduleType);
|
|
|
|
}
|
2016-08-10 21:21:28 -04:00
|
|
|
compileModuleAndAllComponentsSync<T>(moduleType: Type<T>): ModuleWithComponentFactories<T> {
|
2016-07-28 07:54:49 -04:00
|
|
|
return this._compiler.compileModuleAndAllComponentsSync(moduleType);
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
compileModuleAndAllComponentsAsync<T>(moduleType: Type<T>):
|
2016-07-28 07:54:49 -04:00
|
|
|
Promise<ModuleWithComponentFactories<T>> {
|
|
|
|
return this._compiler.compileModuleAndAllComponentsAsync(moduleType);
|
|
|
|
}
|
|
|
|
|
2016-11-02 18:38:00 -04:00
|
|
|
getNgContentSelectors(component: Type<any>): string[] {
|
|
|
|
return this._compiler.getNgContentSelectors(component);
|
|
|
|
}
|
|
|
|
|
2016-09-12 12:44:20 -04:00
|
|
|
overrideModule(ngModule: Type<any>, override: MetadataOverride<NgModule>): void {
|
2016-07-28 07:54:49 -04:00
|
|
|
const oldMetadata = this._moduleResolver.resolve(ngModule, false);
|
|
|
|
this._moduleResolver.setNgModule(
|
2016-09-12 22:14:17 -04:00
|
|
|
ngModule, this._overrider.overrideMetadata(NgModule, oldMetadata, override));
|
2016-07-28 07:54:49 -04:00
|
|
|
}
|
2016-09-12 12:44:20 -04:00
|
|
|
overrideDirective(directive: Type<any>, override: MetadataOverride<Directive>): void {
|
2016-07-28 07:54:49 -04:00
|
|
|
const oldMetadata = this._directiveResolver.resolve(directive, false);
|
|
|
|
this._directiveResolver.setDirective(
|
2016-09-12 22:14:17 -04:00
|
|
|
directive, this._overrider.overrideMetadata(Directive, oldMetadata, override));
|
2016-07-28 07:54:49 -04:00
|
|
|
}
|
2016-09-12 12:44:20 -04:00
|
|
|
overrideComponent(component: Type<any>, override: MetadataOverride<Component>): void {
|
2016-07-28 07:54:49 -04:00
|
|
|
const oldMetadata = this._directiveResolver.resolve(component, false);
|
|
|
|
this._directiveResolver.setDirective(
|
2016-09-12 22:14:17 -04:00
|
|
|
component, this._overrider.overrideMetadata(Component, oldMetadata, override));
|
2016-07-28 07:54:49 -04:00
|
|
|
}
|
2016-09-12 12:44:20 -04:00
|
|
|
overridePipe(pipe: Type<any>, override: MetadataOverride<Pipe>): void {
|
2016-07-28 07:54:49 -04:00
|
|
|
const oldMetadata = this._pipeResolver.resolve(pipe, false);
|
2016-09-12 12:44:20 -04:00
|
|
|
this._pipeResolver.setPipe(pipe, this._overrider.overrideMetadata(Pipe, oldMetadata, override));
|
2016-07-28 07:54:49 -04:00
|
|
|
}
|
|
|
|
clearCache(): void { this._compiler.clearCache(); }
|
2016-08-10 21:21:28 -04:00
|
|
|
clearCacheFor(type: Type<any>) { this._compiler.clearCacheFor(type); }
|
2016-07-28 07:54:49 -04:00
|
|
|
}
|
2016-07-18 06:50:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Platform for dynamic tests
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-08-10 21:21:28 -04:00
|
|
|
export const platformCoreDynamicTesting: (extraProviders?: any[]) => PlatformRef =
|
2016-07-28 07:54:49 -04:00
|
|
|
createPlatformFactory(platformCoreDynamic, 'coreDynamicTesting', [
|
|
|
|
{
|
2016-08-04 14:31:58 -04:00
|
|
|
provide: COMPILER_OPTIONS,
|
2016-07-28 07:54:49 -04:00
|
|
|
useValue: {
|
|
|
|
providers: [
|
|
|
|
MockPipeResolver, {provide: PipeResolver, useExisting: MockPipeResolver},
|
|
|
|
MockDirectiveResolver, {provide: DirectiveResolver, useExisting: MockDirectiveResolver},
|
|
|
|
MockNgModuleResolver, {provide: NgModuleResolver, useExisting: MockNgModuleResolver}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
multi: true
|
|
|
|
},
|
|
|
|
{provide: TestingCompilerFactory, useClass: TestingCompilerFactoryImpl}
|
|
|
|
]);
|