Rack::Sprockets

Description

Sprockets javascript preprocessing for Rack apps.

Installation

gem install rack-sprockets

# optional, for compression
gem install yui-compressor

Basic Usage

Rack::Sprockets 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/sprockets'

# optional - use as necessary
Rack::Sprockets.configure do |config|
  config.compress = :yui
  # other configs ...
end

use Rack::Sprockets,
  :load_path => 'app/scripts',
  :hosted_at => '/'
  # additional options ...

run app

Using with Rails

Add this to your ‘config/environment.rb`:

config.middleware.use "Rack::Sprockets"

Add any configs in an initializer (optional - use as necessary):

Rack::Sprockets.configure do |config|
  config.cache = true
  # additional configs ...
end

You should now see ‘Rack::Sprockets` listed in the middleware pipeline:

rake middleware

Available Options

These are similar to sprockets options and, where applicable, map directly to a corresponding Sprockets option.

  • :root [‘.’]

    • The app root. The reference point for the source and public options. Maps to the ‘:root` Sprockets option.

  • :public [‘public’]

    • The path where static files are located. Maps to the ‘:asset_root` Sprockets option.

  • :source [“app/javascripts”]

    • The path where Sprockets source files are located. Notice this does not map to the ‘:source_files` Sprockets option. It is assumed that any requested resource found in `:source` be treated as a Sprockets source file.

  • :hosted_at [“/javascripts”]

    • The public hosted HTTP path for static javascripts files.

  • :load_path [[“app/javascripts/”, “vendor/javascripts/”]]

    • An ordered array of directory names to search for dependencies in. Maps to the ‘:load_path` Sprockets option.

  • :expand_paths [true]

    • Whether or not to expand filenames according to shell glob rules. Maps to the ‘:expand_paths` Sprockets option.

Available Configurations

  • .cache [false]

    • Whether or not to cache the concatenation 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.

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.