Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing closure compiler from renaming certain variables

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?

like image 372
phidah Avatar asked Jun 11 '10 20:06

phidah


2 Answers

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==
like image 73
Kevin Hakanson Avatar answered Sep 20 '22 17:09

Kevin Hakanson


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)
like image 44
Moishe Lettvin Avatar answered Sep 20 '22 17:09

Moishe Lettvin