PurgeCSS 3.0

Safelist and blocklist options. CLI changes

Floriel
Full Human
3 min readSep 20, 2020

--

PurgeCSS is now a stable library, and while the bugs will keep being worked on and fixed, there’s a new major version introducing a few breaking changes.

If you don’t know what PurgeCSS is, the previous release article will explain what problem PurgeCSS is trying to solve and how it works: link.

Simplifying whitelist option

Whitelist started as a simple option named `whitelist` but grew up with the different use-cases and needs that appear with time. Another option called `whitelistPatterns` appeared, then `whitelistPatternsChildren`, …
It is now more complex and complicated, and somewhat difficult to remember how to use it with different options.

To summarize the changes, the whitelist options are now grouped in one option called safelist. And the most used options can be defined in one array.

Two forms are available. The simple form is:

In this form, safelist is an array that can take a string or a regex.

The complex form is:

In this form, safelist is an object taking optional properties:

New Option: Blocklist

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.

Even if `nav-links` and `usedClass` are found in your content files (HTML, Javascript), they will be removed.

CLI

PurgeCSS is using commander.js for its CLI. The recent version of commander.js introduced the possibility of making an option variadic. This means when specifying multiple option arguments, the parsed option value will be an array.
A few options are now taking advantage of this new feature: content, css, and the newly introduced option safelist and blocklist. It is no longer necessary to separate the list items with a comma.

About the author
I’m Floriel. You can follow me on GitHub and Twitter.

--

--

Floriel
Full Human

Software Engineer and author of PurgeCSS — Writing articles about web development regularly