1 | # PostCSS Purgecss
|
2 | ![David (path)](https://img.shields.io/david/FullHuman/purgecss?path=packages%2Fpostcss-purgecss&style=for-the-badge)
|
3 | ![Dependabot](https://img.shields.io/badge/dependabot-enabled-%23024ea4?style=for-the-badge)
|
4 | ![npm](https://img.shields.io/npm/v/@fullhuman/postcss-purgecss?style=for-the-badge)
|
5 | ![npm](https://img.shields.io/npm/dw/@fullhuman/postcss-purgecss?style=for-the-badge)
|
6 | ![GitHub](https://img.shields.io/github/license/FullHuman/purgecss?style=for-the-badge)
|
7 |
|
8 | [PostCSS] plugin for PurgeCSS.
|
9 |
|
10 | [PostCSS]: https://github.com/postcss/postcss
|
11 |
|
12 | ## Installation
|
13 |
|
14 | ```
|
15 | npm i -D @fullhuman/postcss-purgecss postcss
|
16 | ```
|
17 |
|
18 | ## Usage
|
19 |
|
20 | ```js
|
21 | const purgecss = require('@fullhuman/postcss-purgecss')
|
22 | postcss([
|
23 | purgecss({
|
24 | content: ['./src/**/*.html']
|
25 | })
|
26 | ])
|
27 | ```
|
28 |
|
29 | See [PostCSS] docs for examples for your environment.
|
30 |
|
31 | ## Options
|
32 |
|
33 | All of the options of purgecss are available to use with the plugins.
|
34 | You will find below the main options available. For the complete list, go to the [purgecss documentation website](https://www.purgecss.com/configuration.html#options).
|
35 |
|
36 | ### `content` (**required** or use `contentFunction` instead)
|
37 | Type: `Array<string>`
|
38 |
|
39 | You can specify content that should be analyzed by Purgecss with an array of filenames or globs. The files can be HTML, Pug, Blade, etc.
|
40 |
|
41 | ### `contentFunction` (as alternative to `content`)
|
42 | Type: `(sourceInputFile: string) => Array<string>`
|
43 |
|
44 | The function receives the current source input file. With this you may provide a specific array of globs for each input. E.g. for
|
45 | an angular application only scan the components template counterpart for every component scss file:
|
46 |
|
47 | ```js
|
48 | purgecss({
|
49 | contentFunction: (sourceInputFileName: string) => {
|
50 | if (/component\.scss$/.test(sourceInputFileName))
|
51 | return [sourceInputFileName.replace(/scss$/, 'html')]
|
52 | else
|
53 | return ['./src/**/*.html']
|
54 | },
|
55 | })
|
56 | ```
|
57 |
|
58 | ### `extractors`
|
59 | Type: `Array<Object>`
|
60 |
|
61 | Purgecss can be adapted to suit your needs. If you notice a lot of unused CSS is not being removed, you might want to use a custom extractor.
|
62 | More information about extractors [here](https://www.purgecss.com/extractors.html).
|
63 |
|
64 | ### `safelist`
|
65 |
|
66 | You can indicate which selectors are safe to leave in the final CSS. This can be accomplished with the option `safelist`.
|
67 |
|
68 | Two forms are available for this option.
|
69 |
|
70 | ```ts
|
71 | safelist: ['random', 'yep', 'button', /^nav-/]
|
72 | ```
|
73 |
|
74 | In this form, safelist is an array that can take a string or a regex.
|
75 |
|
76 | The _complex_ form is:
|
77 |
|
78 | ```ts
|
79 | safelist: {
|
80 | standard: ['random', 'yep', 'button', /^nav-/],
|
81 | deep: [],
|
82 | greedy: [],
|
83 | keyframes: [],
|
84 | variables: []
|
85 | }
|
86 | ```
|
87 |
|
88 | ### `blocklist`
|
89 |
|
90 | Blocklist will block the CSS selectors from appearing in the final output CSS. The selectors will be removed even when they are seen as used by PurgeCSS.
|
91 |
|
92 | ```ts
|
93 | blocklist: ['usedClass', /^nav-/]
|
94 | ```
|
95 | Even if nav-links and usedClass are found by an extractor, they will be removed.
|
96 |
|
97 | ### `skippedContentGlobs`
|
98 |
|
99 | If you provide globs for the `content` parameter, you can use this option to exclude certain files or folders that would otherwise be scanned. Pass an array of globs matching items that should be excluded. (Note: this option has no effect if `content` is not globs.)
|
100 |
|
101 | ```ts
|
102 | skippedContentGlobs: ['node_modules/**', 'components/**']
|
103 | ```
|
104 | Here, PurgeCSS will not scan anything in the "node_modules" and "components" folders.
|
105 |
|
106 |
|
107 | ### `rejected`
|
108 | Type: `boolean`
|
109 | Default value: `false`
|
110 |
|
111 | If true, purged selectors will be captured and rendered as PostCSS messages.
|
112 | Use with a PostCSS reporter plugin like [`postcss-reporter`](https://github.com/postcss/postcss-reporter)
|
113 | to print the purged selectors to the console as they are processed.
|
114 |
|
115 | ### `keyframes`
|
116 | Type: `boolean`
|
117 | Default value: `false`
|
118 |
|
119 | If you are using a CSS animation library such as animate.css, you can remove unused keyframes by setting the keyframes option to true.
|
120 |
|
121 | #### `fontFace`
|
122 | Type: `boolean`
|
123 | Default value: `false`
|
124 |
|
125 | If there are any unused @font-face rules in your css, you can remove them by setting the fontFace option to true.
|
126 |
|
127 | ## Contributing
|
128 |
|
129 | Please read [CONTRIBUTING.md](./../../CONTRIBUTING.md) for details on our code of
|
130 | conduct, and the process for submitting pull requests to us.
|
131 |
|
132 | ## Versioning
|
133 |
|
134 | postcss-purgecss use [SemVer](http://semver.org/) for versioning.
|
135 |
|
136 | ## License
|
137 |
|
138 | This project is licensed under the MIT License - see the [LICENSE](./../../LICENSE) file
|
139 | for details.
|