Skip to content

Commit 5e6c4d7

Browse files
fix(defer): evaluate async dependencies where the deferred import sits (#21902)
* fix(defer): evaluate async dependencies where the deferred import sits A deferred import's async transitive dependencies were only started when the importing module awaited its dependencies, so every synchronous sibling ran first. They now start at the import, as the spec's `GatherAsynchronousTransitiveDependencies` requires, skipping a module that is already evaluating so a cycle back to the importer cannot deadlock. Gathering them walks the imports in source order, which the outgoing-connections map did not preserve under production ids. Un-skips test262's import-defer flattening-order case. * perf(defer): gather the started async dependencies with a filter * fix(defer): propagate a rejected async dependency to every importer A rejected async module still sets its done flag, so a later deferred import of the same subgraph read it as finished and never awaited it, dropping the error. It is now awaited whenever it carries one. The old check read the flag from the module record, where it is never set, so it meant "not yet required" and hid this behind the first importer; rewriting it for the evaluation-order fix made it reachable from the predicate itself. * docs: keep the async-dependency comment to two lines
1 parent 7ea4fa3 commit 5e6c4d7

17 files changed

Lines changed: 89 additions & 8 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+
Evaluate a deferred import's async dependencies where the import sits.

‎lib/async-modules/AsyncModuleHelpers.js‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,22 @@ const getOutgoingAsyncModules = (moduleGraph, module) => {
3232
const outgoingConnectionMap =
3333
moduleGraph.getOutgoingConnectionsByModule(module);
3434
if (outgoingConnectionMap) {
35-
for (const [module, connections] of outgoingConnectionMap) {
35+
// Walk the imports in source order: the spec gathers these from
36+
// `[[RequestedModules]]`, and their evaluation order is observable.
37+
for (const dependency of module.dependencies) {
38+
if (!(dependency instanceof HarmonyImportDependency)) continue;
39+
const referenced = moduleGraph.getModule(dependency);
40+
if (!referenced || seen.has(referenced)) continue;
41+
const connections = outgoingConnectionMap.get(referenced);
3642
if (
43+
connections &&
3744
connections.some(
3845
(c) =>
3946
c.dependency instanceof HarmonyImportDependency &&
4047
c.isTargetActive(undefined)
41-
) &&
42-
module
48+
)
4349
) {
44-
g(module);
50+
g(referenced);
4551
}
4652
}
4753
}

‎lib/runtime/AsyncModuleRuntimeModule.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
5757
`${cst} hasUnresolvedAsyncSubgraph = asyncDeps.some((id) => {`,
5858
Template.indent([
5959
`${cst} cache = __webpack_module_cache__[id];`,
60-
"return !cache || cache[webpackDone] === false;"
60+
"if (!cache) return true;",
61+
// A cached entry no longer means finished, and a rejected
62+
// one is done but its error must still reach the importer.
63+
`${cst} deps = cache.exports;`,
64+
"return deps[webpackDone] !== true || deps[webpackError] !== undefined;"
6165
]),
6266
"});",
6367
"if (hasUnresolvedAsyncSubgraph) {",

‎lib/runtime/MakeDeferredNamespaceObjectRuntime.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,17 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
290290
]),
291291
"};",
292292
hasAsync
293-
? `if(isAsync) obj[${RuntimeGlobals.deferredModuleAsyncTransitiveDependenciesSymbol}] = asyncDeps;`
293+
? // `GatherAsynchronousTransitiveDependencies` skips a module that is
294+
// already evaluating, and evaluates the rest right here.
295+
`if(isAsync) obj[${RuntimeGlobals.deferredModuleAsyncTransitiveDependenciesSymbol}] = asyncDeps.filter(${runtimeTemplate.basicFunction(
296+
"id",
297+
[
298+
`${cst} dep = __webpack_module_cache__[id];`,
299+
"if (dep && (dep.evaluating || dep.evaluatingAsync)) return false;",
300+
"r(id);",
301+
"return true;"
302+
]
303+
)});`
294304
: "",
295305
"return obj;"
296306
]),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
await Promise.resolve();
2+
throw new Error("boom");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import "./boom.js";
2+
export const value = 1;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import defer * as ns from "./dep.js";
2+
export const tag = "first";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// A rejected async dependency is "done", so its error must still reach every
2+
// deferred import of the same subgraph, not just the one that started it.
3+
it("should reject every importer of a failed deferred async dependency", async () => {
4+
await expect(import("./first.js")).rejects.toThrow("boom");
5+
await expect(import("./second.js")).rejects.toThrow("boom");
6+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import defer * as ns from "./dep.js";
2+
export const tag = "second";
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
experiments: {
6+
deferImport: true,
7+
topLevelAwait: true
8+
}
9+
};

0 commit comments

Comments
 (0)