【问题标题】:Can't compile javascript using ant and closure compiler because of Jquery's $ is undeclared由于 Jquery 的 $ 未声明,无法使用 ant 和闭包编译器编译 javascript
【发布时间】:2012-08-15 20:34:09
【问题描述】:

我正在尝试让 Google Closure Compiler 编译我的使用 Jquery 的 javascript 代码,但我不断收到变量 $ 未声明有没有办法让它看到 $ 变量。有没有办法让闭包编译器看到 Jquery 库但不编译它。 这是我的蚂蚁脚本

<?xml version="1.0"?>
<project basedir="." default="compile">

<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask"
       classpath="build/compiler.jar"/>

<target name="compile">

<jscomp compilationLevel="simple" warning="verbose" 
        debug="false" output="output/file.js">

  <sources dir="${basedir}/src">
    <file name="js.js"/><!-- the file I'm trying to compile -->
  </sources>

</jscomp>

</target>

</project>

我的 Jquery 库名为 min.js,它在 src 文件夹中带有 js.js

我确定这是一个简单的问题,但我只是遗漏了一些东西。提前致谢!

【问题讨论】:

标签: javascript jquery ant google-closure-compiler


【解决方案1】:

您需要包含 jQuery 外部。 jQuery 的每个主要版本都有自己的外部文件。你可以在http://code.google.com/p/closure-compiler/source/browse/#svn%2Ftrunk%2Fcontrib%2Fexterns找到他们

下载适当的 extern 后,您可以在编译时引用它:

<?xml version="1.0"?>
<project basedir="." default="compile">

<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask"
       classpath="build/compiler.jar"/>

<target name="compile">

<jscomp compilationLevel="simple" warning="verbose" 
    debug="false" output="output/file.js">

  <sources dir="${basedir}/src">
    <file name="js.js"/><!-- the file I'm trying to compile -->
  </sources>

  <externs dir="${basedir}/src">
    <file name="jquery-1.7.js"/>
  </externs>
</jscomp>

</target>

【讨论】:

  • 我不清楚为什么“简单”模式需要外部变量。谷歌自己的闭包编译器服务似乎让我在没有 jquery extern 的情况下进行编译。
  • @jedierikb 不幸的是,Closure-compiler Web 服务以针对演示优化的模式运行,不建议用于生产编译。通过使用third_party 标志,它假定所有未声明的符号都是外部的。您还可以将warning_level 选项设置为QUIET
【解决方案2】:

您的情况中似乎没有包含您的默认外部人员。

此链接将使您更好地理解: https://developers.google.com/closure/compiler/docs/api-tutorial3#externs

【讨论】:

  • 必须明确排除默认外部变量(默认情况下包含它们)。缺少的是必须手动包含的 jQuery externs。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
相关资源
最近更新 更多