欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vite?Vue3?EsLint?Prettier配置步驟極簡方法詳解

 更新時間:2023年09月12日 14:29:54   作者:sunday  
這篇文章主要為大家介紹了Vite?Vue3?EsLint?Prettier配置步驟的極簡方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

使用vite創(chuàng)建一個項目

執(zhí)行命令: yarn create vite
? Project name: 輸入你的項目名稱?(如: 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

選擇語言模塊: (選JavaScript modules)

? What type of modules does your project use? ...
> JavaScript modules (import/export)
  CommonJS (require/exports)
  None of these

選擇語言框架 (選Vue.js)

? Which framework does your project use? ...
  React
> Vue.js
  None of these

是否使用ts (視自己情況而定,我這里不用選No)

? Does your project use TypeScript? ? No / Yes

代碼在哪里運行 (用空格選中 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

您要使用哪個軟件包管理器? (選擇yarn)

? Which package manager do you want to use? ...
  npm
> yarn
  pnpm

安裝完成后 (在項目根目錄會出現(xiàn).eslintrc.js文件)

// .eslintrc.js 文件
// 這里就是上面3步驟初始化的文件,后面的配置都寫這里
module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: "eslint:recommended",
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  rules: {
  }
}

繼續(xù)安裝 vite-plugin-eslint

// 說明: 該包是用于配置vite運行的時候自動檢測eslint規(guī)范
// 問題: 不裝這個包可以嗎? 答案是“可以的”,使用yarn dev時并不會主動檢查代碼
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(),
    // 增加下面的配置項,這樣在運行時就能檢查eslint規(guī)范
    eslintPlugin({
      include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
    })
  ]
})

配置 .prettierrc

如果您沒有安裝第7步驟的那三個包,這個配置你可以忽略跳過

這里主要配置代碼的格式規(guī)范的,有些設(shè)置要與.eslintrc.js配置一致,防止沖突

// 在項目根目錄創(chuàng)建文件 .prettierrc
// 以下配置視自己情況而定,并步是每個都需要的
{
  tabWidth: 4,               // 使用4個空格縮進
  semi: false,               // 代碼結(jié)尾是否加分號
  trailingComma: 'none',     // 代碼末尾不需要逗號
  singleQuote: true,         // 是否使用單引號
  printWidth: 100,           // 超過多少字符強制換行
  arrowParens: 'avoid',      // 單個參數(shù)的箭頭函數(shù)不加括號 x => x
  bracketSpacing: true,      // 對象大括號內(nèi)兩邊是否加空格 { a:0 }
  endOfLine: 'auto',         // 文件換行格式 LF/CRLF
  useTabs: false,            // 不使用縮進符,而使用空格
  quoteProps: 'as-needed',   // 對象的key僅在必要時用引號
  jsxSingleQuote: false,     // jsx不使用單引號,而使用雙引號
  jsxBracketSameLine: false, // jsx標(biāo)簽的反尖括號需要換行
  rangeStart: 0,             // 每個文件格式化的范圍是文件的全部內(nèi)容
  rangeEnd: Infinity,        // 結(jié)尾
  requirePragma: false,      // 不需要寫文件開頭的 @prettier
  insertPragma: false,       // 不需要自動在文件開頭插入 @prettier
  proseWrap: 'preserve',     // 使用默認的折行標(biāo)準
  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
         // 如果你沒有安裝第7步,以下兩個包不要引入,否則報錯 
        '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 
        // 這個包需要安裝了第7步的三個包再引入
        'prettier' // 引入規(guī)范插件  prettier <==>  eslint-plugin-prettier
    ],
    'globals': {
        defineProps: 'readonly',
        defineEmits: 'readonly',
        defineExpose: 'readonly',
        withDefaults: 'readonly'
    },
    // 這里時配置規(guī)則的,自己看情況配置
    "rules": {
        'semi': ['warn', 'never'],           // 禁止尾部使用分號
        '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)空語句塊
        'no-extra-parens': 'warn',           // 禁止不必要的括號
        'no-func-assign': 'warn',            // 禁止對Function聲明重新賦值
        'no-unreachable': 'warn',            // 禁止出現(xiàn)[return|throw]之后的代碼塊
        'no-else-return': 'warn',            // 禁止if語句中return語句之后有else塊
        'no-empty-function': 'warn',         // 禁止出現(xiàn)空的函數(shù)塊
        'no-lone-blocks': 'warn',            // 禁用不必要的嵌套塊
        'no-multi-spaces': 'warn',           // 禁止使用多個空格
        'no-redeclare': 'warn',              // 禁止多次聲明同一變量
        'no-return-assign': 'warn',          // 禁止在return語句中使用賦值語句
        'no-return-await': 'warn',           // 禁用不必要的[return/await]
        'no-self-compare': 'warn',           // 禁止自身比較表達式
        'no-useless-catch': 'warn',          // 禁止不必要的catch子句
        'no-useless-return': 'warn',         // 禁止不必要的return語句
        'no-mixed-spaces-and-tabs': 'warn',  // 禁止空格和tab的混合縮進
        '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',              // 要求盡可能地使用點號
        'default-case': 'warn',              // 要求switch語句中有default分支
        'eqeqeq': 'warn',                    // 要求使用 === 和 !==
        'curly': 'warn',                     // 要求所有控制語句使用一致的括號風(fēng)格
        'space-before-blocks': 'warn',       // 要求在塊之前使用一致的空格
        'space-in-parens': 'warn',           // 要求在圓括號內(nèi)使用一致的空格
        'space-infix-ops': 'warn',           // 要求操作符周圍有空格
        'space-unary-ops': 'warn',           // 要求在一元操作符前后使用一致的空格
        'switch-colon-spacing': 'warn',      // 要求在switch的冒號左右有空格
        'arrow-spacing': 'warn',             // 要求箭頭函數(shù)的箭頭前后使用一致的空格
        'array-bracket-spacing': 'warn',     // 要求數(shù)組方括號中使用一致的空格
        'brace-style': 'warn',               // 要求在代碼塊中使用一致的大括號風(fēng)格
        'camelcase': 'warn',                 // 要求使用駱駝拼寫法命名約定
        'indent': ['warn', 4],               // 要求使用JS一致縮進4個空格
        'max-depth': ['warn', 4],            // 要求可嵌套的塊的最大深度4
        'max-statements': ['warn', 100],     // 要求函數(shù)塊最多允許的的語句數(shù)量20
        'max-nested-callbacks': ['warn', 3], // 要求回調(diào)函數(shù)最大嵌套深度3
        'max-statements-per-line': ['warn', { max: 1 }],   // 要求每一行中所允許的最大語句數(shù)量
        "quotes": ["warn", "single", "avoid-escape"],      // 要求統(tǒng)一使用單引號符號
        "vue/require-default-prop": 0,                     // 關(guān)閉屬性參數(shù)必須默認值
        "vue/singleline-html-element-content-newline": 0,  // 關(guān)閉單行元素必須換行符
        "vue/multiline-html-element-content-newline": 0,   // 關(guān)閉多行元素必須換行符
        // 要求每一行標(biāo)簽的最大屬性不超五個
        'vue/max-attributes-per-line': ['warn', { singleline: 5 }],
        // 要求html標(biāo)簽的縮進為需要4個空格
        "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
// 打開:設(shè)置 -> 文本編輯器 -> 字體 -> 在 settings.json 中編輯
// settings.json文件
{
 // vscode默認啟用了根據(jù)文件類型自動設(shè)置tabsize的選項
 "editor.detectIndentation": false,
 // 重新設(shè)定tabsize
 "editor.tabSize": 2,
 // 每次保存的時候自動格式化 
 "editor.formatOnSave": true,
 // 每次保存的時候?qū)⒋a按eslint格式進行修復(fù)
 "eslint.autoFixOnSave": true,
 // 添加vue支持
 "eslint.validate": [
      "javascript",
      "javascriptreact",
      {
           "language": "vue",
           "autoFix": true
      }
 ],
 // #讓prettier使用eslint的代碼格式進行校驗 
 "prettier.eslintIntegration": true
}

其它 

(整個流程下來安裝的包)

"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",
  }

