Prettier + ESLint 自动格式化配置手册(AI写的,自用)
Prettier + ESLint 自动格式化配置手册
1. 安装依赖
1 2 3 4 5 6 7 8
| npm install --save-dev prettier eslint-config-prettier eslint-plugin-prettier
pnpm add -D prettier eslint-config-prettier eslint-plugin-prettier
yarn add -D prettier eslint-config-prettier eslint-plugin-prettier
|
2. Prettier 配置
2.1 创建 .prettierrc.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| export default { printWidth: 150, tabWidth: 2, useTabs: true, semi: true, singleQuote: true, quoteProps: 'as-needed', jsxSingleQuote: false, trailingComma: 'es5', bracketSpacing: true, bracketSameLine: false, arrowParens: 'always', rangeStart: 0, rangeEnd: Infinity, requirePragma: false, insertPragma: false, proseWrap: 'preserve', htmlWhitespaceSensitivity: 'css', vueIndentScriptAndStyle: false, endOfLine: 'lf', };
|
注意:如果 package.json 中有 "type": "module",必须使用 export default 而不是 module.exports。
2.2 创建 .prettierignore
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # Dependencies node_modules/ pnpm-lock.yaml yarn.lock package-lock.json
# Build outputs dist/ build/ out/
# IDE .vscode/ .idea/
# Config files tsconfig*.json *.config.js
# Other README.md *.md *.log
|
3. ESLint 集成配置
3.1 修改 eslint.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import js from '@eslint/js' import globals from 'globals' import reactHooks from 'eslint-plugin-react-hooks' import reactRefresh from 'eslint-plugin-react-refresh' import tseslint from 'typescript-eslint' import { defineConfig, globalIgnores } from 'eslint/config' import prettier from 'eslint-plugin-prettier' import prettierConfig from 'eslint-config-prettier'
export default defineConfig([ globalIgnores(['dist']), { files: ['**/*.{ts,tsx}'], extends: [ js.configs.recommended, tseslint.configs.recommended, reactHooks.configs['recommended-latest'], reactRefresh.configs.vite, prettierConfig, ], plugins: { prettier, }, rules: { 'prettier/prettier': 'error', }, languageOptions: { ecmaVersion: 2020, globals: globals.browser, }, }, ])
|
4. Package.json 脚本配置
4.1 添加脚本命令
1 2 3 4 5 6 7 8 9 10 11
| { "scripts": { "dev": "vite", "build": "tsc -b && vite build", "lint": "eslint .", "lint:fix": "eslint . --fix", "format": "prettier --write .", "format:check": "prettier --check .", "preview": "vite preview" } }
|
5. VS Code 配置
5.1 创建 .vscode/settings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "editor.formatOnSave": true, "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.codeActionsOnSave": { "source.fixAll.eslint": "explicit" }, "eslint.validate": [ "javascript", "javascriptreact", "typescript", "typescriptreact" ], "prettier.enable": true, "typescript.preferences.importModuleSpecifier": "relative" }
|
5.2 创建 .vscode/extensions.json
1 2 3 4 5 6
| { "recommendations": [ "esbenp.prettier-vscode", "dbaeumer.vscode-eslint" ] }
|
6. 工作流程
6.1 开发时
- 在 VS Code 中编辑代码
- 保存时自动格式化和修复 ESLint 问题
- 无需手动运行命令
6.2 提交前检查
1 2 3 4 5 6 7 8 9 10 11
| pnpm lint
pnpm lint:fix
pnpm format:check
pnpm format
|
6.3 CI/CD 集成
在 CI 流水线中添加:
1 2
| - run: pnpm lint - run: pnpm format:check
|
7. 常见问题解决
7.1 Prettier 不生效
- 检查
.prettierrc.js 语法是否正确
- 确认 VS Code 使用了正确的 formatter
- 检查文件是否在
.prettierignore 中
7.2 ESLint 和 Prettier 冲突
- 确保
eslint-config-prettier 在 extends 数组的最后
- 检查是否有其他 ESLint 规则覆盖了 Prettier 规则
7.3 保存时不自动格式化
- 确认 VS Code 设置中的
editor.formatOnSave 为 true
- 检查默认 formatter 设置为 Prettier
- 确认已安装推荐的扩展
8. 配置验证
完成配置后,运行以下命令验证:
1 2 3 4 5 6 7 8 9
| pnpm format:check
pnpm lint
pnpm lint:fix pnpm format
|
9. 配置说明
9.1 Prettier 配置项说明
printWidth: 150 - 每行最多150个字符
useTabs: true - 使用制表符缩进
singleQuote: true - 使用单引号
semi: true - 语句末尾添加分号
trailingComma: 'es5' - 多行时添加尾随逗号
9.2 集成优势
- 开发体验:保存时自动格式化,无需手动干预
- 代码质量:ESLint 检查代码质量问题
- 风格统一:Prettier 确保代码风格一致
- 团队协作:统一配置,避免风格争议
这个配置手册包含了完整的 Prettier + ESLint 自动格式化解决方案,可以确保代码风格一致性和质量。