Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 471
John Smith Avatar asked May 13 '11 14:05

John Smith


1 Answers

First of all, for dead code removal you need the Advanced Mode. I see that you are already using it.

Then second, your code must be written to conform to the severe restrictions of using Advanced Mode. I suppose that you've done that and checked your code thoroughly -- otherwise the compiled code won't work.

Then you'll need to reference the jQuery "externs" file -- you can get that from the Closure Compiler's web site. Without this externs file, Closure will rename properties and functions in jQuery that it shouldn't.

Lastly, jQuery is not written to work with the Closure Compiler's Advanced Mode. There are many places that it creates "aliases" which cannot be optimized away. With even one alias anywhere within the code base, the entire jQuery object will be pulled in and everything underneath.

Short Answer: It is not possible without a lot of work to use the Closure Compiler's Advanced Mode with jQuery for dead code removal.

Off-Topic: The Dojo Toolkit is so-far the only popular JavaScript library (other than the Closure Library) that can be used with the Closure Compiler in Advanced Mode. All of the Closure Compiler's advanced features (e.g. dead code removal, virtualization of methods, namespace flattening etc.) are supported.

Check this link for a TODO document: http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

like image 161
Stephen Chung Avatar answered Sep 21 '22 16:09

Stephen Chung