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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
2016-08-29 11:52:25 -04:00
|
|
|
import {CompileDiDependencyMetadata, CompileIdentifierMetadata, CompileNgModuleMetadata, CompileProviderMetadata, CompileTokenMetadata} from './compile_metadata';
|
2016-10-19 12:17:36 -04:00
|
|
|
import {createDiTokenExpression} from './compiler_util/identifier_util';
|
2016-09-30 12:26:53 -04:00
|
|
|
import {isPresent} from './facade/lang';
|
2016-09-27 20:12:25 -04:00
|
|
|
import {Identifiers, resolveIdentifier, resolveIdentifierToken} from './identifiers';
|
2016-10-21 16:37:51 -04:00
|
|
|
import {ClassBuilder, createClassStmt} from './output/class_builder';
|
2016-06-28 12:54:42 -04:00
|
|
|
import * as o from './output/output_ast';
|
2016-06-30 16:34:15 -04:00
|
|
|
import {convertValueToOutputAst} from './output/value_util';
|
2016-06-28 12:54:42 -04:00
|
|
|
import {ParseLocation, ParseSourceFile, ParseSourceSpan} from './parse_util';
|
2016-08-30 21:07:40 -04:00
|
|
|
import {LifecycleHooks} from './private_import_core';
|
2016-07-18 06:50:31 -04:00
|
|
|
import {NgModuleProviderAnalyzer} from './provider_analyzer';
|
2016-07-21 14:41:25 -04:00
|
|
|
import {ProviderAst} from './template_parser/template_ast';
|
2016-06-28 12:54:42 -04:00
|
|
|
|
|
|
|
export class ComponentFactoryDependency {
|
|
|
|
constructor(
|
|
|
|
public comp: CompileIdentifierMetadata, public placeholder: CompileIdentifierMetadata) {}
|
|
|
|
}
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
export class NgModuleCompileResult {
|
2016-06-28 12:54:42 -04:00
|
|
|
constructor(
|
2016-07-18 06:50:31 -04:00
|
|
|
public statements: o.Statement[], public ngModuleFactoryVar: string,
|
2016-06-28 12:54:42 -04:00
|
|
|
public dependencies: ComponentFactoryDependency[]) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
2016-07-18 06:50:31 -04:00
|
|
|
export class NgModuleCompiler {
|
|
|
|
compile(ngModuleMeta: CompileNgModuleMetadata, extraProviders: CompileProviderMetadata[]):
|
|
|
|
NgModuleCompileResult {
|
2016-11-12 08:08:58 -05:00
|
|
|
const sourceFileName = isPresent(ngModuleMeta.type.moduleUrl) ?
|
2016-07-18 06:50:31 -04:00
|
|
|
`in NgModule ${ngModuleMeta.type.name} in ${ngModuleMeta.type.moduleUrl}` :
|
|
|
|
`in NgModule ${ngModuleMeta.type.name}`;
|
2016-11-12 08:08:58 -05:00
|
|
|
const sourceFile = new ParseSourceFile('', sourceFileName);
|
|
|
|
const sourceSpan = new ParseSourceSpan(
|
2016-06-28 12:54:42 -04:00
|
|
|
new ParseLocation(sourceFile, null, null, null),
|
|
|
|
new ParseLocation(sourceFile, null, null, null));
|
2016-11-12 08:08:58 -05:00
|
|
|
const deps: ComponentFactoryDependency[] = [];
|
|
|
|
const bootstrapComponentFactories: CompileIdentifierMetadata[] = [];
|
|
|
|
const entryComponentFactories =
|
2016-08-02 09:54:08 -04:00
|
|
|
ngModuleMeta.transitiveModule.entryComponents.map((entryComponent) => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const id = new CompileIdentifierMetadata({name: entryComponent.name});
|
2016-08-02 09:54:08 -04:00
|
|
|
if (ngModuleMeta.bootstrapComponents.indexOf(entryComponent) > -1) {
|
|
|
|
bootstrapComponentFactories.push(id);
|
|
|
|
}
|
|
|
|
deps.push(new ComponentFactoryDependency(entryComponent, id));
|
|
|
|
return id;
|
|
|
|
});
|
2016-11-12 08:08:58 -05:00
|
|
|
const builder = new _InjectorBuilder(
|
2016-08-02 09:54:08 -04:00
|
|
|
ngModuleMeta, entryComponentFactories, bootstrapComponentFactories, sourceSpan);
|
2016-06-28 12:54:42 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const providerParser = new NgModuleProviderAnalyzer(ngModuleMeta, extraProviders, sourceSpan);
|
2016-06-28 12:54:42 -04:00
|
|
|
providerParser.parse().forEach((provider) => builder.addProvider(provider));
|
2016-11-12 08:08:58 -05:00
|
|
|
const injectorClass = builder.build();
|
|
|
|
const ngModuleFactoryVar = `${ngModuleMeta.type.name}NgFactory`;
|
|
|
|
const ngModuleFactoryStmt =
|
2016-07-18 06:50:31 -04:00
|
|
|
o.variable(ngModuleFactoryVar)
|
2016-08-24 20:39:49 -04:00
|
|
|
.set(o.importExpr(resolveIdentifier(Identifiers.NgModuleFactory))
|
2016-06-28 12:54:42 -04:00
|
|
|
.instantiate(
|
2016-07-18 06:50:31 -04:00
|
|
|
[o.variable(injectorClass.name), o.importExpr(ngModuleMeta.type)],
|
2016-06-28 12:54:42 -04:00
|
|
|
o.importType(
|
2016-08-24 20:39:49 -04:00
|
|
|
resolveIdentifier(Identifiers.NgModuleFactory),
|
|
|
|
[o.importType(ngModuleMeta.type)], [o.TypeModifier.Const])))
|
2016-06-28 12:54:42 -04:00
|
|
|
.toDeclStmt(null, [o.StmtModifier.Final]);
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const stmts: o.Statement[] = [injectorClass, ngModuleFactoryStmt];
|
2016-09-01 16:46:08 -04:00
|
|
|
if (ngModuleMeta.id) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const registerFactoryStmt =
|
2016-09-01 16:46:08 -04:00
|
|
|
o.importExpr(resolveIdentifier(Identifiers.RegisterModuleFactoryFn))
|
|
|
|
.callFn([o.literal(ngModuleMeta.id), o.variable(ngModuleFactoryVar)])
|
|
|
|
.toStmt();
|
|
|
|
stmts.push(registerFactoryStmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new NgModuleCompileResult(stmts, ngModuleFactoryVar, deps);
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 16:37:51 -04:00
|
|
|
class _InjectorBuilder implements ClassBuilder {
|
|
|
|
fields: o.ClassField[] = [];
|
|
|
|
getters: o.ClassGetter[] = [];
|
|
|
|
methods: o.ClassMethod[] = [];
|
|
|
|
ctorStmts: o.Statement[] = [];
|
2016-10-24 14:11:31 -04:00
|
|
|
private _tokens: CompileTokenMetadata[] = [];
|
|
|
|
private _instances = new Map<any, o.Expression>();
|
|
|
|
private _createStmts: o.Statement[] = [];
|
|
|
|
private _destroyStmts: o.Statement[] = [];
|
2016-06-28 12:54:42 -04:00
|
|
|
|
|
|
|
constructor(
|
2016-07-18 06:50:31 -04:00
|
|
|
private _ngModuleMeta: CompileNgModuleMetadata,
|
2016-08-02 09:54:08 -04:00
|
|
|
private _entryComponentFactories: CompileIdentifierMetadata[],
|
|
|
|
private _bootstrapComponentFactories: CompileIdentifierMetadata[],
|
|
|
|
private _sourceSpan: ParseSourceSpan) {}
|
2016-06-28 12:54:42 -04:00
|
|
|
|
|
|
|
addProvider(resolvedProvider: ProviderAst) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const providerValueExpressions =
|
2016-06-28 12:54:42 -04:00
|
|
|
resolvedProvider.providers.map((provider) => this._getProviderValue(provider));
|
2016-11-12 08:08:58 -05:00
|
|
|
const propName = `_${resolvedProvider.token.name}_${this._instances.size}`;
|
|
|
|
const instance = this._createProviderProperty(
|
2016-06-28 12:54:42 -04:00
|
|
|
propName, resolvedProvider, providerValueExpressions, resolvedProvider.multiProvider,
|
|
|
|
resolvedProvider.eager);
|
2016-08-02 05:08:10 -04:00
|
|
|
if (resolvedProvider.lifecycleHooks.indexOf(LifecycleHooks.OnDestroy) !== -1) {
|
|
|
|
this._destroyStmts.push(instance.callMethod('ngOnDestroy', []).toStmt());
|
|
|
|
}
|
2016-08-29 11:52:25 -04:00
|
|
|
this._tokens.push(resolvedProvider.token);
|
|
|
|
this._instances.set(resolvedProvider.token.reference, instance);
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
build(): o.ClassStmt {
|
2016-11-12 08:08:58 -05:00
|
|
|
const getMethodStmts: o.Statement[] = this._tokens.map((token) => {
|
|
|
|
const providerExpr = this._instances.get(token.reference);
|
2016-06-28 12:54:42 -04:00
|
|
|
return new o.IfStmt(
|
|
|
|
InjectMethodVars.token.identical(createDiTokenExpression(token)),
|
|
|
|
[new o.ReturnStatement(providerExpr)]);
|
|
|
|
});
|
2016-11-12 08:08:58 -05:00
|
|
|
const methods = [
|
2016-06-28 12:54:42 -04:00
|
|
|
new o.ClassMethod(
|
2016-11-12 08:08:58 -05:00
|
|
|
'createInternal', [], this._createStmts.concat(new o.ReturnStatement(
|
|
|
|
this._instances.get(this._ngModuleMeta.type.reference))),
|
|
|
|
o.importType(this._ngModuleMeta.type)),
|
2016-06-28 12:54:42 -04:00
|
|
|
new o.ClassMethod(
|
|
|
|
'getInternal',
|
|
|
|
[
|
|
|
|
new o.FnParam(InjectMethodVars.token.name, o.DYNAMIC_TYPE),
|
|
|
|
new o.FnParam(InjectMethodVars.notFoundResult.name, o.DYNAMIC_TYPE)
|
|
|
|
],
|
|
|
|
getMethodStmts.concat([new o.ReturnStatement(InjectMethodVars.notFoundResult)]),
|
2016-08-02 05:08:10 -04:00
|
|
|
o.DYNAMIC_TYPE),
|
2016-11-12 08:08:58 -05:00
|
|
|
new o.ClassMethod('destroyInternal', [], this._destroyStmts),
|
2016-06-28 12:54:42 -04:00
|
|
|
];
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const parentArgs = [
|
2016-10-21 16:37:51 -04:00
|
|
|
o.variable(InjectorProps.parent.name),
|
|
|
|
o.literalArr(
|
|
|
|
this._entryComponentFactories.map((componentFactory) => o.importExpr(componentFactory))),
|
|
|
|
o.literalArr(this._bootstrapComponentFactories.map(
|
|
|
|
(componentFactory) => o.importExpr(componentFactory)))
|
|
|
|
];
|
2016-11-12 08:08:58 -05:00
|
|
|
const injClassName = `${this._ngModuleMeta.type.name}Injector`;
|
2016-10-21 16:37:51 -04:00
|
|
|
return createClassStmt({
|
|
|
|
name: injClassName,
|
|
|
|
ctorParams: [new o.FnParam(
|
|
|
|
InjectorProps.parent.name, o.importType(resolveIdentifier(Identifiers.Injector)))],
|
|
|
|
parent: o.importExpr(
|
|
|
|
resolveIdentifier(Identifiers.NgModuleInjector), [o.importType(this._ngModuleMeta.type)]),
|
|
|
|
parentArgs: parentArgs,
|
2016-10-24 14:11:31 -04:00
|
|
|
builders: [{methods}, this]
|
2016-10-21 16:37:51 -04:00
|
|
|
});
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private _getProviderValue(provider: CompileProviderMetadata): o.Expression {
|
2016-11-12 08:08:58 -05:00
|
|
|
let result: o.Expression;
|
2016-06-28 12:54:42 -04:00
|
|
|
if (isPresent(provider.useExisting)) {
|
|
|
|
result = this._getDependency(new CompileDiDependencyMetadata({token: provider.useExisting}));
|
|
|
|
} else if (isPresent(provider.useFactory)) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const deps = provider.deps || provider.useFactory.diDeps;
|
|
|
|
const depsExpr = deps.map((dep) => this._getDependency(dep));
|
2016-06-28 12:54:42 -04:00
|
|
|
result = o.importExpr(provider.useFactory).callFn(depsExpr);
|
|
|
|
} else if (isPresent(provider.useClass)) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const deps = provider.deps || provider.useClass.diDeps;
|
|
|
|
const depsExpr = deps.map((dep) => this._getDependency(dep));
|
2016-06-28 12:54:42 -04:00
|
|
|
result =
|
|
|
|
o.importExpr(provider.useClass).instantiate(depsExpr, o.importType(provider.useClass));
|
|
|
|
} else {
|
2016-06-30 16:34:15 -04:00
|
|
|
result = convertValueToOutputAst(provider.useValue);
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private _createProviderProperty(
|
|
|
|
propName: string, provider: ProviderAst, providerValueExpressions: o.Expression[],
|
|
|
|
isMulti: boolean, isEager: boolean): o.Expression {
|
2016-11-12 08:08:58 -05:00
|
|
|
let resolvedProviderValueExpr: o.Expression;
|
|
|
|
let type: o.Type;
|
2016-06-28 12:54:42 -04:00
|
|
|
if (isMulti) {
|
|
|
|
resolvedProviderValueExpr = o.literalArr(providerValueExpressions);
|
|
|
|
type = new o.ArrayType(o.DYNAMIC_TYPE);
|
|
|
|
} else {
|
|
|
|
resolvedProviderValueExpr = providerValueExpressions[0];
|
|
|
|
type = providerValueExpressions[0].type;
|
|
|
|
}
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!type) {
|
2016-06-28 12:54:42 -04:00
|
|
|
type = o.DYNAMIC_TYPE;
|
|
|
|
}
|
|
|
|
if (isEager) {
|
2016-10-21 16:37:51 -04:00
|
|
|
this.fields.push(new o.ClassField(propName, type));
|
2016-06-28 12:54:42 -04:00
|
|
|
this._createStmts.push(o.THIS_EXPR.prop(propName).set(resolvedProviderValueExpr).toStmt());
|
|
|
|
} else {
|
2016-11-12 08:08:58 -05:00
|
|
|
const internalField = `_${propName}`;
|
2016-10-21 16:37:51 -04:00
|
|
|
this.fields.push(new o.ClassField(internalField, type));
|
2016-06-28 12:54:42 -04:00
|
|
|
// Note: Equals is important for JS so that it also checks the undefined case!
|
2016-11-12 08:08:58 -05:00
|
|
|
const getterStmts = [
|
2016-06-28 12:54:42 -04:00
|
|
|
new o.IfStmt(
|
|
|
|
o.THIS_EXPR.prop(internalField).isBlank(),
|
|
|
|
[o.THIS_EXPR.prop(internalField).set(resolvedProviderValueExpr).toStmt()]),
|
|
|
|
new o.ReturnStatement(o.THIS_EXPR.prop(internalField))
|
|
|
|
];
|
2016-10-21 16:37:51 -04:00
|
|
|
this.getters.push(new o.ClassGetter(propName, getterStmts, type));
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
return o.THIS_EXPR.prop(propName);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getDependency(dep: CompileDiDependencyMetadata): o.Expression {
|
2016-11-12 08:08:58 -05:00
|
|
|
let result: o.Expression = null;
|
2016-06-28 12:54:42 -04:00
|
|
|
if (dep.isValue) {
|
|
|
|
result = o.literal(dep.value);
|
|
|
|
}
|
|
|
|
if (!dep.isSkipSelf) {
|
|
|
|
if (dep.token &&
|
2016-08-29 11:52:25 -04:00
|
|
|
(dep.token.reference === resolveIdentifierToken(Identifiers.Injector).reference ||
|
|
|
|
dep.token.reference ===
|
|
|
|
resolveIdentifierToken(Identifiers.ComponentFactoryResolver).reference)) {
|
2016-06-28 12:54:42 -04:00
|
|
|
result = o.THIS_EXPR;
|
|
|
|
}
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!result) {
|
2016-08-29 11:52:25 -04:00
|
|
|
result = this._instances.get(dep.token.reference);
|
2016-06-28 12:54:42 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-30 12:26:53 -04:00
|
|
|
if (!result) {
|
2016-11-12 08:08:58 -05:00
|
|
|
const args = [createDiTokenExpression(dep.token)];
|
2016-06-28 12:54:42 -04:00
|
|
|
if (dep.isOptional) {
|
|
|
|
args.push(o.NULL_EXPR);
|
|
|
|
}
|
|
|
|
result = InjectorProps.parent.callMethod('get', args);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class InjectorProps {
|
|
|
|
static parent = o.THIS_EXPR.prop('parent');
|
|
|
|
}
|
|
|
|
|
|
|
|
class InjectMethodVars {
|
|
|
|
static token = o.variable('token');
|
|
|
|
static notFoundResult = o.variable('notFoundResult');
|
|
|
|
}
|