public
Description: CSS coverage tool
Homepage:
Clone URL: git://github.com/aanand/deadweight.git
name age message
file .document Tue Jul 21 11:21:30 -0700 2009 Initial commit to deadweight. [aanand]
file .gitignore Tue Jul 21 11:27:26 -0700 2009 ~here it is~ [aanand]
file LICENSE Tue Jul 21 11:21:30 -0700 2009 Initial commit to deadweight. [aanand]
file README.rdoc Wed Oct 21 08:24:13 -0700 2009 README makeover [aanand]
file Rakefile Fri Sep 25 08:24:21 -0700 2009 Get rid of that 'RCov for CSS' rubbish [aanand]
file VERSION Sun Oct 18 16:59:13 -0700 2009 Version bump to 0.1.1 [aanand]
directory bin/ Thu Sep 24 10:54:38 -0700 2009 CLI and HTTP proxy Adds a command-line tool fo... [mojodna]
file deadweight.gemspec Sun Oct 18 17:01:06 -0700 2009 Regenerated gemspec [aanand]
directory lib/ Wed Oct 21 08:49:05 -0700 2009 Vendor css_parser [aanand]
directory test/ Sun Oct 18 16:57:53 -0700 2009 Output just selectors, not rules (which are sti... [aanand]
directory vendor/ Wed Oct 21 08:49:05 -0700 2009 Vendor css_parser [aanand]
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.

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.