Sinatra Sprockets

Sinatra extension for Sprockets integration.


Add this line to your application's Gemfile:

gem 'sinatra-sprockets-ext'

or gem install sinatra-sprockets-ext and then manually require it:

require 'sinatra/sprockets'

Currently requires 1.9 (1.8 is near end-of-life anyway).


Register the plugin in your app.rb:

register Sinatra::Sprockets

In your, add a mapping for Sprockets:

map "/#{Sinatra::Sprockets.config.prefix}" do
  run Sinatra::Sprockets.environment

Optionally configure as needed. Configuration is similar to Rails's Asset Pipeline. Below is an example configuration with some options showing the default values:

Sinatra::Sprockets.configure do |config|
  config.compile = false         # On-the-fly compilation
  config.compress = false        # Compress assets
  config.digest = false          # Append a digest to URLs
  config.css_compressor = false  # CSS compressor instance
  config.js_compressor = false   # JS compressor instance

All options and their defaults are shown in lib/sinatra-sprockets/configuration.rb.

You'll also get helper functions that will be familiar to those who use Rails:

  • javascript_include_tag :asset_name
  • stylesheet_link_tag :asset_name
  • asset_path :asset_name

Credits & License

Copyright © 2012 J. Andrew Marshall. All rights reserved. License is available in the LICENSE file.

Special thanks to those who have contributed!