With Google releasing their Closure tools to the public, the Closure Compiler and its RESTful API provides an accessible way to compile and minify Javascript code.
We’ve been using the RESTful API as part of our automated deployment process and have found it to work very well for us.
The Wiki example to call the web service is in python. A solution in Ruby could be easily deduced.
module Compressor
require 'net/http'
require 'uri'
def compress(compilation_level,jscode)
response = Net::HTTP.post_form(URI.parse('http://closure-compiler.appspot.com/compile'), {
'js_code' => jscode,
'compilation_level' => "#{compilation_level}",
'output_format' => 'text',
'output_info' => 'compiled_code'
})
response.body
end
end
Not much to it but it makes compiling JS code a breeze and can greatly help reduce file size. :-D
You can also use the web version of the Closure Compiler if you need to do one-off tasks.
Filed under: Uncategorized






I tried this but google eventually refuses to serve you if you make more than a handful of requests in an hour. Plus, it doesn’t support really long javascript files.
Yup, that happened to us as well. We started sending file fragments to get over that limitation. Since we use this for our automated build only, it still working for us… for now… if you know of another RESTful API that doesn’t have these limitations… we’re all ears :-)