Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Ruby JavaScript
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib/sinatra Release 0.5.3
spec Some cleanup
.gitignore little gemspec update
.rspec no debugger
Gemfile TravisCI setup for #20
Gemfile.1.3 move those to the root
Gemfile.lock Fix tests for new sinatra
LICENSE Bunch of docs and readme Note about modular apps. Closes #22
Rakefile No debug
sinatra-bundles.gemspec Release 0.5.3


An easy way to bundle CSS and Javascript assets in your sinatra application.


sinatra-bundles combines Javascript and CSS into one file. Meaning, you can bundle 2 or more Javascript files into one, similar with CSS stylesheets. Any bundled files are expected to be in the public directory, under 'javascripts' and 'stylesheets'

Assuming you have the following files in public:


You can bundle these files in your app like this. First, install the gem

% [sudo] gem install sinatra-bundles

And include it in your app:

require 'sinatra'
require 'sinatra/bundles'

stylesheet_bundle(:all, %w(reset fonts grid))
javascript_bundle(:all, %w(jquery lightbox blog))

get '/' do
  'sinatra-bundles rocks!'

Using a Modular style app?

require 'sinatra/base'
require 'sinatra/bundles'

class MyApp < Sinatra::Base
  get '/' do
    'sinatra-bundles rocks!'

  register Sinatra::Bundles

Then in your view, you can use the view helpers to insert the proper script tags:

= javascript_bundle_include_tag(:all)
= stylesheet_bundle_link_tag(:all)

All 6 of those files will be served up in 2 files, and they'll be compressed and have headers and etags set for caching.

You can also use wildcard splats.

= javascript_bundle(:test, %w(test/*))

That will grab all files in the test directory.

= javascript_bundle(:test, %w(test/**/*))

That will grab all files under the test directory recursively. If you don't specify any files, it defaults to 'all files' recursively.

= javascript_bundle(:all)


The defaults are pretty good. In development/test mode:

bundle_cache_time # => 60 * 60 * 24 * 365, or 1 year (length of time a bundle will be cached for)
compress_bundles # => false (compress CSS and Javascript using packr and rainpress)
cache_bundles # => false (set caching headers)
stamp_bundles # => true (append a timestamp to the URL as a query param)
warm_bundle_cache # => false (generate bundle when it is defined)

And in production mode, compression and caching are enabled

compress_bundles # => true
cache_bundles # => true

To change any of these, use set/enable/disable

require 'sinatra'
require 'sinatra/bundles'

stylesheet_bundle(:all, %w(reset fonts grid))
javascript_bundle(:all, %w(jquery lightbox blog))

set(:bundle_cache_time, 60 * 60 * 24)

get '/' do
  'sinatra-bundles rocks!'


Check out the code for my blog for a real example: darkblog on github

What you Need

sinatra >= 1.0

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


  • Patrick Hogan
    • Etag support (with specs!)
    • Wildcard globbing
  • docunext
    • Custom path prefixes for bundles (instead of 'javascripts' and 'stylesheets')


Copyright (c) 2010 Daniel Huckstep. See LICENSE for details.

Something went wrong with that request. Please try again.