External jQuery in Google Closure compiled JS? [duplicate]
Possible Duplicate:
jQuery compiled with Google Closure Compiler
My markup:
<script src="http:开发者_开发知识库//somecdn.com/jquery-1.4.2.min.js"></script>
<script src="/js/mycode.closure_compiled.js"></script>
My code:
goog.provide("mycode");
mycode.foo = function () {
jQuery("#ajaxSpinner").show();
jQuery.get("/ajax/foo", function () { /* ... */ });
}
I want to compile my code with advanced optimizations using the Google Closure Compiler.
How do I achieve the following?
- The compiler should not rename "jQuery" and "jQuery.get".
- The compiler should not throw Errors or Warnings (eg. "unknown type 'jQuery'").
You will have to use externs. these are files that tell the compiler which symbols in your code come from external code and therefor shouldn't be renamed. Basically you will have to specify an extern with the --externs
flag. Some third-party extern files like jQuery are available at the project source code.
jQuery is not compatible with the Closure Compiler in Advanced mode. In fact, out of the popular JavaScript libraries, only the Dojo Toolkit is compatible (see link below).
http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t
However, if you just want to use jQuery with Closure Compiler Advanced mode without renaming, there is a trick that I've developed over time:
- Provide your library file (i.e. jquery.js) as an "extern" to the compiler
The Compiler will not rename anything used by jQuery. Of course, your own code should always be Closure compatible. Also, your compiled output may still not work correct, and there may be a few obscure bugs you need to tackle.
In fact, this was how I used to use Dojo with Closure Advanced mode before I finally got sick of it and made the necessary modifications to get Dojo compatible.
精彩评论