Skip to content

Instantly share code, notes, and snippets.

@juliocesar
Created April 26, 2011 01:18
Show Gist options
  • Save juliocesar/941624 to your computer and use it in GitHub Desktop.
Save juliocesar/941624 to your computer and use it in GitHub Desktop.
Damn simple JavaScripts bundler for Sinatra
# Adjust the line that says Dir.chdir so it points to the path where your
# JavaScripts are stored.
#
# Usage:
# Instead of linking scripts one by one with <script> tags, as in:
# <script src="/js/jquery-1.5.2.min.js"></script>
# <script src="/js/underscore.js"></script>
# <script src="/js/backbone-min.js"></script>
#
# You can now go:
#
# <script src="/scripts.js?these=jquery,underscore,backbone"></script>
#
# Probably wise to add long expiry headers in production.
get '/scripts.js' do
content_type 'application/javascript'
these, scripts = params[:these].split(',').map { |s| s + '*' }.join(','), []
Dir.chdir Sinatra::Application.root + '/public/js'
Dir["{#{these}}.js"].each do |script|
scripts << File.read(script)
end
scripts.join(";")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment