Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save JackDanger/267464 to your computer and use it in GitHub Desktop.

Select an option

Save JackDanger/267464 to your computer and use it in GitHub Desktop.
asset bundler
file "public/stylesheets/cache/bundle.css" => FileList["public/stylesheets/*.css"] do |t|
File.open(t.name, 'w') do |bundle|
bundle.write( t.prerequisites.map {|css| File.read(css) }.join("\n") )
end
puts "***\nWrote #{t.prerequisites.size} Javascript files into bundle.css"
end
file "public/javascripts/cache/bundle.js" => FileList["public/javascripts/*.js"] do |t|
# load Rails
require File.join(Rails.root + "config", "environment")
# find out which js files need to be loaded first
priority = ActionView::Helpers::AssetTagHelper.register_javascript_expansion({})[:defaults]
# fix priority filenames
priority.map! {|file| "public/javascripts/#{file.chomp('.js')}.js" }
# prepend the priority assets
sources = ((priority & t.prerequisites) + t.prerequisites.sort).uniq
# Use Google's Closure compiler for compacting
compiler = File.join(File.dirname(__FILE__), 'google_closure_compiler.jar')
sh "java -jar #{compiler} --compilation_level=SIMPLE_OPTIMIZATIONS #{sources.map{|f|"--js=#{f} "}} --js_output_file=#{t.name}"
puts "***\nWrote #{sources.size} Javascript files into bundle.js"
end
namespace :assets do
task :compile => :bundle do
system "git commit #{File.join(Rails.root, "public")}/*/bundle.* -m 'updating asset bundles'"
end
task :bundle => ["bundle:javascripts", "bundle:stylesheets"]
namespace :bundle do
task :javascripts => "public/javascripts/cache/bundle.js"
task :stylesheets => [:run_sass, "public/stylesheets/cache/bundle.css"]
task :run_sass do
require File.join(Rails.root + "config", "environment")
Sass::Plugin.update_stylesheets
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment