refactor(compiler): add createAotCompiler factory

Also adds 2 more methods to the `AotCompilerHost`:
- `loadResource`
- `resolveFileToImport`
This commit is contained in:
Tobias Bosch
2016-11-15 11:13:20 -08:00
committed by Chuck Jazdzewski
parent 484119e59f
commit 2235048432
13 changed files with 174 additions and 100 deletions

View File

@ -25,6 +25,7 @@ export * from './src/template_parser/template_ast';
export {TEMPLATE_TRANSFORMS} from './src/template_parser/template_parser';
export {CompilerConfig, RenderTypes} from './src/config';
export * from './src/compile_metadata';
export * from './src/aot/compiler_factory';
export * from './src/aot/compiler';
export * from './src/aot/compiler_host';
export * from './src/aot/static_reflector';

View File

@ -0,0 +1,79 @@
/**
* @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
*/
import {ViewEncapsulation} from '@angular/core';
import {AnimationParser} from '../animation/animation_parser';
import {CompilerConfig} from '../config';
import {DirectiveNormalizer} from '../directive_normalizer';
import {DirectiveResolver} from '../directive_resolver';
import {DirectiveWrapperCompiler} from '../directive_wrapper_compiler';
import {Lexer} from '../expression_parser/lexer';
import {Parser} from '../expression_parser/parser';
import {I18NHtmlParser} from '../i18n/i18n_html_parser';
import {CompileMetadataResolver} from '../metadata_resolver';
import {HtmlParser} from '../ml_parser/html_parser';
import {NgModuleCompiler} from '../ng_module_compiler';
import {NgModuleResolver} from '../ng_module_resolver';
import {TypeScriptEmitter} from '../output/ts_emitter';
import {PipeResolver} from '../pipe_resolver';
import {Console} from '../private_import_core';
import {DomElementSchemaRegistry} from '../schema/dom_element_schema_registry';
import {StyleCompiler} from '../style_compiler';
import {TemplateParser} from '../template_parser/template_parser';
import {createOfflineCompileUrlResolver} from '../url_resolver';
import {ViewCompiler} from '../view_compiler/view_compiler';
import {AotCompiler} from './compiler';
import {AotCompilerHost} from './compiler_host';
import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabilities';
import {StaticReflector} from './static_reflector';
export interface AotCompilerOptions {
debug?: boolean;
locale?: string;
i18nFormat?: string;
translations?: string;
}
/**
* Creates a new AotCompiler based on options and a host.
*/
export function createAotCompiler(ngHost: AotCompilerHost, options: AotCompilerOptions):
{compiler: AotCompiler, reflector: StaticReflector} {
let translations: string = options.translations || '';
const urlResolver = createOfflineCompileUrlResolver();
const staticReflector = new StaticReflector(ngHost);
StaticAndDynamicReflectionCapabilities.install(staticReflector);
const htmlParser = new I18NHtmlParser(new HtmlParser(), translations, options.i18nFormat);
const config = new CompilerConfig({
genDebugInfo: options.debug === true,
defaultEncapsulation: ViewEncapsulation.Emulated,
logBindingUpdate: false,
useJit: false
});
const normalizer = new DirectiveNormalizer(
{get: (url: string) => ngHost.loadResource(url)}, urlResolver, htmlParser, config);
const expressionParser = new Parser(new Lexer());
const elementSchemaRegistry = new DomElementSchemaRegistry();
const console = new Console();
const tmplParser =
new TemplateParser(expressionParser, elementSchemaRegistry, htmlParser, console, []);
const resolver = new CompileMetadataResolver(
new NgModuleResolver(staticReflector), new DirectiveResolver(staticReflector),
new PipeResolver(staticReflector), elementSchemaRegistry, normalizer, staticReflector);
// TODO(vicb): do not pass options.i18nFormat here
const compiler = new AotCompiler(
resolver, tmplParser, new StyleCompiler(urlResolver),
new ViewCompiler(config, elementSchemaRegistry),
new DirectiveWrapperCompiler(config, expressionParser, elementSchemaRegistry, console),
new NgModuleCompiler(), new TypeScriptEmitter(ngHost), options.locale, options.i18nFormat,
new AnimationParser(elementSchemaRegistry));
return {compiler, reflector: staticReflector};
}

View File

@ -25,7 +25,17 @@ export interface AotCompilerHost {
getMetadataFor(modulePath: string): {[key: string]: any}|{[key: string]: any}[];
/**
* Converts a module name into a file path.
* Converts an import into a file path.
*/
resolveImportToFile(moduleName: string, containingFile: string): string;
/**
* Converts a file path to an import
*/
resolveFileToImport(importedFilePath: string, containingFilePath: string): string;
/**
* Loads a resource (e.g. html / css)
*/
loadResource(path: string): Promise<string>;
}

