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
3 Answers
Reset to default 5You 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.
版权声明:本文标题:Can't compile javascript using ant and closure compiler because of Jquery's $ is undeclared - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745049936a2639590.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论