Using Google Closure compiler [duplicate]

 ̄綄美尐妖づ 提交于 2019-11-30 15:03:15

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

Yes, if you use simple mode, instead.

Otherwise, no.

Matt Palmerlee

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/

Gabe

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!