purifycss /purifycss

Remove unused CSS. Also works with single-page apps.

★ 9,832⑂ 375👁 142⑃ 109● 124▤ 1
JavaScriptMITCreated 2015-05-26
Download: v1.2.6purifycss-1.2.6 ↓1.2.62017-06-06
+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, opt

Top issues

Latest release

v1.2.6 v1.2.6 2017-06-06

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON