Skip to content

Commit d7d0afc

Browse files
sylverjoshwiens
authored andcommitted
docs: Add an advanced configuration example (#101)
1 parent e200b08 commit d7d0afc

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

README.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<img width="200" height="200" vspace="" hspace="25" src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon-square-big.svg">
1212
</a>
1313
<h1>mini-css-extract-plugin</h1>
14-
<p>desc</p>
1514
</div>
1615

1716
This plugin extract CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.
@@ -39,6 +38,8 @@ npm install --save-dev mini-css-extract-plugin
3938

4039
### Configuration
4140

41+
#### Minimal example
42+
4243
**webpack.config.js**
4344

4445
```js
@@ -66,6 +67,46 @@ module.exports = {
6667
}
6768
```
6869

70+
#### Advanced configuration example
71+
72+
This plugin should be used only on `production` builds without `style-loader` in the loaders chain, especially if you want to have HMR in `development`.
73+
74+
Here is an example to have both HMR in `development` and your styles extracted in a file for `production` builds.
75+
76+
(Loaders options left out for clarity, adapt accordingly to your needs.)
77+
78+
79+
**webpack.config.js**
80+
81+
```js
82+
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
83+
const devMode = process.env.NODE_ENV !== 'production'
84+
85+
module.exports = {
86+
plugins: [
87+
new MiniCssExtractPlugin({
88+
// Options similar to the same options in webpackOptions.output
89+
// both options are optional
90+
filename: devMode ? '[name].css' : '[name].[hash].css',
91+
chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
92+
})
93+
],
94+
module: {
95+
rules: [
96+
{
97+
test: /\.s?[ac]ss$/,
98+
use: [
99+
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
100+
'css-loader',
101+
'postcss-loader',
102+
'sass-loader',
103+
],
104+
}
105+
]
106+
}
107+
}
108+
```
109+
69110
### Minimizing For Production
70111

71112
While webpack 5 is likely to come with a CSS minimizer built-in, with webpack 4 you need to bring your own. To minify the output, use a plugin like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin). Setting `optimization.minimizer` overrides the defaults provided by webpack, so make sure to also specify a JS minimizer:

0 commit comments

Comments
 (0)