View File

@ -12,10 +12,10 @@ import {isBlank, isPresent} from '../facade/lang';
import {EmitterVisitorContext, OutputEmitter} from './abstract_emitter';
import {AbstractJsEmitterVisitor} from './abstract_js_emitter';
import * as o from './output_ast';
import {ImportGenerator} from './path_util';
import {ImportResolver} from './path_util';
export class JavaScriptEmitter implements OutputEmitter {
constructor(private _importGenerator: ImportGenerator) {}
constructor(private _importGenerator: ImportResolver) {}
emitStatements(moduleUrl: string, stmts: o.Statement[], exportedVars: string[]): string {
const converter = new JsEmitterVisitor(moduleUrl);
const ctx = EmitterVisitorContext.createRoot(exportedVars);
@ -25,7 +25,7 @@ export class JavaScriptEmitter implements OutputEmitter {
// Note: can't write the real word for import as it screws up system.js auto detection...
srcParts.push(
`var ${prefix} = req` +
`uire('${this._importGenerator.getImportPath(moduleUrl, importedModuleUrl)}');`);
`uire('${this._importGenerator.resolveFileToImport(importedModuleUrl, moduleUrl)}');`);
});
srcParts.push(ctx.toSource());
return srcParts.join('\n');

View File

@ -9,6 +9,6 @@
/**
* Interface that defines how import statements should be generated.
*/
export abstract class ImportGenerator {
abstract getImportPath(moduleUrlStr: string, importedUrlStr: string): string;
export abstract class ImportResolver {
abstract resolveFileToImport(importedFilePath: string, containingFilePath: string): string;
}

View File

@ -12,7 +12,7 @@ import {isBlank, isPresent} from '../facade/lang';
import {AbstractEmitterVisitor, CATCH_ERROR_VAR, CATCH_STACK_VAR, EmitterVisitorContext, OutputEmitter} from './abstract_emitter';
import * as o from './output_ast';
import {ImportGenerator} from './path_util';
import {ImportResolver} from './path_util';
const _debugModuleUrl = '/debug/lib';
@ -37,7 +37,7 @@ export function debugOutputAstAsTypeScript(ast: o.Statement | o.Expression | o.T
}
export class TypeScriptEmitter implements OutputEmitter {
constructor(private _importGenerator: ImportGenerator) {}
constructor(private _importGenerator: ImportResolver) {}
emitStatements(moduleUrl: string, stmts: o.Statement[], exportedVars: string[]): string {
const converter = new _TsEmitterVisitor(moduleUrl);
const ctx = EmitterVisitorContext.createRoot(exportedVars);
@ -47,7 +47,7 @@ export class TypeScriptEmitter implements OutputEmitter {
// Note: can't write the real word for import as it screws up system.js auto detection...
srcParts.push(
`imp` +
`ort * as ${prefix} from '${this._importGenerator.getImportPath(moduleUrl, importedModuleUrl)}';`);
`ort * as ${prefix} from '${this._importGenerator.resolveFileToImport(importedModuleUrl, moduleUrl)}';`);
});
srcParts.push(ctx.toSource());
return srcParts.join('\n');

View File

@ -511,6 +511,12 @@ class MockAotCompilerHost implements AotCompilerHost {
constructor() {}
loadResource(filePath: string): Promise<string> { throw new Error('Should not be called!'); }
resolveFileToImport(importedFilePath: string, containingFilePath: string): string {
throw new Error('Should not be called!');
}
// In tests, assume that symbols are not re-exported
resolveImportToFile(modulePath: string, containingFile?: string): string {
function splitPath(path: string): string[] { return path.split(/\/|\\/g); }

View File

@ -9,7 +9,7 @@
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
import {assetUrl} from '@angular/compiler/src/identifiers';
import * as o from '@angular/compiler/src/output/output_ast';
import {ImportGenerator} from '@angular/compiler/src/output/path_util';
import {ImportResolver} from '@angular/compiler/src/output/path_util';
import {EventEmitter} from '@angular/core';
import {BaseError} from '@angular/core/src/facade/errors';
import {ViewType} from '@angular/core/src/linker/view_type';
@ -252,6 +252,8 @@ function createOperatorFn(op: o.BinaryOperator) {
o.DYNAMIC_TYPE);
}
export class SimpleJsImportGenerator implements ImportGenerator {
getImportPath(moduleUrlStr: string, importedUrlStr: string): string { return importedUrlStr; }
export class SimpleJsImportGenerator implements ImportResolver {
resolveFileToImport(importedUrlStr: string, moduleUrlStr: string): string {
return importedUrlStr;
}
}