Skip to content

Commit e2e8d57

Browse files
clydinfilipesilva
authored andcommitted
feat(@angular-devkit/build-angular): support targeting ES2017 with Zone.js
This change causes native async functions to be downleveled when an application targets ES2017 within its TypeScript configuration. Any source file that contains the async keyword will be processed including libraries. Since Zone.js does not support native async, this processing allows Zone.js to function with an ES2017 target.
1 parent 5cdf4cc commit e2e8d57

File tree

7 files changed

+110
-8
lines changed

7 files changed

+110
-8
lines changed

packages/angular_devkit/build_angular/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"@angular-devkit/core": "0.0.0",
1414
"@babel/core": "7.12.10",
1515
"@babel/generator": "7.12.11",
16-
"@babel/plugin-transform-runtime": "7.12.10",
16+
"@babel/plugin-transform-async-to-generator": "7.12.1",
17+
"@babel/plugin-transform-runtime": "7.12.10",
1718
"@babel/preset-env": "7.12.11",
1819
"@babel/runtime": "7.12.5",
1920
"@babel/template": "7.12.7",

packages/angular_devkit/build_angular/src/babel/presets/application.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ export default function (api: unknown, options: ApplicationPresetOptions) {
170170

171171
if (options.forceAsyncTransformation) {
172172
// Always transform async/await to support Zone.js
173-
// tslint:disable-next-line: no-implicit-dependencies
174173
plugins.push(require('@babel/plugin-transform-async-to-generator').default);
175174
needRuntimeTransform = true;
176175
}

packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { custom } from 'babel-loader';
99
import { ScriptTarget } from 'typescript';
1010

1111
interface AngularCustomOptions {
12+
forceAsyncTransformation: boolean;
1213
forceES5: boolean;
1314
shouldLink: boolean;
1415
}
@@ -27,7 +28,8 @@ async function checkLinking(
2728
source: string,
2829
): Promise<{ hasLinkerSupport?: boolean; requiresLinking: boolean }> {
2930
// @angular/core and @angular/compiler will cause false positives
30-
if (/[\\\/]@angular[\\\/](?:compiler|core)/.test(path)) {
31+
// Also, TypeScript files do not require linking
32+
if (/[\\\/]@angular[\\\/](?:compiler|core)|\.tsx?$/.test(path)) {
3133
return { requiresLinking: false };
3234
}
3335

@@ -82,11 +84,15 @@ export default custom<AngularCustomOptions>(() => {
8284

8385
// Analyze for ES target processing
8486
let forceES5 = false;
87+
let forceAsyncTransformation = false;
8588
const esTarget = scriptTarget as ScriptTarget;
8689
if (esTarget < ScriptTarget.ES2015) {
87-
forceES5 = true;
90+
// TypeScript files will have already been downlevelled
91+
forceES5 = !/\.tsx?$/.test(this.resourcePath);
92+
} else if (esTarget >= ScriptTarget.ES2017) {
93+
forceAsyncTransformation = source.includes('async');
8894
}
89-
shouldProcess ||= forceES5;
95+
shouldProcess ||= forceAsyncTransformation || forceES5;
9096

9197
// Add provided loader options to default base options
9298
const options: Record<string, unknown> = {
@@ -100,7 +106,7 @@ export default custom<AngularCustomOptions>(() => {
100106
options.ignore = [() => true];
101107
}
102108

103-
return { custom: { forceES5, shouldLink }, loader: options };
109+
return { custom: { forceAsyncTransformation, forceES5, shouldLink }, loader: options };
104110
},
105111
config(configuration, { customOptions }) {
106112
return {
@@ -112,6 +118,7 @@ export default custom<AngularCustomOptions>(() => {
112118
{
113119
angularLinker: customOptions.shouldLink,
114120
forceES5: customOptions.forceES5,
121+
forceAsyncTransformation: customOptions.forceAsyncTransformation,
115122
diagnosticReporter: (type, message) => {
116123
switch (type) {
117124
case 'error':
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { buildWebpackBrowser } from '../../index';
9+
import { BASE_OPTIONS, BROWSER_BUILDER_INFO, describeBuilder } from '../setup';
10+
11+
describeBuilder(buildWebpackBrowser, BROWSER_BUILDER_INFO, (harness) => {
12+
describe('Behavior: "TypeScript Configuration - target"', () => {
13+
it('downlevels async functions when targetting ES2017', async () => {
14+
// Set TypeScript configuration target to ES2017 to enable native async
15+
await harness.modifyFile('src/tsconfig.app.json', (content) => {
16+
const tsconfig = JSON.parse(content);
17+
if (!tsconfig.compilerOptions) {
18+
tsconfig.compilerOptions = {};
19+
}
20+
tsconfig.compilerOptions.target = 'es2017';
21+
22+
return JSON.stringify(tsconfig);
23+
});
24+
25+
// Add a JavaScript file with async code
26+
await harness.writeFile(
27+
'src/async-test.js',
28+
'async function testJs() { console.log("from-async-js-function"); }',
29+
);
30+
31+
// Add an async function to the project as well as JavaScript file
32+
await harness.modifyFile(
33+
'src/main.ts',
34+
(content) =>
35+
'import "./async-test";\n' +
36+
content +
37+
`\nasync function testApp(): Promise<void> { console.log("from-async-app-function"); }`,
38+
);
39+
40+
harness.useTarget('build', {
41+
...BASE_OPTIONS,
42+
});
43+
44+
const { result, logs } = await harness.executeOnce();
45+
46+
expect(result?.success).toBe(true);
47+
expect(logs).not.toContain(
48+
jasmine.objectContaining({
49+
message: jasmine.stringMatching('Zone.js does not support native async/await in ES2017+'),
50+
}),
51+
);
52+
53+
harness.expectFile('dist/main.js').content.not.toMatch(/\sasync\s/);
54+
harness.expectFile('dist/main.js').content.toContain('"from-async-app-function"');
55+
harness.expectFile('dist/main.js').content.toContain('"from-async-js-function"');
56+
});
57+
58+
it('downlevels async functions when targetting greater than ES2017', async () => {
59+
// Set TypeScript configuration target greater than ES2017 to enable native async
60+
await harness.modifyFile('src/tsconfig.app.json', (content) => {
61+
const tsconfig = JSON.parse(content);
62+
if (!tsconfig.compilerOptions) {
63+
tsconfig.compilerOptions = {};
64+
}
65+
tsconfig.compilerOptions.target = 'es2020';
66+
67+
return JSON.stringify(tsconfig);
68+
});
69+
70+
// Add an async function to the project
71+
await harness.writeFile(
72+
'src/main.ts',
73+
'async function test(): Promise<void> { console.log("from-async-function"); }',
74+
);
75+
76+
harness.useTarget('build', {
77+
...BASE_OPTIONS,
78+
});
79+
80+
const { result, logs } = await harness.executeOnce();
81+
82+
expect(result?.success).toBe(true);
83+
expect(logs).not.toContain(
84+
jasmine.objectContaining({
85+
message: jasmine.stringMatching('Zone.js does not support native async/await in ES2017+'),
86+
}),
87+
);
88+
89+
harness.expectFile('dist/main.js').content.not.toMatch(/\sasync\s/);
90+
harness.expectFile('dist/main.js').content.toContain('"from-async-function"');
91+
});
92+
});
93+
});

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
544544
sideEffects: true,
545545
},
546546
{
547-
test: /\.[cm]?js$/,
547+
test: /\.[cm]?js$|\.tsx?$/,
548548
exclude: [/[\/\\](?:core-js|\@babel|tslib|web-animations-js)[\/\\]/, /(ngfactory|ngstyle)\.js$/],
549549
use: [
550550
{

packages/angular_devkit/build_angular/src/webpack/configs/typescript.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function createIvyPlugin(
7979
compilerOptions,
8080
fileReplacements,
8181
emitNgModuleScope: !optimize,
82+
suppressZoneJsIncompatibilityWarning: true,
8283
});
8384
}
8485

@@ -159,6 +160,7 @@ function _createAotPlugin(
159160
directTemplateLoading: true,
160161
...options,
161162
compilerOptions,
163+
suppressZoneJsIncompatibilityWarning: true,
162164
};
163165

164166
pluginOptions = _pluginOptionsOverrides(buildOptions, pluginOptions);

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@
645645
dependencies:
646646
"@babel/helper-plugin-utils" "^7.10.4"
647647

648-
"@babel/plugin-transform-async-to-generator@^7.12.1":
648+
"@babel/plugin-transform-async-to-generator@7.12.1", "@babel/plugin-transform-async-to-generator@^7.12.1":
649649
version "7.12.1"
650650
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-async-to-generator/-/plugin-transform-async-to-generator-7.12.1.tgz#3849a49cc2a22e9743cbd6b52926d30337229af1"
651651
integrity sha512-SDtqoEcarK1DFlRJ1hHRY5HvJUj5kX4qmtpMAm2QnhOlyuMC4TMdCRgW6WXpv93rZeYNeLP22y8Aq2dbcDRM1A==

0 commit comments

Comments
 (0)