UNPKG

4.41 kBMarkdownView Raw
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```
15npm i -D @fullhuman/postcss-purgecss postcss
16```
17
18## Usage
19
20```js
21const purgecss = require('@fullhuman/postcss-purgecss')
22postcss([
23 purgecss({
24 content: ['./src/**/*.html']
25 })
26])
27```
28
29See [PostCSS] docs for examples for your environment.
30
31## Options
32
33All of the options of purgecss are available to use with the plugins.
34You 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)
37Type: `Array<string>`
38
39You 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`)
42Type: `(sourceInputFile: string) => Array<string>`
43
44The function receives the current source input file. With this you may provide a specific array of globs for each input. E.g. for
45an angular application only scan the components template counterpart for every component scss file:
46
47```js
48purgecss({
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`
59Type: `Array<Object>`
60
61Purgecss 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.
62More information about extractors [here](https://www.purgecss.com/extractors.html).
63
64### `safelist`
65
66You can indicate which selectors are safe to leave in the final CSS. This can be accomplished with the option `safelist`.
67
68Two forms are available for this option.
69
70```ts
71safelist: ['random', 'yep', 'button', /^nav-/]
72```
73
74In this form, safelist is an array that can take a string or a regex.
75
76The _complex_ form is:
77
78```ts
79safelist: {
80 standard: ['random', 'yep', 'button', /^nav-/],
81 deep: [],
82 greedy: [],
83 keyframes: [],
84 variables: []
85}
86```
87
88### `blocklist`
89
90Blocklist 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
93blocklist: ['usedClass', /^nav-/]
94```
95Even if nav-links and usedClass are found by an extractor, they will be removed.
96
97### `skippedContentGlobs`
98
99If 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
102skippedContentGlobs: ['node_modules/**', 'components/**']
103```
104Here, PurgeCSS will not scan anything in the "node_modules" and "components" folders.
105
106
107### `rejected`
108Type: `boolean`
109Default value: `false`
110
111If true, purged selectors will be captured and rendered as PostCSS messages.
112Use with a PostCSS reporter plugin like [`postcss-reporter`](https://github.com/postcss/postcss-reporter)
113to print the purged selectors to the console as they are processed.
114
115### `keyframes`
116Type: `boolean`
117Default value: `false`
118
119If 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`
122Type: `boolean`
123Default value: `false`
124
125If 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
129Please read [CONTRIBUTING.md](./../../CONTRIBUTING.md) for details on our code of
130conduct, and the process for submitting pull requests to us.
131
132## Versioning
133
134postcss-purgecss use [SemVer](http://semver.org/) for versioning.
135
136## License
137
138This project is licensed under the MIT License - see the [LICENSE](./../../LICENSE) file
139for details.