Skip to content

Commit 57e54d9

Browse files
committed
fix(runtime): support deferred imports introduced by hot updates
1 parent 8dd291c commit 57e54d9

27 files changed

Lines changed: 198 additions & 19 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+
Support deferred imports introduced by hot updates.

‎lib/hmr/HotModuleReplacementPlugin.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ To fix this, make sure to include [runtime] in the output.hotUpdateMainFilename
967967
runtimeRequirements.add(RuntimeGlobals.hmrDownloadUpdateHandlers);
968968
runtimeRequirements.add(RuntimeGlobals.interceptModuleExecution);
969969
runtimeRequirements.add(RuntimeGlobals.moduleCache);
970+
if (compilation.options.experiments.deferImport) {
971+
runtimeRequirements.add(
972+
RuntimeGlobals.deferredModuleNamespaceCache
973+
);
974+
}
970975
compilation.addRuntimeModule(
971976
chunk,
972977
new HotModuleReplacementRuntimeModule()

‎lib/javascript/JavascriptModulesPlugin.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,13 +1726,6 @@ class JavascriptModulesPlugin {
17261726
// object before evaluation.
17271727
buf.push("// The deferred module cache");
17281728
buf.push(`${bootstrapCst} __webpack_module_deferred_exports__ = {};`);
1729-
// Every defer-import site for one module must yield the same Deferred
1730-
// Module Namespace, distinct from any eager one, so the proxy is cached
1731-
// here for call sites in different files to share.
1732-
buf.push("// The deferred namespace cache");
1733-
buf.push(
1734-
`${bootstrapCst} __webpack_module_deferred_namespace_cache__ = {};`
1735-
);
17361729
buf.push("");
17371730
}
17381731

@@ -1763,6 +1756,12 @@ class JavascriptModulesPlugin {
17631756
buf.push("");
17641757
}
17651758

1759+
if (runtimeRequirements.has(RuntimeGlobals.deferredModuleNamespaceCache)) {
1760+
buf.push("// The deferred namespace cache, shared with hot updates");
1761+
buf.push(`${RuntimeGlobals.deferredModuleNamespaceCache} = {};`);
1762+
buf.push("");
1763+
}
1764+
17661765
if (interceptModuleExecution) {
17671766
buf.push("// expose the module execution interceptor");
17681767
buf.push(`${RuntimeGlobals.interceptModuleExecution} = [];`);
@@ -2187,6 +2186,8 @@ class JavascriptModulesPlugin {
21872186
// skips the deferred exports object, hence the separate flag.
21882187
const needModuleDeferAny =
21892188
needModuleDefer ||
2189+
// Hot updates may introduce deferred imports after this bootstrap runs.
2190+
runtimeRequirements.has(RuntimeGlobals.deferredModuleNamespaceCache) ||
21902191
runtimeRequirements.has(
21912192
RuntimeGlobals.makeOptimizedDeferredNamespaceObject
21922193
);

‎lib/runtime/AsyncModuleRuntimeModule.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
6565
Template.indent([
6666
`${cst} hasUnresolvedAsyncSubgraph = asyncDeps.some((id) => {`,
6767
Template.indent([
68-
`${cst} cache = __webpack_module_cache__[id];`,
68+
`${cst} cache = ${RuntimeGlobals.moduleCache}[id];`,
6969
"if (!cache) return true;",
7070
// A cached entry no longer means finished, and a rejected
7171
// one is done but its error must still reach the importer.
@@ -223,7 +223,7 @@ class AsyncModuleRuntimeModule extends HelperRuntimeModule {
223223
`group && group.forEach(${runtimeTemplate.basicFunction(
224224
"id",
225225
[
226-
`${cst} peer = __webpack_module_cache__[id];`,
226+
`${cst} peer = ${RuntimeGlobals.moduleCache}[id];`,
227227
"if (peer && peer.error === undefined) peer.error = err;"
228228
]
229229
)})`,

‎lib/runtime/MakeDeferredNamespaceObjectRuntime.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,11 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
267267
// Forcing a module that is already evaluating, a cycle reached
268268
// through a deferred import, must throw rather than expose partial
269269
// exports. `syncDeps` carries the closure that catches it first.
270-
`${cst} cachedModule = __webpack_module_cache__[moduleId];`,
270+
`${cst} cachedModule = ${RuntimeGlobals.moduleCache}[moduleId];`,
271271
'if (cachedModule !== undefined && (cachedModule.evaluating || cachedModule.evaluatingAsync)) throw new TypeError("Cannot access a deferred module namespace while the module is being evaluated");',
272272
"if (syncDeps) for (var i = 0; i < syncDeps.length; i++) {",
273273
Template.indent([
274-
`${cst} depModule = __webpack_module_cache__[syncDeps[i]];`,
274+
`${cst} depModule = ${RuntimeGlobals.moduleCache}[syncDeps[i]];`,
275275
'if (depModule !== undefined && (depModule.evaluating || depModule.evaluatingAsync)) throw new TypeError("Cannot access a deferred module namespace while a dependency is being evaluated");'
276276
]),
277277
"}",
@@ -294,7 +294,7 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
294294
`if(isAsync) obj[${RuntimeGlobals.deferredModuleAsyncTransitiveDependenciesSymbol}] = asyncDeps.filter(${runtimeTemplate.basicFunction(
295295
"id",
296296
[
297-
`${cst} dep = __webpack_module_cache__[id];`,
297+
`${cst} dep = ${RuntimeGlobals.moduleCache}[id];`,
298298
"if (dep && dep.evaluating) return false;",
299299
"r(id);",
300300
"return true;"
@@ -373,11 +373,11 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
373373
sealed && synthesized
374374
? "if (!(mode & 8)) exportNames = undefined;"
375375
: "",
376-
`${lt} byMode = __webpack_module_deferred_namespace_cache__[moduleId];`,
376+
`${lt} byMode = ${RuntimeGlobals.deferredModuleNamespaceCache}[moduleId];`,
377377
"if (byMode && byMode[mode] !== undefined) return byMode[mode];",
378-
"if (!byMode) byMode = __webpack_module_deferred_namespace_cache__[moduleId] = {};",
378+
`if (!byMode) byMode = ${RuntimeGlobals.deferredModuleNamespaceCache}[moduleId] = {};`,
379379
synthesized
380-
? `${cst} cachedModule = __webpack_module_cache__[moduleId];`
380+
? `${cst} cachedModule = ${RuntimeGlobals.moduleCache}[moduleId];`
381381
: "",
382382
synthesized
383383
? "if (cachedModule && cachedModule.error === undefined && !(mode & 8)) {"
@@ -397,7 +397,7 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
397397
// A deferred namespace that forces evaluation of a module that is
398398
// already evaluating (a cycle) must throw rather than expose its
399399
// partial exports.
400-
`${cst} evaluatingModule = __webpack_module_cache__[moduleId];`,
400+
`${cst} evaluatingModule = ${RuntimeGlobals.moduleCache}[moduleId];`,
401401
'if (evaluatingModule !== undefined && (evaluatingModule.evaluating || evaluatingModule.evaluatingAsync)) throw new TypeError("Cannot access a deferred module namespace while the module is being evaluated");',
402402
`ns = ${RuntimeGlobals.require}(moduleId);`,
403403
hasAsync

‎lib/runtime/RuntimeGlobals.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ module.exports.deferredModuleAsyncTransitiveDependencies =
121121
module.exports.deferredModuleAsyncTransitiveDependenciesSymbol =
122122
"__webpack_require__.zS";
123123

124+
/**
125+
* cached deferred namespaces, shared across runtime updates
126+
*/
127+
module.exports.deferredModuleNamespaceCache = "__webpack_require__.zC";
128+
124129
/**
125130
* the exported property define getters function
126131
*/

‎lib/runtime/RuntimePlugin.js‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ const GLOBALS_ON_REQUIRE = [
171171
RuntimeGlobals.onChunksLoaded,
172172
RuntimeGlobals.makeOptimizedDeferredNamespaceObject,
173173
RuntimeGlobals.makeDeferredNamespaceObject,
174+
RuntimeGlobals.deferredModuleNamespaceCache,
174175
RuntimeGlobals.concatenationWrap,
175176
RuntimeGlobals.constructRequire
176177
];
@@ -194,11 +195,14 @@ const TREE_DEPENDENCIES = {
194195
RuntimeGlobals.asyncModule
195196
],
196197
[RuntimeGlobals.makeOptimizedDeferredNamespaceObject]: [
197-
RuntimeGlobals.require
198+
RuntimeGlobals.require,
199+
RuntimeGlobals.moduleCache
198200
],
199201
[RuntimeGlobals.makeDeferredNamespaceObject]: [
200202
RuntimeGlobals.createFakeNamespaceObject,
201-
RuntimeGlobals.require
203+
RuntimeGlobals.require,
204+
RuntimeGlobals.moduleCache,
205+
RuntimeGlobals.deferredModuleNamespaceCache
202206
],
203207
[RuntimeGlobals.initializeSharing]: [RuntimeGlobals.shareScopeMap],
204208
[RuntimeGlobals.shareScopeMap]: [RuntimeGlobals.hasOwnProperty],
@@ -418,8 +422,14 @@ class RuntimePlugin {
418422
});
419423
compilation.hooks.runtimeRequirementInTree
420424
.for(RuntimeGlobals.asyncModule)
421-
.tap(PLUGIN_NAME, (chunk) => {
425+
.tap(PLUGIN_NAME, (chunk, set) => {
422426
const experiments = compilation.options.experiments;
427+
if (
428+
experiments.deferImport ||
429+
compilation.outputOptions.strictModuleErrorHandling
430+
) {
431+
set.add(RuntimeGlobals.moduleCache);
432+
}
423433
compilation.addLazyRuntimeModule(
424434
chunk,
425435
getAsyncModuleRuntimeModule,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
/** @type {import("../../../../").Configuration} */
6+
module.exports = {
7+
...require("../../harmony/defer-import-added-async/webpack.config"),
8+
context: path.resolve(__dirname, "../../harmony/defer-import-added-async"),
9+
output: { module: true, chunkFormat: "module" }
10+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
5+
/** @type {import("../../../../").Configuration} */
6+
module.exports = {
7+
...require("../../harmony/defer-import-added/webpack.config"),
8+
context: path.resolve(__dirname, "../../harmony/defer-import-added"),
9+
output: { module: true, chunkFormat: "module" }
10+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { evaluations } from "./state";
2+
await 0;
3+
evaluations.push("async");

0 commit comments

Comments
 (0)