HomeAJAX & Prototype Google's Closure Compiler Service API: Delivering Compiled Code in Different Formats
Google's Closure Compiler Service API: Delivering Compiled Code in Different Formats
In this sixth part of the series, I explore Google's Closure Compiler Service API's “output_format” argument. It permits you to deliver compiled JavaScript code to the client in different formats, including the already familiar “text/javascript” type. I also show you how to deliver code in XML and JSON formats.
As you know, in the last few years Google has branched out from its core search engine service. It has launched a bunch of web services, aimed, at least in theory, at making the life of web developers and designers a bit easier. This time the company has put its effort into the client side by implementing a brand new service called Closure Compiler Service API. This service allows you to optimize custom JavaScript code through a set of easily configurable options that must be passed to the API via POST HTTP requests.
Quite possibly, the most interesting facet of this service is that it can be consumed by using any popular programming language, including PHP. This ability permits you to compile JavaScript snippets programmatically without having to spend valuable time performing this task manually via a graphic user interface (usually an HTML form).
To demonstrate how simple it is to interact with the Closure Compiler Service API via a PHP backend, in previous installments of this series I created some basic examples that showed how to apply different levels of optimization to a specified JavaScript file by assigning distinct values to the API’s “compilation_level” argument. This process revealed that the compiler is capable of doing some useful tasks behind the scenes, such as removing comments and white space from target JavaScript files, shortening function and variable names, and even refactoring large chunks of code.
Moreover, the Closure Compiler Service API also has the ability to return compiled code in different formats, including the familiar “text/javascript” content type, XML and JSON. If you’re interested in learning how to accomplish this, in this sixth part of the series I’m going to set up a few easy-to-grasp code samples that will illustrate how to return compiled JavaScript code in the formats mentioned above.
Ready to continue learning about Google’s Closure Compiler Service API? Then start reading!
Creating a “standard” example: returning compiled JavaScript in “text/javascript” format
As I explained in the introduction, the Closure Compiler Service API is capable of delivering compiled JavaScript code in three different formats: the default “text/javascript” type, XML and JSON. In case you’re wondering how to achieve this, the content type generated by the compiler can be easily controlled by assigning different values to its “output_format” argument. It’s that simple.
Having explained that, the first example that I plan to show you returns compiled code using a mixture of text and JavaScript. Here it is:
<?php
// example using output_format = text try {
// include the autoloader class require_once 'Autoloader.php'; Autoloader::getInstance();
// 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' => 'compiled_code' );
// 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(); $compilerHandler->sendHeader('js'); // print to screen the compiled JavaScript function echo $response; /* displays the following
Admittedly, the code sample shown above looks very similar to others developed in previous parts of this series. It sends back to the client compiled code in the format of text and JavaScript, so the “ClosureCompilerHandler” class also includes the appropriate “Content-type: text/javascript” MIME header, before echoing the output to the browser.
At this point, I’m certain that you've already grasped the logic that drives the previous example. It simply assigns the value “text” to the “output_format” argument passed to the Closure Compiler Service API. As I explained before, though, the API can also be instructed to return compiled code in XML and JSON as well.
In the next segment I’m going to create another example. It will utilize the aforementioned “output_format” argument to return optimized JavaScript code as XML.
Want to see how this will be done? Then click on the link below and keep reading.