Skip to content

Commit

Permalink
feat(@ngtools/webpack): add enableSummariesForJit option
Browse files Browse the repository at this point in the history
create transformer replace initTestEnvironment
  • Loading branch information
Quramy committed Oct 11, 2017
1 parent fe8e14e commit 4b29400
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/@ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
TransformOperation,
makeTransform,
replaceBootstrap,
replaceInitTestEnv,
exportNgFactory,
exportLazyModuleMap,
registerLocaleData,
Expand Down Expand Up @@ -69,6 +70,7 @@ export interface AngularCompilerPluginOptions {
exclude?: string | string[];
include?: string[];
compilerOptions?: ts.CompilerOptions;
enableSummariesForJit?: boolean;
}

export enum PLATFORM {
Expand Down Expand Up @@ -108,7 +110,7 @@ export class AngularCompilerPlugin implements Tapable {

constructor(options: AngularCompilerPluginOptions) {
CompilerCliIsSupported();
this._options = Object.assign({}, options);
this._options = Object.assign({ enableSummariesForJit: true }, options);
this._setupOptions(this._options);
}

Expand Down Expand Up @@ -242,6 +244,14 @@ export class AngularCompilerPlugin implements Tapable {
this._JitMode = options.skipCodeGeneration;
}

// Set ngsummaries option if AOT mode.
if (!this._JitMode || options.enableSummariesForJit !== undefined) {
this._angularCompilerOptions.enableSummariesForJit = options.enableSummariesForJit;
// If module option is 'commonjs', `ReferenceError: AppModuleNgSummary is not defined`
// is thrown at runtime. So overwrite it with 'ES2015' to suppres this error.
this._angularCompilerOptions.module = ts.ModuleKind.ES2015;
}

// Process i18n options.
if (options.hasOwnProperty('i18nInFile')) {
this._angularCompilerOptions.i18nInFile = options.i18nInFile;
Expand Down Expand Up @@ -685,6 +695,10 @@ export class AngularCompilerPlugin implements Tapable {
transformOps.push(...replaceBootstrap(sf, this.entryModule));
}

if (this.options.enableSummariesForJit) {
transformOps.push(...replaceInitTestEnv(sf, [this.entryModule]));
}

// If we have a locale, auto import the locale data file.
if (this._angularCompilerOptions.i18nInLocale) {
transformOps.push(...registerLocaleData(
Expand Down
1 change: 1 addition & 0 deletions packages/@ngtools/webpack/src/ngtools_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface CompilerOptions extends ts.CompilerOptions {
i18nInFile?: string;
i18nInMissingTranslations?: 'error' | 'warning' | 'ignore';
preserveWhitespaces?: boolean;
enableSummariesForJit?: boolean;
}
export interface CompilerHost extends ts.CompilerHost {
moduleNameToFileName(moduleName: string, containingFile?: string): string | null;
Expand Down
1 change: 1 addition & 0 deletions packages/@ngtools/webpack/src/transformers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './make_transform';
export * from './insert_import';
export * from './remove_import';
export * from './replace_bootstrap';
export * from './replace_init_test_env';
export * from './export_ngfactory';
export * from './export_lazy_module_map';
export * from './register_locale_data';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as ts from 'typescript';
import { oneLine, stripIndent } from 'common-tags';
import { transformTypescript } from './ast_helpers';
import { replaceInitTestEnv } from './replace_init_test_env';

describe('@ngtools/webpack transformers', () => {
describe('replace_init_test_env', () => {
it('should replace initTestEnvironment', () => {
const input = stripIndent`
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
getTestBed().initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
`;
const output = stripIndent`
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
import { AppModuleNgSummary } from "./app/app.module.ngsummary";
getTestBed().initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting(), () => [AppModuleNgSummary]);
`;

const transformOpsCb = (sourceFile: ts.SourceFile) =>
replaceInitTestEnv(sourceFile, [{ className: 'AppModule', path: './app/app.module' }]);
const result = transformTypescript(input, transformOpsCb);

expect(oneLine`${result}`).toEqual(oneLine`${output}`);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as ts from 'typescript';

import { findAstNodes } from './ast_helpers';
import { TransformOperation, AddNodeOperation } from './make_transform';
import { insertImport } from './insert_import';

export function replaceInitTestEnv(
sourceFile: ts.SourceFile,
modules: { path: string, className: string }[]
): TransformOperation[] {
if (modules.length === 0) {
return;
}

const ops: TransformOperation[] = [];

// Find the initTestEnvironment calls.
const initTestEnvIdentifiers = findAstNodes<ts.Identifier>(null, sourceFile,
ts.SyntaxKind.Identifier, true)
.filter(identifier => identifier.getText() === 'initTestEnvironment');

if (initTestEnvIdentifiers.length === 0) {
return [];
}

const moduleNgSummaries = modules.map((mod) => {
return {
className: mod.className + 'NgSummary',
path: mod.path + '.ngsummary',
};
});

moduleNgSummaries.forEach((moduleNgSummary) =>
ops.push(...insertImport(sourceFile, moduleNgSummary.className, moduleNgSummary.path)));

initTestEnvIdentifiers.forEach((initTestEnvIdentifier) => {
if (initTestEnvIdentifier.parent.parent.kind !== ts.SyntaxKind.CallExpression) {
return;
}
const initTestEnvCall = initTestEnvIdentifier.parent.parent as ts.CallExpression;
if (initTestEnvCall.arguments.length !== 2) {
return;
}
const lastArgument = initTestEnvCall.arguments[initTestEnvCall.arguments.length - 1];
const ngSummariesArray = ts.createArrayLiteral(moduleNgSummaries.map((summary) =>
ts.createIdentifier(summary.className), true));
const aotSummariesFn = ts.createArrowFunction([], [], [], undefined,
ts.createToken(ts.SyntaxKind.EqualsGreaterThanToken), ngSummariesArray);
ops.push(new AddNodeOperation(sourceFile, lastArgument, null, aotSummariesFn));
});

return ops;
}

0 comments on commit 4b29400

Please sign in to comment.