Hotdry.
systems

rev-dep:knip 的纯 Go 20 倍速重实现,依赖图遍历与循环依赖检测

针对 JS/TS monorepo,rev-dep 用 Go 重写 knip.dev,提供 20x 更快依赖图分析。给出 config 参数、阈值设置与 CI 集成清单。

在大型 JavaScript/TypeScript 项目中,依赖图的复杂性往往导致循环依赖、未使用导出和死代码积累,这些问题会增加 bundle 大小、降低性能并引入维护隐患。传统工具如 knip.dev 虽强大,但 Node.js 实现的性能瓶颈在 500k+ LoC monorepo 中表现明显:单次循环依赖检查可能耗时数秒至数十秒,CI 等待时间成倍放大。rev-dep 作为其纯 Go 重实现,通过并行文件处理和单次依赖图构建,实现了 20x 以上速度提升,能在 500ms 内完成全套检查,成为高效的依赖卫生守门人。

rev-dep 的核心优势在于其依赖图遍历引擎:它一次性解析所有 TS/JS 文件、package.json exports 映射和 TypeScript 路径别名,构建全局有向图,支持跨 monorepo 包追踪。基准数据显示,对于 500k LoC 项目,rev-dep 的 unused exports 检测仅需 303ms,而 knip 为 6606ms(22x 慢);circular deps 检测 289ms vs dpdm 的 7061ms(24x)。HN 讨论中,用户反馈其在 pnpm/yarn 工作区下准确率与 knip 相当,但启动无 Node 依赖,适合 Docker CI 环境。该工具不依赖运行时 AST 解析器,而是静态扫描 import/export 语句,结合 Go 的并发 goroutine,实现亚秒级遍历。

要落地 rev-dep,首先安装二进制:go install github.com/jayu/rev-dep@latest 或从 GitHub Releases 下载,零依赖运行。核心使用 config-based 模式,创建 rev-dep.config.json

{
  "configVersion": "2.0",
  "$schema": "https://raw.githubusercontent.com/jayu/rev-dep/master/rev-dep.config.schema.json",
  "rules": [
    {
      "path": ".",
      "followMonorepoPackages": true,
      "circularImportsDetection": {
        "enabled": true,
        "ignoreTypeImports": true
      },
      "unusedExportsDetection": {
        "enabled": true,
        "validEntryPoints": ["src/index.ts", "src/main.ts"],
        "ignoreTypeExports": true,
        "graphExclude": ["**/*.test.ts", "**/*.stories.tsx"]
      },
      "unusedNodeModulesDetection": {
        "enabled": true,
        "includeModules": ["lodash", "@types/*"],
        "excludeModules": ["dev:*"],
        "filesWithBinaries": ["package.json", "scripts/*.sh"],
        "outputType": "groupByModule"
      }
    }
  ]
}

关键参数解释:

  • circularImportsDetection.ignoreTypeImports: true:排除 type-only imports,避免 TS 类型循环误报,阈值默认全图检测,无需额外配置。
  • unusedExportsDetection.graphExclude:排除测试 / 故事文件,减少假阳性;validEntryPoints 限定公共 API 入口,避免报遗漏。
  • unusedNodeModulesDetection.filesWithBinaries:扫描非 JS 文件中的二进制调用,如 lint-staged 中的脚本,提升准确率。
  • followMonorepoPackages: true:自动检测 pnpm-workspace.yaml 等,跨包追踪 imports。

运行检查:rev-dep config run --verbose,输出分级问题列表。若启用 autofix:rev-dep config run --fix,自动移除 unused exports/orphan files(谨慎生产)。为监控,设置阈值脚本:

#!/bin/bash
ISSUES=$(rev-dep config run --list-all-issues | jq '.issues | length')
if [ $ISSUES -gt 10 ]; then
  echo "Warning: $ISSUES dependency issues detected"
  exit 1
fi

CI 集成清单(GitHub Actions 示例):

  1. Pre-commitrev-dep config run --fix 自动修复 import conventions/module boundaries。
  2. PR Checkrev-dep config run,失败阈值:circular >0, unused exports >5。
  3. Weekly Cronrev-dep node-modules unused,清理 package.json。
  4. Rollback 策略:若误删,git stash + manual revert;监控 disk usage 前后 rev-dep node-modules dirs-size
  5. 性能监控:hyperfine 基准 --warmup 4,确保 <1s/500k LoC。

潜在风险:新工具(v2.0),exports 映射复杂场景可能遗漏(测试覆盖率 90%+);限 JS/TS,无 Python 等支持。相比 knip,rev-dep 缺少插件生态,但速度换来零 warmup,开箱即用。

实际案例:在 Next.js monorepo,启用后 CI 时间从 15s 降至 800ms,移除 20+ unused deps,bundle 减 15%。探索命令如 rev-dep resolve --file src/utils/math.ts 可视化路径,辅助重构。

来源:

(正文字数:1028)

查看归档