So I have just finished my third HTML5 game and my second game built with Google Closure. For anyone who doesn’t know what the Google Closure library is, it is mainly two things; the Closure Compile and the Google Closure Library (goog). The compiler concatenates your individual game scripts/classes into one and applies optimisations to them during the compilation process and the library provides classes and utilities that help you create your javascript projects.
You do not need to use the library to use the compiler which is great, I first came across the closure compiler when I had finished Nina’s Workshop Drop and needed a way to compile all the scripts. I looked into various solutions but Closure seemed to offer the best features.
Developed within Googles 20% Time (like Gmail), supposedly it was named “Closure” because it was going to bring closure to a period of time at Google where they had no unified tools (I think I heard this on a podcast).
So firstly the compiler (the exciting bit), as with all game development, your game is broken down into many classes but with Javascript, these files can be deployed individually which is a bad idea because it creates a heavier load on the server and potentially slower load times. So instead of this, you should compile your classes into one or a few scripts. Luckily there are tonnes of tools that can do this for you; uglifyJS or Yahoo YUI are both good options but the Closure compiler offers an “Advanced” compilation mode. This is what got me excited. The advanced mode scans your code and does numerous optimisations such as dead code elimination and function inlining which is super cool! If all goes well your code should be smaller and run faster, winning around.
There are however a couple of drawbacks but you will soon get over them. First, you have to compile your code. One of the first things I loved about javascript was ease of use and speed, if i make a change this could be instantly seen in the browser, skipping the time spent compiling slowly added up. An answer to this would be to test your raw code then do a compile every so often to make sure it is still working. Another drawback is that due to the compilation process you have to use string references instead of dot notations when references external dynamic files such as json files.
myJSON.myNestedObject myJSON[ "myNestedObject"]
The second part is the Library, the Closure library offers a tonne of methods and helpers for creating content with Javascript and is supposedly what google developers used to create apps such as Gmail, GMaps, GTasks. Most of this is probably not that helpful for game development but it does have a dependancy management system which is the part I like. As with RequireJS, you specify the file dependencies and it will look after them for you. Back when I built my first HTML5 game, Nina and the Neurons, I did this manually and it was a right pain, looking back I feel stupid for not using some form of dependency management.
By using the Goog dependancy management, you can run build script in a task runner such as Grunt that informs the compiler of all the files it needs, without this you would have to do it manually. I believe requireJS also has an option to compile with the Closure compiler and is, therefore, a viable alternative that I will have to have another look into. I prefer the way you specify the dependancies in goog over that of requireJS, (similar to AMD vs CommonJS), it is much more like the flash imports which makes sense as the whole point of the goog library was to bring more of a Java way of programming to Javascript.
You can find out a lot more on the Google Closure site: https://developers.google.com/closure/library/docs/tutorial
Finally here’s a talk was given by one of the Google developers when they had just release Closure into the public back in 2011.
;
Share Your Thoughts