Purgecss is not the only tool to remove unused CSS. Below you will find a list of some of the tools available as well as a comparison with Purgecss.
Purgecss fixes this problem by providing the possibility to create an extractor. An extractor is a function that takes the content of a file and extracts the list of CSS selectors used in it. It allows a perfect removal of unused CSS.
The extractor can be used as a parser that returns an AST (abstract syntax tree) and looks through it to find any CSS selectors. This is the way
purge-from-html works. You can specify which selectors you want to use for each file type, allowing you to get the most accurate results. Additionally, you can use the default or legacy extractor, which will mimic PurifyCSS's behavior.
As indicated in its Readme, UnCSS works in the following way:
- All the stylesheets are parsed by PostCSS.
- document.querySelector filters out selectors that are not found in the HTML files.
- The remaining rules are converted back to CSS.
To remove unused CSS from Pug template files, for example, you would need to convert Pug to HTML and emulate the page inside jsdom. Only after this can UnCSS run
document.querySelector on each selector and perform Step 4.
At the moment, UnCSS is probably the most accurate tool to remove unused CSS if you do not use server-side rendering.