三、Prettier 多语言支持

9 阅读16分钟

1. 概述

Prettier 内置支持前端开发中常见的语言和数据格式,包括 JavaScript/TypeScript、HTML/Vue/Angular、CSS/SCSS/Less、Markdown、JSON、YAML 和 GraphQL 等。

每种语言都有对应的解析器和打印器:解析器负责理解代码结构,打印器负责按配置输出格式化结果。Prettier 会根据文件扩展名自动选择解析器,无需手动配置。对于未内置支持的语言(如 PHP、Svelte),可通过插件扩展。

2. 各语言配置详解

2.1 JavaScript/TypeScript

JavaScript 和 TypeScript 是 Prettier 支持最完善的语言,拥有最丰富的配置选项。

可用解析器对比:

解析器底层实现特点
babel@babel/parser默认选择,支持最新 ECMAScript 提案
babel-flow@babel/parser + Flow支持 Flow 类型注解
babel-ts@babel/parser + TypeScript支持 TS,同时支持更多 JS 提案
typescripttypescript-estree官方 TS 解析器,更严格的类型检查
espreeespree(ESLint 默认)与 ESLint 保持一致
meriyahmeriyah高性能解析器
acornacorn轻量级解析器

JavaScript/TypeScript 专用选项:

选项类型默认值说明
semibooleantrue语句末尾添加分号
singleQuotebooleanfalse使用单引号
jsxSingleQuotebooleanfalseJSX 属性使用单引号
trailingComma"all" | "es5" | "none""all"尾随逗号策略
bracketSpacingbooleantrue对象字面量括号内加空格
arrowParens"always" | "avoid""always"箭头函数单参数是否加括号
quoteProps"as-needed" | "consistent" | "preserve""as-needed"对象属性引号策略

配置示例:

// prettier.config.js
/** @type {import("prettier").Config} */
export default {
  // ✅ 推荐的 JavaScript 项目配置
  semi: true, // 保留分号,避免 ASI 问题
  singleQuote: true, // 单引号更简洁
  trailingComma: "es5", // ES5 兼容模式
  arrowParens: "avoid", // 单参数省略括号:x => x * 2

  overrides: [
    {
      // TypeScript 文件使用官方解析器
      files: ["*.ts", "*.tsx"],
      options: {
        parser: "typescript",
      },
    },
  ],
};

babel vs typescript 解析器选择:

// ✅ 使用 babel-ts:需要最新 JS 提案支持
// 例如:装饰器、管道运算符等实验性语法
{
  "overrides": [
    {
      "files": "*.ts",
      "options": { "parser": "babel-ts" }
    }
  ]
}

// ✅ 使用 typescript:需要更严格的类型检查
// 推荐用于生产项目
{
  "overrides": [
    {
      "files": "*.ts",
      "options": { "parser": "typescript" }
    }
  ]
}

2.2 HTML/Vue/Angular

Prettier 对 HTML 及其衍生模板语言有专门的处理逻辑,主要关注空白字符敏感性问题。

HTML 相关解析器:

解析器用途文件扩展名
html标准 HTML.html, .htm
vueVue 单文件组件.vue
angularAngular 模板.component.html
lwcLightning Web Components.html

HTML 专用选项:

选项类型默认值说明
htmlWhitespaceSensitivity"css" | "strict" | "ignore""css"空白字符敏感度
bracketSameLinebooleanfalse多行元素 > 是否放最后一行
singleAttributePerLinebooleanfalse每行一个属性
vueIndentScriptAndStylebooleanfalseVue 文件缩进 script/style

htmlWhitespaceSensitivity 详解:

<!-- 源代码 -->
<div class="card"><span>Hello</span><span>World</span></div>

<!-- "css"(默认):按 CSS display 属性判断 -->
<!-- div 是 block 元素,可以自由换行;span 是 inline,保持紧贴 -->
<div class="card">
  <span>Hello</span><span>World</span>
</div>

<!-- "strict":所有元素都视为空白敏感 -->
<!-- div 也被当作 inline 处理,闭合标签紧贴 -->
<div class="card"
  ><span>Hello</span><span>World</span></div
>

<!-- "ignore":忽略空白敏感性 -->
<!-- 所有元素自由换行,可读性最好但可能影响渲染 -->
<div class="card">
  <span>Hello</span>
  <span>World</span>
