UNPKG

3.65 kBMarkdownView Raw
1# purgecss-webpack-plugin
2
3[![npm](https://img.shields.io/npm/v/purgecss-webpack-plugin.svg)](https://www.npmjs.com/package/purgecss-webpack-plugin)
4[![license](https://img.shields.io/github/license/fullhuman/purgecss-webpack-plugin.svg)]()
5
6[Webpack](https://github.com/webpack/webpack) plugin to remove unused css.
7
8## Install
9```sh
10npm i purgecss-webpack-plugin -D
11```
12
13## Usage
14
15### With mini-css-extract-plugin
16
17```js
18const path = require('path')
19const glob = require('glob')
20const MiniCssExtractPlugin = require('mini-css-extract-plugin')
21const PurgeCSSPlugin = require('purgecss-webpack-plugin')
22
23const PATHS = {
24 src: path.join(__dirname, 'src')
25}
26
27module.exports = {
28 entry: './src/index.js',
29 output: {
30 filename: 'bundle.js',
31 path: path.join(__dirname, 'dist')
32 },
33 optimization: {
34 splitChunks: {
35 cacheGroups: {
36 styles: {
37 name: 'styles',
38 test: /\.css$/,
39 chunks: 'all',
40 enforce: true
41 }
42 }
43 }
44 },
45 module: {
46 rules: [
47 {
48 test: /\.css$/,
49 use: [
50 MiniCssExtractPlugin.loader,
51 "css-loader"
52 ]
53 }
54 ]
55 },
56 plugins: [
57 new MiniCssExtractPlugin({
58 filename: "[name].css",
59 }),
60 new PurgeCSSPlugin({
61 paths: glob.sync(`${PATHS.src}/**/*`, { nodir: true }),
62 }),
63 ]
64}
65```
66### Multiple paths
67If you need multiple paths use the npm package `glob-all` instead of `glob`, then you can use this syntax:
68```javascript
69new PurgeCSSPlugin({
70 paths: glob.sync([
71 // ...
72 ])
73}),
74```
75to filter out directories see the glob-all documentation [here](https://www.npmjs.com/package/glob-all#filtering-out-directories).
76
77### Options
78
79The options available in purgecss [Configuration](https://www.purgecss.com/configuration.html) are also available in the webpack plugin with the exception of css and content.
80
81* #### paths
82
83With the webpack plugin, you can specify the content that should be analyzed by purgecss with an array of filename. It can be html, pug, blade, ... files. You can use a module like `glob` or `glob-all` to easily get a list of files.
84
85```js
86const PurgeCSSPlugin = require('purgecss-webpack-plugin')
87const glob = require('glob')
88const PATHS = {
89 src: path.join(__dirname, 'src')
90}
91
92// In the webpack configuration
93new PurgeCSSPlugin({
94 paths: glob.sync(`${PATHS.src}/**/*`, { nodir: true })
95})
96```
97
98If you want to regenerate the paths list on every compilation (e.g. with `--watch`), then you can also pass a function:
99```js
100new PurgeCSSPlugin({
101 paths: () => glob.sync(`${PATHS.src}/**/*`, { nodir: true })
102})
103```
104
105* #### only
106
107You can specify entrypoints to the purgecss-webpack-plugin with the option only:
108
109```js
110new PurgeCSSPlugin({
111 paths: glob.sync(`${PATHS.src}/**/*`, { nodir: true }),
112 only: ['bundle', 'vendor']
113})
114```
115
116* #### safelist
117
118Similar as for the `paths` option, you also can define a function for this option:
119
120```js
121function collectSafelist() {
122 return {
123 standard: ['safelisted', /^safelisted-/],
124 deep: [/^safelisted-deep-/],
125 greedy: [/^safelisted-greedy/]
126 }
127}
128
129// In the webpack configuration
130new PurgeCSSPlugin({
131 safelist: collectSafelist
132})
133```
134
135* #### rejected
136
137If `true` all removed selectors are added to the [Stats Data](https://webpack.js.org/api/stats/) as `purged`.
138
139## Contributing
140
141Please read [CONTRIBUTING.md](./../../CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
142
143## Versioning
144
145We use [SemVer](http://semver.org/) for versioning.
146
147## License
148
149This project is licensed under the MIT License - see the [LICENSE](./../../LICENSE) file for details