Skip to content

Commit d31013d

Browse files
docs: recommend (webpack-contrib#757)
1 parent 750ab74 commit d31013d

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

README.md

+32-37
Original file line numberDiff line numberDiff line change
@@ -503,74 +503,69 @@ module.exports = {
503503

504504
## Examples
505505

506-
### Minimal example
506+
### Recommend
507+
508+
For `production` builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on.
509+
This can be achieved by using the `mini-css-extract-plugin`, because it creates separate css files.
510+
For `development` mode (including `webpack-dev-server`) you can use [style-loader](https://github.com/webpack-contrib/style-loader), because it injects CSS into the DOM using multiple <style></style> and works faster.
511+
512+
> i Do not use together `style-loader` and `mini-css-extract-plugin`.
507513
508514
**webpack.config.js**
509515

510516
```js
511517
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
518+
const devMode = process.env.NODE_ENV !== 'production';
512519

513520
module.exports = {
514-
plugins: [
515-
new MiniCssExtractPlugin({
516-
// Options similar to the same options in webpackOptions.output
517-
// all options are optional
518-
filename: '[name].css',
519-
chunkFilename: '[id].css',
520-
ignoreOrder: false, // Enable to remove warnings about conflicting order
521-
}),
522-
],
523521
module: {
524522
rules: [
525523
{
526-
test: /\.css$/,
524+
test: /\.(sa|sc|c)ss$/,
527525
use: [
528-
{
529-
loader: MiniCssExtractPlugin.loader,
530-
options: {
531-
// you can specify a publicPath here
532-
// by default it uses publicPath in webpackOptions.output
533-
publicPath: '../',
534-
},
535-
},
526+
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
536527
'css-loader',
528+
'postcss-loader',
529+
'sass-loader',
537530
],
538531
},
539532
],
540533
},
534+
plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]),
541535
};
542536
```
543537

544-
### Common use case
545-
546-
`mini-css-extract-plugin` is more often used in `production` mode to get separate css files.
547-
For `development` mode (including `webpack-dev-server`) you can use `style-loader`, because it injects CSS into the DOM using multiple `<style></style>` and works faster.
548-
549-
> ⚠️ Do not use `style-loader` and `mini-css-extract-plugin` together.
538+
### Minimal example
550539

551540
**webpack.config.js**
552541

553542
```js
554543
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
555-
const devMode = process.env.NODE_ENV !== 'production';
556-
557-
const plugins = [];
558-
if (!devMode) {
559-
// enable in production only
560-
plugins.push(new MiniCssExtractPlugin());
561-
}
562544

563545
module.exports = {
564-
plugins,
546+
plugins: [
547+
new MiniCssExtractPlugin({
548+
// Options similar to the same options in webpackOptions.output
549+
// all options are optional
550+
filename: '[name].css',
551+
chunkFilename: '[id].css',
552+
ignoreOrder: false, // Enable to remove warnings about conflicting order
553+
}),
554+
],
565555
module: {
566556
rules: [
567557
{
568-
test: /\.(sa|sc|c)ss$/,
558+
test: /\.css$/,
569559
use: [
570-
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
560+
{
561+
loader: MiniCssExtractPlugin.loader,
562+
options: {
563+
// you can specify a publicPath here
564+
// by default it uses publicPath in webpackOptions.output
565+
publicPath: '../',
566+
},
567+
},
571568
'css-loader',
572-
'postcss-loader',
573-
'sass-loader',
574569
],
575570
},
576571
],

0 commit comments

Comments
 (0)