JavaScript/Prettier

来自康健生活

引言

npm install --save-dev --save-exact prettier

过滤文件

.prettierignore
# Ignore artifacts:
build
node_modules
.git
.idea

格式化

 
npx prettier --write .

CI

 
prettier --check .

ESLint

 
npm i --save-dev eslint-config- prettier

配置文件

# .prettierrc or .prettierrc.yaml
# TOML : .prettierrc.toml 
# CommonJS : prettier.config.js, .prettierrc.js, prettier.config.cjs, or .prettierrc.cjs
# ES : prettier.config.js, .prettierrc.js, prettier.config.mjs, or .prettierrc.mjs
# https://prettier.nodejs.cn/docs/en/configuration.html
trailingComma: "all" # <all|es5|none> 在对象或数组最后一个元素后面是否加逗号
tabWidth: 2 # 缩进长度
useTabs: false # 使用空格代替tab缩进
semi: true # 句末使用分号
singleQuote: true # 使用单引号
printWidth: 100 # 单行长度
quoteProps: 'consistent' # <as-needed|consistent|preserve 仅在必需时为对象的key添加引号
jsxSingleQuote: false # jsx中使用单引号
bracketSpacing: true # 对象前后添加空格 eg: { foo: bar }
bracketSameLine: true # 
jsxBracketSameLine: false # 多属性html标签的末尾 '>' 符号折行放置
arrowParens: 'avoid' # always 单参数箭头函数参数周围使用圆括号 eg: (x) => x。avoid:省略括号
requirePragma: false # 无需顶部注释即可格式化
proseWrap: 'preserve' # <always|never|preserve> 不做任何操作,保持原样
insertPragma: true # 在preitter格式化的文件插入指令
htmlWhitespaceSensitivity: 'ignore' # <css|strict|ignore> 对HTML全局空白不敏感
vueIndentScriptAndStyle: false # 不对vue中的script及style标签缩进
endOfLine: 'lf' # <lf|crlf|cr|auto> 结束行形式
embeddedLanguageFormatting: 'off' # <off|auto> 对引用代码进行格式化
disableLanguages: ['md'] # 不格式化文件格式
eslintIntegration: false # 不让prettier使用eslint的代码格式进行校验
tslintIntegration: false # 不让prettier使用tslint的代码格式进行校验
stylelintIntegration: false # 不让prettier使用stylelint的代码格式进行校验
ignorePath: 'ignorePath' # 忽略文件配置
requireConfig: false
singleAttributePerLine: true # 强制每行使用单个属性

vscode

yarn 4

yarn 4 使用 Prettier 异常。

/home/kangkk
yarn dlx @yarnpkg/sdks vscode

参考