admin管理员组

文章数量:1410682

I am trying to get Google Closure Compiler to work to pile my javascript code that uses Jquery but i keep getting variable $ is undeclared is there a way to get it to see the $ variable. Is there a way for closure Compiler to see the Jquery library but not pile it. here is my ant script

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

<taskdef name="jsp" classname=".google.javascript.jsp.ant.CompileTask"
       classpath="build/piler.jar"/>

<target name="pile">

<jsp pilationLevel="simple" warning="verbose" 
        debug="false" output="output/file.js">

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

</jsp>

</target>

</project>

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

I'm sure this is a easy question but I'm just missing something. Thanks in advance!

I am trying to get Google Closure Compiler to work to pile my javascript code that uses Jquery but i keep getting variable $ is undeclared is there a way to get it to see the $ variable. Is there a way for closure Compiler to see the Jquery library but not pile it. here is my ant script

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

<taskdef name="jsp" classname=".google.javascript.jsp.ant.CompileTask"
       classpath="build/piler.jar"/>

<target name="pile">

<jsp pilationLevel="simple" warning="verbose" 
        debug="false" output="output/file.js">

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

</jsp>

</target>

</project>

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

I'm sure this is a easy question but I'm just missing something. Thanks in advance!

Share Improve this question asked Aug 15, 2012 at 20:34 JustinJustin 1,2991 gold badge16 silver badges37 bronze badges 2
  • 2 Here is a similar question, and an article that mentions declaring jQuery as an extern – MrOBrian Commented Aug 15, 2012 at 20:42
  • Seems like your default externs arn't included – LOZ Commented Aug 15, 2012 at 20:43
Add a ment  | 

3 Answers 3

Reset to default 5

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

Once you've downloaded the appropriate extern, here's how you would reference it while piling:

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

<taskdef name="jsp" classname=".google.javascript.jsp.ant.CompileTask"
       classpath="build/piler.jar"/>

<target name="pile">

<jsp pilationLevel="simple" warning="verbose" 
    debug="false" output="output/file.js">

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

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

</target>

Seems like your default externs are not being included in your situation.

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

This haunted me for 3 hours, it took forever to find this one entry. I hope my answer helps someone.

This is the easy way:

java -jar c:\noninstprg\closure-piler.jar --pilation_level=ADVANCED_OPTIMIZATIONS --externs .\jquery-3.3.js .\your_source.js > ..\asset\js\your_source_mini.js

And the jquery-3.3.js above came from here

As of now, the latest jquery is 3.6 , but the 3.3 externs worked okay for me (I guess I don't use too much fancy new stuff), but beware.

本文标签: Can39t compile javascript using ant and closure compiler because of Jquery39sis undeclaredStack Overflow