开发者

Using Google Closure compiler [duplicate]

This question already has answers here: Closed 9 years ago.

Possible Duplicate:

jQuery compiled with Google Closure Compiler

I am using jQuery and I have all of my JS code in application.js file. W开发者_StackOverflowhen I compile "application.js" with the Google Closure compiler (using the advance options) I get a js file with no errors and warning. However, I am unable to use the file in my page, I get an error on page load which says "TypeError: Result of expression '$("div.tile").d' [undefined] is not a function."

My question is can I compile a js file which uses jQuery?


You can also use advanced mode if you specify that your js file is using jQuery by specifying an 'extern' file for jQuery. This way the closure compiler won't change your jQuery function calls inside your javascript.

java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js_output_file application.js --externs jquery-1.4.4.externs.js

You can find some of the jQuery extern files here: http://code.google.com/p/closure-compiler/source/browse/trunk/contrib/externs/


You have to tell the Closure compiler what not to optimize.

I do this with online compiler( http://closure-compiler.appspot.com/home ) by adding a externs_url paramater. When You type in your code on the online compiler it will automatically append a header similar to this, but without an externs_url param by default.

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// @externs_url https://closure-compiler.googlecode.com/git/contrib/externs/jquery-1.9.js
// ==/ClosureCompiler==

You can see what extern files are currently available at https://code.google.com/p/closure-compiler/source/browse/contrib/externs/ . They have most all versions of jQuery.

To do this with the downloadable Java version of the compiler you can just pass the --externs_url paramater on the cli or download the extern file you need and pass that filename with --externs paramater like in Palmerlee's answer.

If you are interested in why you can't just turn on advanced optimizations read through http://code.google.com/closure/compiler/docs/api-tutorial3.html


Yes, if you care to include the jQuery file in with your other file.

Yes, if you use simple mode, instead.

Otherwise, no.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