Closed xccjk closed 2 years ago
在现在的前端开发中,项目常常由多个人共同维护,但是由于每个人的代码风格、提交规范方面、编辑器设置等各方面的原因,导致提交上去的代码风格没有保持统一,常见的有下列几个问题:
git commit -m 'dddddd'
为了保证代码风格统一,便于后期的维护,我们需要使用检查工具来检查自己的代码,常见的几个检查场景:
代码已上传到githubgithub
在之前的业务场景下,平时配置一些代码检查规范,为了拥有对应的检查功能,需要分别安装很多三方包,新建配置文件来达到效果,并且想在其他项目中也配置相同的配置时,需要重复安装众多的包,而且还难以保证包版本一致,导致不同项目间可能存在差异
目的
eslint
npm install xcc-standard-eslint eslint-plugin-react eslint
prettier
npm install xcc-standard-prettier
stylelint
npm install xcc-standard-stylelint
commit
npm install xcc-commitlint husky lint-staged
changelog
npm install conventional-changelog-cli
官网解释:eslint为一个插件化的JavaScript工具,目标是保证代码的一致性和避免错误
.eslintrc.js
useTs
ignorePatterns
rules
// ts中常用配置方式 const { getEslint } = require('xcc-standard-eslint') module.exports = { ...getEslint({ ignorePatterns: ['.dll', 'build', '.temp'], useTs: true, // 需要覆盖默认的配置规则,在有些老项目中可能为了维持稳定,不改变之前的逻辑,就需要兼容之前的语法规则 rules: { 'comma-dangle': 'off', 'function-paren-newline': 'off', 'global-require': 'off', 'import/no-dynamic-require': 'off', 'no-inner-declarations': 'off', 'class-methods-use-this': 'off', 'import/extensions': 'off', 'import/prefer-default-export': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/no-var-requires': 'off', /** * 0: 禁止规则 * 1: 警告 * 2: 必须 */ // render不规范 'react/display-name': 'off', // 变量名不正确,比如 style_id '@typescript-eslint/camelcase': 'off', // 允许any '@typescript-eslint/no-explicit-any': 'off', 'react/prop-types': 'off', // 空函数 '@typescript-eslint/no-empty-function': 0, // 函数先后顺序 '@typescript-eslint/no-use-before-define': 'off', // return null '@typescript-eslint/ban-ts-ignore': 'off', 'require-atomic-updates': 'off', '@typescript-eslint/triple-slash-reference': 'off', // 未使用变量 '@typescript-eslint/no-unused-vars': 0, // 数组统一空格 [1, 2, 3, ...] 'array-bracket-spacing': 2, // prettier 中默认函数名不加空格,类似 function add() {},而eslint中默认为function add () {} 'space-before-function-paren': 0 } }) }
// 在js中的常用配置方式 const { getEslint } = require('xcc-standard-eslint') module.exports = { ...getEslint({ ignorePatterns: ['.dll', 'build', '.temp'], useTs: false, rules: { 'comma-dangle': 'off', 'function-paren-newline': 'off', 'global-require': 'off', 'import/no-dynamic-require': 'off', 'no-inner-declarations': 'off', // New rules 'class-methods-use-this': 'off', 'import/extensions': 'off', 'import/prefer-default-export': 'off', // render不规范 'react/display-name': 'off', 'react/prop-types': 'off', 'require-atomic-updates': 'off', // 数组统一空格 [1, 2, 3, ...] 'array-bracket-spacing': 2, // prettier 中默认函数名不加空格,类似 function add() {},而eslint中默认为function add () {} 'space-before-function-paren': 0 } }) }
.eslintignore
排除不需要执行检查的文件,一般只有src目录文件需要处理
dist build config scripts node_modules public .babelrc !.*.js publish.js server.js version.js tsconfig.json package.json
prettier是一个流行的代码格式化工具的名称,它能够解析代码,使用你自己设定的规则来重新打印出格式规范的代码
【prettier官网使用案例】
.prettierrc.js
const { getPrettier } = require('xcc-standard-prettier') module.exports = { ...getPrettier() }
.prettierignore
排除不需要进行代码美化的文件
.dll build/ .temp node_modules .vscode
stylelint是用来对css文件进行格式化及美化的,再css/less/scss中都可以使用
.stylelintrc.js
const { getStyleLint } = require('xcc-standard-stylelint') module.exports = { ...getStyleLint({ rules: { // 字体文件相关 'font-family-no-missing-generic-family-keyword': null, // 空的样式文件 'no-empty-source': null, // 计算属性 calc() 'function-calc-no-invalid': null } }) }
用来对commit阶段效验的,比如代码,样式,commit提交文案等不符合配置的规范时,都可以终止commit的提交
.commitlintrc.js
const { getCommitLint } = require('xcc-commitlint') module.exports = { ...getCommitLint() }
该文件用来定义项目的编码规范,编辑器的行为会与.editorconfig 文件中定义的一致,并且其优先级比编辑器自身的设置要高,这在多人合作开发项目时十分有用而且必要
.editorconfig.js
.editorconfig
root = true [*] charset = utf-8 end_of_line = lf indent_style = space indent_size = 2 trim_trailing_whitespace = true insert_final_newline = true
打开vscode配置文件setting.json
setting.json
mac系统下command+,,打开编辑器设置界面,输入框输入setting,打开下面的setting.json文件
command+,
{ "editor.formatOnSave": true }
.js .ts .jsx .tsx .less .css .scss .json
// setting.json部分相关配置 "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[javascript|react]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[typescript|react]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[less]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[css]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "prettier.semi": false, "prettier.singleQuote": true, "editor.formatOnType": true, // 设置默认使用本地配置文件规则 "prettier.configPath": ".prettierrc.js", "stylelint.config": ".stylelintrc.js", "[jsonc]": { "editor.defaultFormatter": "rvest.vs-code-prettier-eslint" }, "[markdown]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }
新增了如下命令👇
npm run lint
npm run lint-fix
npm run lint:style
npm run lint-fix:style
npm run changelog
npm run changelog:create
package.json script
"script": { ... "lint": "eslint . --ext .js,.jsx,.ts,.tsx --quiet", "lint-fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix", "lint:style": "stylelint 'src/**/*.less' --syntax less", "lint-fix:style": "npm run lint:style -- --fix", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0", "changelog:create": "npm run changelog -- -s -r 0" }
package.json commit的配置
"husky": { "hooks": { "pre-commit": "lint-staged", "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, "lint-staged": { "*.{js,jsx,ts,tsx}": [ "eslint --fix", "prettier --write", "git add" ] },
changelog文件
在安装了依赖包后,运行命令即可生成对应的changelog文件
安装对应依赖包以及新增相关配置文件和在package.json中添加script命令
会展示不符合规范(standrad)的文件列表
会自动进行格式化及风格美化,一些字段规范等需要手动修改
会展示css不对的文件列表
自动化格式样式文件,极少部分需要手动修改
生成changelog文件,写入更新记录
// 安装changelog包 npm install conventional-changelog-cli
如果你觉得这篇内容对你有帮助的话:
eslint官网 prettier editorconfig vscode配置文档
前言
在现在的前端开发中,项目常常由多个人共同维护,但是由于每个人的代码风格、提交规范方面、编辑器设置等各方面的原因,导致提交上去的代码风格没有保持统一,常见的有下列几个问题:
git commit -m 'dddddd'
等commit记录为了保证代码风格统一,便于后期的维护,我们需要使用检查工具来检查自己的代码,常见的几个检查场景:
代码已上传到githubgithub
项目背景
在之前的业务场景下,平时配置一些代码检查规范,为了拥有对应的检查功能,需要分别安装很多三方包,新建配置文件来达到效果,并且想在其他项目中也配置相同的配置时,需要重复安装众多的包,而且还难以保证包版本一致,导致不同项目间可能存在差异
打造统一规范代码插件
目的
使用方式
安装相关依赖
eslint
相关包prettier
相关包stylelint
相关包commit
相关包changelog
相关包eslint相关
官网解释:eslint为一个插件化的JavaScript工具,目标是保证代码的一致性和避免错误
eslint
相关包.eslintrc.js
,配置👇.eslintrc.js
useTs
是否为typescript编写,默认falseignorePatterns
需要排除的风格转换文件夹rules
自定义配置规则,可以用来覆盖默认规则.eslintignore
排除不需要执行检查的文件,一般只有src目录文件需要处理
prettier相关
prettier是一个流行的代码格式化工具的名称,它能够解析代码,使用你自己设定的规则来重新打印出格式规范的代码
prettier
相关包【prettier官网使用案例】
.prettierrc.js
,配置👇.prettierrc.js
.prettierignore
排除不需要进行代码美化的文件
stylelint相关
stylelint是用来对css文件进行格式化及美化的,再css/less/scss中都可以使用
stylelint
相关包.stylelintrc.js
,配置👇.stylelintrc.js
commit相关
用来对commit阶段效验的,比如代码,样式,commit提交文案等不符合配置的规范时,都可以终止commit的提交
commit
相关包.commitlintrc.js
,配置👇.commitlintrc.js
编辑器相关配置
该文件用来定义项目的编码规范,编辑器的行为会与.editorconfig 文件中定义的一致,并且其优先级比编辑器自身的设置要高,这在多人合作开发项目时十分有用而且必要
.editorconfig.js
,配置👇.editorconfig
vscode配置
eslint
,prettier
对应的插件打开vscode配置文件
setting.json
mac系统下
command+,
,打开编辑器设置界面,输入框输入setting,打开下面的setting.json文件.js .ts .jsx .tsx .less .css .scss .json
格式文件都采用prettier插件进行格式化package.json中相关配置
新增了如下命令👇
npm run lint
: 检查语法等格式,规范不对的js/ts文件npm run lint-fix
: 检查规范不对的文件并修复(语法问题需要手动修复)npm run lint:style
: 列出样式文件风格问题(lint:style中文件需要根据使用的是less或者scss来换)npm run lint-fix:style
: 格式化样式文件npm run changelog
: 列出每次提交的changelog记录npm run changelog:create
: 生成changelog文件package.json script
package.json commit的配置
changelog文件
在安装了依赖包后,运行命令即可生成对应的changelog文件
项目接入
老项目接入
安装对应依赖包以及新增相关配置文件和在package.json中添加script命令
npm run lint
会展示不符合规范(standrad)的文件列表
npm run lint-fix
会自动进行格式化及风格美化,一些字段规范等需要手动修改
npm run lint:style
会展示css不对的文件列表
npm run lint-fix:style
自动化格式样式文件,极少部分需要手动修改
npm run changelog:create
生成changelog文件,写入更新记录
感谢大家
如果你觉得这篇内容对你有帮助的话:
参考文档
eslint官网
prettier
editorconfig
vscode配置文档