Home arrow AJAX & Prototype arrow Google's Closure Compiler Service API: Additional Features

Google's Closure Compiler Service API: Additional Features

In this eighth and final part of a series that explains the various features of Google's Closure Compiler Service API, you'll learn about a few features that didn't fit neatly into the discussions in the previous parts.

  1. Google's Closure Compiler Service API: Additional Features
  2. Formatting compiled code with the pretty_print option
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
October 25, 2010

print this article



If you’re in search of a friendly guide that teaches you in a step-by-step fashion how to work with the most relevant features that comes included with Google’s Closure Compiler Service API using PHP, then look no further because you’ve come to the right place.

Welcome to the final installment of this article series. As the above title suggests, through its various parts, the series illustrates how to programmatically optimize custom JavaScript files via the new web service that the popular search company has released in the last few weeks.

In earlier articles in this series I went through the development of a respectable number of code samples, which hopefully provided you with the right tips to start consuming the Closure Compiler Service API from within your own PHP applications. In this case, I decided to create from scratch an object-oriented back end to interact with the API and pass different parameters to it; however, it’s possible to get the same results using a procedural approach or even a different programming language, as long as it’s capable of triggering and handling POST HTTP requests.

So far, I've covered distinct facets of the API, ranging from applying different levels of optimization to supplied JavaScript snippets, to returning compiled code in a range of formats (text/JavaScript, XML and JSON). In addition, I also discussed how to work with the API’s built-in debugger, which allowed you to send to the client the warnings and errors that might have been generated during the compiling process.

With all of these topics already discussed in depth, it might seem like there’s nothing left to talk about the Closure Compiler Service API. In reality, the API offers a few additional features that are worth looking at, since they’re quite useful when you need to apply a “printer-friendly” format to optimized JavaScript or to retrieve compiling statistics. So, in this final chapter of the series I’m going to show you how to use these miscellaneous features, thus concluding this educational series on Google’s Closure Compiler Service API.

Now, it’s time to learn a few more useful things about this web service. Let’s go!           

Retrieving compilation information: displaying statistics about an optimized JavaScript snippet

If you’re anything like me, you’ll want to see how effective the Closure Compiler Service API actually is when it optimizes JavaScript snippets. Fortunately, the API provides a straightforward method to get this information via its “output_info” argument. What’s more, if the value “statistics” is assigned to the argument, the compiler will return to the client a summary that includes detailed data about the results of the compiling process.

The following example shows how to accomplish this task in a simple manner. Check it out:


// example using output_info = statistics
try {
    // include the autoloader class
    require_once 'Autoloader.php';

    // create an instance of the file handler class and read the specified JavaScript file
    $fileHandler = new FileHandler;
    $js = $fileHandler->read();
    // build the array of arguments that will be passed to the closure compiler API
    $data = array(
         'js_code' => $js,
         'compilation_level' => 'ADVANCED_OPTIMIZATIONS',
         'output_format' => 'text',
         'output_info' => 'statistics'
    // create an instance of the ClosureCompilerHandler class
    $compilerHandler = new ClosureCompilerHandler($data);
    // query the closure compiler API and get the compiled JavaScript function
    $response = $compilerHandler->sendRequest();
    // print to screen the compiled JavaScript function
    echo $response;
    displays the following
    Original Size: 477
    Original Gzip Size: 243
    Compressed Size: 156
    Compressed Gzip Size: 133
    Compilation Time: 0
catch (Exception $e) {
    echo $e->getMessage();

As the above example depicts, simply passing the “statistics” argument to the API will cause the compiler to send information back to the client about the optimized JavaScript code, including its original and compressed sizes. This demonstrates that the API is not only capable of optimizing JavaScript snippets in a truly effective manner, but can be used for getting a full summary of the whole optimization process as well. That’s pretty helpful, indeed.

So far, so good. Now that I have explained how to instruct the compiler to return statistics about the compiled code, it’s time to explore a few extra features included with the API. Thus, in the following segment I’m going to show you how to pass an additional argument to the API called “formatting,” which can be used for serving compiled code in a printer-friendly format.

Now, to learn how to use this new argument, jump ahead and read the lines to come.   

>>> More AJAX & Prototype Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Best AJAX Tutorials for Forms
- The Best AJAX Tutorials
- 8 Great Ajax Tutorials
- Using Ajax and jQuery
- Using Ajax and jQuery with HTML Forms
- Ajax.org Offers Cloud9 IDE for JavaScript
- Java Technologies Provider ICEsoft Releases ...
- Using Recaptcha in AJAX Prototype Framework ...
- Google's Closure Compiler Service API: Addit...
- Installing Google Web Toolkit: Introducing t...
- Google's Closure Compiler Service API: Displ...
- Google's Closure Compiler Service API: Deliv...
- Google's Closure Compiler Service API: the A...
- Google's Closure Compiler Service API: the S...
- Google's Closure Compiler Service API: Optim...

Developer Shed Affiliates


Dev Shed Tutorial Topics: