mojodna / deadweight forked from aanand/deadweight

CSS coverage tool

This URL has Read+Write access

mojodna (author)
Tue Sep 08 18:36:11 -0700 2009
commit  d8885bf1f4a46abd1cc711f093d5de3ca4e5356e
tree    be4bdd286a800cec1a6e355827cc1664a10a76db
parent  b98a16ce59f36bf404babfff7ad367f8dc3139f1
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 Loading commit data...
file Rakefile
file VERSION
directory bin/ Thu Sep 24 10:54:38 -0700 2009 CLI and HTTP proxy Adds a command-line tool fo... [mojodna]
file deadweight.gemspec
directory lib/
directory test/
README.rdoc

deadweight

Deadweight is RCov for CSS, kind of. Given a set of stylesheets and a set of URLs, it determines which selectors are actually used and reports which can be "safely" deleted.

A Simple Example

  # lib/tasks/deadweight.rake

  require 'deadweight'

  desc "run Deadweight (script/server needs to be running)"
  task :deadweight do
    dw = Deadweight.new
    dw.stylesheets = %w( /stylesheets/style.css )
    dw.pages = %w( / /page/1 /about )
    puts dw.run
  end

This will output all unused rules, one per line.

Alternately, you can 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

How You Install It

  gem sources -a http://gems.github.com
  sudo gem install aanand-deadweight

Things to Note

  • By default, it looks at localhost:3000.
  • 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.
  • There is experimental support for Lyndon (github.com/defunkt/lyndon) with -L

A More Complex Example, In Light of All That

  # lib/tasks/deadweight.rake

  require 'deadweight'

  desc "run Deadweight on staging server"
  task :deadweight do
    dw = Deadweight.new

    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/

    puts dw.run
  end

Copyright

Copyright © 2009 Aanand Prasad. See LICENSE for details.