Rack::Less
Description
A better way to use LESS CSS in Ruby web apps.
-
Update: rack-less as of v3.x.x \ uses the latest version of the ruby less parser, github.com/cowboyd/less.rb (based on less.js). To use the older ruby based parse, require rack-less ~> 2.0.
Installation
$ gem install rack-less
Basic Usage
Rack::Less is implemented as a piece of Rack middleware and can be used with any Rack-based application. If your application includes a rackup (‘.ru`) file or uses Rack::Builder to construct the application pipeline, simply require and use as follows:
require 'rack/less'
# optional - use as necessary
Rack::Less.configure do |config|
config.compress = true
# other configs ...
end
use Rack::Less,
:source => 'app/less',
:hosted_at => '/'
# additional options ...
run app
Using with Rails
Add this to your ‘config/environment.rb`:
config.middleware.use "Rack::Less"
Add any configs in an initializer (optional - use as necessary):
Rack::Less.configure do |config|
# for example
config.cache = Rails.configuration.action_controller.perform_caching
if Rails.env.development?
config.cache_bust = true
end
end
You should now see ‘Rack::Less` listed in the middleware pipeline:
rake middleware
Available Configurations
-
.cache [false]
-
Whether to cache the compilation output to a corresponding static file
-
-
.compress [false]
-
Whether or not to apply compression to the concatenation output
-
:yui - use YUI Compressor (gem install yui-compressor)
-
:whitespace - remove extraneous whitespace only.
-
-
-
.combinations [{}]
-
Directives for combining the output of many stylesheets and serving them as a single resource.
-
-
.cache_bust [nil]
-
Directives for timestamping (cache-busting) stylesheet references
-
:false - don’t explicitly cache bust (no value added)
-
:true - use Time.now.to_i as the explicit value (will never cache)
-
:nil - change cache bust value if the file is modified (similar to Rails’ stylesheet_link_tag)
-
:*<any other value>* - add the value as “foo.css?<value>”
-
-
Available Options
-
:root [“.”]
-
The app root. The reference point for the source and public options.
-
-
:source [‘app/stylesheets’]
-
The path (relative to the root) where LESS source files are located
-
-
:public [‘public’]
-
The path where static files are located
-
-
:hosted_at [‘/stylesheets’]
-
The public HTTP path for hosted stylesheets.
-
Using in layouts
Cache Busting
Rails does a lot of helpful things with ‘stylesheet_link_tag’ to help reference your stylesheets into your layouts - things like cache busting stylesheet hrefs. However, Rails’ will only cache bust your stylesheets if the file exists in the public/stylesheets directory. When using Rack::Less a file may never exist at that path or, when caching is used, only exist after the initial request.
To help provide this behavior, Rack::Less provides a helper for generating reference paths to your stylesheets.
# equivalent to: stylesheet_link_tag 'reset'
stylesheet_link_tag Rack::Less.stylesheet('reset')
Combinations
At times, it is useful to combine multiple stylesheets and serve them as one resource. For example you may have two sets of stylesheets: one for traditional web views and one for mobile web views. Rails’ provides the :cache option on ‘stylesheet_link_tag’ helper to provide this function.
stylesheet_link_tag 'reset', 'common', 'app_web', :cache => 'web'
stylesheet_link_tag 'reset', 'common', 'iui', 'app_mobile', :cache => 'mobile'
Rack::Less uses combinations, in conjunction with the :cache config setting, to provide this function. Combinations are configured using a hash, where the key is the resource name and the value is an array of names corresponding to stylesheets to combine as the named resource. For the above example, use a configuration like this:
Rack::Less.configure do |config|
config.combinations = {
'web' => ['reset', 'common', 'app_web'],
'mobile' => ['reset', 'common', 'iui', 'app_mobile']
}
end
and stylesheet link tags like this:
# equivalent to: stylesheet_link_tag 'reset', 'common', 'app_web'
stylesheet_link_tag Rack::Less.stylesheet('web')
# equivalent to: stylesheet_link_tag 'reset', 'common', 'iui', 'app_mobile'
stylesheet_link_tag Rack::Less.stylesheet('mobile')
If you configure Rack::Less to cache, with something like this:
Rack::Less.config.cache = true
then the same stylesheet link tags behave like they have the :cache option set, respectively:
# equivalent to: stylesheet_link_tag 'reset', 'common', 'app_web', :cache => 'web'
stylesheet_link_tag Rack::Less.stylesheet('web')
# equivalent to: stylesheet_link_tag 'reset', 'common', 'iui', 'app_mobile', :cache => 'mobile'
stylesheet_link_tag Rack::Less.stylesheet('mobile')
Links
-
GitHub
-
Less CSS
License
Copyright © 2010 Kelly Redding ([email protected])
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.