xDocxDoc
AI
前端
后端
iOS
Android
Flutter
AI
前端
后端
iOS
Android
Flutter
  • 关于

    • Prettier
    • 构建并强制执行
    • Prettier vs. Linters
    • 选项设计原理
    • 设计理念
  • 使用

    • 安装指南
    • 忽略代码
    • precommit
    • 插件
    • CLI
    • API
    • Browser
    • CI
  • 配置 Prettier

    • options
    • Configuration File
    • 共享配置
  • Editors

    • 编辑器集成
    • WebStorm 设置
    • Vim 配置指南
    • 监视文件变更
  • Misc

    • 技术实现细节
    • 生态
    • 企业解决方案

precommit

你可以将 Prettier 与预提交工具结合使用。这能在提交前自动格式化通过 git add 标记为“已暂存”的文件。

方案一:https://github.com/okonet/lint-staged

适用场景:适合需要同时使用其他代码质量工具(如 ESLint、Stylelint 等)或需要支持部分暂存文件(git add --patch)的情况。

请确保 Prettier 已安装并位于项目的 https://docs.npmjs.com/specifying-dependencies-and-devdependencies-in-a-package-json-file 中

npx mrm@2 lint-staged

该命令将安装 https://github.com/typicode/husky 和 https://github.com/okonet/lint-staged,并在项目的 package.json 中添加配置,在预提交钩子中自动格式化支持的文件。

更多配置详见 https://github.com/okonet/lint-staged#configuration。

方案二:https://github.com/prettier/pretty-quick

适用场景:适合需要对变更/暂存文件进行完整格式化的场景。

搭配 https://github.com/toplenboren/simple-git-hooks 使用:

npm
npm install --save-dev simple-git-hooks pretty-quick
echo '{\n  "pre-commit": "npx pretty-quick --staged"\n}\n' > .simple-git-hooks.json
npx simple-git-hooks
yarn
yarn add --dev simple-git-hooks pretty-quick
echo '{\n  "pre-commit": "yarn pretty-quick --staged"\n}\n' > .simple-git-hooks.json
yarn simple-git-hooks
pnpm
pnpm add --save-dev simple-git-hooks pretty-quick
echo '{\n  "pre-commit": "pnpm pretty-quick --staged"\n}\n' > .simple-git-hooks.json
pnpm simple-git-hooks
bun
bun add --dev simple-git-hooks pretty-quick
echo '{\n  "pre-commit": "bun pretty-quick --staged"\n}\n' > .simple-git-hooks.json
bun simple-git-hooks

更多配置详见 https://github.com/prettier/pretty-quick。

方案三:https://github.com/alirezanet/Husky.Net

适用场景:适用于 .NET 项目,可搭配其他代码质量工具(如 dotnet-format、ESLint 等),支持多种文件状态(暂存/上次提交等)

dotnet new tool-manifest
dotnet tool install husky
dotnet husky install
dotnet husky add pre-commit

安装后在 task-runner.json 添加 Prettier 任务:

{
  "command": "npx",
  "args": ["prettier", "--ignore-unknown", "--write", "${staged}"],
  "pathMode": "absolute"
}

方案四:https://github.com/hallettj/git-format-staged

适用场景:专为处理部分暂存文件设计,提供独特解决方案。

该工具直接操作 Git 对象数据库,确保:

  1. 提交内容始终被格式化
  2. 未暂存变更永远不会被意外暂存
  3. 冲突时工作区文件保持原样
  4. 未暂存变更不会被格式化

需 Python 3+ 环境,搭配 https://github.com/typicode/husky 使用:

npm
npx husky init
npm install --save-dev git-format-staged
node --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
yarn
yarn husky init
yarn add --dev git-format-staged
node --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
pnpm
pnpm exec husky init
pnpm add --save-dev git-format-staged
node --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"
bun
bunx husky init
bun add --dev git-format-staged
bun --eval "fs.writeFileSync('.husky/pre-commit', 'git-format-staged -f \'prettier --ignore-unknown --stdin --stdin-filepath \"{}\"\' .\n')"

可通过调整文件扩展名适配项目,格式化过程始终遵循 .prettierignore 规则。

技术原理详见https://www.olioapps.com/blog/automatic-code-formatting/。

方案五:Shell 脚本

将以下脚本保存为 .git/hooks/pre-commit 并赋予执行权限:

#!/bin/sh
# 获取已暂存文件列表
FILES=$(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
[ -z "$FILES" ] && exit 0

# 格式化所有目标文件
echo "$FILES" | xargs ./node_modules/.bin/prettier --ignore-unknown --write

# 将格式化后的文件重新加入暂存区
echo "$FILES" | xargs git add

exit 0

若提交后文件仍显示修改,可添加后提交脚本来更新 Git 索引:

#!/bin/sh
# 更新 Git 索引
git update-index -g

保存为 .git/hooks/post-commit 并赋予执行权限。

总结

本文系统介绍了五种 Prettier 预提交钩子集成方案:

  1. lint-staged:多工具链集成首选,支持部分暂存文件
  2. pretty-quick:轻量级纯格式化方案,简洁易用
  3. Husky.Net:.NET 生态专属方案,支持混合工具链
  4. git-format-staged:原子化部分暂存处理,避免工作区污染
  5. Shell 脚本:底层定制方案,适合高阶用户

各方案均通过自动化流程确保:

  • 暂存文件在提交前必被格式化
  • 未暂存变更保持独立不受影响
  • 遵循项目级忽略规则(.prettierignore)
  • 无缝集成 Git 工作流

方案选择建议:

  • 新项目优先采用 lint-staged 或 pretty-quick
  • .NET 项目选择 Husky.Net
  • 复杂部分暂存场景使用 git-format-staged
  • 特殊需求考虑自定义 Shell 脚本
最后更新: 2025/8/26 10:07
Prev
忽略代码
Next
插件