How to Use Google Closure's Advanced Mode Without Breaking Everything

For adamia3d I wanted to use Google Closure for minification, and I particularly wanted the maximum setting for dead code removal and function inlining. If I could make anonymous functions that I knew would get inlined, I could organize the code much better in the innermost parts of the engine (where functions are off-limits due to the performance hit of a function call in javascript). However, the maximum setting of Closure, ADVANCED_OPTIMIZATIONS, completely destroys the source for a library. Adamia3d is an open-source library, and I want every class and every property to be considered public and hackable by its users. Unfortunately Closure renames every single class and member unless you modify your source to accomodate (using class['member'] instead of class.member everywhere). Modifying my source like this is unreasonable, so I came up with a nifty hack around it.

The trick is to run Closure twice, using not-well-known command line switches "--property_map_input_file" and "--property_map_output_file". Closure supports exporting a simple text file of all the properties it renamed and how to map the short version to the long version. The hack lies in the fact that it also supports importing the same property map, originally intended for successive recompilations of the same code to have a stable mapping. All we have to do is exploit this feature with the following steps:

  • Run Closure once and generate the property map.
  • Use a simple regex to invert the property map, pointing the short versions back to the long ones.
  • Run Closure again with the inverted property map.

Here is a sample block of an Ant build.xml that runs Closure twice, and also does a hack to prevent @preserve comment blocks from getting lost in the second pass:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.