Skip to content

Instantly share code, notes, and snippets.

@mikedamage
Created February 16, 2011 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikedamage/828806 to your computer and use it in GitHub Desktop.
Save mikedamage/828806 to your computer and use it in GitHub Desktop.
My Thor action for concatenating and minifying JavaScript
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment