6

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?

Community
  • 1
  • 1
iJK
  • 4,655
  • 12
  • 63
  • 95

3 Answers3

16

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/

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Matt Palmerlee
  • 2,668
  • 2
  • 27
  • 28
8

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

Tracker1
  • 19,103
  • 12
  • 80
  • 106
Gabe
  • 2,279
  • 1
  • 23
  • 22
  • How do you use the preamble with downloadable Java version of the compiler? I'm getting a lot of "ERROR - variable X is undeclared" and adding all of them as --externs is very cumbersome. – Gerold Meisinger Mar 23 '13 at 15:43
  • @lambdor I think you have to use the --externs thing for the normal downloadable version. You can also access the online version via their API which uses the preamble version https://developers.google.com/closure/compiler/docs/gettingstarted_api – Gabe Apr 01 '13 at 19:22
  • "To do this with the downloadable Java version of the compiler you can just pass the `--externs_url` parameter on the cli". Not true, unfortunately, "`"--externs_url" is not a valid option`" – kuporific Nov 05 '14 at 19:00
  • These links now appear to be on github https://github.com/google/closure-compiler/tree/master/contrib/externs – notzippy Dec 05 '14 at 19:06
1

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

Yes, if you use simple mode, instead.

Otherwise, no.

Matchu
  • 83,922
  • 18
  • 153
  • 160
  • 1
    This will not work for the above question because he wants to use advanced optimizations; you can't run the jQuery source through the advanced optimizer. You can use only use the simple compiler and specify a code_url paramater of jQuery, but this will include the entirety of jQuery in the output. – Gabe Sep 03 '11 at 05:45