|
8 | 8 | const path = require('path');
|
9 | 9 | const assert = require('assert');
|
10 | 10 |
|
| 11 | +const webpack = require('webpack'); |
| 12 | + |
11 | 13 | const addEntries = require('../lib/utils/addEntries');
|
12 | 14 | const config = require('./fixtures/simple-config/webpack.config');
|
13 | 15 |
|
@@ -174,4 +176,75 @@ describe('Entry', () => {
|
174 | 176 | );
|
175 | 177 | assert.equal(hotClientScript, require.resolve(hotClientScript));
|
176 | 178 | });
|
| 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 | + }); |
177 | 250 | });
|
0 commit comments