|
| 1 | +# cssnano-webpack-plugin |
| 2 | + |
| 3 | +This plugin uses [cssnano](https://cssnano.co) to optimize and minify your CSS. |
| 4 | + |
| 5 | +Fully integrated in Webpack ecosystem: based on compiler hooks, respecting default Webpack output sources and compatible with other plugins like *SourceMapDevToolPlugin* or *webpack-subresource-integrity*. |
| 6 | + |
| 7 | +Just like [optimize-css-assets-webpack-plugin](https://github.com/NMFR/optimize-css-assets-webpack-plugin) but more accurate with source maps and assets using query string. |
| 8 | + |
| 9 | +Works with Webpack 4+. |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +First, install `cssnano-webpack-plugin`: |
| 14 | + |
| 15 | +```console |
| 16 | +$ npm install cssnano-webpack-plugin --save-dev |
| 17 | +``` |
| 18 | + |
| 19 | +Then add the plugin to your `webpack` configuration. For example: |
| 20 | + |
| 21 | +**webpack.config.js** |
| 22 | + |
| 23 | +```js |
| 24 | +const CssnanoPlugin = require('cssnano-webpack-plugin'); |
| 25 | + |
| 26 | +module.exports = { |
| 27 | + module: { |
| 28 | + loaders: [ |
| 29 | + { |
| 30 | + test: /.s?css$/, |
| 31 | + use: [ |
| 32 | + MiniCssExtractPlugin.loader, |
| 33 | + 'css-loader', |
| 34 | + 'sass-loader' |
| 35 | + ] |
| 36 | + } |
| 37 | + ] |
| 38 | + }, |
| 39 | + optimization: { |
| 40 | + minimizer: [ |
| 41 | + new CssnanoPlugin() |
| 42 | + ] |
| 43 | + } |
| 44 | +}; |
| 45 | +``` |
| 46 | + |
| 47 | +This will enable CSS optimization only in production mode. If you want to run it also in development, put the plugin configuration in the `plugins` option array. |
| 48 | + |
| 49 | +## Options |
| 50 | + |
| 51 | +### `test` |
| 52 | + |
| 53 | +Type: `String|RegExp|Array<String|RegExp>` - default: `/\.css(\?.*)?$/i` |
| 54 | + |
| 55 | +Test to match files against. |
| 56 | + |
| 57 | +```js |
| 58 | +module.exports = { |
| 59 | + optimization: { |
| 60 | + minimizer: [ |
| 61 | + new CssnanoPlugin({ |
| 62 | + test: /\.foo\.css$/i |
| 63 | + }) |
| 64 | + ] |
| 65 | + } |
| 66 | +}; |
| 67 | +``` |
| 68 | + |
| 69 | +`include` and `exclude` options are also supported (see [module rules](https://webpack.js.org/configuration/module)). |
| 70 | + |
| 71 | +### `sourceMap` |
| 72 | + |
| 73 | +Type: `Boolean|Object` - default: `false` |
| 74 | + |
| 75 | +Enable (and configure) source map support. Use [PostCss SourceMap options](https://github.com/postcss/postcss-loader#sourcemap). Default configuration when enabled: `{ inline: false }`. |
| 76 | + |
| 77 | +```js |
| 78 | +module.exports = { |
| 79 | + optimization: { |
| 80 | + minimizer: [ |
| 81 | + new CssnanoPlugin({ |
| 82 | + sourceMap: true |
| 83 | + }) |
| 84 | + ] |
| 85 | + } |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +### `cssnanoOptions` |
| 90 | + |
| 91 | +Type: `Object` - default: `{ preset: 'default' }` |
| 92 | + |
| 93 | +Cssnano optimisations [options](https://cssnano.co/guides/optimisations). |
| 94 | + |
| 95 | +```js |
| 96 | +module.exports = { |
| 97 | + optimization: { |
| 98 | + minimizer: [ |
| 99 | + new CssnanoPlugin({ |
| 100 | + cssnanoOptions: ['default', { |
| 101 | + discardComments: { removeAll: true } |
| 102 | + }] |
| 103 | + }) |
| 104 | + ] |
| 105 | + } |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +## Examples |
| 110 | + |
| 111 | +### Use sourcemaps |
| 112 | + |
| 113 | +Don't forget to enable `sourceMap` options for all loaders. |
| 114 | + |
| 115 | +```js |
| 116 | +const CssnanoPlugin = require('cssnano-webpack-plugin'); |
| 117 | + |
| 118 | +module.exports = { |
| 119 | + module: { |
| 120 | + loaders: [ |
| 121 | + { |
| 122 | + test: /.s?css$/, |
| 123 | + use: [ |
| 124 | + MiniCssExtractPlugin.loader, |
| 125 | + { loader: 'css-loader', options: { sourceMap: true } }, |
| 126 | + { loader: 'sass-loader', options: { sourceMap: true } } |
| 127 | + ] |
| 128 | + } |
| 129 | + ] |
| 130 | + }, |
| 131 | + optimization: { |
| 132 | + minimizer: [ |
| 133 | + new CssnanoPlugin({ |
| 134 | + sourceMap: true |
| 135 | + }) |
| 136 | + ] |
| 137 | + } |
| 138 | +}; |
| 139 | +``` |
| 140 | + |
| 141 | +### Remove all comments |
| 142 | + |
| 143 | +Remove all comments (including comments starting with `/*!`). |
| 144 | + |
| 145 | +```js |
| 146 | +module.exports = { |
| 147 | + optimization: { |
| 148 | + minimizer: [ |
| 149 | + new CssnanoPlugin({ |
| 150 | + cssProcessorPluginOptions: { |
| 151 | + preset: ['default', { discardComments: { removeAll: true } }] |
| 152 | + } |
| 153 | + }) |
| 154 | + ] |
| 155 | + } |
| 156 | +}; |
| 157 | +``` |
| 158 | + |
| 159 | +## License |
| 160 | + |
| 161 | +[MIT](./LICENSE) |
0 commit comments