2016-06-23 16:47:54 +00: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-29 04:57:16 +00:00
|
|
|
/**
|
|
|
|
|
* Transform template html and css into executable code.
|
|
|
|
|
* Intended to be used in a build step.
|
|
|
|
|
*/
|
2016-04-29 00:50:03 +00:00
|
|
|
import * as compiler from '@angular/compiler';
|
2016-08-12 21:45:36 +00:00
|
|
|
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
|
2017-07-07 17:22:52 +00:00
|
|
|
import {readFileSync} from 'fs';
|
2016-06-08 23:38:52 +00:00
|
|
|
import * as ts from 'typescript';
|
2016-04-29 04:57:16 +00:00
|
|
|
|
2016-12-01 21:24:51 +00:00
|
|
|
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
|
2016-11-17 20:24:33 +00:00
|
|
|
import {PathMappedCompilerHost} from './path_mapped_compiler_host';
|
2016-04-29 04:57:16 +00:00
|
|
|
|
2016-11-29 23:36:33 +00:00
|
|
|
const GENERATED_META_FILES = /\.json$/;
|
2016-08-13 00:38:29 +00:00
|
|
|
|
2016-04-29 04:57:16 +00:00
|
|
|
const PREAMBLE = `/**
|
2017-01-27 06:30:42 +00:00
|
|
|
* @fileoverview This file is generated by the Angular template compiler.
|
2016-04-29 04:57:16 +00:00
|
|
|
* Do not edit.
|
2017-04-18 22:00:43 +00:00
|
|
|
* @suppress {suspiciousCode,uselessCode,missingProperties,missingOverride}
|
2016-04-29 04:57:16 +00:00
|
|
|
*/
|
2016-05-27 16:16:46 +00:00
|
|
|
/* tslint:disable */
|
|
|
|
|
|
2016-04-29 04:57:16 +00:00
|
|
|
`;
|
|
|
|
|
|
2016-10-06 22:10:44 +00:00
|
|
|
export class CodeGenerator {
|
|
|
|
|
constructor(
|
|
|
|
|
private options: AngularCompilerOptions, private program: ts.Program,
|
2016-11-18 16:40:41 +00:00
|
|
|
public host: ts.CompilerHost, private compiler: compiler.AotCompiler,
|
|
|
|
|
private ngCompilerHost: CompilerHost) {}
|
2016-04-29 04:57:16 +00:00
|
|
|
|
2017-05-25 17:00:26 +00:00
|
|
|
codegen(): Promise<string[]> {
|
2016-11-15 21:57:25 +00:00
|
|
|
return this.compiler
|
2017-05-23 20:40:50 +00:00
|
|
|
.analyzeModulesAsync(this.program.getSourceFiles().map(
|
2016-11-17 20:24:33 +00:00
|
|
|
sf => this.ngCompilerHost.getCanonicalFileName(sf.fileName)))
|
2017-06-05 18:34:25 +00:00
|
|
|
.then(analyzedModules => this.emit(analyzedModules));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
codegenSync(): string[] {
|
|
|
|
|
const analyzed = this.compiler.analyzeModulesSync(this.program.getSourceFiles().map(
|
|
|
|
|
sf => this.ngCompilerHost.getCanonicalFileName(sf.fileName)));
|
|
|
|
|
return this.emit(analyzed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private emit(analyzedModules: compiler.NgAnalyzedModules) {
|
|
|
|
|
const generatedModules = this.compiler.emitAllImpls(analyzedModules);
|
|
|
|
|
return generatedModules.map(generatedModule => {
|
|
|
|
|
const sourceFile = this.program.getSourceFile(generatedModule.srcFileUrl);
|
|
|
|
|
const emitPath = this.ngCompilerHost.calculateEmitPath(generatedModule.genFileUrl);
|
|
|
|
|
const source = generatedModule.source || compiler.toTypeScript(generatedModule, PREAMBLE);
|
|
|
|
|
this.host.writeFile(emitPath, source, false, () => {}, [sourceFile]);
|
|
|
|
|
return emitPath;
|
|
|
|
|
});
|
2016-04-29 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-08 23:38:52 +00:00
|
|
|
static create(
|
2016-08-12 21:45:36 +00:00
|
|
|
options: AngularCompilerOptions, cliOptions: NgcCliOptions, program: ts.Program,
|
2016-11-17 20:24:33 +00:00
|
|
|
tsCompilerHost: ts.CompilerHost, compilerHostContext?: CompilerHostContext,
|
|
|
|
|
ngCompilerHost?: CompilerHost): CodeGenerator {
|
|
|
|
|
if (!ngCompilerHost) {
|
2016-11-15 21:57:25 +00:00
|
|
|
const usePathMapping = !!options.rootDirs && options.rootDirs.length > 0;
|
2016-12-01 21:24:51 +00:00
|
|
|
const context = compilerHostContext || new ModuleResolutionHostAdapter(tsCompilerHost);
|
|
|
|
|
ngCompilerHost = usePathMapping ? new PathMappedCompilerHost(program, options, context) :
|
|
|
|
|
new CompilerHost(program, options, context);
|
2016-11-15 21:57:25 +00:00
|
|
|
}
|
2016-08-12 21:45:36 +00:00
|
|
|
let transContent: string = '';
|
2017-04-13 07:17:37 +00:00
|
|
|
if (cliOptions.i18nFile) {
|
|
|
|
|
if (!cliOptions.locale) {
|
2016-08-26 22:38:48 +00:00
|
|
|
throw new Error(
|
2017-04-13 07:17:37 +00:00
|
|
|
`The translation file (${cliOptions.i18nFile}) locale must be provided. Use the --locale option.`);
|
|
|
|
|
}
|
2017-07-07 17:22:52 +00:00
|
|
|
transContent = readFileSync(cliOptions.i18nFile, 'utf8');
|
2017-04-13 07:17:37 +00:00
|
|
|
}
|
2017-08-16 16:00:03 +00:00
|
|
|
let missingTranslation = compiler.core.MissingTranslationStrategy.Warning;
|
2017-04-13 07:17:37 +00:00
|
|
|
if (cliOptions.missingTranslation) {
|
|
|
|
|
switch (cliOptions.missingTranslation) {
|
|
|
|
|
case 'error':
|
2017-08-16 16:00:03 +00:00
|
|
|
missingTranslation = compiler.core.MissingTranslationStrategy.Error;
|
2017-04-13 07:17:37 +00:00
|
|
|
break;
|
|
|
|
|
case 'warning':
|
2017-08-16 16:00:03 +00:00
|
|
|
missingTranslation = compiler.core.MissingTranslationStrategy.Warning;
|
2017-04-13 07:17:37 +00:00
|
|
|
break;
|
|
|
|
|
case 'ignore':
|
2017-08-16 16:00:03 +00:00
|
|
|
missingTranslation = compiler.core.MissingTranslationStrategy.Ignore;
|
2017-04-13 07:17:37 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Unknown option for missingTranslation (${cliOptions.missingTranslation}). Use either error, warning or ignore.`);
|
2016-08-26 22:38:48 +00:00
|
|
|
}
|
2016-08-12 21:45:36 +00:00
|
|
|
}
|
2017-07-07 23:16:49 +00:00
|
|
|
if (!transContent) {
|
2017-08-16 16:00:03 +00:00
|
|
|
missingTranslation = compiler.core.MissingTranslationStrategy.Ignore;
|
2017-07-07 23:16:49 +00:00
|
|
|
}
|
2016-11-18 16:40:41 +00:00
|
|
|
const {compiler: aotCompiler} = compiler.createAotCompiler(ngCompilerHost, {
|
2016-11-15 19:13:20 +00:00
|
|
|
translations: transContent,
|
2017-07-18 19:52:48 +00:00
|
|
|
i18nFormat: cliOptions.i18nFormat || undefined,
|
|
|
|
|
locale: cliOptions.locale || undefined, missingTranslation,
|
2017-01-11 03:07:03 +00:00
|
|
|
enableLegacyTemplate: options.enableLegacyTemplate !== false,
|
2017-06-09 21:00:03 +00:00
|
|
|
enableSummariesForJit: options.enableSummariesForJit !== false,
|
2016-06-13 17:06:40 +00:00
|
|
|
});
|
2016-11-18 16:40:41 +00:00
|
|
|
return new CodeGenerator(options, program, tsCompilerHost, aotCompiler, ngCompilerHost);
|
2016-04-29 04:57:16 +00:00
|
|
|
}
|
|
|
|
|
}
|