@@ -10,6 +10,7 @@ const HelperRuntimeModule = require("./HelperRuntimeModule");
1010const RuntimeGlobals = require ( "./RuntimeGlobals" ) ;
1111
1212/** @import { ModuleId } from "../graph/ChunkGraph" */
13+ /** @import RuntimeTemplate from "../template/RuntimeTemplate" */
1314/** @import Module, { RuntimeRequirements, ExportsType } from "../module/Module" */
1415/** @import ModuleGraph from "../graph/ModuleGraph" */
1516
@@ -221,6 +222,36 @@ function getOptimizedDeferredModule(
221222 ) } )`;
222223}
223224
225+ /**
226+ * Renders the setup a deferred namespace helper needs when a hot update delivers
227+ * it, since the running bootstrap may not mark modules as evaluating.
228+ * @param {RuntimeTemplate } runtimeTemplate the runtime template
229+ * @returns {string } setup code, run once per runtime
230+ */
231+ function renderHotDeferredSetup ( runtimeTemplate ) {
232+ const cache = RuntimeGlobals . deferredModuleNamespaceCache ;
233+ return Template . asString ( [
234+ // The namespace cache doubles as the flag that the interceptor is installed.
235+ `if (!${ cache } ) {` ,
236+ Template . indent ( [
237+ `${ cache } = {};` ,
238+ `${ RuntimeGlobals . interceptModuleExecution } .push(${ runtimeTemplate . basicFunction (
239+ "options" ,
240+ [
241+ `${ runtimeTemplate . renderConst ( ) } factory = options.factory;` ,
242+ "if (factory) options.factory = function(module) {" ,
243+ Template . indent ( [
244+ "module.evaluating = true;" ,
245+ "try { return factory.apply(this, arguments); } finally { module.evaluating = false; }"
246+ ] ) ,
247+ "};"
248+ ]
249+ ) } );`
250+ ] ) ,
251+ "}"
252+ ] ) ;
253+ }
254+
224255class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
225256 /**
226257 * Returns true, if the runtime module should get it's own scope.
@@ -252,7 +283,12 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
252283 const lt = runtimeTemplate . renderLet ( ) ;
253284 const fn = RuntimeGlobals . makeOptimizedDeferredNamespaceObject ;
254285 const hasAsync = this . hasAsyncRuntime ;
286+ const hot = this . receivesHotUpdates ( ) ;
287+ const moduleCache = hot
288+ ? RuntimeGlobals . moduleCache
289+ : "__webpack_module_cache__" ;
255290 return Template . asString ( [
291+ ...( hot ? [ renderHotDeferredSetup ( runtimeTemplate ) ] : [ ] ) ,
256292 // Note: must be a function (not arrow), because this is used in body!
257293 // `asyncDeps` keeps a fixed positional slot even without async runtime
258294 // so the trailing `syncDeps` (defer-cycle closure) always lines up.
@@ -267,11 +303,11 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
267303 // Forcing a module that is already evaluating, a cycle reached
268304 // through a deferred import, must throw rather than expose partial
269305 // exports. `syncDeps` carries the closure that catches it first.
270- `${ cst } cachedModule = ${ RuntimeGlobals . moduleCache } [moduleId];` ,
306+ `${ cst } cachedModule = ${ moduleCache } [moduleId];` ,
271307 'if (cachedModule !== undefined && (cachedModule.evaluating || cachedModule.evaluatingAsync)) throw new TypeError("Cannot access a deferred module namespace while the module is being evaluated");' ,
272308 "if (syncDeps) for (var i = 0; i < syncDeps.length; i++) {" ,
273309 Template . indent ( [
274- `${ cst } depModule = ${ RuntimeGlobals . moduleCache } [syncDeps[i]];` ,
310+ `${ cst } depModule = ${ moduleCache } [syncDeps[i]];` ,
275311 'if (depModule !== undefined && (depModule.evaluating || depModule.evaluatingAsync)) throw new TypeError("Cannot access a deferred module namespace while a dependency is being evaluated");'
276312 ] ) ,
277313 "}" ,
@@ -294,7 +330,7 @@ class MakeOptimizedDeferredNamespaceObjectRuntimeModule extends HelperRuntimeMod
294330 `if(isAsync) obj[${ RuntimeGlobals . deferredModuleAsyncTransitiveDependenciesSymbol } ] = asyncDeps.filter(${ runtimeTemplate . basicFunction (
295331 "id" ,
296332 [
297- `${ cst } dep = ${ RuntimeGlobals . moduleCache } [id];` ,
333+ `${ cst } dep = ${ moduleCache } [id];` ,
298334 "if (dep && dep.evaluating) return false;" ,
299335 "r(id);" ,
300336 "return true;"
@@ -350,7 +386,14 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
350386 const sealed = this . sealed ;
351387 const synthesized = this . synthesized ;
352388 const init = `${ runtimeTemplate . optionalChaining ( "init" , "()" ) } ;` ;
353- return `${ fn } = ${ runtimeTemplate . basicFunction (
389+ const hot = this . receivesHotUpdates ( ) ;
390+ const moduleCache = hot
391+ ? RuntimeGlobals . moduleCache
392+ : "__webpack_module_cache__" ;
393+ const namespaceCache = hot
394+ ? RuntimeGlobals . deferredModuleNamespaceCache
395+ : "__webpack_module_deferred_namespace_cache__" ;
396+ return `${ hot ? `${ renderHotDeferredSetup ( runtimeTemplate ) } \n` : "" } ${ fn } = ${ runtimeTemplate . basicFunction (
354397 "moduleId, mode, exportNames" ,
355398 [
356399 // WHY: the TC39 import-defer spec makes a deferred namespace distinct
@@ -373,12 +416,10 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
373416 sealed && synthesized
374417 ? "if (!(mode & 8)) exportNames = undefined;"
375418 : "" ,
376- `${ lt } byMode = ${ RuntimeGlobals . deferredModuleNamespaceCache } [moduleId];` ,
419+ `${ lt } byMode = ${ namespaceCache } [moduleId];` ,
377420 "if (byMode && byMode[mode] !== undefined) return byMode[mode];" ,
378- `if (!byMode) byMode = ${ RuntimeGlobals . deferredModuleNamespaceCache } [moduleId] = {};` ,
379- synthesized
380- ? `${ cst } cachedModule = ${ RuntimeGlobals . moduleCache } [moduleId];`
381- : "" ,
421+ `if (!byMode) byMode = ${ namespaceCache } [moduleId] = {};` ,
422+ synthesized ? `${ cst } cachedModule = ${ moduleCache } [moduleId];` : "" ,
382423 synthesized
383424 ? "if (cachedModule && cachedModule.error === undefined && !(mode & 8)) {"
384425 : "" ,
@@ -397,7 +438,7 @@ class MakeDeferredNamespaceObjectRuntimeModule extends HelperRuntimeModule {
397438 // A deferred namespace that forces evaluation of a module that is
398439 // already evaluating (a cycle) must throw rather than expose its
399440 // partial exports.
400- `${ cst } evaluatingModule = ${ RuntimeGlobals . moduleCache } [moduleId];` ,
441+ `${ cst } evaluatingModule = ${ moduleCache } [moduleId];` ,
401442 'if (evaluatingModule !== undefined && (evaluatingModule.evaluating || evaluatingModule.evaluatingAsync)) throw new TypeError("Cannot access a deferred module namespace while the module is being evaluated");' ,
402443 `ns = ${ RuntimeGlobals . require } (moduleId);` ,
403444 hasAsync
0 commit comments