refactor(compiler): make OutputAst contain the moduleName, not the filePath (#16832).

The goal of this change is to simplify the emitters,
as we will soon create a new one to emit TypeScript nodes directly.
This commit is contained in:
Tobias Bosch
2017-05-16 16:30:37 -07:00
committed by Chuck Jazdzewski
parent 3b28c75d1f
commit 6123b9c0c6
28 changed files with 589 additions and 774 deletions

View File

@ -14,58 +14,61 @@ import {CompilerInjectable} from './injectable';
import * as o from './output/output_ast';
import {typeSourceSpan} from './parse_util';
import {NgModuleProviderAnalyzer} from './provider_analyzer';
import {OutputContext} from './util';
import {componentFactoryResolverProviderDef, depDef, providerDef} from './view_compiler/provider_compiler';
export class NgModuleCompileResult {
constructor(public statements: o.Statement[], public ngModuleFactoryVar: string) {}
constructor(public ngModuleFactoryVar: string) {}
}
const LOG_VAR = o.variable('_l');
@CompilerInjectable()
export class NgModuleCompiler {
compile(ngModuleMeta: CompileNgModuleMetadata, extraProviders: CompileProviderMetadata[]):
NgModuleCompileResult {
compile(
ctx: OutputContext, ngModuleMeta: CompileNgModuleMetadata,
extraProviders: CompileProviderMetadata[]): NgModuleCompileResult {
const sourceSpan = typeSourceSpan('NgModule', ngModuleMeta.type);
const entryComponentFactories = ngModuleMeta.transitiveModule.entryComponents;
const bootstrapComponents = ngModuleMeta.bootstrapComponents;
const providerParser = new NgModuleProviderAnalyzer(ngModuleMeta, extraProviders, sourceSpan);
const providerDefs =
[componentFactoryResolverProviderDef(NodeFlags.None, entryComponentFactories)]
.concat(providerParser.parse().map((provider) => providerDef(provider)))
[componentFactoryResolverProviderDef(ctx, NodeFlags.None, entryComponentFactories)]
.concat(providerParser.parse().map((provider) => providerDef(ctx, provider)))
.map(({providerExpr, depsExpr, flags, tokenExpr}) => {
return o.importExpr(createIdentifier(Identifiers.moduleProviderDef)).callFn([
return o.importExpr(Identifiers.moduleProviderDef).callFn([
o.literal(flags), tokenExpr, providerExpr, depsExpr
]);
});
const ngModuleDef =
o.importExpr(createIdentifier(Identifiers.moduleDef)).callFn([o.literalArr(providerDefs)]);
const ngModuleDef = o.importExpr(Identifiers.moduleDef).callFn([o.literalArr(providerDefs)]);
const ngModuleDefFactory = o.fn(
[new o.FnParam(LOG_VAR.name !)], [new o.ReturnStatement(ngModuleDef)], o.INFERRED_TYPE);
const ngModuleFactoryVar = `${identifierName(ngModuleMeta.type)}NgFactory`;
const ngModuleFactoryStmt =
o.variable(ngModuleFactoryVar)
.set(o.importExpr(createIdentifier(Identifiers.createModuleFactory)).callFn([
o.importExpr(ngModuleMeta.type),
o.literalArr(bootstrapComponents.map(id => o.importExpr(id))), ngModuleDefFactory
.set(o.importExpr(Identifiers.createModuleFactory).callFn([
ctx.importExpr(ngModuleMeta.type.reference),
o.literalArr(bootstrapComponents.map(id => ctx.importExpr(id.reference))),
ngModuleDefFactory
]))
.toDeclStmt(
o.importType(
createIdentifier(Identifiers.NgModuleFactory),
[o.importType(ngModuleMeta.type) !], [o.TypeModifier.Const]),
[o.StmtModifier.Final]);
Identifiers.NgModuleFactory,
[o.expressionType(ctx.importExpr(ngModuleMeta.type.reference)) !],
[o.TypeModifier.Const]),
[o.StmtModifier.Final, o.StmtModifier.Exported]);
const stmts: o.Statement[] = [ngModuleFactoryStmt];
ctx.statements.push(ngModuleFactoryStmt);
if (ngModuleMeta.id) {
const registerFactoryStmt =
o.importExpr(createIdentifier(Identifiers.RegisterModuleFactoryFn))
o.importExpr(Identifiers.RegisterModuleFactoryFn)
.callFn([o.literal(ngModuleMeta.id), o.variable(ngModuleFactoryVar)])
.toStmt();
stmts.push(registerFactoryStmt);
ctx.statements.push(registerFactoryStmt);
}
return new NgModuleCompileResult(stmts, ngModuleFactoryVar);
return new NgModuleCompileResult(ngModuleFactoryVar);
}
}