Skip to content
Create a gist now

Instantly share code, notes, and snippets.

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 ={
: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 = => options.munge)
master_js = yui.compress(combined.to_s)
master_js = combined.to_s
say "Writing output to js/master.js", :yellow, 'js', 'master.js'), 'w') {|file| file.write(master_js) }
say "Done!", :green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.