16

I have a javascript file with a global object that must not be renamed (_gat from the Google Analytics async tracker).

This object must not be renamed by the Google Closure Compiler as Google Analytics looks for a variable with this specific name.

I've looked into the Javascript Doc notations that are mentioned: http://code.google.com/closure/compiler/docs/js-for-compiler.html - However, I cannot find anything regarding the "protection" of a variable.

The problem exists no matter if I use simple or advanced compilation.

How can I ensure that the _gat variable is not renamed?

4 Answers 4

16

Using Closure Compiler web app, you can set js_externs. Refer to Advanced Compilation and Externs for more examples.

// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// @output_file_name default.js
// @js_externs _gat
// ==/ClosureCompiler==
0
6

Use the goog.exportSymbol function from base.js. Documentation is here: http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html

Usage is like

goog.exportSymbol("_gat", _gat)
2
  • Can you elaborate a bit on that please? Where do I find further information?
    – fiskah
    Jun 11, 2010 at 20:14
  • (Sorry for the terse initial reply, was replying from my phone and my screen-keyboard-fu is weak). Jun 13, 2010 at 0:37
3

See http://closuretools.blogspot.com/2011/01/property-by-any-other-name-part-1.html the part about "All Unquoted"

putting certain vars inside ' ' >>> 'bob' prevents them from being renamed by the compiler

1
  • 1
    This is an interesting option. Even when performing ADVANCED_OPTIMIZATIONS, Closure will not rename string references to methods. Eg it will not rename window.foo if referenced like: window['foo'] Sep 20, 2012 at 17:03
2

If it isn't declared as part of the code you are compiling than an extern declaration is appropriate: http://code.google.com/closure/compiler/docs/api-tutorial3.html#externs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.