You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-o, --out Filepath directory to write purified css files to [string]
160
-
-w, --whitelist List of classes that should not be removed
161
-
[array] [default: []]
162
-
-h, --help Show help [boolean]
163
-
-v, --version Show version number [boolean]
164
-
```
165
-
166
-
167
-
### Extractor
168
-
169
-
Purgecss can be adapted to suit your need. If you want to purify exclusively html file, you might want
170
-
to consider the _purge-from-html_ extractor.
171
-
Purgecss relies on extractors to get the list of selector used in a file.
172
-
There are multiples types of files that can contains selectors such as html files, templating files like pug, or even javascript file.
173
-
174
-
#### Using an extractor
175
-
176
-
You can use an extractor by settings the extractors option in the purgecss config file.
177
-
```js
178
-
importpurgeJsfrom"purgecss-from-js"
179
-
importpurgeHtmlfrom"purge-from-html"
180
-
181
-
constoptions= {
182
-
content: [],// files to extract the selectors from
183
-
css: [],// css
184
-
extractors: [
185
-
{
186
-
extractor: purgeJs,
187
-
extensions: ["js"]
188
-
},
189
-
{
190
-
extractor: purgeHtml,
191
-
extensions: ["html"]
192
-
}
193
-
]
194
-
}
195
-
exportdefaultoptions
196
-
```
197
-
198
-
#### Default extractor
199
-
200
-
Purgecss provides a default extractor that is working with all types of files but can be limited and not fit exactly the type of files that you are using.
201
-
The default extractor considers every word of a file as a selector.
202
-
The default extractor has a few limitations:
203
-
- Do not consider special characters such as `@`.
204
-
205
-
#### Legacy extractor
206
-
207
-
The legacy extractor reproduces the behavior of _purifycss_. You can use the Legacy extractor by setting the option `legacy: true`.
208
-
The legacy extractor has a few limitations:
209
-
- Do not extract uppercase selector
210
-
- Do not extract numbers
211
-
212
-
#### Create an extractor
213
-
214
-
An extractor is a simple class with one method. The method `extract` takes the content of a file as a string and return an array of selectors.
215
-
By convention, the name of the npm package is `purge-from-[typefile]` (e.g. purge-from-pug). You can look at the list of extractor on npm by searching `purge-from`.
216
-
217
-
```js
218
-
219
-
classPurgeFromJs {
220
-
staticextract(content) {
221
-
// return array of css selectors
222
-
}
223
-
}
224
-
225
-
```
226
-
227
-
228
-
### Differences with
229
-
230
-
#### Purifycss
231
-
232
-
The biggest flaw with purifycss is its lack of modularity. It is also is biggest benefit, purifycss can work with any files,
233
-
not just html or javascript. But purifycss works by looking at all the words in the files and comparing them with the selectors
234
-
in the css. Every words is consider a selector, which means that a lot of selectors can be consider used because you have the
235
-
selector name in a paragraph or somewhere in your files.
236
-
237
-
Purgecss fixes this problem by providing the possibility to create an _extractor_, an extractor is a function that takes the content
238
-
of a file and extract the list of css selectors in it. It allows a perfect removal of unused css. The extractor can used a parser
239
-
that returns an ast and then looks through it to select the css selectors. That is the way `purge-from-html` works.
240
-
You can specified which selectors you want to use for each types of files, and so, get the most accurate results.
241
-
You can still use the default or the legacy extractor that will act the same way as purifycss.
242
-
243
-
#### Uncss
244
-
245
-
As indicated in its Readme, Uncss works the following way:
246
-
1. The HTML files are loaded by jsdom and JavaScript is executed.
247
-
2. All the stylesheets are parsed by PostCSS.
248
-
3. document.querySelector filters out selectors that are not found in the HTML files.
249
-
4. The remaining rules are converted back to CSS.
250
-
251
-
Because of the emulation of html, and the execution of javascript, uncss is effective at removing unused selectors from web application.
252
-
But the emulation can have a cost in term of performance and practicality. Uncss works by emulating the html files. To remove unused css
253
-
from pug template files, you will need to convert pug to html and then emulate the page inside jsdom and uncss will run `document.querySelector`
254
-
on each selectors and step 4.
255
-
Uncss by its design is probably the most accurate tool to remove css out of a web application at this moment.
256
-
257
-
Purgecss does not have an extractor right now for javascript files. But because of its modularity, developers can create an extractor for specific
258
-
framework (vue, react, aurelia) and files (pug, ejs) and get the most accurate result without the need of emulation.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
118
+
## License
281
119
120
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
121
+
for details.
282
122
283
123
## Troubleshooting
284
124
285
125
#### Wrong extractor is selected
286
126
287
-
The extractors needs to be defined from the more specific to the less specific. Meaning that you need to define `js` extractor after `ejs`. So the `js` extractor will not be selected for ejs files.
288
-
> You can specified extensions like `.es.js`.
289
-
290
-
#### Some unused css are not removed
291
-
292
-
If you are using the default or legacy extractor, look here.
293
-
Head over the repository of the extractor and open an issue.
294
-
Be as precise as possible when describing the issue, provide the
295
-
css file and content file if possible.
127
+
The extractors needs to be defined from the more specific to the less specific.
128
+
Meaning that you need to define `js` extractor after `ejs`. So the `js`
0 commit comments