diff --git a/README.md b/README.md index 43d53043..297846ea 100644 --- a/README.md +++ b/README.md @@ -503,74 +503,69 @@ module.exports = { ## Examples -### Minimal example +### Recommend + +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. +This can be achieved by using the `mini-css-extract-plugin`, because it creates separate css files. +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 and works faster. + +> i Do not use together `style-loader` and `mini-css-extract-plugin`. **webpack.config.js** ```js const MiniCssExtractPlugin = require('mini-css-extract-plugin'); +const devMode = process.env.NODE_ENV !== 'production'; module.exports = { - plugins: [ - new MiniCssExtractPlugin({ - // Options similar to the same options in webpackOptions.output - // all options are optional - filename: '[name].css', - chunkFilename: '[id].css', - ignoreOrder: false, // Enable to remove warnings about conflicting order - }), - ], module: { rules: [ { - test: /\.css$/, + test: /\.(sa|sc|c)ss$/, use: [ - { - loader: MiniCssExtractPlugin.loader, - options: { - // you can specify a publicPath here - // by default it uses publicPath in webpackOptions.output - publicPath: '../', - }, - }, + devMode ? 'style-loader' : MiniCssExtractPlugin.loader, 'css-loader', + 'postcss-loader', + 'sass-loader', ], }, ], }, + plugins: [].concat(devMode ? [] : [new MiniCssExtractPlugin()]), }; ``` -### Common use case - -`mini-css-extract-plugin` is more often used in `production` mode to get separate css files. -For `development` mode (including `webpack-dev-server`) you can use `style-loader`, because it injects CSS into the DOM using multiple `` and works faster. - -> ⚠️ Do not use `style-loader` and `mini-css-extract-plugin` together. +### Minimal example **webpack.config.js** ```js const MiniCssExtractPlugin = require('mini-css-extract-plugin'); -const devMode = process.env.NODE_ENV !== 'production'; - -const plugins = []; -if (!devMode) { - // enable in production only - plugins.push(new MiniCssExtractPlugin()); -} module.exports = { - plugins, + plugins: [ + new MiniCssExtractPlugin({ + // Options similar to the same options in webpackOptions.output + // all options are optional + filename: '[name].css', + chunkFilename: '[id].css', + ignoreOrder: false, // Enable to remove warnings about conflicting order + }), + ], module: { rules: [ { - test: /\.(sa|sc|c)ss$/, + test: /\.css$/, use: [ - devMode ? 'style-loader' : MiniCssExtractPlugin.loader, + { + loader: MiniCssExtractPlugin.loader, + options: { + // you can specify a publicPath here + // by default it uses publicPath in webpackOptions.output + publicPath: '../', + }, + }, 'css-loader', - 'postcss-loader', - 'sass-loader', ], }, ],