Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted jQuery functions

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.

like image 581
Alec Smart Avatar asked Aug 24 '10 08:08

Alec Smart


4 Answers

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.

like image 188
jAndy Avatar answered Sep 18 '22 12:09

jAndy


You could use closure compiler:

  • Java version
  • Online version
  • Documentation

It seems to do what you want.

like image 37
pixl coder Avatar answered Sep 17 '22 12:09

pixl coder


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.

like image 43
Pekka Avatar answered Sep 19 '22 12:09

Pekka


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.

like image 35
Anurag Avatar answered Sep 18 '22 12:09

Anurag