2016-06-28 12:54:42 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-05-11 13:26:02 -04:00
|
|
|
import {ɵNodeFlags as NodeFlags} from '@angular/core';
|
2017-02-17 15:55:55 -05:00
|
|
|
|
2017-05-11 13:26:02 -04:00
|
|
|
import {CompileNgModuleMetadata, CompileProviderMetadata, identifierName} from './compile_metadata';
|
|
|
|
import {Identifiers, createIdentifier} from './identifiers';
|
2016-12-15 12:12:40 -05:00
|
|
|
import {CompilerInjectable} from './injectable';
|
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-12-15 12:12:40 -05:00
|
|
|
@CompilerInjectable()
|
2016-07-18 06:50:31 -04:00
|
|
|
export class NgModuleCompiler {
|
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;
|
2016-11-12 08:08:58 -05:00
|
|
|
const providerParser = new NgModuleProviderAnalyzer(ngModuleMeta, extraProviders, sourceSpan);
|
2017-05-11 13:26:02 -04:00
|
|
|
const providerDefs =
|
2017-05-16 19:30:37 -04:00
|
|
|
[componentFactoryResolverProviderDef(ctx, NodeFlags.None, entryComponentFactories)]
|
|
|
|
.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)]);
|
2017-05-11 13:26:02 -04:00
|
|
|
const ngModuleDefFactory = o.fn(
|
|
|
|
[new o.FnParam(LOG_VAR.name !)], [new o.ReturnStatement(ngModuleDef)], o.INFERRED_TYPE);
|
|
|
|
|
2016-11-23 12:42:19 -05:00
|
|
|
const ngModuleFactoryVar = `${identifierName(ngModuleMeta.type)}NgFactory`;
|
2016-11-12 08:08:58 -05:00
|
|
|
const ngModuleFactoryStmt =
|
2016-07-18 06:50:31 -04:00
|
|
|
o.variable(ngModuleFactoryVar)
|
2017-05-16 19:30:37 -04:00
|
|
|
.set(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
|
|
|
]))
|
|
|
|
.toDeclStmt(
|
|
|
|
o.importType(
|
2017-05-16 19:30:37 -04:00
|
|
|
Identifiers.NgModuleFactory,
|
|
|
|
[o.expressionType(ctx.importExpr(ngModuleMeta.type.reference)) !],
|
|
|
|
[o.TypeModifier.Const]),
|
|
|
|
[o.StmtModifier.Final, o.StmtModifier.Exported]);
|
2017-05-11 13:26:02 -04:00
|
|
|
|
2017-05-16 19:30:37 -04:00
|
|
|
ctx.statements.push(ngModuleFactoryStmt);
|
2016-09-01 16:46:08 -04:00
|
|
|
if (ngModuleMeta.id) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const registerFactoryStmt =
|
2017-05-16 19:30:37 -04:00
|
|
|
o.importExpr(Identifiers.RegisterModuleFactoryFn)
|
2016-09-01 16:46:08 -04:00
|
|
|
.callFn([o.literal(ngModuleMeta.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
|
|
|
}
|
|
|
|
}
|