Files
Angular-Docs-Cn/modules/@angular/core/src/linker/compiler.ts
T

139 lines
3.8 KiB
TypeScript
Raw Normal View History

/**
* @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-07-18 03:50:31 -07:00
import {Injector, OpaqueToken} from '../di';
import {BaseException, unimplemented} from '../facade/exceptions';
import {ConcreteType, Type, stringify} from '../facade/lang';
import {NgModuleMetadata, ViewEncapsulation} from '../metadata';
import {ComponentFactory} from './component_factory';
2016-07-18 03:50:31 -07:00
import {ComponentResolver} from './component_resolver';
import {NgModuleFactory} from './ng_module_factory';
2016-07-04 09:37:30 -07:00
/**
* Indicates that a component is still being loaded in a synchronous compile.
*
* @stable
*/
export class ComponentStillLoadingError extends BaseException {
constructor(public compType: Type) {
super(`Can't compile synchronously as ${stringify(compType)} is still being loaded!`);
}
}
/**
* Combination of NgModuleFactory and ComponentFactorys.
*
* @experimental
*/
export class ModuleWithComponentFactories<T> {
constructor(
public ngModuleFactory: NgModuleFactory<T>,
public componentFactories: ComponentFactory<any>[]) {}
}
function _throwError() {
throw new BaseException(`Runtime compiler is not loaded`);
}
/**
* Low-level service for running the angular compiler duirng runtime
* to create {@link ComponentFactory}s, which
* can later be used to create and render a Component instance.
*
2016-07-18 03:50:31 -07:00
* Each `@NgModule` provides an own `Compiler` to its injector,
* that will use the directives/pipes of the ng module for compilation
* of components.
* @stable
*/
export class Compiler {
/**
* Loads the template and styles of a component and returns the associated `ComponentFactory`.
*/
2016-07-18 03:50:31 -07:00
compileComponentAsync<T>(component: ConcreteType<T>, ngModule: Type = null):
Promise<ComponentFactory<T>> {
throw _throwError();
}
/**
* Compiles the given component. All templates have to be either inline or compiled via
2016-07-07 23:02:35 -07:00
* `compileComponentAsync` before. Otherwise throws a {@link ComponentStillLoadingError}.
*/
2016-07-18 03:50:31 -07:00
compileComponentSync<T>(component: ConcreteType<T>, ngModule: Type = null): ComponentFactory<T> {
throw _throwError();
}
2016-06-28 09:54:42 -07:00
/**
* Compiles the given NgModule and all of its components. All templates of the components listed
* in `entryComponents`
* have to be inlined. Otherwise throws a {@link ComponentStillLoadingError}.
2016-06-28 09:54:42 -07:00
*/
compileModuleSync<T>(moduleType: ConcreteType<T>): NgModuleFactory<T> { throw _throwError(); }
/**
* Compiles the given NgModule and all of its components
*/
compileModuleAsync<T>(moduleType: ConcreteType<T>): Promise<NgModuleFactory<T>> {
throw _throwError();
2016-06-28 09:54:42 -07:00
}
/**
* Same as {@link compileModuleSync} put also creates ComponentFactories for all components.
*/
compileModuleAndAllComponentsSync<T>(moduleType: ConcreteType<T>):
ModuleWithComponentFactories<T> {
throw _throwError();
}
/**
* Same as {@link compileModuleAsync} put also creates ComponentFactories for all components.
*/
compileModuleAndAllComponentsAsync<T>(moduleType: ConcreteType<T>):
Promise<ModuleWithComponentFactories<T>> {
throw _throwError();
2016-06-28 09:54:42 -07:00
}
/**
* Clears all caches
*/
clearCache(): void {}
2016-06-28 09:54:42 -07:00
/**
2016-07-18 03:50:31 -07:00
* Clears the cache for the given component/ngModule.
*/
2016-06-28 09:54:42 -07:00
clearCacheFor(type: Type) {}
}
/**
* Options for creating a compiler
*
* @experimental
*/
export type CompilerOptions = {
useDebug?: boolean,
useJit?: boolean,
defaultEncapsulation?: ViewEncapsulation,
providers?: any[],
2016-07-18 03:50:31 -07:00
};
/**
* Token to provide CompilerOptions in the platform injector.
*
* @experimental
*/
export const CompilerOptions = new OpaqueToken('compilerOptions');
/**
* A factory for creating a Compiler
*
* @experimental
*/
export abstract class CompilerFactory {
2016-07-18 03:50:31 -07:00
abstract createCompiler(options?: CompilerOptions[]): Compiler;
}