Helo,
A library like jQuery is fully loaded and comes with many functions that we may not use in our scripts. Am wondering if there is a way to say read my script find out all the jQuery functions that I am using and its dependencies and then remove the remaining functions from the jQuery library. This could be applied to virtually any library and is not really a jQuery specific question.
Do let me know your thoughts on how this is achievable. I know it may be a headache later if say I add a new function to my code and the function does not exist in jQuery. But am willing to take that risk.
Even if I have no clue why, you could do this:
Go to http://github.com/jquery/jquery/blob/master/Makefile
That is the makefile from the jQuery lib. jQuery is splitted into several modules, which are put together. Those base files
are ordered in dependencys, soooo you might peel out modules you aren't using...
I'm not 100% sure if that works, never tried it on my own, but you can give that a shot.
You could use closure compiler:
It seems to do what you want.
jQuery does not offer packaged downloads like Prototype and MooTools do, and building them yourself will probably be hard, because you would have to sort out all the dependencies manually - and again and again for every new jQuery release.
Moreover, at currently 24kb gzipped size for the full library, I put it to you size doesn't really matter. The library gets loaded only once - if you load it from the CDN, it gets centrally cached, making it feasible even for slow modem connections.
If your JavaScript is not highly dynamic in nature, then you can give the Closure Compiler a shot.
Gather all your JavaScript in one place (including jQuery, plugins, other libraries, everything) and feed it to gcc using the advanced compilation option.
This will remove all unused functions which may potentially break your code. I would only recommend this if you either have test cases, or your JS is small enough to fully test manually.
A simple example of the kind of optimization the compiler does is:
function hello(name) {
alert('Hello, ' + name);
}
hello();
will get reduced to:
alert("Hello, undefined");
since that is all that is basically happening.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With