refactor(OfflineCompiler): cleanup
This commit is contained in:
parent
0dc15eb64a
commit
8395aab25d
|
@ -10,7 +10,6 @@ import {SchemaMetadata} from '@angular/core';
|
||||||
|
|
||||||
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeMetadata, CompileProviderMetadata, CompileTokenMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata';
|
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeMetadata, CompileProviderMetadata, CompileTokenMetadata, StaticSymbol, createHostComponentMeta} from './compile_metadata';
|
||||||
import {DirectiveNormalizer} from './directive_normalizer';
|
import {DirectiveNormalizer} from './directive_normalizer';
|
||||||
import {ListWrapper} from './facade/collection';
|
|
||||||
import {Identifiers, resolveIdentifier, resolveIdentifierToken} from './identifiers';
|
import {Identifiers, resolveIdentifier, resolveIdentifierToken} from './identifiers';
|
||||||
import {CompileMetadataResolver} from './metadata_resolver';
|
import {CompileMetadataResolver} from './metadata_resolver';
|
||||||
import {NgModuleCompiler} from './ng_module_compiler';
|
import {NgModuleCompiler} from './ng_module_compiler';
|
||||||
|
@ -58,10 +57,10 @@ export class OfflineCompiler {
|
||||||
compile(
|
compile(
|
||||||
moduleUrl: string, ngModulesSummary: NgModulesSummary, components: StaticSymbol[],
|
moduleUrl: string, ngModulesSummary: NgModulesSummary, components: StaticSymbol[],
|
||||||
ngModules: StaticSymbol[]): Promise<SourceModule[]> {
|
ngModules: StaticSymbol[]): Promise<SourceModule[]> {
|
||||||
let fileSuffix = _splitTypescriptSuffix(moduleUrl)[1];
|
const fileSuffix = _splitTypescriptSuffix(moduleUrl)[1];
|
||||||
let statements: o.Statement[] = [];
|
const statements: o.Statement[] = [];
|
||||||
let exportedVars: string[] = [];
|
const exportedVars: string[] = [];
|
||||||
let outputSourceModules: SourceModule[] = [];
|
const outputSourceModules: SourceModule[] = [];
|
||||||
|
|
||||||
// compile all ng modules
|
// compile all ng modules
|
||||||
exportedVars.push(
|
exportedVars.push(
|
||||||
|
@ -75,12 +74,12 @@ export class OfflineCompiler {
|
||||||
if (!ngModule) {
|
if (!ngModule) {
|
||||||
throw new Error(`Cannot determine the module for component ${compMeta.type.name}!`);
|
throw new Error(`Cannot determine the module for component ${compMeta.type.name}!`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise
|
return Promise
|
||||||
.all([compMeta, ...ngModule.transitiveModule.directives].map(
|
.all([compMeta, ...ngModule.transitiveModule.directives].map(
|
||||||
dirMeta => this._directiveNormalizer.normalizeDirective(dirMeta).asyncResult))
|
dirMeta => this._directiveNormalizer.normalizeDirective(dirMeta).asyncResult))
|
||||||
.then((normalizedCompWithDirectives) => {
|
.then((normalizedCompWithDirectives) => {
|
||||||
const compMeta = normalizedCompWithDirectives[0];
|
const [compMeta, ...dirMetas] = normalizedCompWithDirectives;
|
||||||
const dirMetas = normalizedCompWithDirectives.slice(1);
|
|
||||||
_assertComponent(compMeta);
|
_assertComponent(compMeta);
|
||||||
|
|
||||||
// compile styles
|
// compile styles
|
||||||
|
@ -90,8 +89,9 @@ export class OfflineCompiler {
|
||||||
});
|
});
|
||||||
|
|
||||||
// compile components
|
// compile components
|
||||||
exportedVars.push(this._compileComponentFactory(compMeta, fileSuffix, statements));
|
exportedVars.push(
|
||||||
exportedVars.push(this._compileComponent(
|
this._compileComponentFactory(compMeta, fileSuffix, statements),
|
||||||
|
this._compileComponent(
|
||||||
compMeta, dirMetas, ngModule.transitiveModule.pipes, ngModule.schemas,
|
compMeta, dirMetas, ngModule.transitiveModule.pipes, ngModule.schemas,
|
||||||
stylesCompileResults.componentStylesheet, fileSuffix, statements));
|
stylesCompileResults.componentStylesheet, fileSuffix, statements));
|
||||||
});
|
});
|
||||||
|
@ -106,8 +106,8 @@ export class OfflineCompiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private _compileModule(ngModuleType: StaticSymbol, targetStatements: o.Statement[]): string {
|
private _compileModule(ngModuleType: StaticSymbol, targetStatements: o.Statement[]): string {
|
||||||
const ngModule = this._metadataResolver.getNgModuleMetadata(<any>ngModuleType);
|
const ngModule = this._metadataResolver.getNgModuleMetadata(ngModuleType);
|
||||||
let appCompileResult = this._ngModuleCompiler.compile(ngModule, [
|
const appCompileResult = this._ngModuleCompiler.compile(ngModule, [
|
||||||
new CompileProviderMetadata(
|
new CompileProviderMetadata(
|
||||||
{token: resolveIdentifierToken(Identifiers.LOCALE_ID), useValue: this._localeId}),
|
{token: resolveIdentifierToken(Identifiers.LOCALE_ID), useValue: this._localeId}),
|
||||||
new CompileProviderMetadata({
|
new CompileProviderMetadata({
|
||||||
|
@ -126,18 +126,19 @@ export class OfflineCompiler {
|
||||||
private _compileComponentFactory(
|
private _compileComponentFactory(
|
||||||
compMeta: CompileDirectiveMetadata, fileSuffix: string,
|
compMeta: CompileDirectiveMetadata, fileSuffix: string,
|
||||||
targetStatements: o.Statement[]): string {
|
targetStatements: o.Statement[]): string {
|
||||||
var hostMeta = createHostComponentMeta(compMeta);
|
const hostMeta = createHostComponentMeta(compMeta);
|
||||||
var hostViewFactoryVar =
|
const hostViewFactoryVar =
|
||||||
this._compileComponent(hostMeta, [compMeta], [], [], null, fileSuffix, targetStatements);
|
this._compileComponent(hostMeta, [compMeta], [], [], null, fileSuffix, targetStatements);
|
||||||
var compFactoryVar = _componentFactoryName(compMeta.type);
|
const compFactoryVar = _componentFactoryName(compMeta.type);
|
||||||
targetStatements.push(
|
targetStatements.push(
|
||||||
o.variable(compFactoryVar)
|
o.variable(compFactoryVar)
|
||||||
.set(o.importExpr(resolveIdentifier(Identifiers.ComponentFactory), [o.importType(
|
.set(o.importExpr(resolveIdentifier(Identifiers.ComponentFactory), [o.importType(
|
||||||
compMeta.type)])
|
compMeta.type)])
|
||||||
.instantiate(
|
.instantiate(
|
||||||
[
|
[
|
||||||
o.literal(compMeta.selector), o.variable(hostViewFactoryVar),
|
o.literal(compMeta.selector),
|
||||||
o.importExpr(compMeta.type)
|
o.variable(hostViewFactoryVar),
|
||||||
|
o.importExpr(compMeta.type),
|
||||||
],
|
],
|
||||||
o.importType(
|
o.importType(
|
||||||
resolveIdentifier(Identifiers.ComponentFactory),
|
resolveIdentifier(Identifiers.ComponentFactory),
|
||||||
|
@ -150,15 +151,15 @@ export class OfflineCompiler {
|
||||||
compMeta: CompileDirectiveMetadata, directives: CompileDirectiveMetadata[],
|
compMeta: CompileDirectiveMetadata, directives: CompileDirectiveMetadata[],
|
||||||
pipes: CompilePipeMetadata[], schemas: SchemaMetadata[], componentStyles: CompiledStylesheet,
|
pipes: CompilePipeMetadata[], schemas: SchemaMetadata[], componentStyles: CompiledStylesheet,
|
||||||
fileSuffix: string, targetStatements: o.Statement[]): string {
|
fileSuffix: string, targetStatements: o.Statement[]): string {
|
||||||
var parsedTemplate = this._templateParser.parse(
|
const parsedTemplate = this._templateParser.parse(
|
||||||
compMeta, compMeta.template.template, directives, pipes, schemas, compMeta.type.name);
|
compMeta, compMeta.template.template, directives, pipes, schemas, compMeta.type.name);
|
||||||
var stylesExpr = componentStyles ? o.variable(componentStyles.stylesVar) : o.literalArr([]);
|
const stylesExpr = componentStyles ? o.variable(componentStyles.stylesVar) : o.literalArr([]);
|
||||||
var viewResult =
|
const viewResult =
|
||||||
this._viewCompiler.compileComponent(compMeta, parsedTemplate, stylesExpr, pipes);
|
this._viewCompiler.compileComponent(compMeta, parsedTemplate, stylesExpr, pipes);
|
||||||
if (componentStyles) {
|
if (componentStyles) {
|
||||||
ListWrapper.addAll(targetStatements, _resolveStyleStatements(componentStyles, fileSuffix));
|
targetStatements.push(..._resolveStyleStatements(componentStyles, fileSuffix));
|
||||||
}
|
}
|
||||||
ListWrapper.addAll(targetStatements, _resolveViewStatements(viewResult));
|
targetStatements.push(..._resolveViewStatements(viewResult));
|
||||||
return viewResult.viewFactoryVar;
|
return viewResult.viewFactoryVar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,12 +181,10 @@ export class OfflineCompiler {
|
||||||
function _resolveViewStatements(compileResult: ViewCompileResult): o.Statement[] {
|
function _resolveViewStatements(compileResult: ViewCompileResult): o.Statement[] {
|
||||||
compileResult.dependencies.forEach((dep) => {
|
compileResult.dependencies.forEach((dep) => {
|
||||||
if (dep instanceof ViewFactoryDependency) {
|
if (dep instanceof ViewFactoryDependency) {
|
||||||
let vfd = <ViewFactoryDependency>dep;
|
dep.placeholder.moduleUrl = _ngfactoryModuleUrl(dep.comp.moduleUrl);
|
||||||
vfd.placeholder.moduleUrl = _ngfactoryModuleUrl(vfd.comp.moduleUrl);
|
|
||||||
} else if (dep instanceof ComponentFactoryDependency) {
|
} else if (dep instanceof ComponentFactoryDependency) {
|
||||||
let cfd = <ComponentFactoryDependency>dep;
|
dep.placeholder.name = _componentFactoryName(dep.comp);
|
||||||
cfd.placeholder.name = _componentFactoryName(cfd.comp);
|
dep.placeholder.moduleUrl = _ngfactoryModuleUrl(dep.comp.moduleUrl);
|
||||||
cfd.placeholder.moduleUrl = _ngfactoryModuleUrl(cfd.comp.moduleUrl);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return compileResult.statements;
|
return compileResult.statements;
|
||||||
|
@ -201,7 +200,7 @@ function _resolveStyleStatements(
|
||||||
}
|
}
|
||||||
|
|
||||||
function _ngfactoryModuleUrl(compUrl: string): string {
|
function _ngfactoryModuleUrl(compUrl: string): string {
|
||||||
var urlWithSuffix = _splitTypescriptSuffix(compUrl);
|
const urlWithSuffix = _splitTypescriptSuffix(compUrl);
|
||||||
return `${urlWithSuffix[0]}.ngfactory${urlWithSuffix[1]}`;
|
return `${urlWithSuffix[0]}.ngfactory${urlWithSuffix[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,13 +219,15 @@ function _assertComponent(meta: CompileDirectiveMetadata) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _splitTypescriptSuffix(path: string): string[] {
|
function _splitTypescriptSuffix(path: string): string[] {
|
||||||
if (/\.d\.ts$/.test(path)) {
|
if (path.endsWith('.d.ts')) {
|
||||||
return [path.substring(0, path.length - 5), '.ts'];
|
return [path.slice(0, -5), '.ts'];
|
||||||
}
|
}
|
||||||
let lastDot = path.lastIndexOf('.');
|
|
||||||
|
const lastDot = path.lastIndexOf('.');
|
||||||
|
|
||||||
if (lastDot !== -1) {
|
if (lastDot !== -1) {
|
||||||
return [path.substring(0, lastDot), path.substring(lastDot)];
|
return [path.substring(0, lastDot), path.substring(lastDot)];
|
||||||
} else {
|
|
||||||
return [path, ''];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return [path, ''];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue