Sqwish-ruby
A CSS compressor
This as a Ruby bridge to Sqwish by Dustin Diaz
to compress CSS very efficiently.
Ruby usage
Just install it:
$ gem install sqwish
Then use it like so:
require 'sqwish'
Sqwish.minify "div { color: red; } div { background: black; }"
You can also use the strict: true
flag (it defaults to false):
Sqwish.minify "div { color: red; } div { background: black; }", strict: true
Rails usage
In your Gemfile:
gem 'sqwish'
gem 'sqwish', github: 'rstacruz/sqwish.rb'
In your config/environments/production.rb
:
config.assets.css_compressor = Sqwish
config.assets.css_compressor = Sqwish::Strict
Why would I wanna use Sqwish for Rails?
If you use Sass (or a similar preprocessor), you probably separate your CSS
definitions into multiple files. In Sass, there are two ways to do this:
@import 'common';
@import 'layout';
@import 'chrome';
@import 'pages';
This is very convenient, and efficient in space, but slow. If you change one
Sass file, the rest will have to be recompiled. In big projects, you can be
waiting up to 10 seconds after updating a simple style.
Which leads us to method 2:
/* Method 2: Sprockets require */
//= require common
//= require layout
//= require chrome
//= require pages
This is faster! Updating one will no longer re-compile the others! The only
problem here is that styles may be duplicated in each file, say, if each
file depends on a web-font. You can end up with:
@font-face{font-family:'Montserrat';...}
body{font-family:Montserrat}
@font-face{font-family:'Montserrat';...}
.page h1{font-family:Montserrat}
...which YUI (Rails/Sprocket's default compressor) will not optimize for you.
Use Sqwish! It will remove those duplicate @font-faces for you and combine
anything else that can be combined.
@font-face{font-family:'Montserrat';...}
body,.page h1{font-family:Montserrat}
Authors
Released under the MIT license.