2012-08-15 48 views
4

我试图让谷歌关闭编译合作,编译使用jQuery的我的JavaScript代码,但我不断收到变量$是未申报是有办法让它看到$变量。有没有办法让编译器查看Jquery库但不编译它。 这里是我的Ant脚本

<?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及其与js.js src文件夹

我敢肯定,这是一个简单的问题,但我只是缺少一些东西。提前致谢!

+2

[这里有一个类似的问题(HTTP:// stackoverflow.com/questions/9592534/google-closure-compiler-and-jquery)和[一篇文章](http://www.netmagazine.com/tutorials/shrink-javascript-files-google-closure-compiler)提及将jQuery声明为extern – MrOBrian 2012-08-15 20:42:45

+0

似乎不包含默认的外部函数 – KKP 2012-08-15 20:43:57

回答

5

您需要包括jQuery的实习医生。 jQuery的每个主要版本都有自己的extern文件。您可以在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> 

+0

这就是我所缺少的。 – Justin 2012-08-16 16:59:53

+0

我不清楚为什么extern是'简单'模式所必需的。谷歌自己的闭包编译器服务似乎让我在没有jQuery Extern的情况下进行编译。 – jedierikb 2013-12-18 19:52:06

+1

@jedierikb不幸的是,Closure编译器Web服务以针对演示进行了优化的模式运行,不建议用于生产编译。它假定所有未声明的符号都是使用'third_party'标志的外部符号。您也可以将'warning_level'选项设置为'QUIET'。 – 2013-12-18 22:25:11