in Programming

Use Google Closure Compiler with PHP

The closure compiler from Google merge, minify and optimize your javascript files. You can use it in some different ways and i have prepared some examples.

  • Simple web application called “Closure Compiler Service UI”
  • via a RESTful API
  • As stand-alone command-line application

Closure Compiler Service UI

If you just want to try the compiler you can do this on a website from Google. You can enter an url or copy/paste your javascript into a form field and it simply outputs the result in another textarea.

Google Closure Compiler RESTful API

It’s a good idea to use the official API because there is always the latest version and so you will get the best results. This example shows you how to use PHP5 to communicate with the API. You simply have to do a post request to the endpoint and give them your javascript-code or an url of the javascript file.

The API (see reference) resides under the following URL and requires four params:

The example loads the jQuery javascript source and compiles it:

$script = file_get_contents('');
$ch = curl_init('');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($script));
$output = curl_exec($ch);
echo $output;
i use simple optimizations, it don’t need further config (like advanced) but is
better than whitespace only.
output_format is “text” if you want compile a javascript
output_info use “compiled_code” if you want compile a javascript. You can also use warnings, errors, and statistics and you will get different informations as output.
js_code is your javascript source code, instead you can submit “code_url” param, an url
to a javascript-file

Closure compiler command-line program

First download and unpack the Closure java application to your computer or your server, the download url is: (Google hosting on GitHub now … nice)

On linux servers you can do the download with wget:


After unpacking u get a file named compiler.jar, that’s the application, you can execute it only if you have java installed. If not, see the help page for installing java on debian servers.

java -jar compiler.jar --js hello.js --js_output_file hello-compiled.js

The command above compiles a hello.js in the current working directory to hello-compiled.js with the default settings. For more infos you can show a help screen with:

java -jar compiler.jar --help

Usually you want compile your whole scripts directory. My script reads all files of a given directory, checks if the file is a .js and compiles it to another directory:

$basePath = dirname(__FILE__);
$scriptDir = $basePath.'/scripts/';
$compiledDir = $basePath.'/compiled/';
if (is_dir($scriptDir)) {
    if ($dh = opendir($scriptDir)) {
        while (($file = readdir($dh)) !== false) {
        	// read all files, if file is js and not minified then minify/compile it
        	if (strpos($file, '.js') !== false && strpos($file, '.min.js') === false) {	
        		$compilerCommand = sprintf('/usr/bin/java -jar %s/compiler.jar --js %s --js_output_file %s',
					$basePath, $scriptDir.$file, $compiledDir.str_replace('.js', '.min.js', $file));
				exec($compilerCommand, $return, $code);	
				if ($code != 0) {
					printf("Fuck! Something went wrong: %s (%s)", join('<br />', $return), $code);
				} else {
					printf("%s compiled successfully.", $file);

If you get the error code: (1) or (254) check the permissions of your compiler.jar and “compiled”-directory. Everyone must be allowed to read and execute the compiler.jar and everyone must can write to the compiled folder:

-rwxr-xr-x 1 root root 4,1M 17. Jun 01:09 compiler.jar
drwxrwxrwx 2 root root    4096 11. Sep 15:12 compiled

Set the right permissions with:

chmod a+rx compiler.jar
chmod a+w compiled