vite項(xiàng)目添加eslint?prettier及husky方法實(shí)例
1. 初始化vite項(xiàng)目
npm init vite ? Project name: … vite-project // 項(xiàng)目名稱,默認(rèn) vite-project ? Select a framework: ? react // 選擇框架 ? Select a variant: ? react-ts // 選擇組合
2. 添加eslint
安裝
npm i -D eslint
初始化eslint
配置
npx eslint --init ? How would you like to use ESLint? // To check syntax, find problems, and enforce code style ? What type of modules does your project use? // JavaScript modules (import/export) ? Which framework does your project use? // react ? Does your project use TypeScript? // Yes ? Where does your code run? // browser ? How would you like to define a style for your project? // Use a popular style guide ? Which style guide do you want to follow? // Airbnb ? What format do you want your config file to be in? // JavaScript
解決eslint
報(bào)錯(cuò)
Missing semicolon.eslint
: 末尾加上;
即可
JSX not allowed in files with extension '.tsx'
: 在.eslintrc.json
中添加rules
設(shè)置
{ // ... "react/jsx-filename-extension": [ "error", { "extensions": [".js", ".jsx", ".tsx", ".ts"] } ] };
{count} must be placed on a new line
: 代碼換行即可;
Unable to resolve path to module './App'
: 在.eslintrc.json
中添加rules
設(shè)置
{ // ... rules: { "import/no-unresolved": "off", "import/extensions": "off", "import/no-absolute-path": "off" }, };
'vite' should be listed in the project's dependencies, not devDependencies.:
在.eslintrc.json
中添加rules
設(shè)置
{ // ... "rules": { "import/no-extraneous-dependencies": [ "error", {"devDependencies": true} ] // ... } };
3. 添加 prettier
安裝
npm i -D prettier eslint-config-prettier eslint-plugin-prettier
prettier: 核心模塊
eslint-config-prettier: 關(guān)閉所有不必要或可能跟prettier產(chǎn)生沖突的規(guī)則
eslint-plugin-prettier: 可以讓eslint使用prettier規(guī)則進(jìn)行檢查
配置
根目錄下.eslintrc.json
文件中添加extends
設(shè)置
{ // ... "extends": [ // ... "plugin:prettier/recommended", ], }
根目錄下創(chuàng)建.prettierrc.json
文件
{ "trailingComma": "es5", "tabWidth": 2, "semi": true, "singleQuote": true }
注意??:
修改.prettierrc.json
文件后,需要重啟vscode才生效
如果配置后,配置不生效,嘗試以下設(shè)置:
4. 添加 husky和lint-staged
安裝
npm i -D lint-staged husky
配置
在package.json
中添加腳本
npm set-script prepare "husky install"
package.json
文件的scripts
中,就會(huì)自動(dòng)添加prepare
;
2. 初始化husky,將 git hooks鉤子交由husky執(zhí)行
npm run prepare
會(huì)在根目錄創(chuàng)建.husky
文件夾
3. 配置package.json
package.json
文件如下:
{ "env": { "browser": true, "node": true, "es2021": true }, "extends": [ "plugin:react/recommended", "airbnb", "plugin:prettier/recommended", "plugin:import/recommended" ], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaFeatures": { "jsx": true }, "ecmaVersion": "latest", "sourceType": "module" }, "plugins": ["react", "@typescript-eslint"], "rules": { "import/no-extraneous-dependencies": [ "error", {"devDependencies": true} ], "react/jsx-filename-extension": [ "error", { "extensions": [".js", ".jsx", ".tsx", ".ts"] } ], "import/no-unresolved": "off", "import/extensions": "off", "import/no-absolute-path": "off" } }
添加鉤子pre-commit
npx husky add .husky/pre-commit "npx lint-staged"
5. 配置commitlint
- 作用:規(guī)范提交信息
- 格式:git commit -m '類型: 描述性文字'
類型 | 概念 |
---|---|
build | 編譯相關(guān)的修改,例如發(fā)布版本、對(duì)項(xiàng)目構(gòu)建或者依賴的改動(dòng) |
ci | 持續(xù)集成修改 |
docs | 文檔修改 |
feat | 新特性、新功能 |
fix | 修改bug |
perf | 優(yōu)化相關(guān),比如提升性能、體驗(yàn) |
refactor | 代碼重構(gòu) |
revert | 回滾到上一個(gè)版本 |
style | 代碼格式修改, 注意不是 css 修改 |
test | 測(cè)試用例修改 |
chore | 其他修改,比如改變構(gòu)建流程、或者增加依賴庫(kù)、工具等 |
- 安裝
npm i -D commitlint @commitlint/config-conventional
- 配置
package.json
中配置commitlint
{ // ... "commitlint": { "extends": [ "@commitlint/config-conventional" ] } }
- 添加鉤子
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
git commit
就會(huì)觸發(fā)提交規(guī)范的校驗(yàn)啦;
以上就是vite項(xiàng)目添加eslint prettier及husky方法實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于vite項(xiàng)目添加eslint prettier husky的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue實(shí)現(xiàn)帶自動(dòng)吸附功能的懸浮球
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)帶自動(dòng)吸附功能的懸浮球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue項(xiàng)目打包后提交到git上為什么沒有dist這個(gè)文件的解決方法
這篇文章主要介紹了vue項(xiàng)目打包后提交到git上為什么沒有dist這個(gè)文件的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09VUE動(dòng)態(tài)生成word的實(shí)現(xiàn)
這篇文章主要介紹了VUE動(dòng)態(tài)生成word的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue項(xiàng)目如何讓局域網(wǎng)ip訪問(wèn)配置設(shè)置
這篇文章主要介紹了vue項(xiàng)目如何讓局域網(wǎng)ip訪問(wèn)配置設(shè)置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-09-09在Vue中使用動(dòng)態(tài)import()語(yǔ)法動(dòng)態(tài)加載組件
在Vue中,你可以使用動(dòng)態(tài)import()語(yǔ)法來(lái)動(dòng)態(tài)加載組件,動(dòng)態(tài)導(dǎo)入允許你在需要時(shí)異步加載組件,這樣可以提高應(yīng)用程序的初始加載性能,本文給大家介紹在Vue中使用動(dòng)態(tài)import()語(yǔ)法動(dòng)態(tài)加載組件,感興趣的朋友一起看看吧2023-11-11Vue路由監(jiān)聽實(shí)現(xiàn)同頁(yè)面動(dòng)態(tài)加載的示例
本文主要介紹了Vue基于路由監(jiān)聽實(shí)現(xiàn)同頁(yè)面動(dòng)態(tài)加載的示例,重點(diǎn)在于切換路由的時(shí)候,重新拉取列表數(shù)據(jù),接下來(lái)看看實(shí)現(xiàn)方法吧2021-05-05vue+element-ui集成隨機(jī)驗(yàn)證碼+用戶名+密碼的form表單驗(yàn)證功能
在登入頁(yè)面,我們往往需要通過(guò)輸入驗(yàn)證碼才能進(jìn)行登入,那我們下面就詳講一下在vue項(xiàng)目中如何配合element-ui實(shí)現(xiàn)這個(gè)功能,需要的朋友可以參考下2018-08-08