Skip to content

Commit 2dcde2e

Browse files
revert: restore output.environment.topLevelAwait after the patch release
Reverts #21881, which held back the feature half of #21867 so 5.110.3 could ship as a patch. That release is out, so the option and the startup `await` of an async ESM entry come back, together with their config cases, snapshots and conformance classifier, and the five test262 top-level-await rejection cases leave `knownBugs` again. The changeset the hold-back edited was consumed by 5.110.3, so this adds a new minor one instead. `added: 5.111.0` still matches: main is 5.110.3 with one patch changeset pending, and this minor takes the next release to 5.111.0. `schemas/WebpackOptions.check.js` is minified onto one line, so git could not merge it against #21886's schema change. It was rebuilt by applying the generator's own four insertions for this option to main's validator, then checked against the option and its neighbours.
1 parent 84fd011 commit 2dcde2e

32 files changed

Lines changed: 283 additions & 28 deletions

File tree

‎.changeset/010-top-level-await.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add `output.environment.topLevelAwait` and await async ESM entries.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,11 @@ export interface Environment {
32303230
* The environment supports template literals.
32313231
*/
32323232
templateLiteral?: boolean;
3233+
/**
3234+
* The environment supports top-level await ('await x' at the top level of a module).
3235+
* @since 5.111.0
3236+
*/
3237+
topLevelAwait?: boolean;
32333238
}
32343239
/**
32353240
* A custom resource-hint `<link>` for `output.html.resourceHints`. Exactly one of `href` / `chunk` / `entry` names the target.

‎lib/RuntimeTemplate.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ class RuntimeTemplate {
569569
return this.outputOptions.environment.generator;
570570
}
571571

572+
supportsTopLevelAwait() {
573+
return this.outputOptions.environment.topLevelAwait;
574+
}
575+
572576
supportsOptionalChaining() {
573577
return this.outputOptions.environment.optionalChaining;
574578
}

‎lib/config/browserslistTargetHandler.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,25 @@ const resolve = (browsers) => {
428428
// kaios: Not supported
429429
node: [16, 9]
430430
}),
431+
topLevelAwait: rawChecker({
432+
chrome: 89,
433+
and_chr: 89,
434+
edge: 89,
435+
firefox: 89,
436+
and_ff: 89,
437+
// ie: Not supported,
438+
opera: 75,
439+
op_mob: 63,
440+
safari: 15,
441+
ios_saf: 15,
442+
samsung: 15,
443+
android: 89,
444+
// and_qq: Not supported
445+
// baidu: Not supported
446+
// and_uc: Not supported
447+
// kaios: Not supported
448+
node: [14, 8]
449+
}),
431450
spread: rawChecker({
432451
chrome: 60,
433452
and_chr: 60,

‎lib/config/defaults.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,8 +1748,12 @@ const applyOutputDefaults = (
17481748
"generator",
17491749
() => tp && optimistic(/** @type {boolean | undefined} */ (tp.generator))
17501750
);
1751-
// No `topLevelAwait` here: the option is held back from the patch release,
1752-
// so nothing derives it from `tp` yet. Reapply by reverting #21881.
1751+
F(
1752+
environment,
1753+
"topLevelAwait",
1754+
() =>
1755+
tp && optimistic(/** @type {boolean | undefined} */ (tp.topLevelAwait))
1756+
);
17531757
F(
17541758
environment,
17551759
"forOf",

‎lib/config/target.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ const getDefaultTarget = (context) => {
8282
* @property {boolean | null} templateLiteral template literal is available
8383
* @property {boolean | null} asyncFunction async functions and await are available
8484
* @property {boolean | null} generator generator functions and yield are available
85+
* @property {boolean | null} topLevelAwait top-level await is available
8586
* @property {(boolean | null)=} deferImport deferred evaluation (`import defer`, `import.defer()`) is available (optional; absent means "assume unavailable")
8687
* @property {(boolean | null)=} sourceImport source phase imports (`import source`, `import.source()`) are available (optional; absent means "assume unavailable")
8788
*/
@@ -269,6 +270,8 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
269270
arrowFunction: v(6),
270271
asyncFunction: v(7, 6),
271272
generator: v(4),
273+
// v14.8.0
274+
topLevelAwait: v(14, 8),
272275
forOf: v(5),
273276
destructuring: v(6),
274277
bigIntLiteral: v(10, 4),
@@ -329,6 +332,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
329332
templateLiteral: true,
330333
asyncFunction: true,
331334
generator: true,
335+
topLevelAwait: true,
332336
// import defer - v2.8.0
333337
deferImport: v(2, 8),
334338
// import source - v2.6.0
@@ -380,6 +384,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
380384
arrowFunction: true,
381385
asyncFunction: true,
382386
generator: true,
387+
topLevelAwait: true,
383388
forOf: true,
384389
destructuring: true,
385390
bigIntLiteral: true,
@@ -437,6 +442,8 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
437442
arrowFunction: v(1, 1),
438443
asyncFunction: v(1, 7),
439444
generator: v(1, 1),
445+
// 12.0.0 - Chromium 89
446+
topLevelAwait: v(12),
440447
forOf: v(0, 36),
441448
destructuring: v(1, 1),
442449
bigIntLiteral: v(4),
@@ -486,6 +493,8 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
486493
arrowFunction: v(0, 15),
487494
asyncFunction: v(0, 21),
488495
generator: v(0, 15),
496+
// 0.52.0 - Chromium 89
497+
topLevelAwait: v(0, 52),
489498
forOf: v(0, 13),
490499
destructuring: v(0, 15),
491500
bigIntLiteral: v(0, 32),
@@ -515,6 +524,7 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
515524
destructuring: v >= 2015,
516525
module: v >= 2015,
517526
asyncFunction: v >= 2017,
527+
topLevelAwait: v >= 2022,
518528
generator: v >= 2015,
519529
globalThis: v >= 2020,
520530
symbol: v >= 2015,

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,8 +1680,28 @@ class JavascriptModulesPlugin {
16801680
continue;
16811681
}
16821682
}
1683-
// An async entry is not awaited here: that needs top-level `await`,
1684-
// gated on an option held back from the patch release (revert #21881).
1683+
const entryOptions = chunk.getEntryOptions();
1684+
const library =
1685+
entryOptions && entryOptions.library !== undefined
1686+
? entryOptions.library
1687+
: runtimeTemplate.outputOptions.library;
1688+
// A module library awaits the entry itself, in `ModuleLibraryPlugin`.
1689+
const awaitedByLibrary =
1690+
library !== undefined && library.type === "module";
1691+
// `await` is only legal at the top level of an ESM chunk, never in
1692+
// the startup function rendered below, and only where the target
1693+
// can parse it (`target: "node14"` reads ESM but not top-level await).
1694+
const canAwaitEntry =
1695+
runtimeTemplate.isModule() &&
1696+
runtimeTemplate.supportsTopLevelAwait() &&
1697+
!awaitedByLibrary &&
1698+
!runtimeRequirements.has(RuntimeGlobals.startup) &&
1699+
!(
1700+
runtimeRequirements.has(RuntimeGlobals.startupOnlyBefore) &&
1701+
runtimeRequirements.has(RuntimeGlobals.startupOnlyAfter)
1702+
);
1703+
/** @type {string[]} */
1704+
const awaitedEntries = [];
16851705
let i = jsEntries.length;
16861706
for (const [entryModule, entrypoint] of jsEntries) {
16871707
const chunks =
@@ -1808,11 +1828,19 @@ class JavascriptModulesPlugin {
18081828
)})`
18091829
);
18101830
} else if (useRequire) {
1831+
// An async entry nobody awaits settles after the bundle did,
1832+
// which orphans its rejection.
1833+
const awaitEntry =
1834+
canAwaitEntry && moduleGraph.isAsync(entryModule);
1835+
const target =
1836+
i === 0
1837+
? RuntimeGlobals.exports
1838+
: `${RuntimeGlobals.exports}_${i}`;
1839+
const needsDecl = i === 0 || awaitEntry;
18111840
buf2.push(
1812-
`${i === 0 ? `${exportsDecl} ${RuntimeGlobals.exports} = ` : ""}${
1813-
RuntimeGlobals.require
1814-
}(${moduleIdExpr});`
1841+
`${needsDecl ? `${exportsDecl} ${target} = ` : ""}${RuntimeGlobals.require}(${moduleIdExpr});`
18151842
);
1843+
if (awaitEntry) awaitedEntries.push(target);
18161844
} else {
18171845
if (i === 0) {
18181846
buf2.push(`${exportsDecl} ${RuntimeGlobals.exports} = {};`);
@@ -1855,6 +1883,14 @@ class JavascriptModulesPlugin {
18551883
`${RuntimeGlobals.exports} = ${RuntimeGlobals.onChunksLoaded}(${RuntimeGlobals.exports});`
18561884
);
18571885
}
1886+
// Several need one `Promise.all`, so an entry rejecting first still
1887+
// leaves the others observed. A single one has nothing to orphan.
1888+
if (awaitedEntries.length === 1) {
1889+
buf2.push(`${awaitedEntries[0]} = await ${awaitedEntries[0]};`);
1890+
} else if (awaitedEntries.length > 1) {
1891+
const list = awaitedEntries.join(", ");
1892+
buf2.push(`[${list}] = await Promise.all([${list}]);`);
1893+
}
18581894
if (
18591895
runtimeRequirements.has(RuntimeGlobals.startup) ||
18601896
(runtimeRequirements.has(RuntimeGlobals.startupOnlyBefore) &&

‎schemas/WebpackOptions.check.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎schemas/WebpackOptions.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@
12391239
"templateLiteral": {
12401240
"description": "The environment supports template literals.",
12411241
"type": "boolean"
1242+
},
1243+
"topLevelAwait": {
1244+
"description": "The environment supports top-level await ('await x' at the top level of a module).",
1245+
"type": "boolean",
1246+
"added": "5.111.0"
12421247
}
12431248
}
12441249
},

‎test/Defaults.unittest.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ describe("snapshots", () => {
205205
"spread": true,
206206
"symbol": true,
207207
"templateLiteral": true,
208+
"topLevelAwait": true,
208209
},
209210
"target": "web",
210211
},
@@ -755,6 +756,7 @@ describe("snapshots", () => {
755756
"spread": true,
756757
"symbol": true,
757758
"templateLiteral": true,
759+
"topLevelAwait": true,
758760
},
759761
"filename": "[name].js",
760762
"globalObject": "self",
@@ -4010,6 +4012,9 @@ describe("snapshots", () => {
40104012
+ "nodePrefixForCoreModules": false,
40114013
+ "optionalChaining": false,
40124014
@@ ... @@
4015+
- "topLevelAwait": true,
4016+
+ "topLevelAwait": false,
4017+
@@ ... @@
40134018
- "target": "web",
40144019
+ "target": undefined,
40154020
@@ ... @@
@@ -4057,6 +4062,9 @@ describe("snapshots", () => {
40574062
+ "nodePrefixForCoreModules": false,
40584063
+ "optionalChaining": false,
40594064
@@ ... @@
4065+
- "topLevelAwait": true,
4066+
+ "topLevelAwait": false,
4067+
@@ ... @@
40604068
- "globalObject": "self",
40614069
+ "globalObject": "typeof self !== \\"undefined\\" ? self : global",
40624070
@@ ... @@
@@ -4194,6 +4202,9 @@ describe("snapshots", () => {
41944202
+ "nodePrefixForCoreModules": false,
41954203
+ "optionalChaining": false,
41964204
@@ ... @@
4205+
- "topLevelAwait": true,
4206+
+ "topLevelAwait": false,
4207+
@@ ... @@
41974208
- "target": "web",
41984209
+ "target": undefined,
41994210
@@ ... @@
@@ -4241,6 +4252,9 @@ describe("snapshots", () => {
42414252
+ "nodePrefixForCoreModules": false,
42424253
+ "optionalChaining": false,
42434254
@@ ... @@
4255+
- "topLevelAwait": true,
4256+
+ "topLevelAwait": false,
4257+
@@ ... @@
42444258
- "globalObject": "self",
42454259
+ "globalObject": "typeof self !== \\"undefined\\" ? self : global",
42464260
@@ ... @@
@@ -4888,9 +4902,11 @@ describe("snapshots", () => {
48884902
- "spread": true,
48894903
- "symbol": true,
48904904
- "templateLiteral": true,
4905+
- "topLevelAwait": true,
48914906
+ "spread": false,
48924907
+ "symbol": false,
48934908
+ "templateLiteral": false,
4909+
+ "topLevelAwait": false,
48944910
@@ ... @@
48954911
- "chunkLoadingGlobal": "webpackChunkwebpack",
48964912
+ "chunkLoadingGlobal": "webpackChunkbrowserslist_test",
@@ -4944,9 +4960,11 @@ describe("snapshots", () => {
49444960
- "spread": true,
49454961
- "symbol": true,
49464962
- "templateLiteral": true,
4963+
- "topLevelAwait": true,
49474964
+ "spread": false,
49484965
+ "symbol": false,
49494966
+ "templateLiteral": false,
4967+
+ "topLevelAwait": false,
49504968
@@ ... @@
49514969
- "hotUpdateGlobal": "webpackHotUpdatewebpack",
49524970
+ "hotUpdateGlobal": "webpackHotUpdatebrowserslist_test",
@@ -5506,6 +5524,9 @@ describe("snapshots", () => {
55065524
+ "nodeBuiltinModuleGetter": false,
55075525
+ "nodePrefixForCoreModules": false,
55085526
@@ ... @@
5527+
- "topLevelAwait": true,
5528+
+ "topLevelAwait": false,
5529+
@@ ... @@
55095530
- "target": "web",
55105531
+ "target": "node",
55115532
@@ ... @@
@@ -5565,6 +5586,9 @@ describe("snapshots", () => {
55655586
+ "nodeBuiltinModuleGetter": false,
55665587
+ "nodePrefixForCoreModules": false,
55675588
@@ ... @@
5589+
- "topLevelAwait": true,
5590+
+ "topLevelAwait": false,
5591+
@@ ... @@
55685592
- "filename": "[name].js",
55695593
- "globalObject": "self",
55705594
+ "filename": "[name].mjs",

0 commit comments

Comments
 (0)