简体   繁体   English

您如何只包含应用程序使用的jQuery部分

[英]How do you only include the parts of jQuery that are used by your app

For example, let's say I have a list of the jQuery object functions and its prototype functions: 例如,假设我有一个jQuery对象函数及其原型函数的列表:

/* jQuery object functions example
   jQuery.isArray
   jQuery.isFunction
   jQuery.proxy
   jQuery.ajax
*/

/* jQuery prototype functions example
   jQuery.fn.init
   jQuery.fn.val
   jQuery.fn.css
*/

This is a very small subset of jQuery and I may not necessarily want to include all of jQuery when I know I will only use these functions. 这是jQuery的很小一部分,当我知道我只会使用这些功能时,可能不一定要包括所有jQuery。 I could copy and paste from jQuery, but there are a lot of dependency chains between the jQuery functions and local functions and vars used within the jQuery closure, which are hard to grab all of by hand. 我可以从jQuery复制和粘贴,但是jQuery函数与jQuery闭包中使用的局部函数和var之间有很多依赖关系链,很难手动抓住所有依赖关系。 My list is also quite bigger than this, this was just used as a small example. 我的清单也比这大得多,这只是一个小例子。

My question is if there is a tool to do a static analysis of jQuery whereby you list the functions you want (like above) and get back the needed bits of code. 我的问题是是否有一种工具可以对jQuery进行静态分析,从而列出所需的功能(如上所示)并取回所需的代码位。

There is a mechanism for making your own custom build called jQuery Builder: http://projects.jga.me/jquery-builder/ . 有一种创建自己的自定义构建的机制称为jQuery Builder: http : //projects.jga.me/jquery-builder/

You really ought to first look at the minified version of jQuery as served by a popular CDN though because that will very likely be in the browser cache already and perhaps even have better performance than a smaller customized version that isn't yet cached. 您确实应该首先查看流行的CDN提供的jQuery的缩小版本,因为这很可能已经存在于浏览器缓存中,甚至可能比尚未缓存的较小的自定义版本具有更好的性能。 For reference, the minimized and gzipped version of jQuery 2.0 is only 29k, smaller than many images in a web page. 作为参考,jQuery 2.0的最小化和压缩版本仅为29k,小于网页中的许多图像。

I didn't know about the BYO jQuery tool suggested by jfriend00 so +1 for that. 我不知道jfriend00建议的BYO jQuery工具,所以为此+1。

Another alternative depending on what browsers/functionality you need to support may be using a jQuery replacement like zeptojs or minifiedjs . 这取决于你需要支持,可以使用一个jQuery更换喜欢什么浏览器/功能的另一个替代zeptojsminifiedjs These libraries use jQuery syntax and come in around 3-9k gzipped/minified. 这些库使用jQuery语法,压缩/缩小大小约为3-9k。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM