如何根据自己的需求定制基于 jQuery 的 javascript 库?
我使用一个提供很多很多实用功能的库。它基于 jQuery,所以我也必须包含 jQuery。这两个库的大小都很大,但我只在单个函数中使用单个功能一次。
我将所有代码收集在一个 .js 文件中,并使用 jscoverage 测量了代码覆盖率。看起来只有 13% 的语句被执行了。这意味着超过 200KB 的死代码。
组合后的 .js 文件如下所示:
// jQuery code copy-pasted here
...
// libX code copy-pasted here
...
libX().doSomething();
所以我的问题是:
1) 如何从这个 .js 文件中删除所有死代码?我尝试过 Google Closure 但代码不起作用。我猜它搞乱了所有 jQuery 的东西。
2) 我将向第三方提供这个由 jQuery、另一个库和我自己的小代码组成的 .js 文件,所以我不希望 jQuery 参与其中,即使在无冲突模式下也是如此。我怎样才能根据自己的需要定制这个?也许用任意符号或其他符号替换美元符号?
任何帮助表示赞赏。
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
...
libX().doSomething();
So my questions are:
1) How can I remove all dead code from this one .js file? I've tried Google Closure 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?
Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jquery minified 很小(<20kb),您可以使用 cdn因此不必下载它(因此不会影响性能),并且您可能会在以后使用 jquery 的其余功能。
我不明白这一点:我将提供这个由 jQuery 组成的 .js 文件,...所以我不希望 jQuery 参与
并且 noconfilct 确实会用您的任意符号替换美元符号选择,这就是你想要的不是吗?
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 ?