【工具】Prettier常见配置
Feb 25, 2024工具工程prettierPrettier常见配置
Prettier作为目前最主流的代码格式化工具,已经支持了各主流编辑器和前端框架。本文简单整理prettier配置项,以便后续使用查询。
prettier工具等使用不在本文介绍,可在官网中查询
本人常用配置
1 | // prettier.config.js |
配置文件
package.json文件中的"prettier"配置。- 以 JSON 或 YAML 编写的
.prettierrc文件、.prettierrc.json、.prettierrc.yml、.prettierrc.yaml或.prettierrc.json5文件。 - 使用
export default或module.exports导出对象的.prettierrc.js或prettier.config.js文件(取决于package.json中的type值)。 - 使用
export default导出对象的.prettierrc.mjs或prettier.config.mjs文件。 - 使用
module.exports导出对象的.prettierrc.cjs或prettier.config.cjs文件。
一个.prettierrc.toml文件。
个人相对推荐.prettierrc.js或prettier.config.js形式,比较灵活。
配置字段
1 | module.exports = { |
表格形式:
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| printWidth | number |
80 | 单行长度,超出将换行 |
| tabWidth | number |
2 | 缩进长度 |
| useTabs | boolean |
false | 使用空格或tab缩进 |
| semi | boolean |
true | 语句末尾添加分号 |
| singleQuote | boolean |
false | 使用单引号或双引号 |
| quoteProps | 'as-needed'/'consistent'/'preserve' |
‘as-needed’ | 对象属性添加引号规则 |
| jsxSingleQuote | boolean |
false | jsx中使用单引号或双引号 |
| trailingComma | 'none'/'es5'/'all' |
‘es5’ | 多行时末尾逗号 |
| bracketSpacing | boolean |
true | 对象括号间空格 |
| jsxBracketSameLine | boolean |
false | jsx标签闭合放行 |
| arrowParens | 'always'/'avoid' |
‘always’ | 箭头函数参数括号 |
| rangeStart | number |
0 | 格式化部分代码开始位置 |
| rangeEnd | number |
Infinity | 格式化部分代码结束位置 |
| parser | 'babel'/'flow'/'typescript'等 |
根据文件类型推断 | 指定解析器 |
| htmlWhitespaceSensitivity | 'css'/'strict'/'ignore' |
‘css’ | html全局空白敏感度 |
| endOfLine | 'lf'/'crlf'/'cr'/'auto' |
‘lf’ | 换行类型 |
| embeddedLanguageFormatting | 'auto' |
‘auto’ | 嵌入代码格式化 |
Author
My name is Micheal Wayne and this is my blog.
I am a front-end software engineer.
Contact: michealwayne@163.com