Skip to content

Commit

Permalink
fix: check for name of HotModuleReplacementPlugin to avoid RangeError (
Browse files Browse the repository at this point in the history
  • Loading branch information
jdb8 authored and evilebottnawi committed Jul 27, 2019
1 parent f6653e7 commit 4579775
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/utils/addEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
20 changes: 20 additions & 0 deletions test/server/utils/addEntries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 4579775

Please sign in to comment.