angular/packages/compiler/src/render3/r3_module_factory_compiler.ts
Chuck Jazdzewski b0b9ca3386 feat(ivy): produce Renderer2 back-patching and factories. (#22506)
Produces back-patch as described in the #22235 and referenced in #22480.

This just contains the compiler implementations and the corresponding unit
tests. Connecting the dots as described in #22480 will be in a follow on
change.

PR Close #22506
2018-03-08 22:39:07 -08:00

47 lines
1.8 KiB
TypeScript

/**
* @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 {StaticReflector} from '../aot/static_reflector';
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompilePipeSummary, CompileTypeMetadata, identifierName} from '../compile_metadata';
import {CompileMetadataResolver} from '../metadata_resolver';
import * as o from '../output/output_ast';
import {OutputContext} from '../util';
import {Identifiers as R3} from './r3_identifiers';
/**
* Write a Renderer2 compatibility module factory to the output context.
*/
export function compileModuleFactory(
outputCtx: OutputContext, module: CompileNgModuleMetadata,
backPatchReferenceOf: (module: CompileTypeMetadata) => o.Expression,
resolver: CompileMetadataResolver) {
const ngModuleFactoryVar = `${identifierName(module.type)}NgFactory`;
const parentInjector = 'parentInjector';
const createFunction = o.fn(
[new o.FnParam(parentInjector, o.DYNAMIC_TYPE)],
[new o.IfStmt(
o.THIS_EXPR.prop(R3.PATCH_DEPS).notIdentical(o.literal(true, o.INFERRED_TYPE)),
[
o.THIS_EXPR.prop(R3.PATCH_DEPS).set(o.literal(true, o.INFERRED_TYPE)).toStmt(),
backPatchReferenceOf(module.type).callFn([]).toStmt()
])],
o.INFERRED_TYPE, null, `${ngModuleFactoryVar}_Create`);
const moduleFactoryLiteral = o.literalMap([
{key: 'moduleType', value: outputCtx.importExpr(module.type.reference), quoted: false},
{key: 'create', value: createFunction, quoted: false}
]);
outputCtx.statements.push(
o.variable(ngModuleFactoryVar).set(moduleFactoryLiteral).toDeclStmt(o.DYNAMIC_TYPE, [
o.StmtModifier.Exported, o.StmtModifier.Final
]));
}