refactor(compiler): add ability to produce stub .ngfactory / .ngsummary files (#16963)

These files are needed so that:
- user code can compile even without real codegen
- as tsc transformers cannot create but only change existing files
  in the transformation pipeline.
This commit is contained in:
Tobias Bosch
2017-05-23 13:40:50 -07:00
committed by Chuck Jazdzewski
parent fa809ec8cf
commit eba59aaf87
6 changed files with 211 additions and 84 deletions

View File

@ -50,21 +50,13 @@ export class NgModuleCompiler {
[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(Identifiers.createModuleFactory).callFn([
ctx.importExpr(ngModuleMeta.type.reference),
o.literalArr(bootstrapComponents.map(id => ctx.importExpr(id.reference))),
ngModuleDefFactory
]))
.toDeclStmt(
o.importType(
Identifiers.NgModuleFactory,
[o.expressionType(ctx.importExpr(ngModuleMeta.type.reference)) !],
[o.TypeModifier.Const]),
[o.StmtModifier.Final, o.StmtModifier.Exported]);
this._createNgModuleFactory(
ctx, ngModuleMeta.type.reference, o.importExpr(Identifiers.createModuleFactory).callFn([
ctx.importExpr(ngModuleMeta.type.reference),
o.literalArr(bootstrapComponents.map(id => ctx.importExpr(id.reference))),
ngModuleDefFactory
]));
ctx.statements.push(ngModuleFactoryStmt);
if (ngModuleMeta.id) {
const registerFactoryStmt =
o.importExpr(Identifiers.RegisterModuleFactoryFn)
@ -75,4 +67,22 @@ export class NgModuleCompiler {
return new NgModuleCompileResult(ngModuleFactoryVar);
}
createStub(ctx: OutputContext, ngModuleReference: any) {
this._createNgModuleFactory(ctx, ngModuleReference, o.NULL_EXPR);
}
private _createNgModuleFactory(ctx: OutputContext, reference: any, value: o.Expression) {
const ngModuleFactoryVar = `${identifierName({reference: reference})}NgFactory`;
const ngModuleFactoryStmt =
o.variable(ngModuleFactoryVar)
.set(value)
.toDeclStmt(
o.importType(
Identifiers.NgModuleFactory, [o.expressionType(ctx.importExpr(reference)) !],
[o.TypeModifier.Const]),
[o.StmtModifier.Final, o.StmtModifier.Exported]);
ctx.statements.push(ngModuleFactoryStmt);
}
}