简体   繁体   English

有没有工具删除javascript中未使用的方法?

[英]Is there a tool to remove unused methods in javascript?

I've got a collection of javascript files from a 3rd party, and I'd like to remove all the unused methods to get size down to a more reasonable level. 我收集了第三方的javascript文件集合,我想删除所有未使用的方法,以便将大小降低到更合理的水平。

Does anyone know of a tool that does this for Javascript? 有没有人知道为Javascript执行此操作的工具? At the very least give a list of unused/used methods, so I could do the manually trimming? 至少给出一个未使用/使用过的方法列表,这样我就可以进行手动修剪? This would be in addition to running something like the YUI Javascript compressor tool... 这将是运行类似YUI Javascript压缩器工具的补充...

Otherwise my thought is to write a perl script to attempt to help me do this. 否则我的想法是编写一个perl脚本来尝试帮助我这样做。

不可以。因为您可以像这样以疯狂的动态方式“使用”方法。

obj[prompt("Gimme a method name.")]();

Check out JSCoverage . 看看JSCoverage Generates code coverage statistics that show which lines of a program have been executed (and which have been missed). 生成代码覆盖率统计信息,显示程序的哪些行已执行(以及哪些行已被遗漏)。

I'd like to remove all the unused methods to get size down to a more reasonable level. 我想删除所有未使用的方法,以使大小降低到更合理的水平。

There are a couple of tools available: 有几种工具可用:

npm install -g fixmyjs
fixmyjs <filename or folder>

A configurable module that uses JSHint ( Github , docs ) to flag functions that are unused and perform clean up as well. 一个可配置模块,它使用JSHint( Githubdocs )来标记未使用的函数并执行清理。

I'm not sure that it removes undefined functions as opposed to flagging them. 我不确定它是否删除undefined函数而不是标记它们。 though it is a great tool for cleanup, it appears to lack compatibility with later versions of ECMAScript (more info below). 虽然它是一个很好的清理工具,但似乎缺乏与更高版本的ECMAScript的兼容性(更多信息如下)。

There is also the Google Closure Compiler which claims to remove dead JS but this is more of a build tool. 还有谷歌闭包编译器声称删除死JS,但这更像是一个构建工具。

Updated 更新

If you are using something like Babel, consider adding ESLint to your text editor, which can trigger a warning on unused methods and even variables and has a --fix CLI option for autofixing some errors and style issues. 如果您正在使用Babel之类的东西,请考虑将ESLint添加到文本编辑器中,这可能会触发对未使用的方法甚至变量的警告,并且具有--fix CLI选项,用于自动修复某些错误和样式问题。

