Removing Unwanted jQuery Functions

Helo

A library like jQuery is fully loaded and contains many features that we cannot use in our scripts. I wonder if there is a way to say, read my script to find out all the jQuery functions that I use and its dependencies, and then remove the remaining functions from the jQuery library. This can be applied to almost any library and is not really a jQuery specific issue.

Tell me your thoughts on how this is possible. I know that this can become a headache later if I add a new function to my code and the function does not exist in jQuery. But I'm ready to take a chance.

+6
javascript jquery concept
source share
5 answers

You can use the closure compiler

He seems to be doing what you want.

+2
source share

Even if I don’t know why, you can do this:

Go to http://github.com/jquery/jquery/blob/master/Makefile

This is the makefile from jQuery lib. jQuery is split into several modules that are combined. Those base files ordered in dependencys, soooo you can clear modules that you don't use ...

I am not 100% sure if this works, I have never tried this on my own, but you can do it.

+2
source share

jQuery does not offer batch downloads like Prototype and MooTools, and creating them yourself will probably be difficult because you have to manually sort all the dependencies - and again and again for each new version of jQuery.

In addition, the current gzip size for 24kb is for the full library, I put it to you the size does not matter much. The library is loaded only once - if you download it from the CDN, it receives centralized caching, which makes it feasible even for slow modem connections.

+1
source share

If your JavaScript is not very dynamic in nature, you can give the Closure Compiler a shot.

Collect all your JavaScript in one place (including jQuery, plugins, other libraries, everything) and send it to gcc using the advanced compilation option.

This will remove all unused functions that could potentially violate your code. I would recommend this only if you have test cases, or your JS is small enough to fully verify manually.

A simple example of optimization performed by the compiler is:

 function hello(name) { alert('Hello, ' + name); } hello(); 

will be reduced to:

 alert("Hello, undefined"); 

since that’s all that happens mostly.

+1
source share

That would be a bad idea.

First, you can remove, say, InArray , since you can use the basic javascript alternative, but other methods you can use can rely on InArray.

Basically jQueries methods use each other to perform tasks, this is one way to reduce the size of the package.

If you really want this, I would do this:

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

And start from scratch!

0
source share

All Articles