From 914db1f7ca1ff6ed4eba015b6765add9afac35e3 Mon Sep 17 00:00:00 2001 From: Xiao <784487301@qq.com> Date: Mon, 25 Aug 2025 22:03:25 +0800 Subject: [PATCH] fix: unify the error handling for hmrDownloadManifest (#19835) --- lib/esm/ModuleChunkLoadingRuntimeModule.js | 9 ++++--- lib/node/ReadFileChunkLoadingRuntimeModule.js | 2 +- lib/node/RequireChunkLoadingRuntimeModule.js | 5 +++- .../cjs-chunk-loading/index.js | 22 ++++++++++++++++ .../module-chunk-loading/index.js | 25 +++++++++++++++++++ .../module-chunk-loading/webpack.config.js | 13 ++++++++++ 6 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 test/hotCases/hmr-download-manifest/cjs-chunk-loading/index.js create mode 100644 test/hotCases/hmr-download-manifest/module-chunk-loading/index.js create mode 100644 test/hotCases/hmr-download-manifest/module-chunk-loading/webpack.config.js diff --git a/lib/esm/ModuleChunkLoadingRuntimeModule.js b/lib/esm/ModuleChunkLoadingRuntimeModule.js index 4c36eb229..453320c81 100644 --- a/lib/esm/ModuleChunkLoadingRuntimeModule.js +++ b/lib/esm/ModuleChunkLoadingRuntimeModule.js @@ -392,8 +392,8 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { [ `return ${importFunctionName}(/* webpackIgnore: true */ url).then(onResolve).catch(onReject)` ] - )} - loadScript(url, onResolve, onReject);` + )}`, + "loadScript(url, onResolve, onReject);" ] )});` ]), @@ -411,7 +411,10 @@ class ModuleChunkLoadingRuntimeModule extends RuntimeModule { RuntimeGlobals.getUpdateManifestFilename }()).then(${runtimeTemplate.basicFunction("obj", [ "return obj.default;" - ])}, ${runtimeTemplate.basicFunction("", "")});` + ])}, ${runtimeTemplate.basicFunction("error", [ + "if(['MODULE_NOT_FOUND', 'ENOENT'].includes(error.code)) return;", + "throw error;" + ])});` ])};` ]) : "// no HMR manifest" diff --git a/lib/node/ReadFileChunkLoadingRuntimeModule.js b/lib/node/ReadFileChunkLoadingRuntimeModule.js index eb425f7ce..f4e82e423 100644 --- a/lib/node/ReadFileChunkLoadingRuntimeModule.js +++ b/lib/node/ReadFileChunkLoadingRuntimeModule.js @@ -264,7 +264,7 @@ class ReadFileChunkLoadingRuntimeModule extends RuntimeModule { Template.indent([ "if(err) {", Template.indent([ - 'if(err.code === "ENOENT") return resolve();', + 'if(["MODULE_NOT_FOUND", "ENOENT"].includes(err.code)) return resolve();', "return reject(err);" ]), "}", diff --git a/lib/node/RequireChunkLoadingRuntimeModule.js b/lib/node/RequireChunkLoadingRuntimeModule.js index 415b7d172..e748a8cee 100644 --- a/lib/node/RequireChunkLoadingRuntimeModule.js +++ b/lib/node/RequireChunkLoadingRuntimeModule.js @@ -223,7 +223,10 @@ class RequireChunkLoadingRuntimeModule extends RuntimeModule { RuntimeGlobals.getUpdateManifestFilename }());` ]), - "})['catch'](function(err) { if(err.code !== 'MODULE_NOT_FOUND') throw err; });" + `}).catch(${runtimeTemplate.basicFunction("err", [ + "if(['MODULE_NOT_FOUND', 'ENOENT'].includes(err.code)) return;", + "throw err;" + ])});` ]), "}" ]) diff --git a/test/hotCases/hmr-download-manifest/cjs-chunk-loading/index.js b/test/hotCases/hmr-download-manifest/cjs-chunk-loading/index.js new file mode 100644 index 000000000..c73b9ee48 --- /dev/null +++ b/test/hotCases/hmr-download-manifest/cjs-chunk-loading/index.js @@ -0,0 +1,22 @@ +if (module.hot) { + module.hot.accept() +} +it('Should work', (done) => { + expect(1).toBe(1) + NEXT( + require("../../update")(done, true, () => { + done(); + }) + ); +}) +--- +// https://github.com/webpack/webpack/pull/19832#event-19319802751 +// This is because within the checkForUpdate(hot/signal.js), we will proactively check for updates again. If there are no updates at this time, it is acceptable. +it('Should be no error when the HMR update detects that the hot-update.json file does not exist', (done) => { + expect(1).toBe(1) + __webpack_require__.hmrM().then(() => { + done() + }).catch((err) => { + done(err) + }) +}) diff --git a/test/hotCases/hmr-download-manifest/module-chunk-loading/index.js b/test/hotCases/hmr-download-manifest/module-chunk-loading/index.js new file mode 100644 index 000000000..35093e370 --- /dev/null +++ b/test/hotCases/hmr-download-manifest/module-chunk-loading/index.js @@ -0,0 +1,25 @@ +import update from "../../update.esm.js"; + + +if (import.meta.webpackHot) { + import.meta.webpackHot.accept() +} +it('Should work', (done) => { + expect(1).toBe(1) + NEXT( + update(done, true, () => { + done(); + }) + ); +}) +--- +// https://github.com/webpack/webpack/pull/19832#event-19319802751 +// This is because within the checkForUpdate(hot/signal.js), we will proactively check for updates again. If there are no updates at this time, it is acceptable. +it('Should be no error when the HMR update detects that the hot-update.json file does not exist', (done) => { + expect(1).toBe(1) + __webpack_require__.hmrM().then(() => { + done() + }).catch((err) => { + done(err) + }) +}) diff --git a/test/hotCases/hmr-download-manifest/module-chunk-loading/webpack.config.js b/test/hotCases/hmr-download-manifest/module-chunk-loading/webpack.config.js new file mode 100644 index 000000000..26fb61174 --- /dev/null +++ b/test/hotCases/hmr-download-manifest/module-chunk-loading/webpack.config.js @@ -0,0 +1,13 @@ +"use strict"; + +module.exports = { + experiments: { + outputModule: true + }, + output: { + module: true, + library: { + type: "module" + } + } +};