diff --git a/lib/HotModuleReplacementPlugin.js b/lib/HotModuleReplacementPlugin.js index f7aae12fe..46acc6770 100644 --- a/lib/HotModuleReplacementPlugin.js +++ b/lib/HotModuleReplacementPlugin.js @@ -253,14 +253,9 @@ module.exports = class HotModuleReplacementPlugin { c: {} }; for (const key of Object.keys(records.chunkHashs)) { - let chunkId; - if (compiler.options.optimization.namedChunks) { - chunkId = key; - } else { - chunkId = isNaN(+key) ? key : +key; - } + const chunkId = isNaN(+key) ? key : +key; const currentChunk = compilation.chunks.find( - chunk => chunk.id === chunkId + chunk => `${chunk.id}` === key ); if (currentChunk) { const newModules = currentChunk diff --git a/package.json b/package.json index d36f2bbac..c7ec19460 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "test": "node --max-old-space-size=4096 --trace-deprecation node_modules/jest-cli/bin/jest", "test:update-snapshots": "yarn jest -u", "test:integration": "node --max-old-space-size=4096 --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"/test/*.test.js\"", + "test:HMR": "node --max-old-space-size=4096 --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"/test/{HotModuleReplacementPlugin}.test.js\"", "test:basic": "node --max-old-space-size=4096 --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"/test/{TestCasesNormal,StatsTestCases,ConfigTestCases}.test.js\"", "test:unit": "node --max-old-space-size=4096 --trace-deprecation node_modules/jest-cli/bin/jest --testMatch \"/test/*.unittest.js\"", "travis:integration": "yarn cover:init && yarn cover:integration --ci $JEST", diff --git a/test/HotModuleReplacementPlugin.test.js b/test/HotModuleReplacementPlugin.test.js index 09a0578d1..0cc28c4e0 100644 --- a/test/HotModuleReplacementPlugin.test.js +++ b/test/HotModuleReplacementPlugin.test.js @@ -97,4 +97,78 @@ describe("HotModuleReplacementPlugin", () => { }); }); }); + + it("should correct working when entry is Object and key is a number", done => { + const entryFile = path.join( + __dirname, + "js", + "HotModuleReplacementPlugin", + "entry.js" + ); + const statsFile3 = path.join( + __dirname, + "js", + "HotModuleReplacementPlugin", + "HotModuleReplacementPlugin.test.stats3.txt" + ); + const statsFile4 = path.join( + __dirname, + "js", + "HotModuleReplacementPlugin", + "HotModuleReplacementPlugin.test.stats4.txt" + ); + const recordsFile = path.join( + __dirname, + "js", + "HotModuleReplacementPlugin", + "records.json" + ); + try { + mkdirp.sync(path.join(__dirname, "js", "HotModuleReplacementPlugin")); + } catch (e) { + // empty + } + try { + fs.unlinkSync(recordsFile); + } catch (e) { + // empty + } + const compiler = webpack({ + mode: "development", + cache: false, + entry: { + "0": entryFile + }, + recordsPath: recordsFile, + output: { + path: path.join(__dirname, "js", "HotModuleReplacementPlugin") + }, + plugins: [new webpack.HotModuleReplacementPlugin()], + optimization: { + namedChunks: true + } + }); + fs.writeFileSync(entryFile, "1", "utf-8"); + compiler.run((err, stats) => { + if (err) throw err; + const jsonStats = stats.toJson(); + const hash = jsonStats.hash; + const trunkName = Object.keys(jsonStats.assetsByChunkName)[0]; + fs.writeFileSync(statsFile3, stats.toString()); + compiler.run((err, stats) => { + if (err) throw err; + fs.writeFileSync(statsFile4, stats.toString()); + fs.writeFileSync(entryFile, "2", "utf-8"); + compiler.run((err, stats) => { + if (err) throw err; + fs.writeFileSync(statsFile3, stats.toString()); + const result = JSON.parse( + stats.compilation.assets[`${hash}.hot-update.json`].source() + )["c"][`${trunkName}`]; + expect(result).toBe(true); + done(); + }); + }); + }); + }); });