Remove unwanted jQuery functions
Asked Answered
S

6

11

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.

Strappado answered 24/8, 2010 at 8:16 Comment(3)
Good god, may I ask why?Predate
Yes, you may. We have software which runs on thousands of sites, and the jQuery code is added along with our code. But as we move forward we want to remove the unnecessary parts (as we barely use any of the core jQuery functions except a few). We have been loading jQuery using CDN, but our customers don't like it. They want to self-host everything. Don't ask me why as I don't know.Strappado
If you come up with a sensible caching strategy then you could serve the whole jQuery file locally, but cache it so that you won't have to worry about the cost of the download/additional HTTP request. Caching Tutorial for Web Authors and Webmasters is a great resource.Zendavesta
M
3

You could use closure compiler:

It seems to do what you want.

Mymya answered 26/8, 2010 at 4:21 Comment(1)
How would you use it? Copying all the JS code from JQuery and the other JS all together and removing unused functions?Ratter
P
3

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.

Predate answered 24/8, 2010 at 8:29 Comment(3)
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 :)Serrated
@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.Predate
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 :(Serrated
M
3

You could use closure compiler:

It seems to do what you want.

Mymya answered 26/8, 2010 at 4:21 Comment(1)
How would you use it? Copying all the JS code from JQuery and the other JS all together and removing unused functions?Ratter
A
1

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.

Averil answered 24/8, 2010 at 8:20 Comment(0)
M
1

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.

Mcclendon answered 24/8, 2010 at 8:38 Comment(1)
How would you use it? Copying all the JS code from JQuery and the other JS all together and removing unused functions?Ratter
S
0

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!

Serrated answered 24/8, 2010 at 8:24 Comment(0)
B
0

jQuery does offer a slim build, which excludes the ajax and effects modules: https://jquery.com/download/

As a side note, this page by jQuery gives you the ability to select exactly what you want to include in the package, but for the UI project. Thought it would be useful to indicate:

https://jqueryui.com/download/

Example: enter image description here

Brahmana answered 21/6, 2021 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.