From 45797757803cf51117417caf400d64f1d0cee2e7 Mon Sep 17 00:00:00 2001 From: Joe Bateson Date: Sat, 27 Jul 2019 04:46:00 -0700 Subject: [PATCH] fix: check for name of HotModuleReplacementPlugin to avoid RangeError (#2146) --- lib/utils/addEntries.js | 5 +++-- test/server/utils/addEntries.test.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/utils/addEntries.js b/lib/utils/addEntries.js index fb5e542e75..117bd9a835 100644 --- a/lib/utils/addEntries.js +++ b/lib/utils/addEntries.js @@ -93,8 +93,9 @@ function addEntries(config, options, server) { config.plugins = config.plugins || []; if ( !config.plugins.find( - (plugin) => - plugin.constructor === webpack.HotModuleReplacementPlugin + // Check for the name rather than the constructor reference in case + // there are multiple copies of webpack installed + (plugin) => plugin.constructor.name === 'HotModuleReplacementPlugin' ) ) { config.plugins.push(new webpack.HotModuleReplacementPlugin()); diff --git a/test/server/utils/addEntries.test.js b/test/server/utils/addEntries.test.js index 608bd61a30..1bc6a6cef6 100644 --- a/test/server/utils/addEntries.test.js +++ b/test/server/utils/addEntries.test.js @@ -246,6 +246,26 @@ describe('addEntries util', () => { ]); }); + it("should not add the HMR plugin again if it's already there from a different webpack", () => { + const existingPlugin = new webpack.BannerPlugin('bruce'); + + // Simulate the inclusion of another webpack's HotModuleReplacementPlugin + class HotModuleReplacementPlugin {} + + const webpackOptions = Object.assign({}, config, { + plugins: [new HotModuleReplacementPlugin(), existingPlugin], + }); + const devServerOptions = { hot: true }; + + addEntries(webpackOptions, devServerOptions); + + expect(webpackOptions.plugins).toEqual([ + // Nothing should be injected + new HotModuleReplacementPlugin(), + existingPlugin, + ]); + }); + it('should can prevent duplicate entries from successive calls', () => { const webpackOptions = Object.assign({}, config); const devServerOptions = { hot: true };