The compiler expects that it's given all relevant JavaScript so that it doesn't need to worry about clashes with other scripts. Therefore it assumes that it can unwrap the "unnecessary" anonymous function.
From the FAQ:
When using Advanced Optimizations, Closure Compiler adds new variables to the global scope. How do I make sure my variables don't collide with other scripts on the page?
Closure Compiler's advanced optimizations mode assumes that it's ok to add new variables to the global scope.
In JavaScript, it's often standard practice to wrap your code in an anonymous function, so that variables don't pollute the global scope. Closure Compiler has an
--output_wrapper
flag for exactly this purpose. Invoking it as--output_wrapper "(function() {%output%})();"
will wrap your code in an anonymous function at compile-time.Closure Compiler users often find it easier and simpler to do this wrapping at compile-time, rather than writing the anonymous function wrapper in the original source code.