</div>

Vue 单文件组件配置:

// prettier.config.js
export default {
  // Vue 文件配置
  vueIndentScriptAndStyle: false, // 不缩进 <script> 和 <style> 内容

  overrides: [
    {
      files: "*.vue",
      options: {
        htmlWhitespaceSensitivity: "ignore", // Vue 模板通常不关心空白
        singleAttributePerLine: true, // 每行一个属性,便于 diff
      },
    },
  ],
};

Vue 格式化示例:

<!-- ❌ 格式化前:缩进混乱、属性格式不一致 -->
<template>
<button class="btn primary"
    @click="handleClick" :disabled="loading"
      v-if="visible">Click me</button>
</template>

<!-- ✅ 格式化后:singleAttributePerLine: true -->
<template>
  <button
    class="btn primary"
    :disabled="loading"
    v-if="visible"
    @click="handleClick"
  >
    Click me
  </button>
</template>

Angular 模板配置:

{
  "overrides": [
    {
      "files": "*.component.html",
      "options": {
        "parser": "angular",
        "htmlWhitespaceSensitivity": "strict"
      }
    }
  ]
}

2.3 CSS/SCSS/Less

Prettier 对 CSS 及其预处理器语言提供统一的格式化支持。

CSS 相关解析器:

解析器用途文件扩展名
cssCSS.css
scssSCSS.scss
lessLess.less

CSS 格式化特点:

特点说明
属性顺序保留原始顺序,不自动排序
空行处理保留单个空行,移除多余空行
选择器格式多选择器自动换行
值格式颜色值统一格式,数值单位标准化

为什么 Prettier 不排序 CSS 属性?

Prettier 的设计原则是只处理代码格式,不改变代码的语义或结构。CSS 属性顺序在某些情况下会影响渲染结果(如 marginmargin-top 的覆盖关系),因此 Prettier 选择保留开发者的原始顺序。

如果需要自动排序 CSS 属性,可以使用 stylelint-order 插件配合 Stylelint 实现。

CSS 配置示例:

// prettier.config.js
export default {
  overrides: [
    {
      files: ["*.css", "*.scss", "*.less"],
      options: {
        singleQuote: false, // CSS 使用双引号
        printWidth: 80,
      },
    },
  ],
};

格式化效果:

