Cannot compile javascript with ant and close compiler due to jQuery $ uneclared

I'm trying to get the Google Closure compiler to work to compile javascript code that uses jquery, but I keep getting the $ uneclared variable, there is a way to get it to see the $ variable. Is there a way to close the compiler to view the jQuery library, but not compile it. here is my ant script

<?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> 

My jquery library is called min.js and its in the src folder with js.js

I am sure this is a simple question, but I just missed something. Thanks in advance!

+4
source share
2 answers

You need to enable jQuery externs. Each major version of jQuery has its own external file. You can find them at http://code.google.com/p/closure-compiler/source/browse/#svn%2Ftrunk%2Fcontrib%2Fexterns

Once you download the corresponding extern, here is how you would reference it during compilation:

 <?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> 

+5
source

It seems that your source code is not included in your situation by default.

This link will give you a better understanding: https://developers.google.com/closure/compiler/docs/api-tutorial3#externs

0
source

All Articles