MiniCssExtractPlugin

[![npm][npm]][npm-url] [![deps][deps]][deps-url] [![tests][tests]][tests-url] [![coverage][cover]][cover-url] [![chat][chat]][chat-url]

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

  1. npm install --save-dev mini-css-extract-plugin

Usage

#

Minimal example

webpack.config.js

  1. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  2. module.exports = {
  3. plugins: [
  4. new MiniCssExtractPlugin({
  5. // Options similar to the same options in webpackOptions.output
  6. // both options are optional
  7. filename: "[name].css",
  8. chunkFilename: "[id].css"
  9. })
  10. ],
  11. module: {
  12. rules: [
  13. {
  14. test: /\.css$/,
  15. use: [
  16. {
  17. loader: MiniCssExtractPlugin.loader,
  18. options: {
  19. // you can specify a publicPath here
  20. // by default it use publicPath in webpackOptions.output
  21. publicPath: '../'
  22. }
  23. },
  24. "css-loader"
  25. ]
  26. }
  27. ]
  28. }
  29. }

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

  1. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  2. const devMode = process.env.NODE_ENV !== 'production'
  3. module.exports = {
  4. plugins: [
  5. new MiniCssExtractPlugin({
  6. // Options similar to the same options in webpackOptions.output
  7. // both options are optional
  8. filename: devMode ? '[name].css' : '[name].[hash].css',
  9. chunkFilename: devMode ? '[id].css' : '[id].[hash].css',
  10. })
  11. ],
  12. module: {
  13. rules: [
  14. {
  15. test: /\.(sa|sc|c)ss$/,
  16. use: [
  17. devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
  18. 'css-loader',
  19. 'postcss-loader',
  20. 'sass-loader',
  21. ],
  22. }
  23. ]
  24. }
  25. }

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

  1. const TerserJSPlugin = require("terser-webpack-plugin");
  2. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  3. const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
  4. module.exports = {
  5. optimization: {
  6. minimizer: [
  7. new TerserJSPlugin({}),
  8. new OptimizeCSSAssetsPlugin({})
  9. ]
  10. },
  11. plugins: [
  12. new MiniCssExtractPlugin({
  13. filename: "[name].css",
  14. chunkFilename: "[id].css"
  15. })
  16. ],
  17. module: {
  18. rules: [
  19. {
  20. test: /\.css$/,
  21. use: [
  22. MiniCssExtractPlugin.loader,
  23. "css-loader"
  24. ]
  25. }
  26. ]
  27. }
  28. }

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

  1. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  2. module.exports = {
  3. optimization: {
  4. splitChunks: {
  5. cacheGroups: {
  6. styles: {
  7. name: 'styles',
  8. test: /\.css$/,
  9. chunks: 'all',
  10. enforce: true
  11. }
  12. }
  13. }
  14. },
  15. plugins: [
  16. new MiniCssExtractPlugin({
  17. filename: "[name].css",
  18. })
  19. ],
  20. module: {
  21. rules: [
  22. {
  23. test: /\.css$/,
  24. use: [
  25. MiniCssExtractPlugin.loader,
  26. "css-loader"
  27. ]
  28. }
  29. ]
  30. }
  31. }

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.

  1. const path = require('path');
  2. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  3. function recursiveIssuer(m) {
  4. if (m.issuer) {
  5. return recursiveIssuer(m.issuer);
  6. } else if (m.name) {
  7. return m.name;
  8. } else {
  9. return false;
  10. }
  11. }
  12. module.exports = {
  13. entry: {
  14. foo: path.resolve(__dirname, 'src/foo'),
  15. bar: path.resolve(__dirname, 'src/bar')
  16. },
  17. optimization: {
  18. splitChunks: {
  19. cacheGroups: {
  20. fooStyles: {
  21. name: 'foo',
  22. test: (m,c,entry = 'foo') => m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
  23. chunks: 'all',
  24. enforce: true
  25. },
  26. barStyles: {
  27. name: 'bar',
  28. test: (m,c,entry = 'bar') => m.constructor.name === 'CssModule' && recursiveIssuer(m) === entry,
  29. chunks: 'all',
  30. enforce: true
  31. }
  32. }
  33. }
  34. },
  35. plugins: [
  36. new MiniCssExtractPlugin({
  37. filename: "[name].css",
  38. })
  39. ],
  40. module: {
  41. rules: [
  42. {
  43. test: /\.css$/,
  44. use: [
  45. MiniCssExtractPlugin.loader,
  46. "css-loader"
  47. ]
  48. }
  49. ]
  50. }
  51. }

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

MiniCssExtractPlugin - 图1


  1. Tobias Koppers

License

MIT