Every repository with this icon (
Every repository with this icon (
| name | age | message | |
|---|---|---|---|
| |
.document | Tue Jul 21 11:21:30 -0700 2009 | |
| |
.gitignore | Tue Jul 21 11:27:26 -0700 2009 | |
| |
LICENSE | Tue Jul 21 11:21:30 -0700 2009 | |
| |
README.rdoc | Wed Oct 21 08:24:13 -0700 2009 | |
| |
Rakefile | Fri Sep 25 08:24:21 -0700 2009 | |
| |
VERSION | Sun Oct 18 16:59:13 -0700 2009 | |
| |
bin/ | Thu Sep 24 10:54:38 -0700 2009 | |
| |
deadweight.gemspec | Sun Oct 18 17:01:06 -0700 2009 | |
| |
lib/ | Wed Oct 21 08:49:05 -0700 2009 | |
| |
test/ | Sun Oct 18 16:57:53 -0700 2009 | |
| |
vendor/ | Wed Oct 21 08:49:05 -0700 2009 |
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.







