csso-rails: Stylesheet Optimizer (CSSO) for Rails Asset pipeline

Gem Version Gemnasium Build Status

Ruby adapter for github.com/css/csso.

CSSO does structure-optimization for CSS. CSS is usually reduced more than in half in uncompressed and around 15% in gzipped.

A Real-World Example

A living rails application CSS – some written in less, some handwritten):

Originalsassyui 2.4.7csso% of original
Plain129497107006607586087447%
GZipped1404612047105581047274%

Very close to yui compressor, wining in gzipped (you’re using nginx mod\_gzip_static, don’t you?)

A more hard example – twitter bootstrap.css, already minified:

Originallesscyui 2.4.7csso% of original
Plain8144371520687556767983%
GZipped1238411633116521147792%

Please note than benchmark was taken in summer of 2012, since then things may have changed.

Usage

In Rails 3.1+

Add gem 'csso-rails' to your gemfile, and that’s it! (also you may want to add some javascript runtime for ExecJS to pick up, like gem 'therubyracer')

Upon including it becomes the default compressor even if sass is included too. More explicit way – set in config/environment/production.rb:

“by config.assets.css_compressor = :csso

Sprockets

If you use Sprockets without Rails:

“by require ‘csso’ Csso.install(sprockets_env)

In Plain Ruby

“by require ‘csso’ puts Csso.optimize(“a{ color: #FF0000; }”) # produces “acolor:red”

In maniac mode (Csso.optimize(css, true), default for pipeline) CSS is processed several times until it stops getting lighter (there’re cases when original csso does not do all optimizations for no reason).

In Command Line

ruby_csso non_optimized.css > optimized.css

MIT-License

Original CSSO code - Copyright (C) 2011 by Sergey Kryzhanovsky.

ruby gem - Copyright(C) 2012 Vasily Fedoseyev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.