2016-12-13 20:44:52 -05: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-12-13 20:35:06 -05:00
|
|
|
/**
|
|
|
|
* This is a private API for the ngtools toolkit.
|
|
|
|
*
|
|
|
|
* This API should be stable for NG 2. It can be removed in NG 4..., but should be replaced by
|
|
|
|
* something else.
|
|
|
|
*/
|
|
|
|
|
2016-12-15 12:12:40 -05:00
|
|
|
import {AotCompilerHost, AotSummaryResolver, StaticReflector, StaticSymbolCache, StaticSymbolResolver} from '@angular/compiler';
|
2016-12-13 20:35:06 -05:00
|
|
|
import {AngularCompilerOptions, NgcCliOptions} from '@angular/tsc-wrapped';
|
2016-12-13 20:44:52 -05:00
|
|
|
import * as ts from 'typescript';
|
2016-12-13 20:35:06 -05:00
|
|
|
|
|
|
|
import {CodeGenerator} from './codegen';
|
|
|
|
import {CompilerHost, CompilerHostContext, ModuleResolutionHostAdapter} from './compiler_host';
|
2016-12-19 14:56:10 -05:00
|
|
|
import {Extractor} from './extractor';
|
2016-12-13 20:35:06 -05:00
|
|
|
import {listLazyRoutesOfModule} from './ngtools_impl';
|
|
|
|
import {PathMappedCompilerHost} from './path_mapped_compiler_host';
|
|
|
|
|
|
|
|
export interface NgTools_InternalApi_NG2_CodeGen_Options {
|
|
|
|
basePath: string;
|
|
|
|
compilerOptions: ts.CompilerOptions;
|
|
|
|
program: ts.Program;
|
|
|
|
host: ts.CompilerHost;
|
|
|
|
|
|
|
|
angularCompilerOptions: AngularCompilerOptions;
|
|
|
|
|
|
|
|
// i18n options.
|
2017-03-29 12:34:45 -04:00
|
|
|
i18nFormat?: string;
|
|
|
|
i18nFile?: string;
|
|
|
|
locale?: string;
|
2017-04-13 03:17:37 -04:00
|
|
|
missingTranslation?: string;
|
2016-12-13 20:35:06 -05:00
|
|
|
|
|
|
|
readResource: (fileName: string) => Promise<string>;
|
|
|
|
|
|
|
|
// Every new property under this line should be optional.
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface NgTools_InternalApi_NG2_ListLazyRoutes_Options {
|
|
|
|
program: ts.Program;
|
|
|
|
host: ts.CompilerHost;
|
|
|
|
angularCompilerOptions: AngularCompilerOptions;
|
|
|
|
entryModule: string;
|
|
|
|
|
|
|
|
// Every new property under this line should be optional.
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:44:52 -05:00
|
|
|
export interface NgTools_InternalApi_NG_2_LazyRouteMap { [route: string]: string; }
|
2016-12-13 20:35:06 -05:00
|
|
|
|
2016-12-19 14:56:10 -05:00
|
|
|
export interface NgTools_InternalApi_NG2_ExtractI18n_Options {
|
|
|
|
basePath: string;
|
|
|
|
compilerOptions: ts.CompilerOptions;
|
|
|
|
program: ts.Program;
|
|
|
|
host: ts.CompilerHost;
|
|
|
|
angularCompilerOptions: AngularCompilerOptions;
|
2017-03-29 12:34:45 -04:00
|
|
|
i18nFormat?: string;
|
2016-12-19 14:56:10 -05:00
|
|
|
readResource: (fileName: string) => Promise<string>;
|
|
|
|
// Every new property under this line should be optional.
|
2017-02-16 11:03:18 -05:00
|
|
|
locale?: string;
|
2017-02-15 12:50:03 -05:00
|
|
|
outFile?: string;
|
2016-12-19 14:56:10 -05:00
|
|
|
}
|
2016-12-13 20:35:06 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A ModuleResolutionHostAdapter that overrides the readResource() method with the one
|
|
|
|
* passed in the interface.
|
|
|
|
*/
|
|
|
|
class CustomLoaderModuleResolutionHostAdapter extends ModuleResolutionHostAdapter {
|
2016-12-13 20:44:52 -05:00
|
|
|
constructor(
|
|
|
|
private _readResource: (path: string) => Promise<string>, host: ts.ModuleResolutionHost) {
|
2016-12-13 20:35:06 -05:00
|
|
|
super(host);
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:44:52 -05:00
|
|
|
readResource(path: string) { return this._readResource(path); }
|
2016-12-13 20:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export class NgTools_InternalApi_NG_2 {
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-05-25 13:00:26 -04:00
|
|
|
static codeGen(options: NgTools_InternalApi_NG2_CodeGen_Options): Promise<any> {
|
2016-12-13 20:44:52 -05:00
|
|
|
const hostContext: CompilerHostContext =
|
|
|
|
new CustomLoaderModuleResolutionHostAdapter(options.readResource, options.host);
|
2017-06-09 17:50:57 -04:00
|
|
|
|
2016-12-13 20:35:06 -05:00
|
|
|
const cliOptions: NgcCliOptions = {
|
2017-03-24 12:59:58 -04:00
|
|
|
i18nFormat: options.i18nFormat !,
|
|
|
|
i18nFile: options.i18nFile !,
|
|
|
|
locale: options.locale !,
|
2017-04-13 03:17:37 -04:00
|
|
|
missingTranslation: options.missingTranslation !,
|
2016-12-13 20:35:06 -05:00
|
|
|
basePath: options.basePath
|
|
|
|
};
|
2017-06-09 17:00:03 -04:00
|
|
|
const ngOptions = options.angularCompilerOptions;
|
|
|
|
if (ngOptions.enableSummariesForJit === undefined) {
|
|
|
|
// default to false
|
|
|
|
ngOptions.enableSummariesForJit = false;
|
|
|
|
}
|
2016-12-13 20:35:06 -05:00
|
|
|
|
|
|
|
// Create the Code Generator.
|
2017-06-09 17:00:03 -04:00
|
|
|
const codeGenerator =
|
|
|
|
CodeGenerator.create(ngOptions, cliOptions, options.program, options.host, hostContext);
|
2016-12-13 20:35:06 -05:00
|
|
|
|
|
|
|
return codeGenerator.codegen();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2016-12-13 20:44:52 -05:00
|
|
|
static listLazyRoutes(options: NgTools_InternalApi_NG2_ListLazyRoutes_Options):
|
|
|
|
NgTools_InternalApi_NG_2_LazyRouteMap {
|
2016-12-13 20:35:06 -05:00
|
|
|
const angularCompilerOptions = options.angularCompilerOptions;
|
|
|
|
const program = options.program;
|
|
|
|
|
|
|
|
const moduleResolutionHost = new ModuleResolutionHostAdapter(options.host);
|
2016-12-13 20:44:52 -05:00
|
|
|
const usePathMapping =
|
|
|
|
!!angularCompilerOptions.rootDirs && angularCompilerOptions.rootDirs.length > 0;
|
|
|
|
const ngCompilerHost: AotCompilerHost = usePathMapping ?
|
|
|
|
new PathMappedCompilerHost(program, angularCompilerOptions, moduleResolutionHost) :
|
|
|
|
new CompilerHost(program, angularCompilerOptions, moduleResolutionHost);
|
2016-12-13 20:35:06 -05:00
|
|
|
|
2016-12-15 12:12:40 -05:00
|
|
|
const symbolCache = new StaticSymbolCache();
|
|
|
|
const summaryResolver = new AotSummaryResolver(ngCompilerHost, symbolCache);
|
|
|
|
const symbolResolver = new StaticSymbolResolver(ngCompilerHost, symbolCache, summaryResolver);
|
2017-03-30 17:51:29 -04:00
|
|
|
const staticReflector = new StaticReflector(summaryResolver, symbolResolver);
|
2016-12-13 20:35:06 -05:00
|
|
|
const routeMap = listLazyRoutesOfModule(options.entryModule, ngCompilerHost, staticReflector);
|
|
|
|
|
2016-12-13 20:44:52 -05:00
|
|
|
return Object.keys(routeMap).reduce(
|
|
|
|
(acc: NgTools_InternalApi_NG_2_LazyRouteMap, route: string) => {
|
|
|
|
acc[route] = routeMap[route].absoluteFilePath;
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{});
|
2016-12-13 20:35:06 -05:00
|
|
|
}
|
2016-12-19 14:56:10 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-05-25 13:00:26 -04:00
|
|
|
static extractI18n(options: NgTools_InternalApi_NG2_ExtractI18n_Options): Promise<any> {
|
2016-12-19 14:56:10 -05:00
|
|
|
const hostContext: CompilerHostContext =
|
|
|
|
new CustomLoaderModuleResolutionHostAdapter(options.readResource, options.host);
|
|
|
|
|
|
|
|
// Create the i18n extractor.
|
2017-02-16 11:03:18 -05:00
|
|
|
const locale = options.locale || null;
|
2016-12-19 14:56:10 -05:00
|
|
|
const extractor = Extractor.create(
|
2017-02-16 11:03:18 -05:00
|
|
|
options.angularCompilerOptions, options.program, options.host, locale, hostContext);
|
2016-12-19 14:56:10 -05:00
|
|
|
|
2017-03-24 12:59:58 -04:00
|
|
|
return extractor.extract(options.i18nFormat !, options.outFile || null);
|
2016-12-19 14:56:10 -05:00
|
|
|
}
|
2016-12-13 20:35:06 -05:00
|
|
|
}
|