I’ve been using online version of Google Closure Compiler as a minifier for my projects for some time now. I always wondered how the advanced compilation works, but never really gave it a shot. Lately I started working on a university project and as you may know, academics do not like JavaScript in general. To make it even worse for them, I decided to prepare a very complex JS setup, with fully annotated Google Closure code. Not only out of spice, but also to prove that using a proper approach, it can be fast and well-structured.
I’m not going to describe here how the Compiler works in detail. In short words, it scans your files looking for comments in a specific, JSDoc-like format and makes use of them. There is also a tool called calcdeps which is a linker – it scans files for dependencies and helps compiler to merge files in the correct order.
To make it even more fun, I decided to write a Makefile which will be a final jaw-dropper.
PYTHON = python CALCDEPS = ~/closure/closure-library-read-only/closure/bin/calcdeps.py CLOSURECOMPILER = ~/closure/compiler.jar all: $(PYTHON) $(CALCDEPS) -p src -i src/init.js -o compiled -c $(CLOSURECOMPILER) > scripts/ai-bot-compiled.js
It works like charm, “make” is compiling all required files from src folder, merging them into one.
I was surprised how easy it was – calcdeps takes a path (-p) where you store all your libraries and an initial file (-i). The rest is based on goog.require(classname) and goog.provide(classname) calls from files. I barely used Google Closure Tools so far, but the Compiler is absolutely great!
Google Closure: http://code.google.com/intl/pl/closure/
Book: http://www.amazon.com/Closure-Definitive-Guide-Michael-Bolin/dp/1449381871



