UNPKG

2.79 kBMarkdownView Raw
1# Optimize CSS Assets Webpack Plugin
2
3A Webpack plugin to optimize \ minimize CSS assets.
4
5> :warning: For webpack v5 or above please use [css-minimizer-webpack-plugin](https://github.com/webpack-contrib/css-minimizer-webpack-plugin) instead.
6
7## What does the plugin do?
8
9It will search for CSS assets during the Webpack build and will optimize \ minimize the CSS (by default it uses [cssnano](http://github.com/ben-eb/cssnano) but a custom CSS processor can be specified).
10
11### Solves [extract-text-webpack-plugin](http://github.com/webpack/extract-text-webpack-plugin) CSS duplication problem:
12
13Since [extract-text-webpack-plugin](http://github.com/webpack/extract-text-webpack-plugin) only bundles (merges) text chunks, if it's used to bundle CSS, the bundle might have duplicate entries (chunks can be duplicate free but when merged, duplicate CSS can be created).
14
15## Installation:
16
17Using npm:
18```shell
19$ npm install --save-dev optimize-css-assets-webpack-plugin
20```
21
22> :warning: For webpack v3 or below please use `optimize-css-assets-webpack-plugin@3.2.0`. The `optimize-css-assets-webpack-plugin@4.0.0` version and above supports webpack v4.
23
24## Configuration:
25
26The plugin can receive the following options (all of them are optional):
27* `assetNameRegExp`: A regular expression that indicates the names of the assets that should be optimized \ minimized. The regular expression provided is run against the filenames of the files exported by the `ExtractTextPlugin` instances in your configuration, not the filenames of your source CSS files. Defaults to `/\.css$/g`
28* `cssProcessor`: The CSS processor used to optimize \ minimize the CSS, defaults to [`cssnano`](http://github.com/ben-eb/cssnano). This should be a function that follows `cssnano.process` interface (receives a CSS and options parameters and returns a Promise).
29* `cssProcessorOptions`: The options passed to the `cssProcessor`, defaults to `{}`
30* `cssProcessorPluginOptions`: The plugin options passed to the `cssProcessor`, defaults to `{}`
31* `canPrint`: A boolean indicating if the plugin can print messages to the console, defaults to `true`
32
33## Example:
34
35``` javascript
36var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
37module.exports = {
38 module: {
39 rules: [
40 {
41 test: /\.css$/,
42 loader: ExtractTextPlugin.extract('style-loader', 'css-loader')
43 }
44 ]
45 },
46 plugins: [
47 new ExtractTextPlugin('styles.css'),
48 new OptimizeCssAssetsPlugin({
49 assetNameRegExp: /\.optimize\.css$/g,
50 cssProcessor: require('cssnano'),
51 cssProcessorPluginOptions: {
52 preset: ['default', { discardComments: { removeAll: true } }],
53 },
54 canPrint: true
55 })
56 ]
57};
58```
59
60## License
61
62MIT (http://www.opensource.org/licenses/mit-license.php)
63