Skip to content

Commit 4579775

Browse files
jdb8evilebottnawi
authored andcommitted
fix: check for name of HotModuleReplacementPlugin to avoid RangeError (#2146)
1 parent f6653e7 commit 4579775

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/utils/addEntries.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ function addEntries(config, options, server) {
9393
config.plugins = config.plugins || [];
9494
if (
9595
!config.plugins.find(
96-
(plugin) =>
97-
plugin.constructor === webpack.HotModuleReplacementPlugin
96+
// Check for the name rather than the constructor reference in case
97+
// there are multiple copies of webpack installed
98+
(plugin) => plugin.constructor.name === 'HotModuleReplacementPlugin'
9899
)
99100
) {
100101
config.plugins.push(new webpack.HotModuleReplacementPlugin());

test/server/utils/addEntries.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ describe('addEntries util', () => {
246246
]);
247247
});
248248

249+
it("should not add the HMR plugin again if it's already there from a different webpack", () => {
250+
const existingPlugin = new webpack.BannerPlugin('bruce');
251+
252+
// Simulate the inclusion of another webpack's HotModuleReplacementPlugin
253+
class HotModuleReplacementPlugin {}
254+
255+
const webpackOptions = Object.assign({}, config, {
256+
plugins: [new HotModuleReplacementPlugin(), existingPlugin],
257+
});
258+
const devServerOptions = { hot: true };
259+
260+
addEntries(webpackOptions, devServerOptions);
261+
262+
expect(webpackOptions.plugins).toEqual([
263+
// Nothing should be injected
264+
new HotModuleReplacementPlugin(),
265+
existingPlugin,
266+
]);
267+
});
268+
249269
it('should can prevent duplicate entries from successive calls', () => {
250270
const webpackOptions = Object.assign({}, config);
251271
const devServerOptions = { hot: true };

0 commit comments

Comments
 (0)