Uncss – Find unused CSS
davidwalsh.nameTry Helium. https://github.com/geuis/helium-css
I wrote and maintain this project for the last 4 years.
There are issues with David Walsh's approach that are explained in the Helium README. Automatic stylesheet generation is a terrible idea, for one.
Cool, and this runs in the browser without needing nodejs too.
If this worked across a whole site rather than a single page, that would be great.
Exactly. Sitepoint made a browser extension to do this years ago but it's not really actively maintained anymore: http://www.brothercake.com/dustmeselectors/
There is an add-on for Firebug too (https://addons.mozilla.org/en-us/firefox/addon/css-usage/) -- I use it a big corp. Basically if there is some wild huge monolithic css file I'll cut a new one for a site & use this to pick a decent-looking base subset from the existing dung heap. Then I'll use it again later to analyze widgets etc. if someone complains "how come ______ doesn't look like it does on the old site?".
I'd love for this all to be automated but it seems there are some fundamental flaws in that. Sometimes unused css still fits into a logical unit and should be included. Sometimes in-use css is just a piece of garbage innocuously clinging to some jagged edge of the DOM.
I try to comment blocks of CSS with both starting and ending comments, "______ START" to "_______ END" to avoid this. That way I can extract those pieces later without really worrying that I'm mangling a code module, manually, without much fear.
You can also get a list of un-used CSS Rules using Chrome. In the Developer Tools, under Audits, the list will appear under "Web Page Performance."
Most of us are using CSS precompilers (Less, Sass, Stylus) and then stitching tons of files together into a single .css file. I wonder if there is an easy way to produce Sass/Less/Stylus files with the un-used classes removed.
It would be sweet to have a grunt plugin that scans your CSS and templates and removes unused rules. It would probably have to read JS files to be thorough though.
Certainly possible with sourcemaps to essentially get a code coverage-like view for your precompiled styles. I havent' seen it done yet, however.
First thought was that this should be a grunt plugin, then I read the comments. Here it is: https://github.com/addyosmani/grunt-uncss
I thought there should be a Gulp plugin, and then I found this: https://www.npmjs.org/package/gulp-uncss/
I thought there should be a Broccoli plugin, and then I found this: https://github.com/sindresorhus/broccoli-uncss
This seems to beg for testing integration - run it for every page visited in feature tests, aggregate the results, spit out a "following selectors were defined but not used" report at the end.
This is a great first step. It seems the next step toward usefulness is making it follow some script throughout the site as a build/optimize step to tease out the full set of rule usage (think Selenium on PhantomJS). Automatically finding all rule usage in a data-driven application would be an NP complete problem, but with proper direction from the developers this could be a viable tool. Even more so if it could map back to LESS/SASS rules, which I think would also need some brute forcing or at least hand holding. Nice work!
Neat. There's also a Firebug extension that does this: https://addons.mozilla.org/en-us/firefox/addon/css-usage/ A little rough around the edges, but it works.
The challenge, of course, is that's it's hard to say for sure that something is unused. Maybe it's only called on one page, or only after some interaction with javascript on the page.
>only after some interaction with javascript on the page
That's why I will use a prefix js-[something] for elements that will interact with javascript and then user other classes for style so nothing gets removed by accident by others.
Smart. I end up grepping the whole codebase by hand for each class I want to remove, but even that isn't perfect.
Maybe I missed the info, but does it work for a whole website or only 1 subsite?
As soon as I hit the page I understood what it was and how awesome that was going to be for me. This is genius! I'm sure something like that has been done before but I've never come across it until now.
If you're an ArchLinux AUR user, here's the package: https://aur.archlinux.org/packages/nodejs-uncss
I prefer https://github.com/peterbe/mincss It's based on python :)
I don't see how this could work on a single page application where only a small fraction of the content is loaded on page-load.
I'm using css less and less these days, and letting the browser decide how to render html5 on its own...
I'm trying this out in AngularJS. So there's an issue with classes locked away inside templates (or building with @ng-class, anyone?), and authentication scenarios.
I'm thinking about building URL workflows (stored in arrays) and passing authentication tokens via URL.