Google Closure Javascript Library
Google have released their internal javascript library (no, this isn’t about GWT, that is something different) and called it Closure. Odd name and one that is already heavily in use
- Closure – the first-class function
- Clojure – the lisp dialect
- Clozure – another lisp implementation
- Closure – a shutdown tool for X windows
- Closure – a web browser
- and a whole load more ..
Naming issues aside, the library offers a whole host of functions (like all the other major libraries) for dom manipulation, event handling, asynchronous calls etc. There are a couple of nice extras that it does have over other libraries.
Require and Provide
Instead of loading the entire library in (which would be huge) you can just load the bits you need using the require call. Nice start, but it gets better.
If you load part of the library that requires another part of the library google sorts that out for you and loads in for you; now that is smart.
It does this using the .require & .provide methods included in the base closure file.
In the closure files the first thing each one does is tell you what it does and then what it needs…
goog.provide('goog.math');
goog.require('goog.array');
No more missing dependancies!
Compression
As part of the closure library google they also made a lovely compressor to shrink their code to tiny size and then made it into an API. It has a few settings related to how much it compresses the code by (compilation_level).
WHITESPACE_ONLYSIMPLE_OPTIMIZATIONSADVANCED_OPTIMIZATIONS
WHITESPACE_ONLY does what you might think, it removes all the whitespace from your js like any other minifying tool. SIMPLE_OPTIMIZATIONS is where it starts to get interesting; white space is gone and local variable names get renamed. Now, when you set it ADVANCED_OPTIMIZATIONS the really clever stuff starts to happen.
There are still a few things you need to watch out for when using ADVANCED_OPTIMIZATIONS, mostly to do with consistency and string literals.
The most obvious one is of course external files using the now compiled javascript will struggle as function names have changed and name spaces might no longer be the same (I need to test this out in detail).
As the google complier page isn’t exactly friendly I knocked up a quick site to handle the most common queries and called it closure optimiser
blog comments powered by Disqus



