Updates Instagram Photography Projects About Contact


26 February 2010

At the beginning of 2006 Erik and I started a 20% project to write a JS library for Google. Today it is used by many projects within Google and was recently open sourced.

On its own, the Closure Library isn’t very compelling for the average web developer. The dynamic loading is intended for unit tests and development, and using it in production would cause users to download masses of unused code.

Luckily for us, the Closure Compiler was open sourced at the same time. The compiler knows about a lot of the idioms used in the Closure Library and does a really good job of removing dead code and optimizing what remains. The team also launched a web service with a RESTful API so you don’t even have to run the compiler yourself.

Even with the web service, the development-debug cycle isn’t ideal. So when I found myself wanting to use some of the Closure Library for my new photo site I hacked together a PHP class that provides a convenient interface to the compiler web service, caches the optimized code, and recompiles when the sources have changed. Pretty basic, but quite useful I think.

I know PHP is looked down upon by many programmers, but it is easy, convenient, and widely available. If you have a PHP based site and want to use the Closure Library and/or the Compiler, it might be worth heading over to the project page and seeing if it might be useful.


15 Responses

  1. Chintan says:

    Very Useful!

    I’m thinking of developing a Django/Python based auto-compiling for Soy Templates to JS – its really painful when I make a few UI changes to test in a browser and have to run a batch script that compiles all .soy files.

  2. Dan says:

    Yeah, that would be very useful as well.

  3. matka/html says:

    i’ve a totally different view on php: “its a really powerful language for quick web dev”

  4. mo says:

    is there a possibility that we’ll be seeing a “code_url” option in this php code, since the closure compiler seems to have a higher file size tolerance when files are sent to it via code_url.

    I think there are also some php warning given out on some of the variables that are evaluated in the switch case scenarios. I’ve added this in my own code just to initialize those variables.

    = $errors
    = $warnings
    = $code
    = $originalSize
    = $originalGzipSize
    = $compressedSize
    = $compressedGzipSize
    = $compressedGzipSize
    = $compileTime
    = $node = null;

  5. mo says:

    I just realized my code had duplicates.

  6. Dan says:

    @mo : I don’t run with E_NOTICE so probably missed them, it seems overly persnickety. I’ll try and clean them up.

  7. mo says:

    Yeah : )
    Btw hacking in the code_url option was pretty easy, I’m trying to make a more standardized way of implementing it using your structure now. My Javascript file was just over the 195.3Kb limit so I needed to have that option.

  8. Dan says:

    @mo : Try checking out the latest version and let me know if it still has problems for you.

  9. Dan says:

    And mo, if you want to send a patch for the code_url support I’ll happily take a look.

  10. mo says:

    The patch for code_url isn’t really pretty but sure I’ll send it your way. I’m kind of stuck at the recompile check, it seems to always recompile my code for me. I’m trying to see whats causing it and I’ll get back to you afterwards : )

  11. mo says:

    seems that the recompile only messes up on my local server, it seems to work fine online.

  12. mo says:

    it was the filemtime($_SERVER[“SCRIPT_FILENAME”]) check, since i had included the script and as you warned above the code it would cause the script to repeatedly recompile since my original php file was under heavy editing.

    I’ve send over the code_url addition as soon as I can. : )

  13. mo says:

    @Dan, I’ve sent you my altered code : )

  14. Johannes says:

    Not sure how up-to-date you are with PHP, but your code looks like PHP4, or even PHP3. PHP actually supports visibility on properties, and on methods now! :)

  15. Dan says:

    lol, yeah, I know. It’s PHP4 mainly due to compatibility issues. My servers are now upgraded so I wouldn’t mind too much if it was updated. Care to take a stab and send me a patch via the project page? :)