Sinatra::Sprockets

Plug and play extension for sinatra that mimic rails assets pipeline through Sprockets.

This gem allows you easly deploy assets via Sprockets assets pipeline just as Rails does, also help you configure the rake tasks for compression. The goal is to have a headless configuration extension and be tested (all other aren't) to give users a fast access to all Sprockets goodness.

Installation & Usage

Set on your Gemset file if you are using Bundler:

gem 'sinatra-sprockets-wheel'
# Don't forget the js evaluator: gem 'therubyracer'

If you are using the Classic style just require the extension.

require 'sinatra'
require 'sinatra/sprockets'

# ... Your app

On the other hand if you use the Modular approach, besides requiring register the extension on your Sinatra application.

require 'sinatra/base'
require 'sinatra/sprockets'

class Hello < Sinatra::Base
  register Sinatra::Sprockets

  # ... Your app
end

Following the default configuration you just need to create the /assets folder and Sprockets will search for application.js and application.css to precompile (if you want to set-up a different structure jump to the configuration chapter). The default folder schema is:

hello/
| assets/
| | application.js
| | application.css
|
| sinatra_app.rb

In the application.(js|css) you can include your requirements:

/*
 *= require vendor/normalize
 *= require_self
 *= require_tree .
 */
//= require vendor/normalize
//= require_self
//= require_tree .

By default Sprockets don't serve your file in production so it's up to you compile them, just set-up your Rakefile and run the assets:precompile task.

require 'sinatra/sprockets/task'
require 'sintra_app'

Sinatra::Sprockets.rake_tasks(App)

Helpers

This gem come bundled with sprockets-helpers to help the path resolution of your assets inside sprockets or your application. All methods available in the gem will be at your disposal has helper once you register the extension.

body {
  background-image: image-url('cat.png');
}
<img src="<%= image_path('hello.jpg') %>" />
<script src="<%= javascript_path 'application' %>"></script>
<link rel="stylesheet" href="<%= stylesheet_path 'application' %>">

<%= javascript_tag 'application' %>
<%= stylesheet_tag 'application' %>

<!-- Handle the expansion of assets for debugging like Rails -->
<%= javascript_tag 'application', expand: true %>

Configuration

You can control Sprockets entirely using Sinatra set configuration method. Bellow a list of the configuration:

set :assets_prefix, '/assets'
set :assets_path,   %w[
  app/assets/vendor
  app/assets/stylesheets
  app/assets/javascripts
  app/assets/images
]
set :assets_precompile,     %w(application.js application.css)
set :assets_host,           'cdn.host.com'
set :assets_protocol,       :https

# Debug mode automatically sets
# expand = true, digest = false, manifest = false
set :assets_debug,          true

set :assets_manifest_file,  File.join(public_folder, "assets/manifest.json")
set :assets_css_compressor, :sass
set :assets_js_compressor,  :uglifier

Minification

As seen on the last example of the configurantion you can configure other libraries to compress your assets, Sinatra::Sprockets handle them transparently and it's up to you to require the gems.

SASS

gem 'sass'
set :assets_css_compressor, :sass

Closure

gem 'closure-compiler'
set :assets_css_compressor, :closure

Uglifier

gem 'uglifier'
set :assets_css_compressor, :uglifier

Uglifier

gem 'yui-compressor'
set :assets_css_compressor, :yui

Compass and others gems

The integration is easily done by requiring the sprockets-sass gem.

None the less any gem that have integration with the Sprockets will work seamlessly. If you need any other configuration you can call Sprockets configuration directly.

Copyrights

Copyrights 2012 Edson Hilios edson (at) hilios (dot) com (dot) br

This software is licensed under MIT-LICENSE