¿Cómo adaptar una biblioteca de JavaScript basada en jQuery a mis propias necesidades?

I use a library which provides lots and lots of utility functions. It's based on jQuery, so I have to include jQuery too. Both of these libraries are huge in size but I only use a single functionality in a single function only once.

I've gathered all the code in a single .js file and measured code coverage using jscoverage. It seems that only 13% of the statements are executed. This means more than 200KB's of dead code.

Here is what the combined.js file looks like:

// jQuery code copy-pasted here


// libX code copy-pasted here



Entonces mis preguntas son:

1) How can I remove all dead code from this one .js file? I've tried Cierre de Google but then the code doesn't work. I guess it messed up with all the jQuery stuff.

2) I will be serving this .js file composed of jQuery, another library and my own little code to 3rd parties so I don't want jQuery involved, even with noconflict mode. How can I tailor this to my own needs? Perhaps replace the dollar sign with an arbitrary symbol or something?

Cualquier ayuda apreciada.

preguntado el 31 de enero de 12 a las 16:01

jQuery is JavaScript so if Google Closure failed somehow, it had nothing to do with jQuery. -

Well Google Closure wouldn't even work or accept as input if jQuery was not Javascript but something else... Not saying it's jQuery's fault, but just that Closure compiler didn't work for me when trying to remove dead code... -

My only point was that there's nothing special or proprietary within the jQuery file... it's just written in pure JavaScript. -

1 Respuestas

jquery minified is small(<20kb), you can use a cdn so it won't have to be downloaded (and thus won't effect performance) and you'll probably use the rest of jquery's functionality at a later date.

I don't understand this : I will be serving this .js file composed of jQuery, ... so I don't want jQuery involved

And noconfilct does replace the dollar sign with an arbitrary symbol of your choosing, thats what you want isn't it ?

Respondido el 31 de enero de 12 a las 21:01

jquery-min might be small but the other library isn't. Furthermore; only 3-4 kb's of the 20kb is being used. So why should I serve 16+ kb's extra for each and every usage of my js file? It's expected to be requested 10k+ times a day. As for the noconflict mode, if some 3rd party has some other library on his page (YUI or Prototype or whatever), and decides to use my js file as well, it might mess up with things. - Murat Derya Özen

why ? becuase its probably cached in client pc if you use a cdn so it won't make a difference to performance, and you'll find a use for the rest of jquery a later date. The third party library strip out the dead code, minifiy it yourself, is it hosted on a cdn elsewhere. I don't understand what you mean regarding noconflict mode. - NimChimpsky

Thanks Nim but the only reason I have jQuery included is because libX is dependent on a small portion of it. I appreciate your help but please do not make any assumptions for my specific situation and contribute an answer for my questions only, rather than questioning them. - Murat Derya Özen

@Murat: Nim seems to be trying to actually understand your needs, yet you're harassing him for the effort. - Chris Farmer

@murat: your scolding attitude towards Nim is very unwelcome. Anyone in this community is free to question any OP in order to better understand and solve problems. We're not here to serve your whims. - Sparky

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.