Skip to content

Instantly share code, notes, and snippets.

@paul
Created July 15, 2010 14:57
Show Gist options
  • Save paul/477058 to your computer and use it in GitHub Desktop.
Save paul/477058 to your computer and use it in GitHub Desktop.
# Put Google's javascript closure_compesser.jar in ./lib
namespace :bundle do
desc "Bundle javascript"
task :js do
compression_method = "closure"
require 'lib/js_minimizer' if compression_method != "closure"
closure_path = File.join(Rails.root,'lib/closure_compresser.jar')
paths = get_top_level_directories('/public/javascripts')
targets = []
paths.each do |bundle_directory|
bundle_name = File.basename(bundle_directory)
files = recursive_file_list(bundle_directory, ".js")
next if files.empty? || bundle_name == 'dev'
target = File.join(Rails.root, "/public/javascripts/bundle_#{bundle_name}.js")
if compression_method == "closure"
puts "java -jar #{closure_path} --js #{files.join(" --js ")} --js_output_file #{target}"
`java -jar #{closure_path} --js #{files.join(" --js ")} --js_output_file #{target}`
else
File.open(target, 'w+') do |f|
f.puts JSMinimizer.minimize_files(*files)
end
end
targets << target
end
targets.each do |target|
puts "=> bundled js at #{target}"
end
end
def get_top_level_directories(base_path)
Dir[File.join(Rails.root, base_path, "*")].map do |path|
path if File.directory?(path)
end.compact
end
end
require 'find'
# A helper to manage which script tags to use in development vs production.
module BundleHelper
# Use this to indicate the local name of a js lib, and a publicly hosted one. The local (unminimized) will be included in development mode, and the public one used for production
#
# Examples:
#
# != javascript_dev ['jquery-1.4.1', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js']
# != javascript_dev ['jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js']
#
def javascript_dev(*sources)
output = ""
sources = sources.to_a
sources.each do |pair|
output << javascript_src_tag(Rails.env == 'development' ? "dev/#{pair[0]}" : pair[1], {})
end
output
end
# This will include every file under 'public/javascript/{source}/*'. In development mode, it will have one <script> tag per file. In production, it will have one script tag for 'public/javascript/{source}.js'
#
# Example:
#
# != javascript_bundle 'common'
#
def javascript_bundle(*sources)
sources = sources.to_a
bundle_files? ? javascript_include_bundles(sources) : javascript_include_files(sources)
end
# This method assumes you have manually bundled js using a rake command
# or similar. So there better be bundle_* files.
def javascript_include_bundles(bundles)
output = ""
bundles.each do |bundle|
output << javascript_src_tag("bundle_#{bundle}", {}) + "\n"
end
output
end
def javascript_include_files(bundles)
output = ""
bundles.each do |bundle|
files = recursive_file_list("public/javascripts/#{bundle}", ".js")
files.each do |file|
file = file.gsub('public/javascripts/', '')
output << javascript_src_tag(file, {}) + "\n"
end
end
output
end
def bundle_files?
Rails.env == 'production' || Rails.env == 'staging' || params[:bundle] || cookies[:bundle] == "yes"
end
def recursive_file_list(basedir, extname)
Dir["#{basedir}/**/*#{extname}"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment