Skip to content

Commit 178e6cc

Browse files
glenjaminevilebottnawi
authored andcommitted
feat: automatically add the HMR plugin when hot or hotOnly is enabled (#1612)
1 parent 4b7a828 commit 178e6cc

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lib/utils/addEntries.js

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
array-bracket-spacing,
77
space-before-function-paren
88
*/
9+
const webpack = require('webpack');
910
const createDomain = require('./createDomain');
1011

1112
function addEntries (config, options, server) {
@@ -48,6 +49,13 @@ function addEntries (config, options, server) {
4849

4950
[].concat(config).forEach((config) => {
5051
config.entry = prependEntry(config.entry || './src');
52+
53+
if (options.hot || options.hotOnly) {
54+
config.plugins = config.plugins || [];
55+
if (!config.plugins.find(plugin => plugin.constructor === webpack.HotModuleReplacementPlugin)) {
56+
config.plugins.push(new webpack.HotModuleReplacementPlugin());
57+
}
58+
}
5159
});
5260
}
5361
}

test/Entry.test.js

+73
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
const path = require('path');
99
const assert = require('assert');
1010

11+
const webpack = require('webpack');
12+
1113
const addEntries = require('../lib/utils/addEntries');
1214
const config = require('./fixtures/simple-config/webpack.config');
1315

@@ -174,4 +176,75 @@ describe('Entry', () => {
174176
);
175177
assert.equal(hotClientScript, require.resolve(hotClientScript));
176178
});
179+
180+
it('doesn\'t add the HMR plugin if not hot and no plugins', () => {
181+
const webpackOptions = Object.assign({}, config);
182+
const devServerOptions = { };
183+
184+
addEntries(webpackOptions, devServerOptions);
185+
186+
assert.equal('plugins' in webpackOptions, false);
187+
});
188+
it('doesn\'t add the HMR plugin if not hot and empty plugins', () => {
189+
const webpackOptions = Object.assign({}, config, { plugins: [] });
190+
const devServerOptions = { };
191+
192+
addEntries(webpackOptions, devServerOptions);
193+
194+
assert.deepStrictEqual(webpackOptions.plugins, []);
195+
});
196+
it('doesn\'t add the HMR plugin if not hot and some plugins', () => {
197+
const existingPlugin1 = new webpack.BannerPlugin('happy birthday');
198+
const existingPlugin2 = new webpack.DefinePlugin({ foo: 'bar' });
199+
const webpackOptions = Object.assign({}, config, {
200+
plugins: [existingPlugin1, existingPlugin2]
201+
});
202+
const devServerOptions = { };
203+
204+
addEntries(webpackOptions, devServerOptions);
205+
206+
assert.deepStrictEqual(
207+
webpackOptions.plugins,
208+
[existingPlugin1, existingPlugin2]
209+
);
210+
});
211+
it('adds the HMR plugin if hot', () => {
212+
const existingPlugin = new webpack.BannerPlugin('bruce');
213+
const webpackOptions = Object.assign({}, config, {
214+
plugins: [existingPlugin]
215+
});
216+
const devServerOptions = { hot: true };
217+
218+
addEntries(webpackOptions, devServerOptions);
219+
220+
assert.deepStrictEqual(
221+
webpackOptions.plugins,
222+
[existingPlugin, new webpack.HotModuleReplacementPlugin()]
223+
);
224+
});
225+
it('adds the HMR plugin if hot-only', () => {
226+
const webpackOptions = Object.assign({}, config);
227+
const devServerOptions = { hotOnly: true };
228+
229+
addEntries(webpackOptions, devServerOptions);
230+
231+
assert.deepStrictEqual(
232+
webpackOptions.plugins,
233+
[new webpack.HotModuleReplacementPlugin()]
234+
);
235+
});
236+
it('doesn\'t add the HMR plugin again if it\'s already there', () => {
237+
const existingPlugin = new webpack.BannerPlugin('bruce');
238+
const webpackOptions = Object.assign({}, config, {
239+
plugins: [new webpack.HotModuleReplacementPlugin(), existingPlugin]
240+
});
241+
const devServerOptions = { hot: true };
242+
243+
addEntries(webpackOptions, devServerOptions);
244+
245+
assert.deepStrictEqual(
246+
webpackOptions.plugins,
247+
[new webpack.HotModuleReplacementPlugin(), existingPlugin]
248+
);
249+
});
177250
});

0 commit comments

Comments
 (0)