Comment on page
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-htmlworks. 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:
- 2.All the stylesheets are parsed by PostCSS.
- 3.document.querySelector filters out selectors that are not found in the HTML files.
- 4.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.querySelectoron 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.