refactor(compiler): renames

- `NgHost` to `CompilerHost`
- `AotCompilerHost.resolveFileToImport` to `AotCompilerHost.fileNameToModuleName`
- `AotCompilerHoset.resolveImportToFile` to `AotCompilerHost.moduleNameToFileName`
This commit is contained in:
Tobias Bosch
2016-11-17 12:24:33 -08:00
committed by Chuck Jazdzewski
parent dddbb1c1cb
commit adeea5d86a
15 changed files with 92 additions and 86 deletions

View File

@ -40,12 +40,12 @@ import {StaticReflector} from './static_reflector';
/**
* Creates a new AotCompiler based on options and a host.
*/
export function createAotCompiler(ngHost: AotCompilerHost, options: AotCompilerOptions):
export function createAotCompiler(compilerHost: AotCompilerHost, options: AotCompilerOptions):
{compiler: AotCompiler, reflector: StaticReflector} {
let translations: string = options.translations || '';
const urlResolver = createOfflineCompileUrlResolver();
const staticReflector = new StaticReflector(ngHost);
const staticReflector = new StaticReflector(compilerHost);
StaticAndDynamicReflectionCapabilities.install(staticReflector);
const htmlParser = new I18NHtmlParser(new HtmlParser(), translations, options.i18nFormat);
const config = new CompilerConfig({
@ -55,7 +55,7 @@ export function createAotCompiler(ngHost: AotCompilerHost, options: AotCompilerO
useJit: false
});
const normalizer = new DirectiveNormalizer(
{get: (url: string) => ngHost.loadResource(url)}, urlResolver, htmlParser, config);
{get: (url: string) => compilerHost.loadResource(url)}, urlResolver, htmlParser, config);
const expressionParser = new Parser(new Lexer());
const elementSchemaRegistry = new DomElementSchemaRegistry();
const console = new Console();
@ -69,7 +69,7 @@ export function createAotCompiler(ngHost: AotCompilerHost, options: AotCompilerO
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), staticReflector, options);
new NgModuleCompiler(), new TypeScriptEmitter(compilerHost), options.locale,
options.i18nFormat, new AnimationParser(elementSchemaRegistry), staticReflector, options);
return {compiler, reflector: staticReflector};
}

View File

@ -25,14 +25,17 @@ export interface AotCompilerHost {
getMetadataFor(modulePath: string): {[key: string]: any}[];
/**
* Converts an import into a file path.
* Converts a module name that is used in an `import` to a file path.
* I.e.
* `path/to/containingFile.ts` containing `import {...} from 'module-name'`.
*/
resolveImportToFile(moduleName: string, containingFile: string): string;
moduleNameToFileName(moduleName: string, containingFile: string): string;
/**
* Converts a file path to an import
* Converts a file path to a module name that can be used as an `import.
* I.e. `path/to/importedFile.ts` should be imported by `path/to/containingFile.ts`.
*/
resolveFileToImport(importedFilePath: string, containingFilePath: string): string;
fileNameToModuleName(importedFile: string, containingFile: string): string;
/**
* Loads a resource (e.g. html / css)

View File

@ -214,7 +214,7 @@ export class StaticReflector implements ReflectorReader {
private resolveExportedSymbol(filePath: string, symbolName: string): StaticSymbol {
const resolveModule = (moduleName: string): string => {
const resolvedModulePath = this.host.resolveImportToFile(moduleName, filePath);
const resolvedModulePath = this.host.moduleNameToFileName(moduleName, filePath);
if (!resolvedModulePath) {
throw new Error(`Could not resolve module '${moduleName}' relative to file ${filePath}`);
}
@ -269,7 +269,7 @@ export class StaticReflector implements ReflectorReader {
return symbol;
}
try {
const filePath = this.host.resolveImportToFile(module, containingFile);
const filePath = this.host.moduleNameToFileName(module, containingFile);
if (!filePath) {
// If the file cannot be found the module is probably referencing a declared module

View File

@ -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.resolveFileToImport(importedModuleUrl, moduleUrl)}');`);
`uire('${this._importGenerator.fileNameToModuleName(importedModuleUrl, moduleUrl)}');`);
});
srcParts.push(ctx.toSource());
return srcParts.join('\n');

View File

@ -10,5 +10,5 @@
* Interface that defines how import statements should be generated.
*/
export abstract class ImportResolver {
abstract resolveFileToImport(importedFilePath: string, containingFilePath: string): string;
abstract fileNameToModuleName(importedFilePath: string, containingFilePath: string): string;
}

View File

@ -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.resolveFileToImport(importedModuleUrl, moduleUrl)}';`);
`ort * as ${prefix} from '${this._importGenerator.fileNameToModuleName(importedModuleUrl, moduleUrl)}';`);
});
srcParts.push(ctx.toSource());
return srcParts.join('\n');

View File

@ -511,12 +511,12 @@ class MockAotCompilerHost implements AotCompilerHost {
loadResource(filePath: string): Promise<string> { throw new Error('Should not be called!'); }
resolveFileToImport(importedFilePath: string, containingFilePath: string): string {
fileNameToModuleName(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 {
moduleNameToFileName(modulePath: string, containingFile?: string): string {
function splitPath(path: string): string[] { return path.split(/\/|\\/g); }
function resolvePath(pathParts: string[]): string {

View File

@ -253,7 +253,7 @@ function createOperatorFn(op: o.BinaryOperator) {
}
export class SimpleJsImportGenerator implements ImportResolver {
resolveFileToImport(importedUrlStr: string, moduleUrlStr: string): string {
fileNameToModuleName(importedUrlStr: string, moduleUrlStr: string): string {
return importedUrlStr;
}
}