I've tried to compile my JS+jQuery script with Google Closure Compiler. It works alright with the simple optimization option. However, when I switch to advanced optimization some errors occur.
For instance, it changes jQuery $.ajax() method to $.i(). That simply makes the browser go crazy.
Any ideas on why this is occurring?
The advanced optimization will mangle names, including property accesses. In order to use the advanced optimizations, you have to write code specifically for that purpose. At the end of a lot of library code written for the closure compiler you'll see stuff that looks like this:
window['MarkerClusterer'] = MarkerClusterer;
MarkerClusterer.prototype['addMarker'] = MarkerClusterer.prototype.addMarker;
MarkerClusterer.prototype['addMarkers'] = MarkerClusterer.prototype.addMarkers;
MarkerClusterer.prototype['clearMarkers'] = MarkerClusterer.prototype.clearMarkers;
or for a simplified example
window['myLibraryName'] = myLibrary;
myLibrary['someMethod'] = myLibrary.someMethod;
Reason being that the compiler will obviously not mangle string literals and now those names can be properly exported via the myLibrary
object.
So unless jQuery (or any other library) is specifically written to play nice with the feature, you can't use advanced optimizations. Which is a criticism frequently leveled at the closure compiler. OTOH, this is something that google wrote for themselves, for their own codebase, and were nice enough to give us for free, so...