Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
CSS coverage tool
branch: master

This branch is 36 commits behind aanand:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
lib
test
vendor/gems/css_parser-1.0.0
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION
deadweight.gemspec

README.rdoc

deadweight

Deadweight is a CSS coverage tool. Given a set of stylesheets and a set of URLs, it determines which selectors are actually used and reports which can be “safely” deleted.

Screencast!

Ryan Bates has worked his magic once again. Head over here for an excellent introduction to deadweight: railscasts.com/episodes/180-finding-unused-css

Install It

gem install deadweight -s http://gemcutter.org

Make a Rake Task

# lib/tasks/deadweight.rake

require 'deadweight'

Deadweight::RakeTask.new do |dw|
  dw.stylesheets = %w( /stylesheets/style.css )
  dw.pages = %w( / /page/1 /about )
end

Running rake deadweight will output all unused rules, one per line. Note that it looks at localhost:3000 by default, so you'll need to have script/server (or whatever your server command looks like) running.

Or Run it From the Command Line

$ deadweight -s styles.css -s ie.css index.html about.html

You can pipe in CSS rules from STDIN:

$ cat styles.css | deadweight index.html

And you can use it as an HTTP proxy:

$ deadweight -l deadweight.log -s styles.css -w http://github.com/ -P

There's also experimental support for Lyndon (github.com/defunkt/lyndon) with -L.

Or Call it Directly

require 'deadweight'

dw = Deadweight.new
dw.stylesheets = %w( /stylesheets/style.css )
dw.pages = %w( / /page/1 /about )
puts dw.run

Things to Note

  • You can tell it what URL root to use (rather than localhost:3000) by setting root.

  • It's completely dumb about any classes, IDs or tags that are only added by your Javascript layer, but you can filter them out by setting ignore_selectors.

  • You can optionally tell it to use Mechanize, and set up more complicated targets for scraping by specifying them as Procs.

  • If you have the colored gem installed, it'll spruce up the STDERR output.

A More Complex Example, In Light of All That

# lib/tasks/deadweight.rake

require 'deadweight'

Deadweight::RakeTask.new do |dw|
  dw.mechanize = true

  dw.root = 'http://staging.example.com'

  dw.stylesheets = %w( /stylesheets/style.css )

  dw.pages = %w( / /page/1 /about )

  dw.pages << proc {
    fetch('/login')
    form = agent.page.forms.first
    form.username = 'username'
    form.password = 'password'
    agent.submit(form)
    fetch('/secret-page')
  }

  dw.ignore_selectors = /hover|lightbox|superimposed_kittens/
end

Copyright

Copyright © 2009 Aanand Prasad. See LICENSE for details.

Something went wrong with that request. Please try again.