From 178e6cc3f604750620e9de557517e583924334a8 Mon Sep 17 00:00:00 2001 From: Glen Mailer Date: Thu, 27 Dec 2018 09:42:39 +0000 Subject: [PATCH] feat: automatically add the HMR plugin when hot or hotOnly is enabled (#1612) --- lib/utils/addEntries.js | 8 +++++ test/Entry.test.js | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/lib/utils/addEntries.js b/lib/utils/addEntries.js index d74412c9fb..dd3c5e5365 100644 --- a/lib/utils/addEntries.js +++ b/lib/utils/addEntries.js @@ -6,6 +6,7 @@ array-bracket-spacing, space-before-function-paren */ +const webpack = require('webpack'); const createDomain = require('./createDomain'); function addEntries (config, options, server) { @@ -48,6 +49,13 @@ function addEntries (config, options, server) { [].concat(config).forEach((config) => { config.entry = prependEntry(config.entry || './src'); + + if (options.hot || options.hotOnly) { + config.plugins = config.plugins || []; + if (!config.plugins.find(plugin => plugin.constructor === webpack.HotModuleReplacementPlugin)) { + config.plugins.push(new webpack.HotModuleReplacementPlugin()); + } + } }); } } diff --git a/test/Entry.test.js b/test/Entry.test.js index 6f6501ce60..b694c702ba 100644 --- a/test/Entry.test.js +++ b/test/Entry.test.js @@ -8,6 +8,8 @@ const path = require('path'); const assert = require('assert'); +const webpack = require('webpack'); + const addEntries = require('../lib/utils/addEntries'); const config = require('./fixtures/simple-config/webpack.config'); @@ -174,4 +176,75 @@ describe('Entry', () => { ); assert.equal(hotClientScript, require.resolve(hotClientScript)); }); + + it('doesn\'t add the HMR plugin if not hot and no plugins', () => { + const webpackOptions = Object.assign({}, config); + const devServerOptions = { }; + + addEntries(webpackOptions, devServerOptions); + + assert.equal('plugins' in webpackOptions, false); + }); + it('doesn\'t add the HMR plugin if not hot and empty plugins', () => { + const webpackOptions = Object.assign({}, config, { plugins: [] }); + const devServerOptions = { }; + + addEntries(webpackOptions, devServerOptions); + + assert.deepStrictEqual(webpackOptions.plugins, []); + }); + it('doesn\'t add the HMR plugin if not hot and some plugins', () => { + const existingPlugin1 = new webpack.BannerPlugin('happy birthday'); + const existingPlugin2 = new webpack.DefinePlugin({ foo: 'bar' }); + const webpackOptions = Object.assign({}, config, { + plugins: [existingPlugin1, existingPlugin2] + }); + const devServerOptions = { }; + + addEntries(webpackOptions, devServerOptions); + + assert.deepStrictEqual( + webpackOptions.plugins, + [existingPlugin1, existingPlugin2] + ); + }); + it('adds the HMR plugin if hot', () => { + const existingPlugin = new webpack.BannerPlugin('bruce'); + const webpackOptions = Object.assign({}, config, { + plugins: [existingPlugin] + }); + const devServerOptions = { hot: true }; + + addEntries(webpackOptions, devServerOptions); + + assert.deepStrictEqual( + webpackOptions.plugins, + [existingPlugin, new webpack.HotModuleReplacementPlugin()] + ); + }); + it('adds the HMR plugin if hot-only', () => { + const webpackOptions = Object.assign({}, config); + const devServerOptions = { hotOnly: true }; + + addEntries(webpackOptions, devServerOptions); + + assert.deepStrictEqual( + webpackOptions.plugins, + [new webpack.HotModuleReplacementPlugin()] + ); + }); + it('doesn\'t add the HMR plugin again if it\'s already there', () => { + const existingPlugin = new webpack.BannerPlugin('bruce'); + const webpackOptions = Object.assign({}, config, { + plugins: [new webpack.HotModuleReplacementPlugin(), existingPlugin] + }); + const devServerOptions = { hot: true }; + + addEntries(webpackOptions, devServerOptions); + + assert.deepStrictEqual( + webpackOptions.plugins, + [new webpack.HotModuleReplacementPlugin(), existingPlugin] + ); + }); });