Skip to content

Commit f050ade

Browse files
fix(esm): let the startup alone await async library entries
The startup now awaits every async entry wherever top-level await reaches it and tells ModuleLibraryPlugin through `exportsAwaited`, so the last entry is no longer awaited twice and no entry-count check is needed.
1 parent c62f5ca commit f050ade

4 files changed

Lines changed: 28 additions & 29 deletions

File tree

‎lib/esm/ModuleChunkFormatPlugin.js‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,7 @@ class ModuleChunkFormatPlugin {
243243
);
244244
}
245245

246-
const entryOptions = chunk.getEntryOptions();
247-
const library =
248-
entryOptions && entryOptions.library !== undefined
249-
? entryOptions.library
250-
: runtimeTemplate.outputOptions.library;
251-
// `ModuleLibraryPlugin` awaits only the last entry; multiple entries
252-
// must all be awaited here.
253-
const canAwaitEntry =
254-
runtimeTemplate.supportsTopLevelAwait() &&
255-
!(
256-
entries.length === 1 &&
257-
library !== undefined &&
258-
library.type === "module"
259-
);
246+
const canAwaitEntry = runtimeTemplate.supportsTopLevelAwait();
260247
/** @type {string[]} */
261248
const awaitedEntries = [];
262249
/** @type {Set<Chunk>} */
@@ -333,14 +320,22 @@ class ModuleChunkFormatPlugin {
333320
for (const line of renderAwaitedEntries(awaitedEntries)) {
334321
startupSource.add(`${line}\n`);
335322
}
323+
const exportsAwaited = awaitedEntries.includes(
324+
RuntimeGlobals.exports
325+
);
336326

337327
entrySource.add(
338328
hooks.renderStartup.call(
339329
startupSource,
340330
entries[entries.length - 1][0],
341331
inlinedEntryModule
342-
? { ...renderContext, inlined: true, inlinedInIIFE: false }
343-
: renderContext
332+
? {
333+
...renderContext,
334+
inlined: true,
335+
inlinedInIIFE: false,
336+
exportsAwaited
337+
}
338+
: { ...renderContext, exportsAwaited }
344339
)
345340
);
346341
return entrySource;

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ const printGeneratedCodeForStack = (module, code) => {
259259
* @property {boolean=} inlinedInIIFE the inlined entry module is wrapped in an IIFE
260260
* @property {boolean=} needExportsDeclaration whether the top-level exports declaration needs to be generated
261261
* @property {Map<string, string>=} renamedDeclarations the inlined entry's top-level declarations renamed to avoid a collision, original to new name
262+
* @property {boolean=} exportsAwaited the startup already awaited the last entry, so `__webpack_exports__` holds its settled exports
262263
*/
263264

264265
/**
@@ -433,7 +434,7 @@ const PLUGIN_NAME = "JavascriptModulesPlugin";
433434
// parsed map is far more expensive to rebuild than a `toString`.
434435
const CLEAR_SOURCE_COPIES = { maps: false, source: true };
435436

436-
/** @typedef {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean }} Bootstrap */
437+
/** @typedef {{ header: string[], beforeStartup: string[], startup: string[], afterStartup: string[], allowInlineStartup: boolean, exportsAwaited: boolean }} Bootstrap */
437438
/** @typedef {{ source: Source, needModule: boolean, needExports: boolean, needRequire: boolean, needThisAsExports: boolean, needStrict: boolean | undefined, renderShorthand: boolean, next: ModuleFactoryCacheEntry | undefined }} ModuleFactoryCacheEntry */
438439

439440
/**
@@ -1569,7 +1570,8 @@ class JavascriptModulesPlugin {
15691570
{
15701571
...renderContext,
15711572
inlined: false,
1572-
needExportsDeclaration: true
1573+
needExportsDeclaration: true,
1574+
exportsAwaited: bootstrap.exportsAwaited
15731575
}
15741576
),
15751577
toSource(bootstrap.afterStartup, "webpack/after-startup"),
@@ -1681,14 +1683,15 @@ class JavascriptModulesPlugin {
16811683
requireFunction || interceptModuleExecution || moduleUsed;
16821684

16831685
/**
1684-
* @type {{ startup: string[], beforeStartup: string[], header: string[], afterStartup: string[], allowInlineStartup: boolean }}
1686+
* @type {Bootstrap}
16851687
*/
16861688
const result = {
16871689
header: [],
16881690
beforeStartup: [],
16891691
startup: [],
16901692
afterStartup: [],
1691-
allowInlineStartup: true
1693+
allowInlineStartup: true,
1694+
exportsAwaited: false
16921695
};
16931696

16941697
const { header: buf, startup, beforeStartup, afterStartup } = result;
@@ -1798,19 +1801,11 @@ class JavascriptModulesPlugin {
17981801
chunkGraph
17991802
);
18001803
const merge = mergedModules !== undefined;
1801-
// A single-entry module library awaits its entry in `ModuleLibraryPlugin`.
1802-
// Multiple entries must all be awaited here, including merged exports.
1803-
const awaitedByLibrary =
1804-
!merge &&
1805-
jsEntries.length === 1 &&
1806-
library !== undefined &&
1807-
library.type === "module";
18081804
// `await` is legal only at an ESM chunk's top level, never in the
18091805
// startup function below, and only where the target parses it.
18101806
const canAwaitEntry =
18111807
runtimeTemplate.isModule() &&
18121808
runtimeTemplate.supportsTopLevelAwait() &&
1813-
!awaitedByLibrary &&
18141809
!runtimeRequirements.has(RuntimeGlobals.startup) &&
18151810
!(
18161811
runtimeRequirements.has(RuntimeGlobals.startupOnlyBefore) &&
@@ -2027,6 +2022,7 @@ class JavascriptModulesPlugin {
20272022
);
20282023
}
20292024
buf2.push(...renderAwaitedEntries(awaitedEntries));
2025+
result.exportsAwaited = awaitedEntries.includes(RuntimeGlobals.exports);
20302026
// Every entry has run by now, and the getters below read through to
20312027
// whichever one provides each name.
20322028
if (merge) {

‎lib/library/ModuleLibraryPlugin.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
546546
codeGenerationResults,
547547
inlined,
548548
inlinedInIIFE,
549+
exportsAwaited,
549550
runtimeTemplate
550551
} = renderContext;
551552
let result = new ConcatSource(source);
@@ -640,7 +641,8 @@ class ModuleLibraryPlugin extends AbstractLibraryPlugin {
640641
let needExportsDeclaration =
641642
!isInlinedEntryWithoutIIFE || isAsync || needHarmonyCompatibilityFlag;
642643

643-
if (isAsync) {
644+
// The startup awaits the entry wherever top-level `await` reaches it.
645+
if (isAsync && !exportsAwaited) {
644646
result.add(
645647
`${RuntimeGlobals.exports} = await ${RuntimeGlobals.exports};\n`
646648
);

‎types.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ declare interface Bootstrap {
13811381
startup: string[];
13821382
afterStartup: string[];
13831383
allowInlineStartup: boolean;
1384+
exportsAwaited: boolean;
13841385
}
13851386

13861387
/**
@@ -30344,6 +30345,11 @@ declare interface StartupRenderContext {
3034430345
* the inlined entry's top-level declarations renamed to avoid a collision, original to new name
3034530346
*/
3034630347
renamedDeclarations?: Map<string, string>;
30348+
30349+
/**
30350+
* the startup already awaited the last entry, so `__webpack_exports__` holds its settled exports
30351+
*/
30352+
exportsAwaited?: boolean;
3034730353
}
3034830354

3034930355
/**

0 commit comments

Comments
 (0)