Created
April 26, 2011 01:18
-
-
Save juliocesar/941624 to your computer and use it in GitHub Desktop.
Damn simple JavaScripts bundler for Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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