Created
June 18, 2010 10:50
-
-
Save balaclark/443503 to your computer and use it in GitHub Desktop.
Command line script to compress & combine javascript files using the google closure compiler. "sources" needs to be a plain text file with urls of each javascript file to compress on a new line, comments are fine.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rest_client' | |
sources = "sources" | |
output = "scripts.min.js" | |
output_path = (File.expand_path(File.dirname(__FILE__))) + "/" + output | |
sources_path = (File.expand_path(File.dirname(__FILE__))) + "/" + sources | |
# TODO: only open file if no errors, maybe save a tmp file, then copy it over the real one? | |
js = File.new(output_path, "w") | |
# write credits at the top | |
js.write("\n/*\n * " + output + " generated by min.rb \n * " + Time.new.inspect + "\n */\n") | |
File.new(sources_path).readlines.each do |line| | |
next if line.match(/^#/) # skip comments | |
# compress javascript | |
if line.match(/\.js$/) | |
puts "compressing: " + line | |
response = RestClient.post("http://closure-compiler.appspot.com/compile", { | |
:code_url => line, | |
:compilation_level => "SIMPLE_OPTIMIZATIONS", | |
:output_format => "text", | |
:output_info => "compiled_code" | |
}) | |
# save the compressed js if no errors were thrown | |
unless response.match(/^Error/) | |
js.write("\n// " + File.basename(line) + "// source: " + line + response) | |
else | |
puts response | |
end | |
end | |
# TODO: compress css | |
if line.match(/\.css$/) | |
puts "no css support yet, skipping " + line | |
end | |
end | |
puts "result: " + output_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment