防止闭包编译器重命名某些变量

发布于 2024-09-05 12:25:11 字数 452 浏览 3 评论 0原文

我有一个带有全局对象的 javascript 文件,该对象不得重命名(来自 Google Analytics 异步跟踪器的 _gat)。

该对象不得由 Google Closure 编译器重命名,因为 Google Analytics 会查找具有该特定名称的变量。

我研究了提到的 Javascript 文档符号: http: //code.google.com/closure/compiler/docs/js-for-compiler.html - 但是,我找不到有关变量“保护”的任何内容。

无论我使用简单编译还是高级编译,问题都存在。

如何确保 _gat 变量不被重命名?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

☆獨立☆ 2024-09-12 12:25:11

使用 Closure Compiler Web 应用程序,您可以设置 js_externs。有关更多示例,请参阅高级编译和外部

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

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==
岁吢 2024-09-12 12:25:11

使用 base.js 中的 goog.exportSymbol 函数。文档位于:http://closure-library.googlecode.com/svn /docs/closure_goog_base.js.html

用法就像

goog.exportSymbol("_gat", _gat)

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)
醉酒的小男人 2024-09-12 12:25:11

请参阅http://closuretools.blogspot。 部分>>>

com/2011/01/property-by-any-other-name-part-1.html 关于“All Unquoted”将某些变量放入 ' ' 的 'bob' 阻止编译器重命名它们

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

别理我 2024-09-12 12:25:11

如果它没有声明为您正在编译的代码的一部分,则外部声明是合适的:
http://code.google.com/closure/compiler/文档/api-tutorial3.html#externs

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文