【问题标题】:Is it possible to use Closure Compiler ADVANCED_OPTIMIZATIONS with jQuery?是否可以将 Closure Compiler ADVANCED_OPTIMIZATIONS 与 jQuery 一起使用?
【发布时间】:2010-10-25 18:03:50
【问题描述】:

我不断收到给定对象的函数(重命名)不存在的错误。是否有版本或设置或其他东西可以使它工作?

【问题讨论】:

  • 我发布了一个让 jQuery 成为完全闭包编译器兼容的请求 :) 这将允许删除所有未使用的 jQuery 并使编译的代码非常神秘:) bugs.jquery.com/ticket/7318

标签: javascript jquery compiler-construction google-closure google-closure-compiler


【解决方案1】:

在将 Closure Compiler Advanced Mode 与 jQuery 一起使用时,您必须为 jQuery 使用 externs 文件。 extern 文件会告诉编译器这些是保留方法并接受这些参数和参数数据类型应该是这样的,所以不要缩小方法名称并以这种方式检查参数数据类型。

您可以获取由 Google Developers here 创建的 externs 文件。该文件为 jQuery-1.4.3 生成。但目前可能适用于任何版本的 jQuery。新添加的方法可能不包含在该文件中,因此您应该将其添加到 externs 文件中,语法和同义词非常简单。

您还可以使用额外的编译器标志向编译器说明 externs 文件的位置。语法应该是这样的

--compiler_flags= "--externs=path/to/jquery.externs.js"

您也可以使用this tool,它是闭包库的在线外部提取器。

【讨论】:

    【解决方案2】:

    你必须declare jQuery as an extern to the compiler...但是我不确定是否有人制作了there was an extern file for 1.3.2,但我还没有看到任何 1.4+ 版本。

    编辑: this issue thread here has the community building a 1.4 version.

    【讨论】:

    【解决方案3】:

    jQuery 与高级模式下的 Closure Compiler 不兼容(目前)。我同意让它兼容是一件非常好的事情,因为它的方法链语法非常容易用于原型虚拟化,从而大大提高了执行速度。

    事实上,在流行的 JavaScript 库中(即 Closure 库除外),只有 Dojo Toolkit 兼容 Closure Advanced 模式。

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

    【讨论】:

    • 您可以将 jQuery 与具有高级模式的 Google Closure Compiler 一起使用。我在所有项目中都是这样工作的。您只需要一个为 jQuery 方法创建的 externs 文件。
    • 是的,但是您忽略了高级模式的许多最佳优势——几乎看起来像超级简单模式。对 jQuery 的适当重写应该甚至删除 jQuery 对象本身并去虚拟化每个方法。
    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多