public
Created

My Thor action for concatenating and minifying JavaScript

  • Download Gist
concat-and-minify.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# module: javascript
class Javascript < Thor
include Thor::Actions
 
desc "compile", "Combine and minify JavaScript files to js/master.js"
method_option :minify, :type => :boolean, :default => true
method_option :munge, :type => :boolean, :default => false
def compile
%w(rubygems sprockets yui/compressor).each {|lib| require lib }
 
say "Setting up Sprockets...", :blue
sprockets = Sprockets::Secretary.new({
:asset_root => File.dirname(__FILE__),
:load_path => File.join(File.dirname(__FILE__), 'js'),
:source_files => [File.join(File.dirname(__FILE__), 'js', 'main.js')]
})
 
say "Combining JS files...", :blue
combined = sprockets.concatenation
 
if options.minify
say "Compressing master JS...", :blue
yui = YUI::JavaScriptCompressor.new(:munge => options.munge)
master_js = yui.compress(combined.to_s)
else
master_js = combined.to_s
end
 
say "Writing output to js/master.js", :yellow
File.open(File.join(File.dirname(__FILE__), 'js', 'master.js'), 'w') {|file| file.write(master_js) }
say "Done!", :green
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.