uncss: Find Unused CSS
You know what's better than adding features to a website or app, from a code perspective? Removing stuff you don't need. Whether it be code, images, or dependencies, getting rid of the crap stale code is like the first sip of a big glass of wine after a long day of stressful work. Running a directory of images through ImageOptim is a euphoric experience, am I right? What if there was a tool, however, which would allow you to find unused CSS for a given website? There is, and it's called uncss
, a NodeJS powered utility. Let's take a look at how uncss
works!
A basic usage of uncss
's command line tool would be:
uncss http://davidwalsh.name > styles.css
The output of this execution is a stylesheet featuring only the used CSS rules -- unused rules are removed. So how does uncss
work? I'll let them tell you how:
- The HTML files are loaded by PhantomJS and JavaScript is executed.
- Used stylesheets are extracted from the resulting HTML.
- The stylesheets are concatenated and the rules are parsed by css-parse.
document.querySelector
filters out selectors that are not found in the HTML files.- The remaining rules are converted back to CSS.
Like just about every NodeJS-based utility, you can also take advantage of its JavaScript API. Here's an example usage:
var uncss = require('uncss'); var files = ['my', 'array', 'of', 'HTML', 'files'], options = { ignore : ['#added_at_runtime', /test\-[0-9]+/], media : ['(min-width: 700px) handheld and (orientation: landscape)'], csspath : '../public/css/', raw : 'h1 { color: green }', stylesheets : ['lib/bootstrap/dist/css/bootstrap.css', 'src/public/css/main.css'], ignoreSheets : [/fonts.googleapis/], urls : ['http://localhost:3000/mypage', '...'], // Deprecated timeout : 1000, htmlroot : 'public' }; uncss(files, options, function (error, output) { console.log(output); }); /* Look Ma, no options! */ uncss(files, function (error, output) { console.log(output); }); /* Specifying raw HTML */ var raw_html = '...'; uncss(raw_html, options, function (error, output) { console.log(output); });
There's no arguing that years of maintaining, adding, and removing from a site will add excess code to the codebase. That excess code comes at the cost of users who have load the extra code, so eliminating the dead code is important. Give uncss
a try -- it's an easy to use, automated helper to keep your codebase as tight as possible!
This looks awesome David, gunna give it a whirl tonight and see what gaping hue holes there are lol
Does this take into account prefixes? If so, this would make a great addition to my Grunt builder!
Addy created a Grunt extension :)
https://github.com/addyosmani/grunt-uncss
If you’re in the browser, you can use Google Chrome for this. Open the Web Dev tools, Click on the Audits tab, click Run, and then look through the “Remove unused CSS rules” list.
Only for a single page though.
Slightly different, but I have always used “Dust me selectors”, its a browser plugin that helps to find and build a list of unused selectors.
Does this work for a single page ? Or for the entire website. Because there may be few styles that are written in a common .css file but of which say 40 lines of css are written exclusively for page 1 and 20 lines of css exclusively for page 2.
Is that handled ?
I also use Dust-Me Selectors in Firefox. You can crawl your whole site supplying it with a sitemap.
While these housekeeping tasks of clearing out images, js, css, have lower priority for many, it would be handy to have a sophisticated solution to make it easy to maintain overall
how does this handle a CMS (such as WordPress) with dynamically generated URLs?