Prettier 是一个代码格式化工具,它通过解析代码并使用自己的规则重新打印它,强制执行一致的样式。它消除了原始样式,确保所有输出的代码符合一致的样式。
说实话,没有 Prettier 之前,代码评审最烦人的就是跟同事争论代码格式问题: “这个花括号要不要换行啊?” “缩进到底是用 tab 还是空格?” “这个逗号要不要加?” …
现在好了,有了 Prettier 这个"独裁者",它用固定的规则重新格式化你的代码,从此解放你的大脑,专注写逻辑就完事了。
它有这些让人爱不释手的特点:
无论是 Vue 还是 React 项目,首先装上 Prettier,这个必须的:
# npm党
npm install --save-dev prettier
# yarn党
yarn add --dev prettier
# pnpm党
pnpm add -D prettier
这些插件都挺好使:
npm i -D prettier prettier-plugin-tailwindcss @trivago/prettier-plugin-sort-imports @prettier/plugin-pug
File: prettier.config.cjs
/**
* @typedef {import('prettier').Config} PrettierConfig
* @typedef {import('prettier').PluginDescriptor} PrettierPluginDescriptor
*/
/**
* Vue项目的Prettier配置
* @type {PrettierConfig & { plugins: PrettierPluginDescriptor[] }}
*/
module.exports = {
/** 每行最大宽度 @default 80 */
printWidth: 120,
/** 缩进空格数 @default 2 */
tabWidth: 2,
/** 使用空格缩进 @default false */
useTabs: false,
/** Vue风格指南推荐不使用分号 @default true */
semi: false,
/** 使用单引号 @default false */
singleQuote: true,
/**
* 对象属性的引号使用
* @default "as-needed"
* "as-needed" - 需要时使用
* "consistent" - 有一个需要引号,则全部使用
* "preserve" - 保持原样
*/
quoteProps: 'as-needed',
/**
* 多行时尾逗号配置
* @default "all"
* "all" - 尽可能使用尾逗号
* "es5" - 在ES5中有效的地方使用
* "none" - 不使用
*/
trailingComma: 'none',
/** 对象字面量括号空格 @default true */
bracketSpacing: true,
/** 多行HTML标签的>放在最后一行末尾 @default false */
bracketSameLine: false,
/**
* 箭头函数参数括号
* @default "always"
* "avoid" - 可以省略括号时省略
* "always" - 总是使用括号
*/
arrowParens: 'avoid',
/** 换行符类型 @default "lf" */
endOfLine: 'lf',
/** HTML空格敏感度 @default "css" */
htmlWhitespaceSensitivity: 'ignore',
/** Vue文件中的script和style标签内容是否缩进 @default false */
vueIndentScriptAndStyle: true,
/** HTML标签属性换行 @default false */
singleAttributePerLine: false,
/** 插件 */
plugins: [
'prettier-plugin-tailwindcss', // Tailwind CSS支持
'@trivago/prettier-plugin-sort-imports', // import排序
'@prettier/plugin-pug' // 可选,如果使用Pug模板
]
};
node_modules
dist
.nuxt
auto-imports.d.ts
components.d.ts
npm i -D prettier prettier-plugin-organize-imports prettier-plugin-tailwindcss @trivago/prettier-plugin-sort-imports prettier-plugin-styled-components
File: prettier.config.cjs
/**
* @typedef {import('prettier').Config} PrettierConfig
* @typedef {import('prettier').PluginDescriptor} PrettierPluginDescriptor
*/
/**
* React项目的Prettier配置
* @type {PrettierConfig & { plugins: PrettierPluginDescriptor[] }}
*/
module.exports = {
/** 每行最大宽度 @default 80 */
printWidth: 120,
/** 缩进空格数 @default 2 */
tabWidth: 2,
/** 使用空格缩进 @default false */
useTabs: false,
/** 语句末尾使用分号 @default true */
semi: true,
/** 使用单引号 @default false */
singleQuote: true,
/** JSX中使用单引号 @default false */
jsxSingleQuote: true,
/**
* 对象属性的引号使用
* @default "as-needed"
* "as-needed" - 需要时使用
* "consistent" - 有一个需要引号,则全部使用
* "preserve" - 保持原样
*/
quoteProps: 'as-needed',
/**
* 多行时尾逗号配置
* @default "all"
* "all" - 尽可能使用尾逗号
* "es5" - 在ES5中有效的地方使用
* "none" - 不使用
*/
trailingComma: 'none',
/** 对象字面量括号空格 @default true */
bracketSpacing: true,
/** 多行JSX元素的>放在最后一行末尾 @default false */
bracketSameLine: false,
/**
* 箭头函数参数括号
* @default "always"
* "avoid" - 可以省略括号时省略
* "always" - 总是使用括号
*/
arrowParens: 'avoid',
/** 换行符类型 @default "lf" */
endOfLine: 'lf',
/** HTML空格敏感度 @default "css" */
htmlWhitespaceSensitivity: 'css',
/** HTML标签属性换行 @default false */
singleAttributePerLine: false,
/** 插件 */
plugins: [
'prettier-plugin-organize-imports', // 组织import语句
'prettier-plugin-tailwindcss', // Tailwind CSS支持
'@trivago/prettier-plugin-sort-imports', // import排序
]
};
node_modules
dist
build
coverage
.next
out
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"prettier.requireConfig": true,
"prettier.configPath": "prettier.config.cjs"
}
{
"scripts": {
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx,vue,css,less,scss,json,md}\"",
"format:check": "prettier --check \"src/**/*.{js,jsx,ts,tsx,vue,css,less,scss,json,md}\""
}
}
这个问题很多同学都遇到过,其实装两个依赖就能解决:
npm i -D eslint-config-prettier eslint-plugin-prettier
然后在 .eslintrc.js 加上这行配置就完事了:
module.exports = {
extends: [
// 其他配置...
'plugin:prettier/recommended'
]
};
创建 .prettierignore 文件,把不想格式化的文件写进去就行:
# 构建产物
dist
build
# 自动生成的文件
auto-imports.d.ts
components.d.ts
# 第三方库
node_modules
Prettier 会按以下顺序查找配置文件:
Prettier 确实是个好东西:
最后的建议:别花太多时间纠结配置,用默认的就挺好,真的。