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

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎lib/utils/addEntries.js‎

Lines changed: 3 additions & 2 deletions
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(newwebpack.HotModuleReplacementPlugin());

‎test/server/utils/addEntries.test.js‎

Lines changed: 20 additions & 0 deletions
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+
constexistingPlugin=newwebpack.BannerPlugin('bruce');
251+
252+
// Simulate the inclusion of another webpack's HotModuleReplacementPlugin
253+
classHotModuleReplacementPlugin{}
254+
255+
constwebpackOptions=Object.assign({},config,{
256+
plugins: [newHotModuleReplacementPlugin(),existingPlugin],
257+
});
258+
constdevServerOptions={hot: true};
259+
260+
addEntries(webpackOptions,devServerOptions);
261+
262+
expect(webpackOptions.plugins).toEqual([
263+
// Nothing should be injected
264+
newHotModuleReplacementPlugin(),
265+
existingPlugin,
266+
]);
267+
});
268+
249269
it('should can prevent duplicate entries from successive calls',()=>{
250270
constwebpackOptions=Object.assign({},config);
251271
constdevServerOptions={hot: true};

0 commit comments

Comments
 (0)