2016-06-24 08:46:43 -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-07-04 09:37:30 -07:00
|
|
|
import {Injector} from '../di';
|
2016-06-24 08:46:43 -07:00
|
|
|
import {BaseException} from '../facade/exceptions';
|
|
|
|
|
import {ConcreteType, Type, stringify} from '../facade/lang';
|
2016-07-08 10:47:17 -07:00
|
|
|
import {ViewEncapsulation} from '../metadata';
|
2016-06-28 09:54:42 -07:00
|
|
|
import {AppModuleMetadata} from '../metadata/app_module';
|
2016-06-24 08:46:43 -07:00
|
|
|
|
2016-06-28 09:54:42 -07:00
|
|
|
import {AppModuleFactory} from './app_module_factory';
|
2016-06-24 08:46:43 -07:00
|
|
|
import {ComponentFactory} from './component_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!`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 08:46:43 -07:00
|
|
|
/**
|
|
|
|
|
* 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-06-30 13:07:17 -07:00
|
|
|
*
|
|
|
|
|
* Each `@AppModule` provides an own `Compiler` to its injector,
|
|
|
|
|
* that will use the directives/pipes of the app module for compilation
|
|
|
|
|
* of components.
|
2016-06-24 08:46:43 -07:00
|
|
|
* @stable
|
|
|
|
|
*/
|
|
|
|
|
export class Compiler {
|
2016-07-04 09:37:30 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the injector with which the compiler has been created.
|
|
|
|
|
*/
|
|
|
|
|
get injector(): Injector {
|
|
|
|
|
throw new BaseException(`Runtime compiler is not loaded. Tried to read the injector.`);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 08:46:43 -07:00
|
|
|
/**
|
|
|
|
|
* Loads the template and styles of a component and returns the associated `ComponentFactory`.
|
|
|
|
|
*/
|
2016-06-30 13:07:17 -07:00
|
|
|
compileComponentAsync<T>(component: ConcreteType<T>): Promise<ComponentFactory<T>> {
|
2016-06-24 08:46:43 -07:00
|
|
|
throw new BaseException(
|
|
|
|
|
`Runtime compiler is not loaded. Tried to compile ${stringify(component)}`);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 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-06-24 08:46:43 -07:00
|
|
|
*/
|
2016-06-30 13:07:17 -07:00
|
|
|
compileComponentSync<T>(component: ConcreteType<T>): ComponentFactory<T> {
|
2016-06-24 08:46:43 -07:00
|
|
|
throw new BaseException(
|
|
|
|
|
`Runtime compiler is not loaded. Tried to compile ${stringify(component)}`);
|
|
|
|
|
}
|
2016-06-28 09:54:42 -07:00
|
|
|
/**
|
|
|
|
|
* Compiles the given App Module. All templates of the components listed in `precompile`
|
|
|
|
|
* have to be either inline or compiled before via `compileComponentAsync` /
|
2016-07-07 23:02:35 -07:00
|
|
|
* `compileAppModuleAsync`. Otherwise throws a {@link ComponentStillLoadingError}.
|
2016-06-28 09:54:42 -07:00
|
|
|
*/
|
|
|
|
|
compileAppModuleSync<T>(moduleType: ConcreteType<T>, metadata: AppModuleMetadata = null):
|
|
|
|
|
AppModuleFactory<T> {
|
|
|
|
|
throw new BaseException(
|
|
|
|
|
`Runtime compiler is not loaded. Tried to compile ${stringify(moduleType)}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
compileAppModuleAsync<T>(moduleType: ConcreteType<T>, metadata: AppModuleMetadata = null):
|
|
|
|
|
Promise<AppModuleFactory<T>> {
|
|
|
|
|
throw new BaseException(
|
|
|
|
|
`Runtime compiler is not loaded. Tried to compile ${stringify(moduleType)}`);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 08:46:43 -07:00
|
|
|
/**
|
|
|
|
|
* Clears all caches
|
|
|
|
|
*/
|
|
|
|
|
clearCache(): void {}
|
2016-06-28 09:54:42 -07:00
|
|
|
|
2016-06-24 08:46:43 -07:00
|
|
|
/**
|
2016-06-28 09:54:42 -07:00
|
|
|
* Clears the cache for the given component/appModule.
|
2016-06-24 08:46:43 -07:00
|
|
|
*/
|
2016-06-28 09:54:42 -07:00
|
|
|
clearCacheFor(type: Type) {}
|
2016-06-24 08:46:43 -07:00
|
|
|
}
|
2016-07-08 10:47:17 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Options for creating a compiler
|
|
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export type CompilerOptions = {
|
|
|
|
|
useDebug?: boolean,
|
|
|
|
|
useJit?: boolean,
|
|
|
|
|
defaultEncapsulation?: ViewEncapsulation,
|
|
|
|
|
providers?: any[],
|
|
|
|
|
deprecatedAppProviders?: any[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A factory for creating a Compiler
|
|
|
|
|
*
|
|
|
|
|
* @experimental
|
|
|
|
|
*/
|
|
|
|
|
export abstract class CompilerFactory {
|
|
|
|
|
static mergeOptions(defaultOptions: CompilerOptions = {}, newOptions: CompilerOptions = {}):
|
|
|
|
|
CompilerOptions {
|
|
|
|
|
return {
|
|
|
|
|
useDebug: _firstDefined(newOptions.useDebug, defaultOptions.useDebug),
|
|
|
|
|
useJit: _firstDefined(newOptions.useJit, defaultOptions.useJit),
|
|
|
|
|
defaultEncapsulation:
|
|
|
|
|
_firstDefined(newOptions.defaultEncapsulation, defaultOptions.defaultEncapsulation),
|
|
|
|
|
providers: _mergeArrays(defaultOptions.providers, newOptions.providers),
|
|
|
|
|
deprecatedAppProviders:
|
|
|
|
|
_mergeArrays(defaultOptions.deprecatedAppProviders, newOptions.deprecatedAppProviders)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
withDefaults(options: CompilerOptions = {}): CompilerFactory {
|
|
|
|
|
return new _DefaultApplyingCompilerFactory(this, options);
|
|
|
|
|
}
|
|
|
|
|
abstract createCompiler(options?: CompilerOptions): Compiler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _DefaultApplyingCompilerFactory extends CompilerFactory {
|
|
|
|
|
constructor(private _delegate: CompilerFactory, private _options: CompilerOptions) { super(); }
|
|
|
|
|
|
|
|
|
|
createCompiler(options: CompilerOptions = {}): Compiler {
|
|
|
|
|
return this._delegate.createCompiler(CompilerFactory.mergeOptions(this._options, options));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _firstDefined<T>(...args: T[]): T {
|
|
|
|
|
for (var i = 0; i < args.length; i++) {
|
|
|
|
|
if (args[i] !== undefined) {
|
|
|
|
|
return args[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function _mergeArrays(...parts: any[][]): any[] {
|
|
|
|
|
let result: any[] = [];
|
2016-07-11 17:25:40 -07:00
|
|
|
parts.forEach((part) => result.push.apply(result, part));
|
2016-07-08 10:47:17 -07:00
|
|
|
return result;
|
|
|
|
|
}
|