-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@ngtools/webpack): add enableSummariesForJit option
create transformer replace initTestEnvironment
- Loading branch information
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/@ngtools/webpack/src/transformers/replace_init_test_env.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
packages/@ngtools/webpack/src/transformers/replace_init_test_env.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |