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.
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.
Share Improve this question asked Aug 24, 2010 at 8:16 Alec SmartAlec Smart 95.9k39 gold badges124 silver badges186 bronze badges 3- Good god, may I ask why? – jAndy Commented Aug 24, 2010 at 8:19
- 1 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. – Alec Smart Commented Aug 24, 2010 at 8:34
- 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. – Walter Rumsby Commented Aug 26, 2010 at 4:50
6 Answers
Reset to default 3You could use closure compiler:
- Java version
- Online version
- Documentation
It seems to do what you want.
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.
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.
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!
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: