views:

79

answers:

5

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.

+1  A: 

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.

Pekka
A: 

This would be a bad idea.

Firstly you may want to remove lets say InArray as you can use a base javascript alternative but other methods you keep may rely on InArray.

Basically jQueries methods use each-other to complete tasks, this is one of the ways they keep there package size down.

If you really want to do this I would do like so:

$M = MyjQuery = function(element,context)
{
    this.fn = {}
    this.extend = function(base,new)
    {
        //Extend them here
    }
}

And start from scratch!

RobertPitt
+2  A: 

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.

jAndy
Even tho there split into several files for the development stages does not meen that they are able to run as standalone, it just the jquery team organize there workers, for example, the data team would be working on `data.js` and so on, then they would have a bunch of testers who would test the new methods / functionality out. so yea its just a design process nothing more - nice reference though :)
RobertPitt
@RobertPitt: I don't know, it looks like a dependency tree to me. The order makes sense. So I really believe you could just remove the "Dimensions" for instance.
jAndy
dimentions may be possible as other entites such as `css` work with `elem.offsetWidth ` and such but removing sub-packages like css would then cause huge trouble as its used throughout the library! - not to mention the new libraries added, they would suffer aswell :(
RobertPitt
+1  A: 

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.

Anurag
A: 

You could use closure compiler

It seems to do what you want.

pixl coder