Compressing Javascript Code with Google Closure Compiler and Ruby

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(''), {
'js_code' => jscode,
'compilation_level' => "#{compilation_level}",
'output_format' => 'text',
'output_info' => 'compiled_code'

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.

About these ads

2 Responses

  1. 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 :-)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: