在项目开发团队中,想要统一大家的代码风格,并且不影响同事原来的编码习惯、所使用的的开发工具,此时就需要通过脚本命令或自动格式化进行处理了。
安装 prettier
安装 prettier:
npm install -g prettier
创建配置文件
.prettierignore
/node_modules/**
/dist/
/dist*
/public/*
/docs/*
/vite.config.ts
/docs/**/*
.prettierrc.cjs
module.exports = {
printWidth: 100, // 每行代码长度(默认80)
tabWidth: 2, // 每个tab相当于多少个空格(默认2)ab进行缩进(默认false)
useTabs: false, // 是否使用tab
semi: false, // 声明结尾使用分号(默认true)
vueIndentScriptAndStyle: false,
singleQuote: true, // 使用单引号(默认false)
quoteProps: 'as-needed',
bracketSpacing: true, // 对象字面量的大括号间使用空格(默认true)
trailingComma: 'none', // 多行使用拖尾逗号(默认none)
jsxSingleQuote: false,
// 箭头函数参数括号 默认avoid 可选 avoid| always
// avoid 能省略括号的时候就省略 例如x => x
// always 总是有括号
arrowParens: 'always',
insertPragma: false,
requirePragma: false,
proseWrap: 'never',
htmlWhitespaceSensitivity: 'strict',
endOfLine: 'auto',
rangeStart: 0
}
package.json配置命令
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview",
...
"lint:format": "prettier --write --log-level warn \"src/**/*.{js,css,less,scss,vue,html,md}\""
}