Skip to content

Commit c62f5ca

Browse files
committed
fix(esm): await all async library entries
1 parent 94a4bcd commit c62f5ca

12 files changed

Lines changed: 107 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Await every async entry before resolving an ESM library import.

‎lib/esm/ModuleChunkFormatPlugin.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,15 @@ class ModuleChunkFormatPlugin {
248248
entryOptions && entryOptions.library !== undefined
249249
? entryOptions.library
250250
: runtimeTemplate.outputOptions.library;
251-
// A module library awaits the entry itself, in `ModuleLibraryPlugin`.
251+
// `ModuleLibraryPlugin` awaits only the last entry; multiple entries
252+
// must all be awaited here.
252253
const canAwaitEntry =
253254
runtimeTemplate.supportsTopLevelAwait() &&
254-
!(library !== undefined && library.type === "module");
255+
!(
256+
entries.length === 1 &&
257+
library !== undefined &&
258+
library.type === "module"
259+
);
255260
/** @type {string[]} */
256261
const awaitedEntries = [];
257262
/** @type {Set<Chunk>} */

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,10 +1798,13 @@ class JavascriptModulesPlugin {
17981798
chunkGraph
17991799
);
18001800
const merge = mergedModules !== undefined;
1801-
// A module library awaits the entry itself, in `ModuleLibraryPlugin`
1802-
// — but it only sees the merged object, so merging awaits them here.
1801+
// A single-entry module library awaits its entry in `ModuleLibraryPlugin`.
1802+
// Multiple entries must all be awaited here, including merged exports.
18031803
const awaitedByLibrary =
1804-
!merge && library !== undefined && library.type === "module";
1804+
!merge &&
1805+
jsEntries.length === 1 &&
1806+
library !== undefined &&
1807+
library.type === "module";
18051808
// `await` is legal only at an ESM chunk's top level, never in the
18061809
// startup function below, and only where the target parses it.
18071810
const canAwaitEntry =
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import state from "./state.js";
2+
3+
await new Promise(resolve => setTimeout(resolve, 0));
4+
state.ready = true;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it("should await every entry before resolving the library import", async () => {
2+
const library = await import("library");
3+
expect(library.isReady()).toBe(true);
4+
expect(library.value).toBe(42);
5+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
await Promise.resolve();
2+
export { value, isReady } from "./last.js";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import state from "./state.js";
2+
3+
export const value = 42;
4+
/** @returns {boolean} whether the async entry has finished */
5+
export const isReady = () => state.ready;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./first.js";
2+
export { value, isReady } from "./last.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default { ready: false };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
module.exports = {
4+
/**
5+
* @param {number} index configuration index
6+
* @param {import("../../../../").Configuration} options compiler options
7+
* @returns {string} test bundle
8+
*/
9+
findBundle(index, options) {
10+
return `./${options.name}/main.mjs`;
11+
}
12+
};

0 commit comments

Comments
 (0)