- Notifications
You must be signed in to change notification settings - Fork380
Remove unused CSS. Also works with single-page apps.
License
purifycss/purifycss
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A function that takes content (HTML/JS/PHP/etc) and CSS, and returns only theused 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.
- Bootstrap file: ~140k
- App using ~40% of selectors.
- Minified: ~117k
- Purified + Minified:~35k
Installation
npm i -D purify-css
importpurifyfrom"purify-css"constpurify=require("purify-css")letcontent=""letcss=""letoptions={output:"filepath/output.css"}purify(content,css,options)
$ npm install -g purify-css
$ purifycss -hpurifycss <css> <content> [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]
Statically analyzes your code to pick up which selectors are used.
But will it catch all of the cases?
<!-- html --><!-- class directly on element --><divclass="button-active">click</div>
// javascript// Anytime your class name is together in your files, it will find it.$(button).addClass('button-active');
// Can detect if class is split.varhalf='button-';$(button).addClass(half+'active');// Can detect if class is joined.vardynamicClass=['button','active'].join('-');$(button).addClass(dynamicClass);// Can detect various more ways, including all Javascript frameworks.// A React example.varclasses=classNames({'button-active':this.state.buttonActive});return(<buttonclassName={classes}>Submit</button>;);
varcontent='<button> Login </button>';varcss='.button-active { color: green; } .unused-class { display: block; }';console.log(purify(content,css));
logs out:
.button-active { color: green; }
Example withglob file patterns + writing to a file
varcontent=['**/src/js/*.js','**/src/html/*.html'];varcss=['**/src/css/*.css'];varoptions={// Will write purified CSS to this file.output:'./dist/purified.css'};purify(content,css,options);
Example with bothglob file patterns and source strings + minify + logging rejected selectors
varcontent=['**/src/js/*.js','**/src/html/*.html'];varcss='.button-active { color: green; } .unused-class { display: block; }';varoptions={output:'./dist/purified.css',// Will minify CSS code in addition to purify.minify:true,// Logs out removed selectors.rejected:true};purify(content,css,options);
logs out:
.unused-class
varcontent=['**/src/js/*.js','**/src/html/*.html'];varcss=['**/src/css/*.css'];purify(content,css,function(purifiedResult){console.log(purifiedResult);});
varcontent=['**/src/js/*.js','**/src/html/*.html'];varcss=['**/src/css/*.css'];varoptions={minify:true};purify(content,css,options,function(purifiedAndMinifiedResult){console.log(purifiedAndMinifiedResult);});
// Four possible arguments.purify(content,css,options,callback);
Array
ofglob file patterns to the files to search through for used classes (HTML, JS, PHP, ERB, Templates, anything that uses CSS selectors).
String
of content to look at for used classes.
Array
ofglob file patterns to the CSS files you want to filter.
String
of CSS to purify.
minify:
Set totrue
to minify. Default:false
.output:
Filepath to write purified CSS to. Returns raw string iffalse
. Default:false
.info:
Logs info on how much CSS was removed iftrue
. Default:false
.rejected:
Logs the CSS rules that were removed iftrue
. Default:false
.whitelist
Array of selectors to always leave in. Ex.['button-active', '*modal*']
this will leave any selector that includesmodal
in it and selectors that matchbutton-active
. (wrapping the string with *'s, leaves all selectors that include it)
A function that will receive the purified CSS as it's argument.
purify(content,css,options,function(purifiedCSS){console.log(purifiedCSS,' is the result of purify');});
purify(content,css,function(purifiedCSS){console.log('callback without options and received',purifiedCSS);});
$ purifycss src/css/main.css src/css/bootstrap.css src/js/main.js --min --info --out src/dist/index.css
This will concat bothmain.css
andbootstrap.css
and purify it by looking at what CSS selectors were used inside ofmain.js
. It will then write the result todist/index.css
The--min
flag minifies the result.
The--info
flag will print this to stdout:
________________________________________________ | | PurifyCSS has reduced the file size by ~ 33.8% | ________________________________________________
The CLI currently does not support file patterns.
About
Remove unused CSS. Also works with single-page apps.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.