/* ❌ 格式化前 */
.button{color:red;background:#fff;padding:10px 20px;}
.button:hover,.button:focus{color:blue;}

/* ✅ 格式化后 */
.button {
  color: red;
  background: #fff;
  padding: 10px 20px;
}
.button:hover,
.button:focus {
  color: blue;
}

SCSS 嵌套格式化:

// ❌ 格式化前:缩进混乱
.card {
    padding: 1rem;
  &__title {
      font-size: 1.5rem;
    font-weight: bold;}
    &__content {margin-top: 0.5rem;}
  @media (min-width: 768px) {
      padding: 2rem;}}

// ✅ 格式化后:统一缩进、规范空行
.card {
  padding: 1rem;

  &__title {
    font-size: 1.5rem;
    font-weight: bold;
  }

  &__content {
    margin-top: 0.5rem;
  }

  @media (min-width: 768px) {
    padding: 2rem;
  }
}

2.4 Markdown

Prettier 对 Markdown 的格式化主要关注一致性,同时尊重文档的可读性。

Markdown 相关解析器:

解析器用途文件扩展名
markdown标准 Markdown.md, .markdown
mdxMDX.mdx

Markdown 专用选项:

选项类型默认值说明
proseWrap"always" | "never" | "preserve""preserve"段落换行处理
tabWidthnumber2列表缩进宽度

proseWrap 详解:

<!-- 源文本(包含手动换行) -->

Prettier 是一个代码格式化工具,
支持多种编程语言。它通过解析代码
并按照统一的规则重新输出。

<!-- proseWrap: "preserve"(默认):保留原始换行 -->

Prettier 是一个代码格式化工具,
支持多种编程语言。它通过解析代码
并按照统一的规则重新输出。

<!-- proseWrap: "always":按 printWidth 重新换行 -->
<!-- 假设 printWidth: 40 -->

Prettier 是一个代码格式化工具,支持
多种编程语言。它通过解析代码并按照
统一的规则重新输出。

<!-- proseWrap: "never":合并为一行 -->

Prettier 是一个代码格式化工具,支持多种编程语言。它通过解析代码并按照统一的规则重新输出。

Markdown 配置建议:

{
  "overrides": [
    {
      "files": "*.md",
      "options": {
        "proseWrap": "preserve",
        "printWidth": 80,
        "tabWidth": 2
      }
    }
  ]
}

Markdown 格式化效果:

<!-- ❌ 格式化前:列表缩进不一致 -->

-  第一项
   - 子项 A
    - 子项 B
-   第二项

<!-- ✅ 格式化后:统一缩进 -->

- 第一项
  - 子项 A
  - 子项 B
- 第二项

嵌入代码块格式化:

// prettier.config.js
export default {
  // embeddedLanguageFormatting 控制嵌入代码的格式化
  embeddedLanguageFormatting: "auto", // 自动格式化代码块内的代码

  overrides: [
    {
      files: "*.md",
      options: {
        embeddedLanguageFormatting: "auto", // 格式化 ```js 代码块
      },
    },
  ],
};

2.5 JSON/YAML

数据格式文件的格式化主要关注一致的缩进和尾随逗号处理。

JSON 相关解析器:

解析器用途文件扩展名
json标准 JSON.json
json5JSON5(宽松语法).json5
jsoncJSON with Comments.jsonc, tsconfig.json

JSON 配置特点:

特点说明
尾随逗号标准 JSON 不支持,Prettier 自动移除
注释json 解析器不支持注释,使用 jsonc
引号始终使用双引号(JSON 规范要求)

JSON 配置示例:

{
  "overrides": [
    {
      "files": ["*.json", "package.json"],
      "options": {
        "parser": "json",
        "trailingComma": "none",
        "tabWidth": 2
      }
    },
    {
      "files": ["tsconfig.json", "jsconfig.json", ".vscode/*.json"],
      "options": {
        "parser": "jsonc",
        "trailingComma": "none"
      }
    }
  ]
}

YAML 格式化:

解析器用途文件扩展名
yamlYAML.yml, .yaml
# ❌ 格式化前:缩进混乱
services:
    web:
      image: nginx
      ports:
       - "80:80"

# ✅ 格式化后:统一缩进
services:
  web:
    image: nginx
    ports:
      - "80:80"

YAML 配置:

{
  "overrides": [
    {
      "files": ["*.yml", "*.yaml"],
      "options": {
        "tabWidth": 2,
        "proseWrap": "preserve"
      }
    }
  ]
}

2.6 GraphQL

Prettier 内置支持 GraphQL schema 和 query 的格式化。

解析器用途文件扩展名
graphqlGraphQL.graphql, .gql

GraphQL 格式化示例:

# ❌ 格式化前
query GetUser($id:ID!){user(id:$id){name email posts{title}}}

# ✅ 格式化后
query GetUser($id: ID!) {
  user(id: $id) {
    name
    email
    posts {
      title
    }
  }
}

GraphQL 配置:

{
  "overrides": [
    {
      "files": ["*.graphql", "*.gql"],
      "options": {
        "printWidth": 80,
        "tabWidth": 2
      }
    }
  ]
}

3. overrides 多语言配置

overrides 是 Prettier 实现多语言差异化配置的核心机制。本节聚焦多语言场景下的 overrides 用法,关于 overrides 的基础语法请参阅 二、Prettier 配置文件指南

3.1 按扩展名定制

基本语法:

{
  "overrides": [
    {
      "files": "*.ts",
      "options": {
        "parser": "typescript"
      }
    }
  ]
}

多扩展名匹配:

{
  "overrides": [
    {
      "files": "*.{js,jsx,ts,tsx}",
      "options": {
        "singleQuote": true,
        "semi": true
      }
    },
    {
      "files": ["*.json", "*.json5", "*.jsonc"],
      "options": {
        "trailingComma": "none"
      }
    }
  ]
}

为特殊文件指定解析器:

{
  "overrides": [
    {
      "files": ".prettierrc",
      "options": { "parser": "json" }
    },
    {
      "files": ".babelrc",
      "options": { "parser": "json" }
    },
    {
      "files": "*.svg",
      "options": { "parser": "html" }
    },
    {
      "files": ".env*",
      "options": { "parser": "sh" }
    }
  ]
}

3.2 按目录定制

按目录设置不同规则:

{
  "printWidth": 80,
  "overrides": [
    {
      "files": "src/client/**/*.{js,jsx,ts,tsx}",
      "options": {
        "printWidth": 100,
        "jsxSingleQuote": false
      }
    },
    {
      "files": "src/server/**/*.{js,ts}",
      "options": {
        "printWidth": 120
      }
    },
    {
      "files": "legacy/**/*",
      "options": {
        "tabWidth": 4,
        "printWidth": 120
      }
    }
  ]
}

Monorepo 项目配置:

// prettier.config.js
export default {
  printWidth: 100,
  singleQuote: true,

  overrides: [
    // 前端应用
    {
      files: "apps/web/**/*.{js,jsx,ts,tsx}",
      options: {
        printWidth: 80,
        singleAttributePerLine: true,
      },
    },
    // 文档站点
    {
      files: "apps/docs/**/*.{md,mdx}",
      options: {
        proseWrap: "always",
        printWidth: 80,
      },
    },
    // 共享 UI 组件库
    {
      files: "packages/ui/**/*.{js,jsx,ts,tsx}",
      options: {
        printWidth: 100,
        jsxSingleQuote: false,
      },
    },
    // 工具库
    {
      files: "packages/utils/**/*.ts",
      options: {
        printWidth: 120,
      },
    },
  ],
};

3.3 glob 模式技巧

常用 glob 模式:

模式匹配说明
*.js当前目录的 .js 文件
**/*.js所有目录的 .js 文件
src/**/*.jssrc 目录下所有 .js 文件
*.{js,ts}.js 和 .ts 文件
*.test.{js,ts}测试文件
!*.min.js排除压缩文件
[abc].jsa.js, b.js, c.js
?.js单字符文件名

复杂匹配示例:

{
  "overrides": [
    {
      "files": ["**/*.test.{js,ts}", "**/*.spec.{js,ts}", "**/__tests__/**/*"],
      "options": {
        "printWidth": 120
      }
    },
    {
      "files": ["*.config.{js,ts,mjs,cjs}", "*rc.{js,cjs}"],
      "options": {
        "printWidth": 100
      }
    },
    {
      "files": ["**/*.stories.{js,jsx,ts,tsx}", "**/*.story.{js,jsx,ts,tsx}"],
      "options": {
        "printWidth": 100,
        "singleAttributePerLine": true
      }
    }
  ]
}

3.4 多条件组合

数组形式匹配多种文件:

{
  "overrides": [
    {
      "files": ["*.html", "*.vue", "*.component.html"],
      "options": {
        "htmlWhitespaceSensitivity": "ignore",
        "printWidth": 120
      }
    }
  ]
}

组合目录和扩展名:

// prettier.config.js
export default {
  overrides: [
    {
      // API 目录的所有文件使用宽行
      files: ["src/api/**/*.ts", "src/services/**/*.ts"],
      options: {
        printWidth: 120,
      },
    },
    {
      // 组件目录使用严格格式
      files: ["src/components/**/*.{vue,jsx,tsx}"],
      options: {
        printWidth: 80,
        singleAttributePerLine: true,
      },
    },
    {
      // 测试和快照
      files: [
        "**/__tests__/**/*",
        "**/*.test.*",
        "**/*.spec.*",
        "**/__snapshots__/**/*",
      ],
      options: {
        printWidth: 120,
      },
    },
  ],
};

overrides 优先级规则:

规则说明
后定义优先数组中后面的 override 会覆盖前面的
精确匹配更精确的模式不会自动优先
完全覆盖override 中的 options 完全替换匹配的选项
{
  "singleQuote": false,
  "overrides": [
    {
      "files": "*.js",
      "options": { "singleQuote": true }
    },
    {
      "files": "legacy/*.js",
      "options": { "singleQuote": false }
    }
  ]
}

注意legacy/*.js 只会使用 singleQuote: false,不会继承第一个 override 的其他选项。

4. 解析器与打印器

Prettier 的多语言支持依赖于解析器(Parser)和打印器(Printer)的协作。本节介绍它们的工作原理。

4.1 工作流程概述

Prettier 格式化代码的流程分为两个阶段:

源代码 → 解析器(Parser) → AST → 打印器(Printer) → 格式化代码
阶段组件输入输出
解析解析器源代码AST
打印打印器AST + options格式化代码

每种语言都有对应的解析器和打印器。Prettier 内置了常用语言的支持,也可以通过插件扩展更多语言(见 第 5 节:插件扩展)。

提示:完整的解析器列表见 6.2 解析器综合对照表

4.2 解析器(Parser)

解析器负责将源代码解析为抽象语法树(AST)。不同语言需要不同的解析器来理解其语法结构。

解析器的作用:

职责说明
词法分析将源代码拆分为 token(标识符、运算符等)
语法分析根据语言规则构建 AST
错误检测报告语法错误

同一语言的多个解析器:

某些语言有多个可选的解析器,适用于不同场景:

语言解析器选项选择建议
JavaScriptbabel、espree、meriyah、acornbabel(默认,支持最新语法)
TypeScripttypescript、babel-tstypescript(更严格)
Flowbabel-flow唯一选项
// 示例:为 TypeScript 文件指定解析器
{
  "overrides": [
    {
      "files": "*.ts",
      "options": { "parser": "typescript" }  // 使用官方 TS 解析器
    }
  ]
}

4.3 打印器(Printer)

打印器负责将 AST 转换为格式化后的代码。它读取用户配置的 options,决定如何输出代码。

打印器的作用:

职责说明
读取 AST遍历解析器生成的语法树
应用 options根据配置选项决定格式化规则
生成代码输出符合规则的格式化代码

options 如何影响打印器输出:

不同语言的打印器读取不同的 options。这就是为什么某些选项只对特定语言生效:

打印器类型读取的 options示例效果
JS/TS 打印器semi、singleQuote、trailingComma、arrowParens分号、引号风格、尾逗号
HTML 打印器htmlWhitespaceSensitivity、bracketSameLine空白处理、标签闭合位置
CSS 打印器singleQuote引号风格
Markdown 打印器proseWrap、tabWidth段落换行、列表缩进

JavaScript 打印器示例:

// 配置
{ "semi": false, "singleQuote": true }

// 输入
const message = "Hello, World!";

// 输出(打印器根据 options 调整)
const message = 'Hello, World!'

HTML 打印器示例:

// 配置
{ "htmlWhitespaceSensitivity": "ignore", "singleAttributePerLine": true }
<!-- 输入 -->
<button class="btn" @click="handleClick" :disabled="loading">Click</button>

<!-- 输出(打印器根据 options 调整) -->
<button
  class="btn"
  :disabled="loading"
  @click="handleClick"
>
  Click
</button>

为什么不同语言有不同的专用选项?

每种语言的打印器只关心与该语言相关的格式化规则:

  • semi 只对 JS/TS 有意义(CSS、HTML 没有分号的概念)
  • htmlWhitespaceSensitivity 只对 HTML 类语言有意义
  • proseWrap 只对 Markdown 有意义

这种设计让每个打印器专注于自己语言的特点,配置选项也更加清晰。

4.4 解析器选择机制

Prettier 根据以下流程选择解析器:

         文件路径
            │
            ↓
┌─────────────────────────┐
│ 1. 检查 overrides 配置   │ ← 是否显式指定 parser
└───────────┬─────────────┘
            │ 否
            ↓
┌─────────────────────────┐
│ 2. 匹配文件扩展名          │ ← 内置扩展名映射表
└───────────┬─────────────┘
            │ 未匹配
            ↓
┌─────────────────────────┐
│ 3. 检查插件支持的扩展名     │ ← 插件提供的映射
└───────────┬─────────────┘
            │ 未匹配
            ↓
         跳过该文件

显式指定解析器的场景:

场景示例配置方式
无扩展名配置文件.prettierrc, .babelrc"parser": "json"
非标准扩展名.wxss, .wxml"parser": "css" / "parser": "html"
强制使用特定解析器.ts 用 babel-ts"parser": "babel-ts"

配置示例:

{
  "overrides": [
    {
      "files": [".prettierrc", ".babelrc", ".eslintrc"],
      "options": { "parser": "json" }
    },
    {
      "files": "*.wxss",
      "options": { "parser": "css" }
    },
    {
      "files": "*.wxml",
      "options": { "parser": "html" }
    }
  ]
}

CLI 指定解析器:

# 格式化并指定解析器
prettier --parser typescript --write "src/**/*.ts"

# 格式化标准输入
echo "const x=1" | prettier --parser babel

解析器选择建议:

场景推荐解析器原因
普通 JavaScript 项目babel支持最新语法
TypeScript 项目typescript更严格的类型检查
使用实验性语法的 TS 项目babel-ts支持更多 JS 提案
Flow 项目babel-flowFlow 类型支持
与 ESLint 集成espreeAST 一致性

5. 插件扩展

对于 Prettier 未内置支持的语言,可以通过插件扩展。本节介绍插件的工作机制和常用插件。

5.1 插件的作用

插件为 Prettier 提供三种扩展能力:

类型说明示例
新增语言支持提供新的解析器和打印器@prettier/plugin-php, prettier-plugin-svelte
修改现有行为在现有解析器基础上增强功能prettier-plugin-tailwindcss(类名排序)
新增配置选项提供额外的格式化选项prettier-plugin-organize-imports

插件结构:

每个插件需要提供解析器和打印器,使 Prettier 能够处理新的语言:

// 一个 Prettier 插件的基本结构
export const languages = [
  {
    name: "My Language",
    parsers: ["my-parser"],
    extensions: [".mylang"],
  },
];

export const parsers = {
  "my-parser": {
    parse: (text) => {
      /* 返回 AST */
    },
    astFormat: "my-ast",
  },
};

export const printers = {
  "my-ast": {
    print: (path, options, print) => {
      /* 返回格式化代码 */
    },
  },
};

5.2 插件加载机制

加载方式:

方式说明适用场景
自动发现Prettier 扫描 node_modules最简便,推荐
显式配置在 plugins 数组中指定需要控制加载顺序
本地文件指定文件路径自定义插件开发

自动发现机制(Prettier 3.x):

Prettier 会自动扫描 node_modules 中符合以下命名模式的包:

模式示例
prettier-plugin-*prettier-plugin-tailwindcss
@*/prettier-plugin-*@company/prettier-plugin-custom
@prettier/plugin-*@prettier/plugin-php

大多数情况下,只需安装插件即可使用,无需手动配置 plugins 数组。

显式配置示例:

// prettier.config.js
export default {
  plugins: [
    // npm 包名(从 node_modules 加载)
    "prettier-plugin-tailwindcss",
    "@prettier/plugin-php",

    // 本地插件文件
    "./plugins/my-custom-plugin.js",
  ],
};

注意:插件加载顺序可能影响格式化结果。例如 prettier-plugin-tailwindcss 需要在其他处理 HTML/JSX 的插件之后加载。

5.3 常用社区插件

官方维护的插件:

插件名用途安装命令
@prettier/plugin-phpPHPnpm i -D @prettier/plugin-php
@prettier/plugin-xmlXMLnpm i -D @prettier/plugin-xml
@prettier/plugin-rubyRubynpm i -D @prettier/plugin-ruby
@prettier/plugin-pugPugnpm i -D @prettier/plugin-pug

社区热门插件:

插件名用途安装命令
prettier-plugin-tailwindcssTailwind 类名排序npm i -D prettier-plugin-tailwindcss
prettier-plugin-svelteSveltenpm i -D prettier-plugin-svelte
prettier-plugin-astroAstronpm i -D prettier-plugin-astro
prettier-plugin-organize-imports导入语句排序npm i -D prettier-plugin-organize-imports
prettier-plugin-sqlSQLnpm i -D prettier-plugin-sql
prettier-plugin-shShell 脚本npm i -D prettier-plugin-sh
prettier-plugin-tomlTOMLnpm i -D prettier-plugin-toml
prettier-plugin-prismaPrisma Schemanpm i -D prettier-plugin-prisma

5.4 插件配置示例

Tailwind CSS 插件:

// prettier.config.js
export default {
  plugins: ["prettier-plugin-tailwindcss"],

  // Tailwind 插件选项
  tailwindConfig: "./tailwind.config.js",
  tailwindFunctions: ["clsx", "cn"], // 自定义类名函数
};

格式化效果:

// 格式化前:类名无序
<div className="p-4 flex mt-2 items-center bg-white justify-between">

// 格式化后:按 Tailwind 推荐顺序
<div className="flex items-center justify-between bg-white p-4 mt-2">

Svelte 插件:

// prettier.config.js
export default {
  plugins: ["prettier-plugin-svelte"],

  overrides: [
    {
      files: "*.svelte",
      options: {
        parser: "svelte",
        svelteSortOrder: "options-scripts-markup-styles",
        svelteIndentScriptAndStyle: true,
      },
    },
  ],
};

多插件配置:

// prettier.config.js
export default {
  printWidth: 100,
  singleQuote: true,

  // 加载多个插件(注意顺序)
  plugins: [
    "prettier-plugin-svelte",
    "prettier-plugin-tailwindcss", // Tailwind 需要在 Svelte 之后
    "@prettier/plugin-php",
  ],

  overrides: [
    {
      files: "*.svelte",
      options: { parser: "svelte" },
    },
    {
      files: "*.php",
      options: { phpVersion: "8.2" },
    },
  ],
};

验证插件是否生效:

# 检查特定文件使用的配置
npx prettier --find-config-path ./src/App.svelte

# 检查插件是否正确加载
npx prettier --check "**/*.svelte"

6. 总结与速查

6.1 核心要点回顾

要点说明
内置语言支持JS/TS/CSS/HTML/Vue/Markdown/JSON/YAML/GraphQL
解析器自动选择根据文件扩展名自动匹配,可通过 overrides 覆盖
打印器与 options每种语言的打印器读取对应的 options 控制格式化
overrides 机制按文件模式为不同文件设置不同配置
插件扩展通过插件支持 PHP/Ruby/Svelte/Astro 等更多语言
配置优先级CLI > overrides > 基础配置 > 默认值

6.2 解析器综合对照表

解析器语言/格式文件扩展名底层实现说明
babelJavaScript、JSX.js, .mjs, .cjs, .jsx@babel/parser默认 JS 解析器,支持最新 ECMAScript 提案
babel-flowFlow.js(需指定)@babel/parser支持 Flow 类型注解
babel-tsTypeScript.ts, .tsx(需指定)@babel/parserBabel 的 TS 支持,兼容更多 JS 提案
typescriptTypeScript.ts, .mts, .cts, .tsxtypescript-estree官方 TS 解析器,更严格
espreeJavaScript.js(需指定)espreeESLint 默认解析器,保持 AST 一致性
meriyahJavaScript.js(需指定)meriyah高性能解析器
acornJavaScript.js(需指定)acorn轻量级解析器
cssCSS.csspostcssCSS 解析
lessLess.lesspostcss-lessLess 预处理器
scssSCSS.scsspostcss-scssSCSS 预处理器
htmlHTML.html, .htmangular-html-parserHTML 解析
vueVue SFC.vuevue-eslint-parserVue 单文件组件
angularAngular 模板.component.htmlangular-html-parserAngular 模板
lwcLWC.html(需指定)angular-html-parserLightning Web Components
markdownMarkdown.md, .markdownremarkMarkdown 解析
mdxMDX.mdxremark + mdxMDX 格式
yamlYAML.yml, .yamlyamlYAML 解析
jsonJSON.json@babel/parser标准 JSON
json5JSON5.json5json5宽松 JSON 语法
jsoncJSON with Comments.jsonc, tsconfig.json@babel/parser带注释的 JSON
graphqlGraphQL.graphql, .gqlgraphql-jsGraphQL 解析
glimmerHandlebars.hbs, .handlebarsglimmer-engineHandlebars 模板

6.3 多语言配置速查表

语言解析器关键选项
JavaScriptbabelsemi, singleQuote, trailingComma
TypeScripttypescriptparser, semi, singleQuote
HTMLhtmlhtmlWhitespaceSensitivity
VuevuevueIndentScriptAndStyle
CSScsssingleQuote
MarkdownmarkdownproseWrap, embeddedLanguageFormatting
JSONjson/jsonctrailingComma: "none"
YAMLyamltabWidth
GraphQLgraphqlprintWidth

6.4 常见问题速查

问题解决方案
文件未被格式化检查 .prettierignore,确认解析器支持该扩展名
格式不符合预期使用 --find-config-path 检查配置加载
插件未生效确认插件已安装,检查 plugins 数组配置
不同文件需要不同配置使用 overrides 按文件模式设置
非标准扩展名文件在 overrides 中显式指定 parser
特殊文件(如 .prettierrc)在 overrides 中指定 parser: "json"

参考资源