Skip to content

Git Hooks 检查项

checks 字段支持以下检查项,可按场景任意组合。

检查项一览

检查项说明
line_count单文件行数检查(双阈值:警告/错误)
go_vetgo vet ./...
go_buildgo build ./...(无 entries 时)或 go build <entry> 逐入口编译
staticcheckstaticcheck ./...
lintoxlint / eslint / biome(自动检测)
buildvite / npm build(构建后扫描产物,拦截 Vite/Rolldown 将 Node.js 依赖静默外部化为 vite-browser-external 的运行时崩溃隐患)
formatoxfmt / prettier
vue-typesvue-tsc 类型检查
ts-typesTS 类型检查
inline_styles禁止 Vue style= / :style=
relative_imports禁止 ../ 跨级导入

组合示例

Go 后端

toml
checks = ["line_count", "go_vet", "go_build", "staticcheck"]

Vue 3 前端

toml
framework = "vue3"
check_order = ["line_count", "lint", "build", "format", "inline_styles_block", "vue-types", "error_handling"]

React 前端

toml
framework = "react"
check_order = ["line_count", "lint", "build", "format", "ts-types", "error_handling"]

行数检查逻辑

  • 文件行数 > warn_lines:输出警告,不阻断
  • 文件行数 > max_lines:输出错误,退出码非 0
  • 默认 warn_lines=250max_lines=300

工具自动检测

lintformat 等不指定具体工具,运行期按目录内配置自动选择 oxlint / eslint / biome / oxfmt / prettier。

构建安全检测(vite-browser-external)

build 检查在构建命令成功后,会扫描产物目录(dist / build / out)下的 *.js / *.mjs / *.cjs,检测是否含有 vite-browser-external 字符串。

背景:当某个 CJS 依赖缺失(例如只改了 package.json 的版本却未更新 pnpm-lock.yaml)时,Vite/Rolldown 不会对未解析的 require() 报错,而是将其静默外部化vite-browser-external 代理对象。构建命令会"成功"退出,但产物在浏览器运行时抛出 Cannot read properties of undefined,难以在构建阶段发现。

vite-browser-external 是打包器自身生成的固定标识符,与具体依赖无关,因此只需在产物中搜索此字符串即可覆盖所有外部化场景(无论被外部化的是 qsside-channel 还是其他任何 CJS 模块)。

命中后检查失败并阻断提交,同时给出修复建议:

bash
pnpm install --frozen-lockfile   # 同步锁文件与 package.json
pnpm ls --depth=99               # 检查缺失的依赖后重新安装

该检测对所有 build 路径生效:TOML 中 [build.modes] 指定的命令、build.command 以及内置的 vite build / npm run build 兜底路径,构建成功后均会执行。

go_build 入口配置

go_build 检查支持可选的 [[go_build.entries]] 子配置,用于声明多个可独立编译的入口(如 cmd/servercmd/client)。

  • 有 entries:逐个执行 go build <entry.path> 仅校验入口编译(不产出二进制)
  • 无 entries(公共库场景):自动回退到 go build ./... 编译全部包

公共库(无 main 包)无需配置 entries,go_build 会自动使用 go build ./...

toml
[go_build]
enabled = true

# 公共库无需以下 entries 配置,自动使用 go build ./...
# 仅多入口项目(如 cmd/server + cmd/client)需要声明:
# [[go_build.entries]]
# name = "server"
# path = "cmd/server"
# output = "server"

注意事项

  • inline_stylesrelative_imports 主要面向 Vue 项目,其他项目可省略。
  • 组合越多检查越严格,建议按模块类型选择最小必要集合。
  • go_build.entries 是可选配置,公共库无需声明。
  • go_nilaway 默认开启,工具未安装时自动跳过;可设为 enabled = false 关闭。

基于 MIT 协议开源