2016-06-28 12:54:42 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-28 12:54:42 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-05-11 13:26:02 -04:00
|
|
|
import {CompileNgModuleMetadata, CompileProviderMetadata, identifierName} from './compile_metadata';
|
2017-05-18 16:46:51 -04:00
|
|
|
import {CompileReflector} from './compile_reflector';
|
2017-08-16 12:00:03 -04:00
|
|
|
import {NodeFlags} from './core';
|
2017-05-18 16:46:51 -04:00
|
|
|
import {Identifiers} from './identifiers';
|
2016-06-28 12:54:42 -04:00
|
|
|
import * as o from './output/output_ast';
|
2017-05-11 13:26:02 -04:00
|
|
|
import {typeSourceSpan} from './parse_util';
|
2016-07-18 06:50:31 -04:00
|
|
|
import {NgModuleProviderAnalyzer} from './provider_analyzer';
|
2017-05-16 19:30:37 -04:00
|
|
|
import {OutputContext} from './util';
|
2017-05-11 13:26:02 -04:00
|
|
|
import {componentFactoryResolverProviderDef, depDef, providerDef} from './view_compiler/provider_compiler';
|
2016-06-28 12:54:42 -04:00
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
export class NgModuleCompileResult {
|
2017-05-16 19:30:37 -04:00
|
|
|
constructor(public ngModuleFactoryVar: string) {}
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
|
2017-05-11 13:26:02 -04:00
|
|
|
const LOG_VAR = o.variable('_l');
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
export class NgModuleCompiler {
|
2017-05-18 16:46:51 -04:00
|
|
|
constructor(private reflector: CompileReflector) {}
|
2017-05-16 19:30:37 -04:00
|
|
|
compile(
|
|
|
|
ctx: OutputContext, ngModuleMeta: CompileNgModuleMetadata,
|
|
|
|
extraProviders: CompileProviderMetadata[]): NgModuleCompileResult {
|
2017-03-14 12:16:15 -04:00
|
|
|
const sourceSpan = typeSourceSpan('NgModule', ngModuleMeta.type);
|
2017-05-11 13:26:02 -04:00
|
|
|
const entryComponentFactories = ngModuleMeta.transitiveModule.entryComponents;
|
|
|
|
const bootstrapComponents = ngModuleMeta.bootstrapComponents;
|
2017-05-18 16:46:51 -04:00
|
|
|
const providerParser =
|
|
|
|
new NgModuleProviderAnalyzer(this.reflector, ngModuleMeta, extraProviders, sourceSpan);
|
2017-05-11 13:26:02 -04:00
|
|
|
const providerDefs =
|
2017-05-18 16:46:51 -04:00
|
|
|
[componentFactoryResolverProviderDef(
|
|
|
|
this.reflector, ctx, NodeFlags.None, entryComponentFactories)]
|
2017-05-16 19:30:37 -04:00
|
|
|
.concat(providerParser.parse().map((provider) => providerDef(ctx, provider)))
|
2017-05-11 13:26:02 -04:00
|
|
|
.map(({providerExpr, depsExpr, flags, tokenExpr}) => {
|
2017-05-16 19:30:37 -04:00
|
|
|
return o.importExpr(Identifiers.moduleProviderDef).callFn([
|
2017-05-11 13:26:02 -04:00
|
|
|
o.literal(flags), tokenExpr, providerExpr, depsExpr
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2017-05-16 19:30:37 -04:00
|
|
|
const ngModuleDef = o.importExpr(Identifiers.moduleDef).callFn([o.literalArr(providerDefs)]);
|
2020-04-08 13:14:18 -04:00
|
|
|
const ngModuleDefFactory =
|
|
|
|
o.fn([new o.FnParam(LOG_VAR.name!)], [new o.ReturnStatement(ngModuleDef)], o.INFERRED_TYPE);
|
2017-05-11 13:26:02 -04:00
|
|
|
|
2016-11-23 12:42:19 -05:00
|
|
|
const ngModuleFactoryVar = `${identifierName(ngModuleMeta.type)}NgFactory`;
|
2017-05-23 16:40:50 -04:00
|
|
|
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
|
|
|
|
]));
|
2017-05-11 13:26:02 -04:00
|
|
|
|
2016-09-01 16:46:08 -04:00
|
|
|
if (ngModuleMeta.id) {
|
2018-03-27 14:56:23 -04:00
|
|
|
const id = typeof ngModuleMeta.id === 'string' ? o.literal(ngModuleMeta.id) :
|
|
|
|
ctx.importExpr(ngModuleMeta.id);
|
|
|
|
const registerFactoryStmt = o.importExpr(Identifiers.RegisterModuleFactoryFn)
|
|
|
|
.callFn([id, o.variable(ngModuleFactoryVar)])
|
|
|
|
.toStmt();
|
2017-05-16 19:30:37 -04:00
|
|
|
ctx.statements.push(registerFactoryStmt);
|
2016-09-01 16:46:08 -04:00
|
|
|
}
|
|
|
|
|
2017-05-16 19:30:37 -04:00
|
|
|
return new NgModuleCompileResult(ngModuleFactoryVar);
|
2017-02-28 02:08:19 -05:00
|
|
|
}
|
2017-05-23 16:40:50 -04:00
|
|
|
|
|
|
|
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(
|
2020-04-08 13:14:18 -04:00
|
|
|
Identifiers.NgModuleFactory, [o.expressionType(ctx.importExpr(reference))!],
|
2017-05-23 16:40:50 -04:00
|
|
|
[o.TypeModifier.Const]),
|
|
|
|
[o.StmtModifier.Final, o.StmtModifier.Exported]);
|
|
|
|
|
|
|
|
ctx.statements.push(ngModuleFactoryStmt);
|
|
|
|
}
|
2017-02-28 02:08:19 -05:00
|
|
|
}
|