是什么,有什么用

​ jscpd 是一个开源的 js 工具库,用于检测项目的代码重复率,针对复制粘贴的代码尤其有效,支持超过 150 种的源码文件格式。从而进行代码抽离提炼,优化代码,缩小体积,从而增强代码的可维护性、可扩展性和可复用性方面等等。

如何使用

  • 安装
1
npm install jscpd -g

查看是否安装成功

1
jscpd -V
  • 在项目中使用

在 package.json 中增加 jscpd 配置属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"jscpd": {
"threshold": 1,
"reporters": [
"html",
"console"
],
"ignore": [
".git",
"node_modules",
"public",
".husky"
],
"format": ["javascript", "typescript"],
"absolute": true
}
  • 输出报告

项目中业务代码一般会在 src 目录下,所以直接检测该目录,-o report 设置输入报告为根目录下的 report 文件夹中。

1
jscpd ./src -o 'report'

如图所示

weappcode

通过 html 文件就可以查看所有的检测报告,可点击详情查看具体的代码。

weappcode