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