Vite?Vue3?EsLint?Prettier配置步驟極簡(jiǎn)方法詳解
使用vite創(chuàng)建一個(gè)項(xiàng)目
執(zhí)行命令: yarn create vite
? Project name: 輸入你的項(xiàng)目名稱?(如: esvue)
? Select a framework: 選擇安裝的腳手架 (這里選vue)
vanilla
> vue
react
Done. Now run:
cd esvue
yarn
yarn dev安裝EsLint
yarn add -D eslint
初始化配置EsLint
npx eslint --init
選擇模式: (To check syntax and find problems)
You can also run this command directly using 'npm init @eslint/config'. ? How would you like to use ESLint? ... To check syntax only > To check syntax and find problems To check syntax, find problems, and enforce code style
選擇語(yǔ)言模塊: (選JavaScript modules)
? What type of modules does your project use? ... > JavaScript modules (import/export) CommonJS (require/exports) None of these
選擇語(yǔ)言框架 (選Vue.js)
? Which framework does your project use? ... React > Vue.js None of these
是否使用ts (視自己情況而定,我這里不用選No)
? Does your project use TypeScript? ? No / Yes
代碼在哪里運(yùn)行 (用空格選中 Browser+Node)
? Where does your code run? ... (Press <space> to select, <a> to toggle all, <i> to invert selection) √ Browser √ Node
您希望您的配置文件是什么格式? (選JavaScript)
? What format do you want your config file to be in? ... > JavaScript YAML JSON
您想現(xiàn)在安裝它們嗎? (選擇Yes)
? Would you like to install them now? ? No / Yes
您要使用哪個(gè)軟件包管理器? (選擇yarn)
? Which package manager do you want to use? ... npm > yarn pnpm
安裝完成后 (在項(xiàng)目根目錄會(huì)出現(xiàn).eslintrc.js文件)
// .eslintrc.js 文件
// 這里就是上面3步驟初始化的文件,后面的配置都寫(xiě)這里
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: "eslint:recommended",
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
}
}繼續(xù)安裝 vite-plugin-eslint
// 說(shuō)明: 該包是用于配置vite運(yùn)行的時(shí)候自動(dòng)檢測(cè)eslint規(guī)范 // 問(wèn)題: 不裝這個(gè)包可以嗎? 答案是“可以的”,使用yarn dev時(shí)并不會(huì)主動(dòng)檢查代碼 yarn add -D vite-plugin-eslint
繼續(xù)安裝 eslint-parser
yarn add -D @babel/core yarn add -D @babel/eslint-parser
繼續(xù)安裝prettier (用于規(guī)范代碼格式,不需要可以不裝)
yarn add -D prettier yarn add -D eslint-config-prettier // eslint兼容的插件 yarn add -D eslint-plugin-prettier // eslint的prettier
配置 vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint' //導(dǎo)入包
export default defineConfig({
plugins: [
vue(),
// 增加下面的配置項(xiàng),這樣在運(yùn)行時(shí)就能檢查eslint規(guī)范
eslintPlugin({
include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
})
]
})配置 .prettierrc
如果您沒(méi)有安裝第7步驟的那三個(gè)包,這個(gè)配置你可以忽略跳過(guò)
這里主要配置代碼的格式規(guī)范的,有些設(shè)置要與.eslintrc.js配置一致,防止沖突
// 在項(xiàng)目根目錄創(chuàng)建文件 .prettierrc
// 以下配置視自己情況而定,并步是每個(gè)都需要的
{
tabWidth: 4, // 使用4個(gè)空格縮進(jìn)
semi: false, // 代碼結(jié)尾是否加分號(hào)
trailingComma: 'none', // 代碼末尾不需要逗號(hào)
singleQuote: true, // 是否使用單引號(hào)
printWidth: 100, // 超過(guò)多少字符強(qiáng)制換行
arrowParens: 'avoid', // 單個(gè)參數(shù)的箭頭函數(shù)不加括號(hào) x => x
bracketSpacing: true, // 對(duì)象大括號(hào)內(nèi)兩邊是否加空格 { a:0 }
endOfLine: 'auto', // 文件換行格式 LF/CRLF
useTabs: false, // 不使用縮進(jìn)符,而使用空格
quoteProps: 'as-needed', // 對(duì)象的key僅在必要時(shí)用引號(hào)
jsxSingleQuote: false, // jsx不使用單引號(hào),而使用雙引號(hào)
jsxBracketSameLine: false, // jsx標(biāo)簽的反尖括號(hào)需要換行
rangeStart: 0, // 每個(gè)文件格式化的范圍是文件的全部?jī)?nèi)容
rangeEnd: Infinity, // 結(jié)尾
requirePragma: false, // 不需要寫(xiě)文件開(kāi)頭的 @prettier
insertPragma: false, // 不需要自動(dòng)在文件開(kāi)頭插入 @prettier
proseWrap: 'preserve', // 使用默認(rèn)的折行標(biāo)準(zhǔn)
htmlWhitespaceSensitivity: 'css' // 根據(jù)顯示樣式?jīng)Q定html要不要折行
}配置 .eslintrc.js
module.exports = {
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": [
"eslint:recommended", // 使用推薦的eslint
'plugin:vue/vue3-recommended' // 使用插件支持vue3
// 如果你沒(méi)有安裝第7步,以下兩個(gè)包不要引入,否則報(bào)錯(cuò)
'plugin:prettier/recommended',
'eslint-config-prettier'
],
"parserOptions": {
"ecmaVersion": 13,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
'jsx': true
},
"requireConfigFile": false,
"parser": '@babel/eslint-parser'
},
// eslint-plugin-vue
'plugins': [
'vue' // 引入vue的插件 vue <==> eslint-plugin-vue
// 這個(gè)包需要安裝了第7步的三個(gè)包再引入
'prettier' // 引入規(guī)范插件 prettier <==> eslint-plugin-prettier
],
'globals': {
defineProps: 'readonly',
defineEmits: 'readonly',
defineExpose: 'readonly',
withDefaults: 'readonly'
},
// 這里時(shí)配置規(guī)則的,自己看情況配置
"rules": {
'semi': ['warn', 'never'], // 禁止尾部使用分號(hào)
'no-console': 'warn', // 禁止出現(xiàn)console
'no-debugger': 'warn', // 禁止出現(xiàn)debugger
'no-duplicate-case': 'warn', // 禁止出現(xiàn)重復(fù)case
'no-empty': 'warn', // 禁止出現(xiàn)空語(yǔ)句塊
'no-extra-parens': 'warn', // 禁止不必要的括號(hào)
'no-func-assign': 'warn', // 禁止對(duì)Function聲明重新賦值
'no-unreachable': 'warn', // 禁止出現(xiàn)[return|throw]之后的代碼塊
'no-else-return': 'warn', // 禁止if語(yǔ)句中return語(yǔ)句之后有else塊
'no-empty-function': 'warn', // 禁止出現(xiàn)空的函數(shù)塊
'no-lone-blocks': 'warn', // 禁用不必要的嵌套塊
'no-multi-spaces': 'warn', // 禁止使用多個(gè)空格
'no-redeclare': 'warn', // 禁止多次聲明同一變量
'no-return-assign': 'warn', // 禁止在return語(yǔ)句中使用賦值語(yǔ)句
'no-return-await': 'warn', // 禁用不必要的[return/await]
'no-self-compare': 'warn', // 禁止自身比較表達(dá)式
'no-useless-catch': 'warn', // 禁止不必要的catch子句
'no-useless-return': 'warn', // 禁止不必要的return語(yǔ)句
'no-mixed-spaces-and-tabs': 'warn', // 禁止空格和tab的混合縮進(jìn)
'no-multiple-empty-lines': 'warn', // 禁止出現(xiàn)多行空行
'no-trailing-spaces': 'warn', // 禁止一行結(jié)束后面不要有空格
'no-useless-call': 'warn', // 禁止不必要的.call()和.apply()
'no-var': 'warn', // 禁止出現(xiàn)var用let和const代替
'no-delete-var': 'off', // 允許出現(xiàn)delete變量的使用
'no-shadow': 'off', // 允許變量聲明與外層作用域的變量同名
'dot-notation': 'warn', // 要求盡可能地使用點(diǎn)號(hào)
'default-case': 'warn', // 要求switch語(yǔ)句中有default分支
'eqeqeq': 'warn', // 要求使用 === 和 !==
'curly': 'warn', // 要求所有控制語(yǔ)句使用一致的括號(hào)風(fēng)格
'space-before-blocks': 'warn', // 要求在塊之前使用一致的空格
'space-in-parens': 'warn', // 要求在圓括號(hào)內(nèi)使用一致的空格
'space-infix-ops': 'warn', // 要求操作符周?chē)锌崭?
'space-unary-ops': 'warn', // 要求在一元操作符前后使用一致的空格
'switch-colon-spacing': 'warn', // 要求在switch的冒號(hào)左右有空格
'arrow-spacing': 'warn', // 要求箭頭函數(shù)的箭頭前后使用一致的空格
'array-bracket-spacing': 'warn', // 要求數(shù)組方括號(hào)中使用一致的空格
'brace-style': 'warn', // 要求在代碼塊中使用一致的大括號(hào)風(fēng)格
'camelcase': 'warn', // 要求使用駱駝拼寫(xiě)法命名約定
'indent': ['warn', 4], // 要求使用JS一致縮進(jìn)4個(gè)空格
'max-depth': ['warn', 4], // 要求可嵌套的塊的最大深度4
'max-statements': ['warn', 100], // 要求函數(shù)塊最多允許的的語(yǔ)句數(shù)量20
'max-nested-callbacks': ['warn', 3], // 要求回調(diào)函數(shù)最大嵌套深度3
'max-statements-per-line': ['warn', { max: 1 }], // 要求每一行中所允許的最大語(yǔ)句數(shù)量
"quotes": ["warn", "single", "avoid-escape"], // 要求統(tǒng)一使用單引號(hào)符號(hào)
"vue/require-default-prop": 0, // 關(guān)閉屬性參數(shù)必須默認(rèn)值
"vue/singleline-html-element-content-newline": 0, // 關(guān)閉單行元素必須換行符
"vue/multiline-html-element-content-newline": 0, // 關(guān)閉多行元素必須換行符
// 要求每一行標(biāo)簽的最大屬性不超五個(gè)
'vue/max-attributes-per-line': ['warn', { singleline: 5 }],
// 要求html標(biāo)簽的縮進(jìn)為需要4個(gè)空格
"vue/html-indent": ["warn", 4, {
"attribute": 1,
"baseIndent": 1,
"closeBracket": 0,
"alignAttributesVertically": true,
"ignores": []
}],
// 取消關(guān)閉標(biāo)簽不能自閉合的限制設(shè)置
"vue/html-self-closing": ["error", {
"html": {
"void": "always",
"normal": "never",
"component": "always"
},
"svg": "always",
"math": "always"
}]
}
}配置VScode
1、安裝“ESLint”插件【作者: Microsoft】
2、安裝“Prettier ESLint”插件 【作者:Rebecca Vest】
// 配置vscode
// 打開(kāi):設(shè)置 -> 文本編輯器 -> 字體 -> 在 settings.json 中編輯
// settings.json文件
{
// vscode默認(rèn)啟用了根據(jù)文件類型自動(dòng)設(shè)置tabsize的選項(xiàng)
"editor.detectIndentation": false,
// 重新設(shè)定tabsize
"editor.tabSize": 2,
// 每次保存的時(shí)候自動(dòng)格式化
"editor.formatOnSave": true,
// 每次保存的時(shí)候?qū)⒋a按eslint格式進(jìn)行修復(fù)
"eslint.autoFixOnSave": true,
// 添加vue支持
"eslint.validate": [
"javascript",
"javascriptreact",
{
"language": "vue",
"autoFix": true
}
],
// #讓prettier使用eslint的代碼格式進(jìn)行校驗(yàn)
"prettier.eslintIntegration": true
}其它
(整個(gè)流程下來(lái)安裝的包)
"devDependencies": {
// eslint解析相關(guān)的包
"@babel/core": "^7.18.2",
"@babel/eslint-parser": "^7.18.2",
// eslint相關(guān)的包
"eslint": "^8.17.0",
"eslint-plugin-vue": "^9.1.0",
"vue-eslint-parser": "^9.0.2",
"vite-plugin-eslint": "^1.6.1",
// prettier相關(guān)的包
"prettier": "^2.6.2",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
}寄語(yǔ)
個(gè)人不太喜歡prettier, 有時(shí)候還是喜歡按照自己的格式
比如說(shuō)有很多配置項(xiàng),我需要在每個(gè)配置加上注釋,并且讓注釋對(duì)齊,
如果你使用了prettier你的注釋是沒(méi)辦法對(duì)齊的,會(huì)自動(dòng)縮回去
以上就是Vite Vue3 EsLint Prettier配置步驟極簡(jiǎn)方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Vite Vue3 EsLint Prettier配置步驟的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue3中的數(shù)據(jù)劫持的最新實(shí)現(xiàn)方案的proxy示例詳解
Vue3中使用Proxy實(shí)現(xiàn)數(shù)據(jù)劫持,解決了Vue2中數(shù)組和對(duì)象劫持的遺留問(wèn)題,Proxy可以修改某些操作的默認(rèn)行為,通過(guò)get和set方法實(shí)現(xiàn)數(shù)據(jù)的劫持和保護(hù)機(jī)制,感興趣的朋友跟隨小編一起看看吧2024-11-11
vue實(shí)現(xiàn)select下拉顯示隱藏功能
這篇文章主要介紹了vue實(shí)現(xiàn)select下拉顯示隱藏功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Vue實(shí)現(xiàn)實(shí)時(shí)刷新時(shí)間的功能
這篇文章主要為大家詳細(xì)介紹了如何Vue利用實(shí)現(xiàn)實(shí)時(shí)刷新時(shí)間的功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解下2023-12-12
Vue實(shí)現(xiàn)省市區(qū)級(jí)聯(lián)下拉選擇框
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)省市區(qū)級(jí)聯(lián)下拉選擇框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn)
這篇文章主要介紹了Vue props 單向數(shù)據(jù)流的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
vue代理如何配置重寫(xiě)方法(pathRewrite與rewrite)
這篇文章主要介紹了vue代理如何配置重寫(xiě)方法(pathRewrite與rewrite),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

