For example, in aggressive mode, Google Closure will rename functions. If someone includes my closure compiled script alongside another script someone else also compiled with closure, are renaming conflicts likely to occur?
In short, I want to minify my code, but it will be used on other websites and I want to avoid conflicts with other scripts.
You ALWAYS have to worry about collisions of variables defined in the global scope in JavaScript, REGARDLESS of whether you minify your scripts or not. Use a functional closure wrapper to wrap your code if you want to minimize chances of collision.
Closure's Advanced Mode only makes the potential collisions worse, as it compiles many objects into new global objects (which are named similarly, e.g. a
, b
etc.) for the fastest performance. That's why Closure's Advanced Mode is used best with all the program files at once, never piecemeal.
Things like ga.js
(Google Analytics) are designed so that they expose only a few objects to the global scope and everything else wrapped up in a closure. The file itself is aggressively optimized. Beware -- the fact that it is minimized/not-minimized has nothing to do with collisions. You can have a plain-script JavaScript file with tons of collisions, or you can have a heavily-optimized script with no collisions.
Collisions have nothing to do with minification or variable renaming. You avoid collisions by avoiding creating objects in the global scope. Anything else that is not created in the global scope does not collide with other scripts. For example, you CAN collide ga.js
if you load another script which overwrites the _gat
or _gaq
global variables. Try it, and Google Analytics will no longer work. In other words, ga.js
works with different web pages not because it is collision-free, but because it creates global variables (i.e. _gat
and _gaq
) with names that are very unlikely to be chosen by other scripts.