I like ESLint because it contains multiple plugins for alternate libs (like React warnings if you're missing a prop), allowing you to catch bugs in advance. 我喜欢ESLint,因为它包含多个备用库的插件(如果你缺少一个道具,就像React警告一样),允许你提前捕获bug。 They have a solid ecosystem. 他们拥有坚实的生态系统。

As an example: on my NodeJS projects, the config I use is based off of the Airbnb Style Guide . 举个例子:在我的NodeJS项目中,我使用的配置是基于Airbnb风格指南

Unless the library author kept track of dependencies and provided a way to download the minimal code [eg MooTools Core download ], it will be hard to to identify 'unused' functions. 除非库作者跟踪依赖关系并提供了下载最小代码的方法[例如MooTools Core下载 ],否则很难识别“未使用”的功能。

The problem is that JS is a dynamic language and there are several ways to call a function. 问题是JS是一种动态语言,有几种方法可以调用函数。

Eg you may have a method like 例如,您可能有类似的方法

function test() 
{
   //
}

You can call it like 你可以这样称呼它

   test();

   var i = 10;
   var hello = i > 1 ? 'test' : 'xyz';

   window[hello]();

You'll have to write a perl script. 你必须编写一个perl脚本。 Take no notice of the nay-sayers above. 不要注意上面的那些说话者。

Such a tool could work with libraries that are designed to only make function calls explicitly. 这样的工具可以使用专门用于显式调用函数的库。 That means no delegates or pointers to functions would be allowed, the use of which in any case only results in unreadable "spaghetti code" and is not best practice. 这意味着不允许任何代表或指向函数的指针,在任何情况下使用它只会导致不可读的“意大利面条代码”并且不是最佳实践。 Even if it removes some of these hidden functions you'll discover most if not all of them in testing. 即使它删除了一些隐藏的功能,如果不是全部测试,你也会发现它们。 The ones you dont discover will be so infrequently used that they will not be worth your time fixing them. 你没有发现的那些很少被使用,以至于他们不值得你花时间修理它们。 Dont obsess with perfection. 不要痴迷于完美。 People go mad doing that. 人们为此疯狂。

So applying this one restriction to JavaScript (and libraries) will result in incredible reductions in page size and therefore load times, not to mention readability and maintainability. 因此,将这一限制应用于JavaScript(和库)将导致页面大小和加载时间的惊人减少,更不用说可读性和可维护性。 This is already the case for tools that remove unused CSS such as grunt_CSS and unCSS (see http://addyosmani.com/blog/removing-unused-css/ ) and which report typical reductions down to one tenth the original size. 对于删除未使用的CSS的工具(例如grunt_CSS和unCSS)(参见http://addyosmani.com/blog/removing-unused-css/ )并且报告典型的减少量低至原始大小的十分之一就已经是这种情况了。

Its a win/win situation. 这是一个双赢的局面。

Its noteworthy that all interpreters must address this issue of how to manage self modifying code. 值得注意的是,所有解释器都必须解决如何管理自修改代码的问题。 For the life of me I dont understand why people want to persist with unrestrained freedom. 对于我的生活,我不明白为什么人们想要坚持无拘无束的自由。 As noted by Triptych above Javascript functions can be called in ways that are literally "insane". 如上面的Triptych所述,Javascript函数可以用字面上“疯狂”的方式调用。 This insane fexibility corrupts the fundamental doctrine of separation of code and data, enables real-time code injection, and invalidates any attempt to maintain code integrity. 这种疯狂的灵活性破坏了代码和数据分离的基本原则,实现了实时代码注入,并使任何维护代码完整性的尝试无效。 The result is always unreadable code that is impossible to debug and the side effect to JavaScript - removing the ability to run automatic code pre-optimisation and validation - is much much worse than any possible benefit. 结果总是无法读取的代码无法调试,而JavaScript的副作用 - 无法运行自动代码预优化和验证 - 比任何可能的好处都要糟糕得多。

AND - you'd have to feel pretty insecure about your work to want to deliberately obsficate it from both your collegues and yourself. 并且 - 你必须对你的工作感到非常不安全,想要故意从你的同事和你自己那里搞砸它。 Browser clients that do work extremely well take the "less is more" approach and the best example I've seeen to date is Microsoft Office combination of Access Web Forms paired with SharePoint Access Servcies. 工作得非常好的浏览器客户端采用“少即是多”的方法,我见过的最好的例子是Microsoft Office组合的Access Web Forms与SharePoint Access Servcies配对。 The productivity of having a ubiquitous heavy tightly managed runtime interpreter client and its server side clone is absolutely phenomenal. 拥有无处不在的严格管理的运行时解释器客户端及其服务器端克隆的生产力绝对是惊人的。

The future of JavaScript self modifying code technologies therfore is bringing them back into line to respect the... JavaScript自我修改代码技术的未来使他们重新回到尊重......

KISS principle of code and data: Keep It Seperate, Stupid. KISS原理代码和数据:保持独立,愚蠢。

I know this is an old question by UglifyJS2 supports removing unused code which may be what you are looking for. 我知道这是一个老问题, UglifyJS2支持删除可能正在寻找的未使用的代码。

Also worth noting that eslint supports an option called no-unused-vars which actually does some basic handling of detecting if functions are being used or not. 另外值得注意的是,eslint支持一个名为no-unused-vars的选项,它实际上会对检测函数是否被使用进行一些基本处理。 It definitely detects it if you make the function anonymous and store it as a variable (but just be aware that as a variable the function declaration doesn't get hoisted immediately) 如果你使函数匿名并将其存储为变量,它肯定会检测到它(但要注意,作为变量,函数声明不会立即被提升)

In the context of detecting unused functions, while extreme, you can consider breaking up a majority of your functions into separate modules because there are packages and tools to help detect unused modules. 在检测未使用的功能的情况下,虽然极端,但您可以考虑将大部分功能分解为单独的模块,因为有一些软件包和工具可以帮助检测未使用的模块。 There is a little segment of sindreshorus's thoughts on tiny modules which might be relevant to that philosophy but that may be extreme for your use case. sindreshorus对微小模块一小部分想法可能与该哲学相关,但对于您的用例可能是极端的。

Following would help: 以下将有所帮助:

  1. If you have fully covered test cases, running Code Coverage tool like istanbul ( https://github.com/gotwarlost/istanbul ) or nyc ( https://github.com/istanbuljs/nyc ), would give a hint of untouched functions. 如果您已完全覆盖测试用例,运行代码覆盖率工具(如istanbul( https://github.com/gotwarlost/istanbul )或nyc( https://github.com/istanbuljs/nyc ))将提供一些未触及的功能。

  2. At least the above will help find the covered functions, that you may thought unused. 至少以上内容将有助于找到您可能认为未使用的涵盖功能。

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

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