手把手教你搭建規範的團隊vue項目,包含commitlint,eslint,prettier,husky,commitizen等等
1,前言
本文主要分享一個項目的規範約束從0到1的流程,從通過vue-cli創建項目,到團隊協作插件安裝(husky、eslint、commitlint、prettier等)。
- 本文vue-cli腳手架為5.x
- 本文webpack版本為5.x
- 本文vue版本為3.x
2,創建項目
如果你的vue-cli不是5.x版本,並且不知道怎麼創建vue-cli項目,請先查看該文章:傳送門
首先進入一個空間足夠的磁盤,比如樓主是進的L盤,輸入以下命令:
vue create demo
創建完畢後,項目結構如下圖:
此時可以打開package.json
,查看項目當前裝的依賴。默認是已經安裝了eslint
、babel
和vue
"dependencies": {
"core-js": "^3.8.3",
"vue": "^3.2.13"
},
"devDependencies": {
"@babel/core": "^7.12.16",
"@babel/eslint-parser": "^7.12.16",
"@vue/cli-plugin-babel": "~5.0.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-service": "~5.0.0",
"eslint": "^7.32.0",
"eslint-plugin-vue": "^8.0.3"
}
2,安裝vue全家桶
先安裝一些常用的vue生態,包括axios
,vue-router
,vuex
,qs
,element-plus
等,具體使用可看下方教程鏈接:
npm install axios vue-router vuex qs element-plus --save
再安裝typescript
npm install [email protected] --save -D
3,配置prettier
-
首先在根目錄創建
.prettierrc.js
文件,這個文件是項目的prettier
規則,內容如下:module.exports = { tabWidth: 2, // tab縮進大小,默認為2 useTabs: false, // 使用tab縮進,默認false semi: false, // 使用分號, 默認true singleQuote: true, // 使用單引號, 默認false(在jsx中配置無效, 默認都是雙引號) jsxBracketSameLine: false, // 在jsx中把'>' 是否單獨放一行 jsxSingleQuote: false, // 在jsx中使用單引號代替雙引號 proseWrap: 'preserve', // "always" - 當超出print width(上面有這個參數)時就折行 "never" - 不折行 "perserve" - 按照文件原樣折行 trailingComma: 'none', // 對象最後一項默認格式化會加逗號 arrowParens: 'avoid', // 箭頭函數參數括號 默認avoid 可選 avoid(能省略括號的時候就省略)| always(總是有括號) bracketSpacing: true, // 對象中的空格 默認true{ foo: bar } false:{foo: bar} printWidth: 100 // 一行多長,超過的會換行 }
-
然後在根目錄創建
.prettierignore
文件,這個是設置有那些文件需要忽略eslint
的檢查,內容如下:node_modules dist public .vscode
-
安裝
prettier
的擴展。eslint-plugin-prettier
,eslint-config-prettier
npm install eslint-config-prettier eslint-plugin-prettier --save -D
4,配置eslint
-
首先在根目錄創建
.eslintrc.js
,這個文件是項目的eslint
規則,內容如下:module.exports = { root: true, env: { browser: true, node: true, commonjs: true, es6: true, amd: true, }, globals: { // 允許的全局變量 TAny: true, TAnyType: true, TAnyArray: true, TAnyFunc: true, TDictArray: true, TDictObject: true }, extends: ['plugin:vue/vue3-essential', 'airbnb-base', '@vue/typescript/recommended'], // 擴展插件 parserOptions: { ecmaVersion: 2020, sourceType: 'module', parser: '@typescript-eslint/parser', ecmaFeatures: { tsx: true, // 允許解析TSX jsx: true, } }, settings: { 'import/resolver': { node: { extensions: ['.js', '.jsx', '.ts', '.tsx', 'vue'] } } }, plugins: ['prettier'], rules: { // 0表示不不處理,1表示警告,2表示錯誤並退出 'vue/multi-word-component-names': 'off', // 要求組件名稱始終為多字 '@typescript-eslint/no-unused-vars': [ 'error', { varsIgnorePattern: '.*', args: 'none' } ], camelcase: 1, // 駝峰命名 'prettier/prettier': 0, // 會優先採用prettierrc.json的配置,不符合規則會提示錯誤 'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off', 'comma-dangle': 'off', 'import/prefer-default-export': 'off', // 優先export default導出 'no-param-reassign': 'off', // 函數參數屬性的賦值 semi: 'off', '@typescript-eslint/no-explicit-any': 'warn', 'no-unused-expressions': 'off', // 聯式調用使用? 'import/no-cycle': 'off', // 導入循環引用報錯 'arrow-parens': 'off', // 箭頭函數一個參數可以不要括號 'no-underscore-dangle': 'off', // 無下劃線 'no-plusplus': 'off', // 使用一元運算符 'object-curly-newline': 'off', 'no-restricted-syntax': 'off', // 使用for of 'operator-linebreak': 'off', // after 'arrow-body-style': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', // ts每個函數都要顯式聲明返回值 // 暫時屏蔽檢測@別名 'import/no-useless-path-segments': 'off', 'import/no-unresolved': 'off', 'import/extensions': 'off', 'import/no-absolute-path': 'off', 'import/no-extraneous-dependencies': 'off', 'newline-per-chained-call': ['error', { ignoreChainWithDepth: 10 }], 'linebreak-style': [0, 'error', 'windows'], 'no-shadow': 'off', // 注意你必須禁用基本規則,因為它可以報告不正確的錯誤 '@typescript-eslint/no-shadow': ['error'], '@typescript-eslint/member-delimiter-style': [ 'error', { multiline: { delimiter: 'none', requireLast: true, }, singleline: { delimiter: 'semi', requireLast: false, }, }, ], 'keyword-spacing': [ 2, { before: true, after: true, }, ] } }
-
然後在根目錄創建
.eslintignore
文件,這個是設置那些文件需要忽略eslint
的檢查,內容如下:node_modules dist .vscode
-
安裝
eslint
的擴展。eslint-config-airbnb-base
,@typescript-eslint/eslint-plugin
,@vue/eslint-config-typescript
,@typescript-eslint/parser
,@vue/eslint-config-typescript
,eslint-plugin-import
。npm install eslint-config-airbnb-base @typescript-eslint/eslint-plugin @vue/eslint-config-typescript @typescript-eslint/parser @vue/eslint-config-typescript eslint-plugin-import --save -D
到這一步,就可以在package.json
文件里的scripts
對象里添加一行自動修復文件的命令 lint-fix
:
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint-fix": "vue-cli-service lint --fix ./src --ext .vue,.js,.ts"
}
在項目根目錄打開終端,輸入npm run lint-fix
,會按照你的eslint
要求自動進行修復,部分修復不了的需要手動修改。
5,配置husky + git鉤子
husky
是一個讓配置git
鉤子變得更簡單的工具,支持所有的git
鉤子。它可以將git
內置的鉤子暴露出來,很方便地進行鉤子命令注入,而不需要在.git/hooks
目錄下自己寫shell
腳本了。您可以使用它來lint
您的提交消息、運行測試、lint
代碼等。當你commit
或push
的時候。husky
觸發所有git
鉤子腳本。
-
安裝
husky
npm install husky --save -D
-
啟用
husky
,啟用後,根目錄會出現一個.husky
的文件夾npx husky install
-
編輯
package.json
文件,在scripts
中添加"prepare": "husky install"
命令"scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint-fix": "vue-cli-service lint --fix ./src --ext .vue,.js,.ts", "prepare": "husky install" },
-
在
.husky
文件夾中,新建pre-commit
文件,寫入以下代碼:#!/bin/sh . "$(dirname "$0")/_/husky.sh" npx lint-staged --allow-empty
-
安裝
lint-staged
,這是本地暫存代碼的檢查工具,當你git
提交代碼時,會自動檢查是否符合項目eslint
和prettier
規範npm install lint-staged --save -D
-
在項目根目錄創建
.lintstagedrc.json
文件,寫入以下代碼:{ "*.{ts,js,vue,tsx,jsx}": ["npm run lint-fix", "prettier --write"] }
到這一步,git
提交的時候,會自動根據項目eslint
和prettier
規範修復代碼並提交,如果碰到修復不了的,會取消提交。
6,配置commitlint
在git
提交時,如果能找按照規範寫好提交信息,能提高可讀性以及項目維護效率,方便回溯。這裡我們使用commitlint
規範git commit
提交的信息。
6.1,配置commitlint格式檢查
-
首先安裝
@commitlint/cli
和@commitlint/config-conventional
(如果要自定義提交規範,就不用安裝@commitlint/config-conventional
)npm install @commitlint/cli @commitlint/config-conventional --save -D
-
在項目根目錄的
.husky
文件夾中,新建commit-msg
文件,寫入以下內容:#!/bin/sh . "$(dirname "$0")/_/husky.sh" npx commitlint --edit $1
-
在項目根目錄新建
commitlint.config.js
文件,寫入以下內容:/* "off"或者0:關閉規則 "warn"或1:開啟規則拋出警告 "error"或2:開啟規則拋出錯誤 */ module.exports = { extends: ['@commitlint/config-conventional'], rules: { 'body-leading-blank': [2, 'always'], // body上面有換行 'footer-leading-blank': [1, 'always'], // footer上面有換行 'header-max-length': [2, 'always', 108], // header上最大108字符 'type-case': [0], 'type-empty': [0], 'scope-empty': [0], 'scope-case': [0], 'subject-full-stop': [0, 'never'], 'subject-case': [0, 'never'], 'type-enum': [ 2, 'always', [ 'feat', // 新增功能、頁面 'fix', // 修補bug 'docs', // 修改文檔、注釋 'style', // 格式:不影響代碼運行的變動、空格、格式化等等 'ui', // ui修改:布局、css樣式等等 'hotfix', // 修複線上緊急bug 'build', // 改變構建流程,新增依賴庫、工具等(例如:修改webpack) 'refactor', // 代碼重構,未新增任何功能和修復任何bug 'revert', // 回滾到上一個版本 'perf', // 優化:提升性能、用戶體驗等 'ci', // 對CI/CD配置文件和腳本的更改 'chore', // 其他不修改src或測試文件的更改 'test', // 測試用例:包括單元測試、集成測試 'update' // 更新:普通更新 ] ] } }
-
在git提交時,填寫的
commit
信息格式規範如下:<type>(<scope>): <subject> // 必填 // 空一行 <body> // 必填 // 空一行 <footer> // 可忽略不填
例子:
git commit -m 'style(home.vue):修改樣式 修改了home.vue的樣式,添加了背景色'
6.2,安裝自定義的輔助提交依賴
-
首先需要安裝
commitizen
和commitlint-config-cz
和cz-customizable
npm install commitizen commitlint-config-cz cz-customizable --save -D
-
然後新建
.cz-config.js
文件,內容如下:'use strict' module.exports = { types: [ { value: 'feat', name: '新增:新增功能、頁面' }, { value: 'fix', name: 'bug:修復某個bug' }, { value: 'docs', name: '文檔:修改增加文檔、注釋' }, { value: 'style', name: '格式:不影響代碼運行的變動、空格、格式化等等' }, { value: 'ui', name: 'ui修改:布局、css樣式等等' }, { value: 'hotfix', name: 'bug:修複線上緊急bug' }, { value: 'build', name: '測試:添加一個測試' }, { value: 'refactor', name: '重構:代碼重構,未新增任何功能和修復任何bug' }, { value: 'revert', name: '回滾:代碼回退到某個版本節點' }, { value: 'perf', name: '優化:提升性能、用戶體驗等' }, { value: 'ci', name: '自動化構建:對CI/CD配置文件和腳本的更改' }, { value: 'chore', name: '其他修改:不修改src目錄或測試文件的修改' }, { value: 'test', name: '測試:包括單元測試、集成測試' }, { value: 'update', name: '更新:普通更新' } ], // 交互提示信息 messages: { type: '選擇一種你的提交類型:', scope: '選擇一個影響範圍(可選):', customScope: '表示此更改的範圍:', subject: '短說明:\n', body: '長說明,使用"|"符號換行(可選):\n', breaking: '非兼容性說明(可選):\n', footer: '關閉的issue,例如:#31, #34(可選):\n', confirmCommit: '確定提交說明?(yes/no)' }, allowCustomScopes: true, // 設置選擇了那些type,才詢問 breaking message allowBreakingChanges: ['feat', 'fix', 'ui', 'hotfix', 'update', 'perf'], subjectLimit: 100 }
-
然後修改
commitlint.config.js
文件的extends
選項,改成['cz']
module.exports = { extends: ['cz'], ...... }
-
編輯
package.json
文件,在scripts
中添加"commit": "git-cz"
命令"scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "lint-fix": "vue-cli-service lint --fix ./src --ext .vue,.js,.ts", "prepare": "husky install", "commit": "git-cz" }
-
在根目錄終端,運行以下命令初始化命令行的選項信息:
npx commitizen init cz-customizable --save-dev --save-exact
-
運行完成後,在
package.json
中會出現如下選項:"config": { "commitizen": { "path": "./node_modules/cz-customizable" } }
此時,代碼提交過程就多了一個可選的規範提示,提交流程是先git add .
,提交到暫存區,然後終端運行npm run commit
,根據提示選擇信息或者輸入即可,最後git push
推送,如下gif圖所示:
附幾個我個人的項目模板:
- Vite-H5-Template
- Vite-PC-Template
- Webpack-PC-Template
- H5-V2-Template
- Uniapp-Cli-V2-Template
- Uniapp-Cli-Ts-V3-Template
- PC-V2-Template
如果看了覺得有幫助的,我是@上進的鵬多多,歡迎 點贊 關注 評論;END
PS:在本頁按F12,在console中輸入document.querySelectorAll(‘.diggit’)[0].click(),有驚喜哦
公眾號
往期文章
- Vue2全家桶+Element搭建的PC端在線音樂網站
- 超詳細的Cookie增刪改查
- 助你上手Vue3全家桶之Vue-Router4教程
- 助你上手Vue3全家桶之Vue3教程
- 助你上手Vue3全家桶之VueX4教程
- 使用nvm管理node.js版本以及更換npm淘寶鏡像源
- 超詳細!Vue-Router手把手教程
- 超詳細!Vue的九種通信方式
- 超詳細!Vuex手把手教程
個人主頁