You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Prettier Git Action中配置缩进为4个制表位及自定义规则?

为Prettier Git Action配置自定义规则

有两种方法可以修改Prettier的缩进等格式化规则,适配你的Git Action工作流:

方法一:直接在prettier_options中传递参数

在你的YAML配置里,直接给prettier_options追加缩进相关参数即可。比如要设置4个制表位宽度,可这样修改:

on:
  pull_request:
  push:
    branches:
      - master
      - validation

jobs:
  prettier:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          # Make sure the actual branch is checked out when running on pull requests
          ref: ${{ github.head_ref }}

      - name: Prettify code
        uses: creyD/prettier_action@v4.3
        with:
          prettier_options: --write **/*.{js,md} --tab-width 4 --use-tabs
  • --tab-width 4:指定制表位宽度为4
  • 若想用空格而非制表位,移除--use-tabs,改用--tab-width 4即可(可搭配--print-width 80调整换行宽度,按需选择)

方法二:添加项目级Prettier配置文件

在项目根目录创建Prettier配置文件(支持.prettierrc、.prettierrc.json、prettier.config.js等格式),Git Action会自动读取文件中的规则:

示例.prettierrc.json内容:

{
  "tabWidth": 4,
  "useTabs": true,
  "printWidth": 80,
  "singleQuote": true
}

这种方式更推荐,能统一本地开发和CI环境的格式化规则,避免两端格式化结果不一致。

注意:如果同时使用两种方式,prettier_options中的参数会覆盖配置文件里的对应规则。

内容的提问来源于stack exchange,提问作者EvyatarDot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 12:52:07