+0
Daily
9,832
stars
375
forks
142
watchers
109
prs
124
issues
1
releases
README
PurifyCSS
!Join the chat at https://gitter.im/purifycss/purifycss
A function that takes content (HTML/JS/PHP/etc) and CSS, and returns only the used CSS.
PurifyCSS does not modify the original CSS files. You can write to a new file, like minification.
If your application is using a CSS framework, this is especially useful as many selectors are often unused.
Potential reduction
Bootstrap file: ~140k
App using ~40% of selectors.
Minified: ~117k
Purified + Minified: ~35k
Usage
Standalone
Installation
bash
npm i -D purify-css
javascript
import purify from "purify-css"
const purify = require("purify-css")
let content = ""
let css = ""
let options = {
output: "filepath/output.css"
}
purify(content, css, options)
Build Time
- Grunt
- Gulp
- Webpack
CLI Usage
$ npm install -g purify-css
$ purifycss -h
purifycss [option]
Options:
-m, --min Minify CSS [boolean] [default: false]
-o, --out Filepath to write purified css to [string]
-i, --info Logs info on how much css was removed
[boolean] [default: false]
-r, --rejected Logs the CSS rules that were removed
[boolean] [default: false]
-w, --whitelist List of classes that should not be removed
[array] [default: []]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
How it works
Used selector detection
Statically analyzes your code to pick up which selectors are used.
But will it catch all of the cases?
Let's start off simple.
Detecting the use of: button-active
html
click
javascript
// javascript
// Anytime your class name is together in your files, it will find it.
$(button).addClass('button-active');
Now let's get crazy.
Detecting the use of: button-active
javascript
// Can detect if class is split.
var half = 'button-';
$(button).addClass(half + 'active');
// Can detect if class is joined.
var dynamicClass = ['button', 'active'].join('-');
$(button).addClass(dynamicClass);
// Can detect various more ways, including all Javascript frameworks.
// A React example.
var classes = classNames({
'button-active': this.state.buttonActive
});
return (
Submit;
);
Examples
Example with source strings
js
var content = ' Login ';
var css = '.button-active { color: green; } .unused-class { display: block; }';
console.log(purify(content, css));
logs out:
.button-active { color: green; }
Example with glob file patterns + writing to a file
js
var content = ['/src/js/.js', '/src/html/.html'];
var css = ['/src/css/.css'];
var options = {
// Will write purified CSS to this file.
output: './dist/purified.css'
};
purify(content, css, optTop issues
#144Error: property missing ':'12 comments · 2017-03-03#71example on how to use12 comments · 2015-06-30#147Assuming as equal two classes differentiated by a number at the end 8 comments · 2017-03-16#180Running npm outdated show latest 1.2.57 comments · 2017-07-14#135Bootstrap Components using JS don't work well7 comments · 2016-10-28#150Not working with PostCSS output6 comments · 2017-03-17#175chaining rules not filtered properly5 comments · 2017-06-22#116Purify not pruning the css file5 comments · 2016-04-06