Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

minify - Use closure compiler to remove unused parts of jQuery

Is it possible to use the closure compiler to remove unused parts of jQuery?

I have a script which only uses jQuery's networking (json) functions, and I'd like a minified script which removes everything else.

I've tried invoking it with:

    java -jar compiler.jar --compilation_level=ADVANCED_OPTIMIZATIONS --js=jquery-latest.js --js=my_script.js  --js_output_file=out.js

But I end up with a file no smaller than the regular minified jQuery source.

Edit: I should mention the reason behind this. This script is going to be included in 3rd party websites, and it requires a later version of jQuery (1.5 or 1.6). I thought the easiest way to handle this was to bundle the latest version inside the script (only available to my script, not touching window.jQuery), removing unused parts to reduce size. That way if they already had an older version of jQuery, it wouldn't interfere.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not possible to remove "unused" methods in jQuery because you can call methods in insane ways like:

<input id="test" value="hello!"/>

alert($('#test')[prompt('Method?')]()); // input "val" in the prompt box

The closure compiler can't possibly know what methods will be used or not.

Fiddle: http://jsfiddle.net/garreh/xDDXt/


As some side notes:

  • The latest production version of jQuery (1.6) is only 31kb. With proper caching control, this will be downloaded once and cached locally by the browser.
  • You'll probably be doing yourself a favour to optimize things that are generally much bigger in size i.e. images.
  • ... or reducing browser requests, such as using the CSS sprite technique to better optimize your website.
  • Place your jQuery code <script> tag at the bottom of the page, to achieve greater download parallelization. http://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_5/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...