@@ -297,7 +297,6 @@ module.exports = {
297
297
// you can specify a publicPath here
298
298
// by default it uses publicPath in webpackOptions.output
299
299
publicPath: ' ../' ,
300
- hmr: process .env .NODE_ENV === ' development' , // webpack 4 only
301
300
},
302
301
},
303
302
' css-loader' ,
@@ -356,32 +355,37 @@ Here is an example to have both HMR in `development` and your styles extracted i
356
355
357
356
(Loaders options left out for clarity, adapt accordingly to your needs.)
358
357
358
+ You should not use ` HotModuleReplacementPlugin ` plugin if you are using a ` webpack-dev-server ` .
359
+ ` webpack-dev-server ` enables / disables HMR using ` hot ` option.
360
+
359
361
** webpack.config.js**
360
362
361
363
``` js
364
+ const webpack = require (' webpack' );
362
365
const MiniCssExtractPlugin = require (' mini-css-extract-plugin' );
363
366
const devMode = process .env .NODE_ENV !== ' production' ;
364
367
368
+ const plugins = [
369
+ new MiniCssExtractPlugin ({
370
+ // Options similar to the same options in webpackOptions.output
371
+ // both options are optional
372
+ filename: devMode ? ' [name].css' : ' [name].[hash].css' ,
373
+ chunkFilename: devMode ? ' [id].css' : ' [id].[hash].css' ,
374
+ }),
375
+ ];
376
+ if (devMode) {
377
+ // only enable hot in development
378
+ plugins .push (new webpack.HotModuleReplacementPlugin ());
379
+ }
380
+
365
381
module .exports = {
366
- plugins: [
367
- new MiniCssExtractPlugin ({
368
- // Options similar to the same options in webpackOptions.output
369
- // both options are optional
370
- filename: devMode ? ' [name].css' : ' [name].[hash].css' ,
371
- chunkFilename: devMode ? ' [id].css' : ' [id].[hash].css' ,
372
- }),
373
- ],
382
+ plugins,
374
383
module: {
375
384
rules: [
376
385
{
377
386
test: / \. (sa| sc| c)ss$ / ,
378
387
use: [
379
- {
380
- loader: MiniCssExtractPlugin .loader ,
381
- options: {
382
- hmr: process .env .NODE_ENV === ' development' , // webpack 4 only
383
- },
384
- },
388
+ MiniCssExtractPlugin .loader ,
385
389
' css-loader' ,
386
390
' postcss-loader' ,
387
391
' sass-loader' ,
@@ -404,20 +408,30 @@ While we attempt to hmr css-modules. It is not easy to perform when code-splitti
404
408
` reloadAll ` is an option that should only be enabled if HMR isn't working correctly.
405
409
The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename.
406
410
411
+ You should not use ` HotModuleReplacementPlugin ` plugin if you are using a ` webpack-dev-server ` .
412
+ ` webpack-dev-server ` enables / disables HMR using ` hot ` option.
413
+
407
414
** webpack.config.js**
408
415
409
416
``` js
417
+ const webpack = require (' webpack' );
410
418
const MiniCssExtractPlugin = require (' mini-css-extract-plugin' );
411
419
420
+ const plugins = [
421
+ new MiniCssExtractPlugin ({
422
+ // Options similar to the same options in webpackOptions.output
423
+ // both options are optional
424
+ filename: devMode ? ' [name].css' : ' [name].[hash].css' ,
425
+ chunkFilename: devMode ? ' [id].css' : ' [id].[hash].css' ,
426
+ }),
427
+ ];
428
+ if (devMode) {
429
+ // only enable hot in development
430
+ plugins .push (new webpack.HotModuleReplacementPlugin ());
431
+ }
432
+
412
433
module .exports = {
413
- plugins: [
414
- new MiniCssExtractPlugin ({
415
- // Options similar to the same options in webpackOptions.output
416
- // both options are optional
417
- filename: ' [name].css' ,
418
- chunkFilename: ' [id].css' ,
419
- }),
420
- ],
434
+ plugins,
421
435
module: {
422
436
rules: [
423
437
{
@@ -426,8 +440,6 @@ module.exports = {
426
440
{
427
441
loader: MiniCssExtractPlugin .loader ,
428
442
options: {
429
- // only enable hot in development (webpack 4 only)
430
- hmr: process .env .NODE_ENV === ' development' ,
431
443
// if hmr does not work, this is a forceful method.
432
444
reloadAll: true ,
433
445
},
0 commit comments