Google Closure 中的外部 jQuery 编译了 JS?

发布于 2024-09-11 09:35:59 字数 728 浏览 1 评论 0原文

可能的重复:
使用 Google Closure 编译器编译的 jQuery

我的标记:

<script src="http://somecdn.com/jquery-1.4.2.min.js"></script>
<script src="/js/mycode.closure_compiled.js"></script>

我的代码:

goog.provide("mycode");

mycode.foo = function () {
    jQuery("#ajaxSpinner").show();
    jQuery.get("/ajax/foo", function () { /* ... */ });
}

我要编译我的代码使用 Google Closure 编译器进行了高级优化。

我如何实现以下目标?

  1. 编译器不应重命名“jQuery”和“jQuery.get”。
  2. 编译器不应抛出错误或警告(例如“未知类型'jQuery'”)。

Possible Duplicate:
jQuery compiled with Google Closure Compiler

My markup:

<script src="http://somecdn.com/jquery-1.4.2.min.js"></script>
<script src="/js/mycode.closure_compiled.js"></script>

My code:

goog.provide("mycode");

mycode.foo = function () {
    jQuery("#ajaxSpinner").show();
    jQuery.get("/ajax/foo", function () { /* ... */ });
}

I want to compile my code with advanced optimizations using the Google Closure Compiler.

How do I achieve the following?

  1. The compiler should not rename "jQuery" and "jQuery.get".
  2. The compiler should not throw Errors or Warnings (eg. "unknown type 'jQuery'").

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

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

发布评论

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

评论(2

多像笑话 2024-09-18 09:35:59

您必须使用外部。这些文件告诉编译器代码中的哪些符号来自外部代码,因此不应重命名。基本上,您必须使用 --externs 标志指定外部。一些第三方外部文件像 jQuery 一样,可以在项目源代码中找到。

You will have to use externs. these are files that tell the compiler which symbols in your code come from external code and therefor shouldn't be renamed. Basically you will have to specify an extern with the --externs flag. Some third-party extern files like jQuery are available at the project source code.

木槿暧夏七纪年 2024-09-18 09:35:59

jQuery 与高级模式下的闭包编译器不兼容。事实上,在流行的 JavaScript 库中,只有 Dojo Toolkit 是兼容的(请参见下面的链接)。

http://dojo-toolkit.33424 .n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

但是,如果您只想将 jQuery 与 Closure Compiler 高级模式一起使用而不重命名,我开发了一个技巧随着时间的推移:

  • 将您的库文件(即 jquery.js)作为“外部”提供给编译器

编译器不会重命名 jQuery 使用的任何内容。当然,您自己的代码应该始终与闭包兼容。此外,您的编译输出可能仍然无法正常工作,并且可能存在一些您需要解决的晦涩错误。

事实上,这就是我过去使用 Dojo 与 Closure Advanced 模式的方式,直到我最终厌倦了它并进行了必要的修改以兼容 Dojo。

jQuery is not compatible with the Closure Compiler in Advanced mode. In fact, out of the popular JavaScript libraries, only the Dojo Toolkit is compatible (see link below).

http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

However, if you just want to use jQuery with Closure Compiler Advanced mode without renaming, there is a trick that I've developed over time:

  • Provide your library file (i.e. jquery.js) as an "extern" to the compiler

The Compiler will not rename anything used by jQuery. Of course, your own code should always be Closure compatible. Also, your compiled output may still not work correct, and there may be a few obscure bugs you need to tackle.

In fact, this was how I used to use Dojo with Closure Advanced mode before I finally got sick of it and made the necessary modifications to get Dojo compatible.

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