Skip to content

Instantly share code, notes, and snippets.

@blacktm
Created April 25, 2012 20:31
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 blacktm/2493107 to your computer and use it in GitHub Desktop.
Save blacktm/2493107 to your computer and use it in GitHub Desktop.
A JavaScript minifier and dependency loader in Ruby.
#!/usr/bin/env ruby
# Notes:
# * Your JS files in the "manifest" will be minified using Google's Closure Compiler
# and added to the final build in the order listed.
# * Vendor libraries (like jQuery) and the "compiler.jar" must be in your
# "vendor_lib_path" directory.
# * Write your JS in modules for best results!
# Running file directly
if __FILE__ == $0
##############################################################################
# Configuration (YOU EDIT THIS PART!)
# Source libraries
# (Vendor libraries are NOT minified, only your source files)
vendor_libs = ["jquery.min.js", "raphael.min.js"]
# (Add your JS files here in the order to be loaded)
# (DO NOT ADD THE .JS EXTENSION)
manifest = ["file_1", "tests/file_2", "tests/other/file_3"]
# Base paths
scripts_path = "scripts/"
vendor_lib_path = "vendor/"
build_path = "public/js/" # Where you want the final JS build to end up
# Content (add your own copyright or license)
copyright = <<COPYRIGHT
/**
* My Product
* Your Name (c) 2012, All rights reserved.
*/
COPYRIGHT
##############################################################################
# Start compilation (YOU DON'T EDIT THIS PART - UNLESS YOU WANT TO)
# Start the timer
beginning = Time.now
puts "\n"
# The final build containing all the JS
js_build = ""
# Load vendor libraries
vendor_libs.each do |lib|
js_build << File.read("#{vendor_lib_path}#{lib}") << "\n"
end
# Example:
# "java -jar compiler.jar --js hi.js --js_output_file hi.min.js"
compiler_cmd = "java -jar #{vendor_lib_path}compiler.jar "
manifest.each do |js_file|
puts "Adding #{js_file}.js"
compiler_cmd << "--js #{scripts_path}#{js_file}.js "
end
puts "\nCompiling...\n"
js_build << copyright
result = IO.popen(compiler_cmd)
js_build << result.readlines.join
File.open("#{build_path}main-build.js", "w") { |f| f.puts(js_build) }
puts "\nAll done!"
t_elapsed = (Time.now - beginning).round(2)
puts "Finished in #{t_elapsed} seconds.\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment