0x1 問題
更新時間:2021-01-13 19:10:25
2021-01-13 19:10:25
stylelint --fix --allow-empty-input
增加--allow-empty-input
參數防止意外提交卡頓坎背。2020-11-13 14:30:02
增加
stylelint
css 代碼校驗医寿,增加 package.json 校驗命令
- 改變配置文件名符合共識
lint-staged
拆分楞抡,更加可讀uni-app
開發(fā)項目最好用npm
裝包灸拍!2020-09-22 13:10:40
- 增加
commit
之前用prettier
按照配置格式化一遍呢诬。防止 ?? 一樣的代碼蚕泽。2020-09-10 18:27:21
- 增加
eslint
和prettier
配置(對vscode
用戶友好)2020-08-28 21:41:15
- 去除
package.json
內lint-staged
的git add
任務惋啃。- 增加
.eslintignore
配置
公司需要用到 uni-app
開發(fā)新的項目歹苦,關于 uni-app
的介紹可以移步官網介紹。
核心就是用 Vue
寫多端應用程序谆棺,但是 uni-app
的官方編輯器 HbuilderX
對代碼校驗這一塊基本是空白栽燕。自己對Hbuilderx
配置也是有點模糊。
用 Vue-cli
創(chuàng)建的項目默認啟用了保存代碼檢查改淑,但是 uni-app
開發(fā)方式有點區(qū)別碍岔。編譯和打包集成到了工具內部。所以我們只能在提交代碼之前來做代碼校驗了朵夏。
關于為什么要做代碼校驗蔼啦,一個人開發(fā)還好。要是團隊合作仰猖,沒有統一的規(guī)則捏肢。你是天津狗不理,我是長沙臭豆腐饥侵。那個工程寫出來鸵赫,基本沒有維護性可言。要自己去維護別人寫的代碼就是一個態(tài)度躏升,他寫的代碼像 ?? 一樣辩棒,看不懂,只能重構膨疏。大多數人都是這個樣子一睁,沒有例外。作為一個程序員佃却,是要想想為什么那些大佬的代碼一看就能懂者吁,寫代碼就像寫詩,在看看自己寫的饲帅,過幾天出了問題复凳,等到親切問候了全家之后再來看是自己寫的瘤泪。。染坯。??
由此可見規(guī)范是很重要的均芽。
0202 年了, ECMAScript 都已經 2020 版本了单鹿,別再用
var
定義變量掀宋、用==
做if
做判斷了。
0x2 規(guī)范提交代碼
可以參考約定式提交
一種用于給提交信息增加人機可讀含義的規(guī)范仲锄。
簡單來說就是提交代碼的規(guī)范劲妙。
已經詳細記錄到個人的文檔網站,這里不做詳細介紹 地址
0x3 安裝需要用到的依賴
npm i eslint babel-eslint eslint-plugin-vue husky lint-staged prettier @vue/eslint-config-prettier eslint-plugin-prettier -D
- eslint - 校驗代碼的核心
- babel-eslint - babel 插件儒喊,用 babel 解析 js 文件
- eslint-plugin-vue - vue 官方的 eslint 插件
- husky - 可以讓 git hooks 的使用變得更簡單方便
- lint-staged - 可以在 git staged 階段的文件上執(zhí)行 linters镣奋,簡單點來說就是當我們運行
eslint
或stylelint
的命令時,只會檢查我們通過git add
添加到暫存區(qū)的文件怀愧,可以避免我們每次檢查都把整個項目的代碼都檢查一遍
stylelint
需要用到的依賴
npm i stylelint stylelint-config-prettier stylelint-config-standard stylelint-order -D
0x4 配置
以下設置適用于
cli
創(chuàng)建的項目侨颈,HbuilderX
創(chuàng)建的項目,路徑需要修改芯义。例如./src/
改成./
這樣哈垢。
prettier
${app}/prettier.config.js
module.exports = {
// 行寬 default:80
printWidth: 100,
// tab 寬度 default:2
tabWidth: 2,
// 使用 tab 鍵 default:false
useTabs: false,
// 語句行末是否添加分號 default:true
semi: false,
// 是否使用單引號 default:false
singleQuote: true,
// 對象需要引號在加 default:"as-needed"
quoteProps: 'as-needed',
// jsx單引號 default:false
jsxSingleQuote: true,
// 最后一個對象元素加逗號 default:"es5"
trailingComma: 'es5',
// 在對象字面量聲明所使用的的花括號后({)和前(})輸出空格 default:true
bracketSpacing: true,
// 將 > 多行 JSX 元素放在最后一行的末尾,而不是單獨放在下一行(不適用于自閉元素)扛拨。default:false
jsxBracketSameLine: false,
// (x) => {} 是否要有小括號 default:"always"
arrowParens: 'always',
// default:0
rangeStart: 0,
// default:Infinity
rangeEnd: Infinity,
// default:false
insertPragma: false,
// default:false
requirePragma: false,
// 不包裝 markdown text default:"preserve"
proseWrap: 'never',
// HTML空白敏感性 default:"css"
htmlWhitespaceSensitivity: 'strict',
// 在 *.vue 文件中 Script 和 Style 標簽內的代碼是否縮進 default:false
vueIndentScriptAndStyle: true,
// 末尾換行符 default:"lf"
endOfLine: 'auto',
// default:"auto"
embeddedLanguageFormatting: 'auto',
overrides: [
{
files: '*.md',
options: {
tabWidth: 2,
},
},
],
}
.prettierignore
# 忽略打包的文件
src/unpackage
# 忽略uni-app官方的組件庫錯誤和警告耘分,官方的竟然通不過...
src/components/uni-**
eslint
eslint 的配置復制于
PanJiaChen
大佬的項目,vue-admin-template绑警,關閉了一些實在是太變態(tài)的警告求泰。。计盒。(大部分是格式問題)
${app}/.eslintrc.js
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
// 配置js全局變量渴频,因為是uni-app,全局的uni是不需要引入的北启,還有5+的plus對象
globals: {
uni: 'readonly',
plus: 'readonly',
wx: 'readonly',
},
extends: ['plugin:vue/essential', 'eslint:recommended', '@vue/prettier'],
parserOptions: {
parser: 'babel-eslint',
},
rules: {
'no-console': [
'warn',
{
allow: ['warn', 'error'],
},
],
'no-eval': 'error',
'no-alert': 'error',
'vue/max-attributes-per-line': [
0,
{
singleline: 10,
multiline: {
max: 1,
allowFirstLine: false,
},
},
],
'vue/singleline-html-element-content-newline': 'off',
'vue/multiline-html-element-content-newline': 'off',
'vue/name-property-casing': ['error', 'PascalCase'],
'vue/no-v-html': 'off',
'accessor-pairs': 2,
'block-spacing': [2, 'always'],
'brace-style': [
2,
'1tbs',
{
allowSingleLine: true,
},
],
camelcase: [
0,
{
properties: 'always',
},
],
'comma-dangle': [2, 'only-multiline'],
'comma-style': [2, 'last'],
'constructor-super': 2,
curly: [2, 'multi-line'],
'dot-location': [2, 'property'],
'eol-last': 2,
eqeqeq: [
'warn',
'always',
{
null: 'ignore',
},
],
'generator-star-spacing': [
2,
{
before: true,
after: true,
},
],
'handle-callback-err': [2, '^(err|error)$'],
'jsx-quotes': [2, 'prefer-single'],
'new-cap': [
2,
{
newIsCap: true,
capIsNew: false,
},
],
'new-parens': 2,
'no-array-constructor': 2,
'no-caller': 2,
'no-class-assign': 2,
'no-cond-assign': 2,
'no-const-assign': 2,
'no-control-regex': 0,
'no-delete-var': 2,
'no-dupe-args': 2,
'no-dupe-class-members': 2,
'no-dupe-keys': 2,
'no-duplicate-case': 2,
'no-empty-character-class': 2,
'no-empty-pattern': 2,
'no-ex-assign': 2,
'no-extend-native': 2,
'no-extra-bind': 2,
'no-extra-boolean-cast': 2,
'no-extra-parens': [2, 'functions'],
'no-fallthrough': 2,
'no-floating-decimal': 2,
'no-func-assign': 2,
'no-implied-eval': 2,
'no-inner-declarations': [2, 'functions'],
'no-invalid-regexp': 2,
'no-irregular-whitespace': 2,
'no-iterator': 2,
'no-label-var': 2,
'no-labels': [
2,
{
allowLoop: false,
allowSwitch: false,
},
],
'no-lone-blocks': 2,
'no-mixed-spaces-and-tabs': 1,
'no-multi-spaces': 2,
'no-multi-str': 2,
'no-native-reassign': 2,
'no-negated-in-lhs': 2,
'no-new-object': 2,
'no-new-require': 2,
'no-new-symbol': 2,
'no-new-wrappers': 2,
'no-obj-calls': 2,
'no-octal': 2,
'no-octal-escape': 2,
'no-path-concat': 2,
'no-proto': 2,
'no-redeclare': 2,
'no-regex-spaces': 2,
'no-return-assign': [2, 'except-parens'],
'no-self-assign': 2,
'no-self-compare': 2,
'no-sequences': 2,
'no-shadow-restricted-names': 2,
'no-spaced-func': 2,
'no-sparse-arrays': 2,
'no-this-before-super': 2,
'no-throw-literal': 2,
'no-trailing-spaces': 0,
'no-undef': 2,
'no-undef-init': 2,
'no-unexpected-multiline': 2,
'no-unmodified-loop-condition': 2,
'no-unneeded-ternary': [
2,
{
defaultAssignment: false,
},
],
'no-unreachable': 2,
'no-unsafe-finally': 2,
'no-unused-vars': [
2,
{
vars: 'all',
args: 'none',
},
],
'no-useless-call': 2,
'no-useless-computed-key': 2,
'no-useless-constructor': 2,
'no-useless-escape': 0,
'no-whitespace-before-property': 2,
'no-with': 2,
'one-var': [
2,
{
initialized: 'never',
},
],
'operator-linebreak': [
2,
'after',
{
overrides: {
'?': 'before',
':': 'before',
},
},
],
'padded-blocks': [2, 'never'],
quotes: [
2,
'single',
{
avoidEscape: true,
allowTemplateLiterals: true,
},
],
'semi-spacing': [
2,
{
before: false,
after: true,
},
],
'space-in-parens': [2, 'never'],
'space-infix-ops': 1,
'space-unary-ops': [
2,
{
words: true,
nonwords: false,
},
],
'template-curly-spacing': [2, 'never'],
'use-isnan': 2,
'valid-typeof': 2,
'wrap-iife': [2, 'any'],
'yield-star-spacing': [2, 'both'],
yoda: [2, 'never'],
'prefer-const': 2,
'array-bracket-spacing': [2, 'never'],
'no-prototype-builtins': 0,
// 自定義開始
'vue/html-indent': 0,
'vue/html-closing-bracket-newline': 0,
'vue/html-self-closing': 0,
indent: 0,
semi: 0,
'comma-spacing': 0,
'space-before-blocks': 0,
'keyword-spacing': 0,
'key-spacing': 0,
'no-multiple-empty-lines': 0,
'spaced-comment': 0,
'space-before-function-paren': 0,
'arrow-spacing': 0,
'object-curly-spacing': 0,
},
}
.eslintignore
需要忽略打包的文件卜朗,像
.gitignore
一樣的寫法就行
# 忽略打包的文件
unpackage
# 忽略uni-app官方的組件庫錯誤和警告,官方的竟然通不過...
components/uni-**
stylelint
用來校驗
scss,less,styl,css,html,vue
的css
${app}/stylelint.config.js
module.exports = {
root: true,
plugins: ['stylelint-order'],
extends: ['stylelint-config-standard', 'stylelint-config-prettier'],
rules: {
'selector-pseudo-class-no-unknown': [
true,
{
ignorePseudoClasses: ['global'],
},
],
'at-rule-no-unknown': [
true,
{
ignoreAtRules: ['function', 'if', 'each', 'include', 'mixin', 'for'],
},
],
'no-duplicate-selectors': null,
'no-empty-source': null,
'unicode-bom': 'never',
'no-descending-specificity': null,
'font-family-no-missing-generic-family-keyword': null,
'declaration-colon-space-after': 'always-single-line',
'declaration-colon-space-before': 'never',
'declaration-block-trailing-semicolon': 'always',
'rule-empty-line-before': [
'always',
{
ignore: ['after-comment', 'first-nested'],
},
],
'property-no-unknown': [
true,
{
ignoreProperties: ['lines'],
},
],
'media-feature-name-no-unknown': [
true,
{
ignoreMediaFeatureNames: 'min-device-pixel-ratio',
},
],
'unit-no-unknown': [
true,
{
ignoreUnits: ['rpx'],
},
],
'selector-pseudo-element-no-unknown': [
true,
{
ignorePseudoElements: ['v-deep'],
},
],
// 指定聲明塊內屬性的字母順序
'order/properties-order': [
'position',
'top',
'right',
'bottom',
'left',
'z-index',
'display',
'float',
'width',
'height',
'max-width',
'max-height',
'min-width',
'min-height',
'padding',
'padding-top',
'padding-right',
'padding-bottom',
'padding-left',
'margin',
'margin-top',
'margin-right',
'margin-bottom',
'margin-left',
'margin-collapse',
'margin-top-collapse',
'margin-right-collapse',
'margin-bottom-collapse',
'margin-left-collapse',
'overflow',
'overflow-x',
'overflow-y',
'clip',
'clear',
'font',
'font-family',
'font-size',
'font-smoothing',
'osx-font-smoothing',
'font-style',
'font-weight',
'hyphens',
'src',
'line-height',
'letter-spacing',
'word-spacing',
'color',
'text-align',
'text-decoration',
'text-indent',
'text-overflow',
'text-rendering',
'text-size-adjust',
'text-shadow',
'text-transform',
'word-break',
'word-wrap',
'white-space',
'vertical-align',
'list-style',
'list-style-type',
'list-style-position',
'list-style-image',
'pointer-events',
'cursor',
'background',
'background-attachment',
'background-color',
'background-image',
'background-position',
'background-repeat',
'background-size',
'border',
'border-collapse',
'border-top',
'border-right',
'border-bottom',
'border-left',
'border-color',
'border-image',
'border-top-color',
'border-right-color',
'border-bottom-color',
'border-left-color',
'border-spacing',
'border-style',
'border-top-style',
'border-right-style',
'border-bottom-style',
'border-left-style',
'border-width',
'border-top-width',
'border-right-width',
'border-bottom-width',
'border-left-width',
'border-radius',
'border-top-right-radius',
'border-bottom-right-radius',
'border-bottom-left-radius',
'border-top-left-radius',
'border-radius-topright',
'border-radius-bottomright',
'border-radius-bottomleft',
'border-radius-topleft',
'content',
'quotes',
'outline',
'outline-offset',
'opacity',
'filter',
'visibility',
'size',
'zoom',
'transform',
'box-align',
'box-flex',
'box-orient',
'box-pack',
'box-shadow',
'box-sizing',
'table-layout',
'animation',
'animation-delay',
'animation-duration',
'animation-iteration-count',
'animation-name',
'animation-play-state',
'animation-timing-function',
'animation-fill-mode',
'transition',
'transition-delay',
'transition-duration',
'transition-property',
'transition-timing-function',
'background-clip',
'backface-visibility',
'resize',
'appearance',
'user-select',
'interpolation-mode',
'direction',
'marks',
'page',
'set-link-source',
'unicode-bidi',
'speak',
],
},
}
.stylelintignore
# 忽略打包的文件
src/unpackage
# 忽略uni-app官方的組件庫錯誤和警告暖庄,官方的竟然通不過...
src/components/uni-**
lint-staged
用來對你已經
git add
的文件進行校驗聊替,所以不需要指定路徑楼肪。
${app}/lint-staged.config.js
module.exports = {
'*.{js,jsx,ts,tsx}': ['eslint --fix', 'prettier --write'],
'{!(package)*.json,*.code-snippets,.!(browserslist)*rc}': ['prettier --write--parser json'],
'package.json': ['prettier --write'],
'*.vue': ['prettier --write', 'stylelint --fix --allow-empty-input'],
'*.{scss,less,styl,css,html}': ['stylelint --fix --allow-empty-input', 'prettier --write'],
'*.md': ['prettier --write'],
}
package.json
刪除了不必要的信息
${app}/package.json
{
"scripts": {
"lint:eslint": "eslint --fix --ext \"src/**/*.{vue,less,css,scss}\"",
"lint:prettier": "prettier --write --loglevel warn \"src/**/*.{js,json,tsx,css,less,scss,vue,html,md}\"",
"lint:stylelint": "stylelint --fix \"**/*.{vue,less,postcss,css,scss}\" --cache --cache-location node_modules/.cache/stylelint/",
"gc": "git add -A && git-cz && git pull && git push"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
0x5 使用
提交代碼
本地執(zhí)行
yarn gc
# 實際執(zhí)行培廓,這是個人自己定義的命令,因為經常需要推送和拉取代碼春叫,核心就是git cz替代git commit -m ''可以選擇本次提交的類型肩钠,很方便
git add -A && git cz && git pull && git push
一條命令搞定了代碼提交泣港、校驗、拉取价匠、推送当纱。當然遇到有沖突的情況還是需要自己手動解決。
代碼校驗 - lint:eslint
yarn lint:eslint
# 實際執(zhí)行
eslint --fix --ext \"src/**/*.{vue,less,css,scss}\"
代碼校驗 - lint:prettier
yarn lint:prettier
# 實際執(zhí)行
prettier --write --loglevel warn \"src/**/*.{js,json,tsx,css,less,scss,vue,html,md}\"
代碼校驗 - lint:stylelint
yarn lint:prettier
# 實際執(zhí)行
stylelint --fix \"**/*.{vue,less,postcss,css,scss}\" --cache --cache-location node_modules/.cache/stylelint/
0x6 總結
經過這么配置之后踩窖,代碼的質量和規(guī)范相對于沒有配置之前維護性大大提高坡氯,同時也可以避免一些錯誤的發(fā)生。但工具終究是工具洋腮,寫代碼的終究是人箫柳,在編寫代碼的同時,養(yǎng)成好的編碼習慣和優(yōu)化意識才是最重要的啥供。
在我眼中程序員可以不穿很時髦的衣服悯恍,但是一定要穿的干凈。多多少少有點潔癖伙狐、有點強迫癥涮毫、??
0x7 其他
參考文章:lint-staged 和 husky 在 pre-commit 階段做代碼檢查
配置參考:https://github.com/anncwb/vue-vben-admin
關于我
SunSeekerX,前端開發(fā)贷屎、Nodejs 開發(fā)罢防、小程序、uni-app
開發(fā)豫尽、等等
喜歡探討技術實現方案和細節(jié)篙梢,完美主義者,見不得 bug
美旧。
Github:https://github.com/SunSeekerX
個人博客:https://yoouu.cn/
個人在線筆記:https://sunseekerx.yoouu.cn/