Skip to content

Commit 2a3b4a8

Browse files
refactor: next
1 parent b935f26 commit 2a3b4a8

33 files changed

+8514
-3674
lines changed

README.md

+46-39
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ module.exports = {
155155
### `esModule`
156156

157157
Type: `Boolean`
158-
Default: `false`
158+
Default: `true`
159159

160-
By default, `mini-css-extract-plugin` generates JS modules that use the CommonJS modules syntax.
160+
By default, `mini-css-extract-plugin` generates JS modules that use the ES modules syntax.
161161
There are some cases in which using ES modules is beneficial, like in the case of [module concatenation](https://webpack.js.org/plugins/module-concatenation-plugin/) and [tree shaking](https://webpack.js.org/guides/tree-shaking/).
162162

163-
You can enable a ES module syntax using:
163+
You can enable a CommonJS syntax using:
164164

165165
**webpack.config.js**
166166

@@ -177,7 +177,7 @@ module.exports = {
177177
{
178178
loader: MiniCssExtractPlugin.loader,
179179
options: {
180-
esModule: true,
180+
esModule: false,
181181
},
182182
},
183183
'css-loader',
@@ -297,7 +297,6 @@ module.exports = {
297297
// you can specify a publicPath here
298298
// by default it uses publicPath in webpackOptions.output
299299
publicPath: '../',
300-
hmr: process.env.NODE_ENV === 'development', // webpack 4 only
301300
},
302301
},
303302
'css-loader',
@@ -356,32 +355,37 @@ Here is an example to have both HMR in `development` and your styles extracted i
356355

357356
(Loaders options left out for clarity, adapt accordingly to your needs.)
358357

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+
359361
**webpack.config.js**
360362

361363
```js
364+
const webpack = require('webpack');
362365
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
363366
const devMode = process.env.NODE_ENV !== 'production';
364367

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+
365381
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,
374383
module: {
375384
rules: [
376385
{
377386
test: /\.(sa|sc|c)ss$/,
378387
use: [
379-
{
380-
loader: MiniCssExtractPlugin.loader,
381-
options: {
382-
hmr: process.env.NODE_ENV === 'development', // webpack 4 only
383-
},
384-
},
388+
MiniCssExtractPlugin.loader,
385389
'css-loader',
386390
'postcss-loader',
387391
'sass-loader',
@@ -400,37 +404,38 @@ The `mini-css-extract-plugin` supports hot reloading of actual css files in deve
400404
Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules.
401405
Below is an example configuration of mini-css for HMR use with CSS modules.
402406

403-
While we attempt to hmr css-modules. It is not easy to perform when code-splitting with custom chunk names.
404-
`reloadAll` is an option that should only be enabled if HMR isn't working correctly.
405-
The core challenge with css-modules is that when code-split, the chunk ids can and do end up different compared to the filename.
407+
You should not use `HotModuleReplacementPlugin` plugin if you are using a `webpack-dev-server`.
408+
`webpack-dev-server` enables / disables HMR using `hot` option.
406409

407410
**webpack.config.js**
408411

409412
```js
413+
const webpack = require('webpack');
410414
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
411415

416+
const plugins = [
417+
new MiniCssExtractPlugin({
418+
// Options similar to the same options in webpackOptions.output
419+
// both options are optional
420+
filename: devMode ? '[name].css' : '[name].[hash].css',
421+
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
422+
}),
423+
];
424+
if (devMode) {
425+
// only enable hot in development
426+
plugins.push(new webpack.HotModuleReplacementPlugin());
427+
}
428+
412429
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-
],
430+
plugins,
421431
module: {
422432
rules: [
423433
{
424434
test: /\.css$/,
425435
use: [
426436
{
427437
loader: MiniCssExtractPlugin.loader,
428-
options: {
429-
// only enable hot in development (webpack 4 only)
430-
hmr: process.env.NODE_ENV === 'development',
431-
// if hmr does not work, this is a forceful method.
432-
reloadAll: true,
433-
},
438+
options: {},
434439
},
435440
'css-loader',
436441
],
@@ -580,9 +585,11 @@ module.exports = {
580585
};
581586
```
582587

583-
### Module Filename Option
588+
### Filename Option as function
584589

585-
With the `moduleFilename` option you can use chunk data to customize the filename. This is particularly useful when dealing with multiple entry points and wanting to get more control out of the filename for a given entry point/chunk. In the example below, we'll use `moduleFilename` to output the generated css into a different directory.
590+
With the `filename` option you can use chunk data to customize the filename.
591+
This is particularly useful when dealing with multiple entry points and wanting to get more control out of the filename for a given entry point/chunk.
592+
In the example below, we'll use `filename` to output the generated css into a different directory.
586593

587594
**webpack.config.js**
588595

@@ -592,7 +599,7 @@ const MiniCssExtractPlugin = require('mini-css-extract-plugin');
592599
module.exports = {
593600
plugins: [
594601
new MiniCssExtractPlugin({
595-
moduleFilename: ({ name }) => `${name.replace('/js/', '/css/')}.css`,
602+
filename: ({ chunk }) => `${chunk.name.replace('/js/', '/css/')}.css`,
596603
}),
597604
],
598605
module: {

0 commit comments

Comments
 (0)