This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps.
It builds on top of a new webpack v4 feature (module types) and requires webpack 4 to work.
Compared to the extract-text-webpack-plugin:
- Async loading
- No duplicate compilation (performance)
- Easier to use
- Specific to CSS
TODO:
- HMR support
Install
npm install --save-dev mini-css-extract-plugin
Usage
Configuration
Minimal example
webpack.config.js
const MiniCssExtractPlugin = ;moduleexports = plugins: // Options similar to the same options in webpackOptions.output // both options are optional filename: "[name].css" chunkFilename: "[id].css" module: rules: test: /\.css$/ use: loader: MiniCssExtractPluginloader options: // you can specify a publicPath here // by default it use publicPath in webpackOptions.output publicPath: '../' "css-loader"
Advanced configuration example
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
.
Here is an example to have both HMR in development
and your styles extracted in a file for production
builds.
(Loaders options left out for clarity, adapt accordingly to your needs.)
webpack.config.js
const MiniCssExtractPlugin = ;const devMode = processenvNODE_ENV !== 'production' moduleexports = plugins: // Options similar to the same options in webpackOptions.output // both options are optional filename: devMode ? '[name].css' : '[name].[hash].css' chunkFilename: devMode ? '[id].css' : '[id].[hash].css' module: rules: test: /\.ss$/ use: devMode ? 'style-loader' : MiniCssExtractPluginloader 'css-loader' 'postcss-loader' 'sass-loader'
Minimizing For Production
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. Setting optimization.minimizer
overrides the defaults provided by webpack, so make sure to also specify a JS minimizer:
webpack.config.js
const UglifyJsPlugin = ;const MiniCssExtractPlugin = ;const OptimizeCSSAssetsPlugin = ;moduleexports = optimization: minimizer: cache: true parallel: true sourceMap: true // set to true if you want JS source maps {} plugins: filename: "[name].css" chunkFilename: "[id].css" module: rules: test: /\.css$/ use: MiniCssExtractPluginloader "css-loader"
Features
Using preloaded or inlined CSS
The runtime code detects already added CSS via <link>
or <style>
tag.
This can be useful when injecting CSS on server-side for Server-Side-Rendering.
The href
of the <link>
tag has to match the URL that will be used for loading the CSS chunk.
The data-href
attribute can be used for <link>
and <style>
too.
When inlining CSS data-href
must be used.
Extracting all CSS in a single file
Similar to what extract-text-webpack-plugin does, the CSS
can be extracted in one CSS file using optimization.splitChunks.cacheGroups
.
webpack.config.js
const MiniCssExtractPlugin = ;moduleexports = optimization: splitChunks: cacheGroups: styles: name: 'styles' test: /\.css$/ chunks: 'all' enforce: true plugins: filename: "[name].css" module: rules: test: /\.css$/ use: MiniCssExtractPluginloader "css-loader"
Extracting CSS based on entry
You may also extract the CSS based on the webpack entry name. This is especially useful if you import routes dynamically but want to keep your CSS bundled according to entry. This also prevents the CSS duplication issue one had with the ExtractTextPlugin.
const path = ;const MiniCssExtractPlugin = ; { if missuer return ; else if mname return mname; else return false; } moduleexports = entry: foo: path bar: path optimization: splitChunks: cacheGroups: fooStyles: name: 'foo' mconstructorname === 'CssModule' && === entry chunks: 'all' enforce: true barStyles: name: 'bar' mconstructorname === 'CssModule' && === entry chunks: 'all' enforce: true plugins: filename: "[name].css" module: rules: test: /\.css$/ use: MiniCssExtractPluginloader "css-loader"
Long Term Caching
For long term caching use filename: "[contenthash].css"
. Optionally add [name]
.
Media Query Plugin
If you'd like to extract the media queries from the extracted CSS (so mobile users don't need to load desktop or tablet specific CSS anymore) you should use one of the following plugins:
Maintainers
Tobias Koppers |