2016-06-23 09:47:54 -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
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								/**
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * Transform template html and css into executable code.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * Intended to be used in a build step.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import * as compiler from '@angular/compiler';
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {ComponentMetadata, NgModuleMetadata, ViewEncapsulation} from '@angular/core';
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 14:45:36 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import * as path from 'path';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import * as ts from 'typescript';
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-22 11:48:33 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {PathMappedReflectorHost} from './path_mapped_reflector_host';
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {CompileMetadataResolver, DirectiveNormalizer, DomElementSchemaRegistry, HtmlParser, Lexer, NgModuleCompiler, Parser, StyleCompiler, TemplateParser, TypeScriptEmitter, ViewCompiler} from './private_import_compiler';
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import {Console} from './private_import_core';
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 17:38:29 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {ReflectorHost, ReflectorHostContext} from './reflector_host';
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 17:31:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {StaticAndDynamicReflectionCapabilities} from './static_reflection_capabilities';
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import {StaticReflector, StaticSymbol} from './static_reflector';
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 14:45:36 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const nodeFs = require('fs');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 17:38:29 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const GENERATED_FILES = /\.ngfactory\.ts$|\.css\.ts$|\.css\.shim\.ts$/;
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-23 16:26:35 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const GENERATED_OR_DTS_FILES = /\.d\.ts$|\.ngfactory\.ts$|\.css\.ts$|\.css\.shim\.ts$/;
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 17:38:29 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const PREAMBLE = `/**
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * This file is generated by the Angular 2 template compiler.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 * Do not edit.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								 */
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-27 09:16:46 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								 /* tslint:disable */
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								`;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								export class CodeGenerator {
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  constructor(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      private options: AngularCompilerOptions, private program: ts.Program,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      public host: ts.CompilerHost, private staticReflector: StaticReflector,
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      private compiler: compiler.OfflineCompiler, private reflectorHost: ReflectorHost) {}
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  private readFileMetadata(absSourcePath: string): FileMetadata {
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-31 11:00:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const moduleMetadata = this.staticReflector.getModuleMetadata(absSourcePath);
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const result: FileMetadata = {components: [], ngModules: [], fileUrl: absSourcePath};
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-31 11:00:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!moduleMetadata) {
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      console.log(`WARNING: no metadata found for ${absSourcePath}`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return result;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-31 11:00:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const metadata = moduleMetadata['metadata'];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const symbols = metadata && Object.keys(metadata);
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if (!symbols || !symbols.length) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return result;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    for (const symbol of symbols) {
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-31 11:00:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      if (metadata[symbol] && metadata[symbol].__symbolic == 'error') {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        // Ignore symbols that are only included to record error information.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        continue;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      const staticType = this.reflectorHost.findDeclaration(absSourcePath, symbol, absSourcePath);
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      const annotations = this.staticReflector.annotations(staticType);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      annotations.forEach((annotation) => {
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if (annotation instanceof NgModuleMetadata) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          result.ngModules.push(staticType);
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        } else if (annotation instanceof ComponentMetadata) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          result.components.push(staticType);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      });
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return result;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 11:50:55 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  // Write codegen in a directory structure matching the sources.
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-09 14:58:19 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  private calculateEmitPath(filePath: string): string {
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-24 10:53:48 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    let root = this.options.basePath;
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 11:50:55 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    for (let eachRootDir of this.options.rootDirs || []) {
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-24 10:53:48 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      if (this.options.trace) {
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 18:49:59 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        console.log(`Check if ${filePath} is under rootDirs element ${eachRootDir}`);
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 11:50:55 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      if (path.relative(eachRootDir, filePath).indexOf('.') !== 0) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        root = eachRootDir;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-03 21:34:03 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    // transplant the codegen path to be inside the `genDir`
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    var relativePath: string = path.relative(root, filePath);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    while (relativePath.startsWith('..' + path.sep)) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // Strip out any `..` path such as: `../node_modules/@foo` as we want to put everything
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // into `genDir`.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      relativePath = relativePath.substr(3);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return path.join(this.options.genDir, relativePath);
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 11:50:55 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-02 09:38:46 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  codegen(): Promise<any> {
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-23 16:26:35 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    // Compare with false since the default should be true
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const skipFileNames = (this.options.generateCodeForLibraries === false) ?
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        GENERATED_OR_DTS_FILES :
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        GENERATED_FILES;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    let filePaths = this.program.getSourceFiles()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        .filter(sf => !skipFileNames.test(sf.fileName))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        .map(sf => this.reflectorHost.getCanonicalFileName(sf.fileName));
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const fileMetas = filePaths.map((filePath) => this.readFileMetadata(filePath));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const ngModules = fileMetas.reduce((ngModules, fileMeta) => {
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      ngModules.push(...fileMeta.ngModules);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return ngModules;
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    }, <StaticSymbol[]>[]);
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const analyzedNgModules = this.compiler.analyzeModules(ngModules);
							 | 
						
					
						
							
								
									
										
										
										
											2016-09-01 16:54:37 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return Promise.all(fileMetas.map(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        (fileMeta) =>
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            this.compiler
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                .compile(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    fileMeta.fileUrl, analyzedNgModules, fileMeta.components, fileMeta.ngModules)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                .then((generatedModules) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                  generatedModules.forEach((generatedModule) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    const sourceFile = this.program.getSourceFile(fileMeta.fileUrl);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    const emitPath = this.calculateEmitPath(generatedModule.moduleUrl);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                    this.host.writeFile(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        emitPath, PREAMBLE + generatedModule.source, false, () => {}, [sourceFile]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                  });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                })));
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  static create(
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 14:45:36 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      options: AngularCompilerOptions, cliOptions: NgcCliOptions, program: ts.Program,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      compilerHost: ts.CompilerHost, reflectorHostContext?: ReflectorHostContext,
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      resourceLoader?: compiler.ResourceLoader): CodeGenerator {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    resourceLoader = resourceLoader || {
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 20:07:00 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      get: (s: string) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if (!compilerHost.fileExists(s)) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          // TODO: We should really have a test for error cases like this!
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          throw new Error(`Compilation failed. Resource file not found: ${s}`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return Promise.resolve(compilerHost.readFile(s));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    };
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 14:45:36 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const transFile = cliOptions.i18nFile;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const locale = cliOptions.locale;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    let transContent: string = '';
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-26 15:38:48 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (transFile) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      if (!locale) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        throw new Error(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            `The translation file (${transFile}) locale must be provided. Use the --locale option.`);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 14:45:36 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      transContent = nodeFs.readFileSync(transFile, 'utf8');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const urlResolver: compiler.UrlResolver = compiler.createOfflineCompileUrlResolver();
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-22 11:48:33 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const usePathMapping = !!options.rootDirs && options.rootDirs.length > 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const reflectorHost = usePathMapping ?
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        new PathMappedReflectorHost(program, compilerHost, options, reflectorHostContext) :
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        new ReflectorHost(program, compilerHost, options, reflectorHostContext);
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const staticReflector = new StaticReflector(reflectorHost);
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-03 17:31:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    StaticAndDynamicReflectionCapabilities.install(staticReflector);
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 22:13:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const htmlParser =
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        new compiler.I18NHtmlParser(new HtmlParser(), transContent, cliOptions.i18nFormat);
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-13 10:06:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const config = new compiler.CompilerConfig({
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-15 14:56:56 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      genDebugInfo: options.debug === true,
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-13 10:06:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      defaultEncapsulation: ViewEncapsulation.Emulated,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      logBindingUpdate: false,
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-08 13:40:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      useJit: false
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-13 10:06:40 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const normalizer = new DirectiveNormalizer(resourceLoader, urlResolver, htmlParser, config);
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-24 14:31:35 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const expressionParser = new Parser(new Lexer());
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-28 19:39:10 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const elementSchemaRegistry = new DomElementSchemaRegistry();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const console = new Console();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const tmplParser =
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        new TemplateParser(expressionParser, elementSchemaRegistry, htmlParser, console, []);
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const resolver = new CompileMetadataResolver(
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        new compiler.NgModuleResolver(staticReflector),
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        new compiler.DirectiveResolver(staticReflector), new compiler.PipeResolver(staticReflector),
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-19 12:51:01 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        elementSchemaRegistry, staticReflector);
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 22:13:54 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    // TODO(vicb): do not pass cliOptions.i18nFormat here
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const offlineCompiler = new compiler.OfflineCompiler(
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        resolver, normalizer, tmplParser, new StyleCompiler(urlResolver), new ViewCompiler(config),
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-12 14:45:36 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        new NgModuleCompiler(), new TypeScriptEmitter(reflectorHost), cliOptions.locale,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        cliOptions.i18nFormat);
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    return new CodeGenerator(
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-09 10:12:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        options, program, compilerHost, staticReflector, offlineCompiler, reflectorHost);
							 | 
						
					
						
							
								
									
										
										
										
											2016-04-28 21:57:16 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							
								
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								interface FileMetadata {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  fileUrl: string;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  components: StaticSymbol[];
							 | 
						
					
						
							
								
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  ngModules: StaticSymbol[];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |