简体   繁体   English

如何在 web 项目中找到未使用/死代码(javascript 中的 90% 代码)

[英]How to find unused/dead code in web projects (90% code in javascript)

I did find a very interesting tool for identify unused css definitions in a web project.我确实找到了一个非常有趣的工具来识别 web 项目中未使用的 css 定义。 http://www.sitepoint.com/dustmeselectors/ http://www.sitepoint.com/dustmeselectors/

Are there similar tools also for javascript projects?是否也有用于 javascript 项目的类似工具?

PS I know there is no program for deterministically finding unused code. PS 我知道没有程序可以确定地查找未使用的代码。 But I am looking for a report to identify possible unused code.但我正在寻找一份报告来确定可能未使用的代码。 Then the last decision will always be your own.那么最后的决定将永远是你自己的。

Problem is there is no way to be really sure.问题是没有办法真正确定。 Suppose the following:假设如下:

  1. The initial HTML site is practically empty.最初的 HTML 站点实际上是空的。 There is a lot of JS code though, which seems to be unused.虽然有很多JS代码,但似乎没有使用。
  2. OnLoad, a function is called which launches an AJAX query to the server. OnLoad,调用一个函数向服务器发起 AJAX 查询。 The server returns a lot of HTML code, which is the body of the site.服务器返回大量 HTML 代码,这些代码是站点的主体。 This body contains lots of JavaScript functions.该主体包含许多 JavaScript 函数。
  3. The initial body is replaced with the body received via AJAX.初始正文替换为通过 AJAX 接收的正文。 Suddenly, all code is used.突然间,所有的代码都用上了。

Static analysis utilities are therefore useless.因此,静态分析实用程序是无用的。 I do not know whether there exists a browser extension that marks all JS usage from a running browser though.我不知道是否存在一个浏览器扩展来标记正在运行的浏览器中的所有 JS 使用情况。

You can try using tombstones to safely locate and remove dead code from your JavaScript.您可以尝试使用墓碑从 JavaScript 中安全地定位和删除死代码。

https://blog.bugsnag.com/javascript-refactoring-with-bugsnag-and-tombstones/ https://blog.bugsnag.com/javascript-refactoring-with-bugsnag-and-tombstones/

In order to find the unused assets, to remove manually, you can use deadfile library: https://m-izadmehr.github.io/deadfile/为了找到未使用的资产,手动删除,您可以使用死文件库: https : //m-izadmehr.github.io/deadfile/

It can simply find unused files, in any JS project.它可以简单地在任何 JS 项目中找到未使用的文件。

Without any config, it supports ES6, JSX, and Vue files:无需任何配置,支持 ES6、JSX 和 Vue 文件: 在此处输入图片说明

The one that comes to mind most quickly is Javascript LINT ( http://www.javascriptlint.com /) and JSLint ( http://www.jslint.com/ ).最容易想到的是 Javascript LINT ( http://www.javascriptlint.com/ ) 和 JSLint ( http://www.jslint.com/ )。

Beware though: the latter hurts your feelings.不过要小心:后者会伤害你的感情。

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

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