Skip to content

Commit 38b71bc

Browse files
clydindgp1130
authored andcommitted
perf(@angular-devkit/build-angular): use esbuild in esbuild builder to downlevel native async/await
esbuild now allows specifying whether individual JavaScript features should be supported in addition to specifying the target JavaScript version for the output. This capability is now used to provide the native async/await downleveling that is required by Zone.js when using the experimental esbuild- based browser application builder. Since esbuild does not yet support downleveling async iteration or async generators, Babel is still used when either of these syntax features are detected. (cherry picked from commit 8f9cee3)
1 parent 344ef77 commit 38b71bc

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/angular_devkit/build_angular/src/builders/browser-esbuild/compiler-plugin.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ export function createCompilerPlugin(
341341
[
342342
angularApplicationPreset,
343343
{
344-
forceAsyncTransformation: data.includes('async'),
344+
forceAsyncTransformation: /for\s+await\s*\(|async\s+function\s*\*/.test(data),
345345
optimize: pluginOptions.advancedOptimizations && {},
346346
},
347347
],
@@ -388,7 +388,8 @@ export function createCompilerPlugin(
388388
linkerPluginCreator,
389389
},
390390
forceAsyncTransformation:
391-
!/[\\/][_f]?esm2015[\\/]/.test(args.path) && data.includes('async'),
391+
!/[\\/][_f]?esm2015[\\/]/.test(args.path) &&
392+
/for\s+await\s*\(|async\s+function\s*\*/.test(data),
392393
optimize: pluginOptions.advancedOptimizations && {
393394
looseEnums: angularPackage,
394395
pureTopLevel: angularPackage,

packages/angular_devkit/build_angular/src/builders/browser-esbuild/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,18 @@ async function bundleCode(
307307
entryNames: outputNames.bundles,
308308
assetNames: outputNames.media,
309309
target: 'es2020',
310+
supported: {
311+
// Native async/await is not supported with Zone.js. Disabling support here will cause
312+
// esbuild to downlevel async/await to a Zone.js supported form.
313+
'async-await': false,
314+
// Zone.js also does not support async generators or async iterators. However, esbuild does
315+
// not currently support downleveling either of them. Instead babel is used within the JS/TS
316+
// loader to perform the downlevel transformation. They are both disabled here to allow
317+
// esbuild to handle them in the future if support is ever added.
318+
// NOTE: If esbuild adds support in the future, the babel support for these can be disabled.
319+
'async-generator': false,
320+
'for-await': false,
321+
},
310322
mainFields: ['es2020', 'browser', 'module', 'main'],
311323
conditions: ['es2020', 'es2015', 'module'],
312324
resolveExtensions: ['.ts', '.tsx', '.mjs', '.js'],

0 commit comments

Comments
 (0)