Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Closure compiler [duplicate]

Possible Duplicate:
jQuery compiled with Google Closure Compiler

I am using jQuery and I have all of my JS code in application.js file. When 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?

like image 380
iJK Avatar asked Feb 09 '10 02:02

iJK


3 Answers

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/

like image 122
Matt Palmerlee Avatar answered Nov 07 '22 13:11

Matt Palmerlee


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

like image 33
3 revs, 2 users 94% Avatar answered Nov 07 '22 13:11

3 revs, 2 users 94%


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

Yes, if you use simple mode, instead.

Otherwise, no.

like image 27
Matchu Avatar answered Nov 07 '22 13:11

Matchu