寄語

個人不太喜歡prettier, 有時候還是喜歡按照自己的格式

比如說有很多配置項,我需要在每個配置加上注釋,并且讓注釋對齊,

如果你使用了prettier你的注釋是沒辦法對齊的,會自動縮回去

以上就是Vite Vue3 EsLint Prettier配置步驟極簡方法詳解的詳細內(nèi)容,更多關(guān)于Vite Vue3 EsLint Prettier配置步驟的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue3中的數(shù)據(jù)劫持的最新實現(xiàn)方案的proxy示例詳解

    vue3中的數(shù)據(jù)劫持的最新實現(xiàn)方案的proxy示例詳解

    Vue3中使用Proxy實現(xiàn)數(shù)據(jù)劫持,解決了Vue2中數(shù)組和對象劫持的遺留問題,Proxy可以修改某些操作的默認行為,通過get和set方法實現(xiàn)數(shù)據(jù)的劫持和保護機制,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • vue3封裝Notification組件的完整步驟記錄

    vue3封裝Notification組件的完整步驟記錄

    在我們使用vue的開發(fā)過程中總會遇到這樣的場景,封裝自己的業(yè)務(wù)組件,下面這篇文章主要給大家介紹了關(guān)于vue3封裝Notification組件的完整步驟,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-03-03
  • vue實現(xiàn)select下拉顯示隱藏功能

    vue實現(xiàn)select下拉顯示隱藏功能

    這篇文章主要介紹了vue實現(xiàn)select下拉顯示隱藏功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • 基于element-ui封裝表單金額輸入框的方法示例

    基于element-ui封裝表單金額輸入框的方法示例

    這篇文章主要介紹了基于element-ui封裝表單金額輸入框的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Vue實現(xiàn)實時刷新時間的功能

    Vue實現(xiàn)實時刷新時間的功能

    這篇文章主要為大家詳細介紹了如何Vue利用實現(xiàn)實時刷新時間的功能,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以了解下
    2023-12-12
  • vue的安裝及element組件的安裝方法

    vue的安裝及element組件的安裝方法

    下面小編就為大家分享一篇vue的安裝及element組件的安裝,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Vue實現(xiàn)省市區(qū)級聯(lián)下拉選擇框

    Vue實現(xiàn)省市區(qū)級聯(lián)下拉選擇框

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)省市區(qū)級聯(lián)下拉選擇框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue props 單向數(shù)據(jù)流的實現(xiàn)

    Vue props 單向數(shù)據(jù)流的實現(xiàn)

    這篇文章主要介紹了Vue props 單向數(shù)據(jù)流的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • vue代理如何配置重寫方法(pathRewrite與rewrite)

    vue代理如何配置重寫方法(pathRewrite與rewrite)

    這篇文章主要介紹了vue代理如何配置重寫方法(pathRewrite與rewrite),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 封裝一下vue中的axios示例代碼詳解

    封裝一下vue中的axios示例代碼詳解

    這篇文章主要介紹了封裝一下vue中的axios,本文通過示例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02

最新評論