From b079429ec82a8b0e7ad43f032b05935d5c74ec80 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 15 Jan 2020 10:43:58 +0100 Subject: [PATCH 1/2] use correct string or number form of chunk id in HMR --- lib/HotModuleReplacementPlugin.js | 3 +- .../numeric-ids/add-remove-chunks/chunk.js | 1 + .../numeric-ids/add-remove-chunks/chunk2.js | 3 ++ .../numeric-ids/add-remove-chunks/file.js | 5 ++++ .../numeric-ids/add-remove-chunks/index.js | 28 +++++++++++++++++++ .../numeric-ids/add-remove-chunks/module.js | 5 ++++ test/hotCases/numeric-ids/production/file.js | 3 ++ test/hotCases/numeric-ids/production/index.js | 15 ++++++++++ .../numeric-ids/production/webpack.config.js | 8 ++++++ 9 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 test/hotCases/numeric-ids/add-remove-chunks/chunk.js create mode 100644 test/hotCases/numeric-ids/add-remove-chunks/chunk2.js create mode 100644 test/hotCases/numeric-ids/add-remove-chunks/file.js create mode 100644 test/hotCases/numeric-ids/add-remove-chunks/index.js create mode 100644 test/hotCases/numeric-ids/add-remove-chunks/module.js create mode 100644 test/hotCases/numeric-ids/production/file.js create mode 100644 test/hotCases/numeric-ids/production/index.js create mode 100644 test/hotCases/numeric-ids/production/webpack.config.js diff --git a/lib/HotModuleReplacementPlugin.js b/lib/HotModuleReplacementPlugin.js index 38d28071b..dcdaf6988 100644 --- a/lib/HotModuleReplacementPlugin.js +++ b/lib/HotModuleReplacementPlugin.js @@ -308,12 +308,12 @@ class HotModuleReplacementPlugin { }; const allRemovedModules = new Set(); for (const key of Object.keys(records.chunkHashs)) { - const chunkId = key; const currentChunk = find( compilation.chunks, chunk => `${chunk.id}` === key ); if (currentChunk) { + const chunkId = currentChunk.id; const newModules = chunkGraph .getChunkModules(currentChunk) .filter(module => updatedModules.has(module)); @@ -372,6 +372,7 @@ class HotModuleReplacementPlugin { hotUpdateMainContent.c.push(chunkId); } } else { + const chunkId = `${+key}` === key ? +key : key; hotUpdateMainContent.r.push(chunkId); for (const id of records.chunkModuleIds[chunkId]) allRemovedModules.add(id); diff --git a/test/hotCases/numeric-ids/add-remove-chunks/chunk.js b/test/hotCases/numeric-ids/add-remove-chunks/chunk.js new file mode 100644 index 000000000..628afead3 --- /dev/null +++ b/test/hotCases/numeric-ids/add-remove-chunks/chunk.js @@ -0,0 +1 @@ +export { value } from "./file"; diff --git a/test/hotCases/numeric-ids/add-remove-chunks/chunk2.js b/test/hotCases/numeric-ids/add-remove-chunks/chunk2.js new file mode 100644 index 000000000..b4567329a --- /dev/null +++ b/test/hotCases/numeric-ids/add-remove-chunks/chunk2.js @@ -0,0 +1,3 @@ +import { value as v } from "./file"; + +export const value = v + 0.5; diff --git a/test/hotCases/numeric-ids/add-remove-chunks/file.js b/test/hotCases/numeric-ids/add-remove-chunks/file.js new file mode 100644 index 000000000..62336cdc2 --- /dev/null +++ b/test/hotCases/numeric-ids/add-remove-chunks/file.js @@ -0,0 +1,5 @@ +export var value = 1; +--- +export var value = 1.5; +--- +export var value = 3; diff --git a/test/hotCases/numeric-ids/add-remove-chunks/index.js b/test/hotCases/numeric-ids/add-remove-chunks/index.js new file mode 100644 index 000000000..48a48286c --- /dev/null +++ b/test/hotCases/numeric-ids/add-remove-chunks/index.js @@ -0,0 +1,28 @@ +import m from "./module"; + +it("should add and remove chunks", done => { + return m() + .then(chunk => { + expect(chunk.value).toBe(1); + let update = 0; + module.hot.accept("./module", () => { + m() + .then(chunk => { + switch (update) { + case 0: + expect(chunk.value).toBe(2); + break; + case 1: + expect(chunk.value).toBe(3); + done(); + return; + } + update++; + NEXT(require("../../update")(done)); + }) + .catch(done); + }); + NEXT(require("../../update")(done)); + }) + .catch(done); +}); diff --git a/test/hotCases/numeric-ids/add-remove-chunks/module.js b/test/hotCases/numeric-ids/add-remove-chunks/module.js new file mode 100644 index 000000000..286db1035 --- /dev/null +++ b/test/hotCases/numeric-ids/add-remove-chunks/module.js @@ -0,0 +1,5 @@ +export default () => import(/* webpackChunkName: "1e1" */ "./chunk"); +--- +export default () => import(/* webpackChunkName: "10" */ "./chunk2"); +--- +export default () => import(/* webpackChunkName: "1e1" */ "./chunk"); diff --git a/test/hotCases/numeric-ids/production/file.js b/test/hotCases/numeric-ids/production/file.js new file mode 100644 index 000000000..5b2c52ba4 --- /dev/null +++ b/test/hotCases/numeric-ids/production/file.js @@ -0,0 +1,3 @@ +export var value = 1; +--- +export var value = 2; diff --git a/test/hotCases/numeric-ids/production/index.js b/test/hotCases/numeric-ids/production/index.js new file mode 100644 index 000000000..cd36482c2 --- /dev/null +++ b/test/hotCases/numeric-ids/production/index.js @@ -0,0 +1,15 @@ +import { value } from "./file"; + +it("should auto-import a ES6 imported value on accept", function(done) { + expect(value).toBe(1); + module.hot.accept("./file", function() { + expect(value).toBe(2); + outside(); + done(); + }); + NEXT(require("../../update")(done)); +}); + +function outside() { + expect(value).toBe(2); +} diff --git a/test/hotCases/numeric-ids/production/webpack.config.js b/test/hotCases/numeric-ids/production/webpack.config.js new file mode 100644 index 000000000..1e91d5bfb --- /dev/null +++ b/test/hotCases/numeric-ids/production/webpack.config.js @@ -0,0 +1,8 @@ +"use strict"; + +module.exports = { + mode: "production", + optimization: { + minimize: false + } +}; From c07c1ed3ff60d379a037b70e54e8af7437ba7481 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 15 Jan 2020 15:19:35 +0100 Subject: [PATCH 2/2] typo Co-Authored-By: James George --- test/hotCases/numeric-ids/production/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/hotCases/numeric-ids/production/index.js b/test/hotCases/numeric-ids/production/index.js index cd36482c2..ef6ff5ab2 100644 --- a/test/hotCases/numeric-ids/production/index.js +++ b/test/hotCases/numeric-ids/production/index.js @@ -1,6 +1,6 @@ import { value } from "./file"; -it("should auto-import a ES6 imported value on accept", function(done) { +it("should auto-import an ES6 imported value on accept", function(done) { expect(value).toBe(1); module.hot.accept("./file", function() { expect(value).toBe(2);