The Asset Pipeline in Sinatra
The spectacular asset pipeline that shipped with Rails 3.1 is an easy feature to get used to once you've started using it, and when writing light Sinatra apps it's one feature that I miss enough to pull in. Initially, I'd boot up a Sprockets module directly in my application's rackup file (that's
config.ru) and map it to a particular URL path. This approach works well, but makes a mess in the rackup file and offers little in terms of fine-grained control. More recently I've found that I can get better flexibility and clarity by running it from a custom Sinatra module.
Add Sprockets and Yahoo's YUI compressor to your
gem "sprockets" gem "yui-compressor" # I find it well worth to include CoffeeScript and SASS as well gem "coffee-script" gem "sass"
Your assets file structure should look something like this:
app.css as well (includes
The Sinatra module should look something like this:
Now use the assets module as middleware in
config.ru, and delegate everything else to your main app:
use Assets run Sinatra::Application