Vite配置ESLint

Vite vue3 ESLint 配置

1. 使用 Vite 创建一个项目

# npm 6.x
npm create vite@latest my-vue-app --template vue

# npm 7+, extra double-dash is needed:
npm create vite@latest my-vue-app -- --template vue

# yarn
yarn create vite my-vue-app --template vue

# pnpm
pnpm create vite my-vue-app --template vue

2. 安装并初始化配置 ESLint

2.1. 安装 ESLint @babel/eslint-parser

npm i -D eslint @babel/eslint-parser

2.2. 初始化配置 ESLint

npx eslint --init

ESLint 的相关的配置设置根据实际情况选择即可

You can also run this command directly using 'npm init @eslint/config'.
? How would you like to use ESLint? ...
√ How would you like to use ESLint? · problems 
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · No / Yes
√ Where does your code run? · browser, node
√ How would you like to define a style for your project? · guide
√ Which style guide do you want to follow? · standard
√ What format do you want your config file to be in? · JavaScript
Checking peerDependencies of eslint-config-standard@latest
The config that you've selected requires the following dependencies:

eslint-plugin-vue@latest eslint-config-standard@latest eslint@^8.0.1 eslint-plugin-import@^2.25.2 eslint-plugin-n@^15.0.0 eslint-plugin-promise@^6.0.0
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · npm

配置完成会自动安装相关依赖并生成 .eslintrc.cjs 文件

module.exports = {
  "env": {
    "browser": true,
    "es2021": true,
    "node": true
  },
  "extends": [
    "eslint:recommended",
    "plugin:vue/vue3-essential"
  ],
  "overrides": [],
  "parserOptions": {
    "ecmaVersion": "latest",
    "sourceType": "module"
  },
  "plugins": [
    "vue"
  ],
  "rules": {}
}


3. 安装并配置 vite-plugin-eslint

3.1. 安装 vite-plugin-eslint

npm i -D vite-plugin-eslint

3.2. 配置 vite.config.js 文件

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    // 添加 ESLint 插件配置
    eslintPlugin({
      include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
    })
  ],
})


4. ESLint 规则配置

4.1. 修改 ESLint 默认配置

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'plugin:vue/vue3-recommended', // https://eslint.vuejs.org/user-guide/#usage
    "eslint:recommended", // http://eslint.cn/docs/rules/
  ],
  overrides: [
  ],
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    requireConfigFile: false,
    parser: '@babel/eslint-parser'
  },
  plugins: [
    'vue'
  ],
  // http://eslint.cn/docs/rules/
  // https://eslint.vuejs.org/rules/
  rules: {
    // 配置自定义的规则
  }
}

4.2. 规则配置推荐!!!

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: [
    'plugin:vue/vue3-recommended', // https://eslint.vuejs.org/user-guide/#usage
    'eslint:recommended', // http://eslint.cn/docs/rules/
  ],
  globals: { },
  overrides: [],
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    requireConfigFile: false,
    parser: '@babel/eslint-parser',
  },
  plugins: ['vue'],
  /*
   * http://eslint.cn/docs/rules/
   * https://eslint.vuejs.org/rules/
   */
  rules: {
    // 配置自定义的规则
    'no-console': process.env.NODE_ENV === 'production' ? 1 : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 1 : 'off',
    'vue/max-attributes-per-line': [
      2,
      {
        singleline: { max: 5 },
        multiline: { max: 1 },
      },
    ],
    'vue/component-tags-order': [2, { order: ['template', 'script', 'style']}],
    'vue/custom-event-name-casing': [2, 'kebab-case'],
    'vue/no-multiple-objects-in-class': 2,
    'vue/no-potential-component-option-typo': [
      2,
      {
        presets: ['all'],
        custom: [],
      },
    ],
    'vue/padding-line-between-blocks': [2, 'always'],
    'vue/require-direct-export': 2,
    'vue/v-for-delimiter-style': [2, 'in'],
    /* 强制 getter 和 setter 在对象中成对出现 */
    'accessor-pairs': 2,
    /* 强制数组方法的回调函数中有 return 语句 */
    'array-callback-return': 2,
    /* 在数组开括号后和闭括号前强制换行 */
    'array-bracket-newline': [2, 'consistent'],
    /* 强制数组方括号中使用一致的空格 */
    'array-bracket-spacing': [
      2,
      'never',
      {
        singleValue: false,
        objectsInArrays: false,
        arraysInArrays: false,
      },
    ],
    /* 强制数组元素间出现换行 */
    'array-element-newline': [2, 'consistent'],
    'arrow-body-style': [2, 'as-needed'], // 要求箭头函数体使用大括号
    'arrow-parens': [2, 'as-needed'], // 要求箭头函数的参数使用圆括号
    /* 强制箭头函数的箭头前后使用一致的空格 */
    'arrow-spacing': [
      2,
      {
        before: true,
        after: true,
      },
    ],
    /* 强制在代码块中使用一致的大括号风格 */
    'brace-style': [2, 'stroustrup'],
    /* 强制使用骆驼拼写法命名约定 */
    camelcase: [0, { properties: 'always' }],
    'block-spacing': [2, 'always'],
    'comma-dangle': [2, 'always-multiline'], // 要求或禁止末尾逗号
    /* 强制在逗号前后使用一致的空格 */
    'comma-spacing': [
      2,
      {
        before: false,
        after: true,
      },
    ],
    /* 强制使用一致的逗号风格 */
    'comma-style': [2, 'last'],
    /* 强制所有控制语句使用一致的括号风格 */
    curly: 2,
    /* 要求 switch 语句中有 default 分支 */
    'default-case': 1,
    /* 强制尽可能地使用点号 */
    'dot-location': [2, 'property'],
    /* 强制尽可能地使用点号 */
    'dot-notation': 1,
    /* 要求或禁止在函数标识符和其调用之间有空格 */
    'func-call-spacing': 2,
    /* 强制使用一致的缩进 */
    indent: [2, 2, { SwitchCase: 1 }],
    /* 强制在 JSX 属性中一致地使用双引号或单引号 */
    'jsx-quotes': [2, 'prefer-single'],
    /* 要求或禁止文件末尾存在空行 */
    'eol-last': 2,
    /* 要求使用 === 和 !== */
    eqeqeq: [2, 'always', { null: 'ignore' }],
    /* 强制在对象字面量的属性中键和值之间使用一致的间距 */
    'key-spacing': [
      2,
      {
        beforeColon: false,
        afterColon: true,
      },
    ],
    /* 强制在关键字前后使用一致的空格 */
    'keyword-spacing': [
      2,
      {
        before: true,
        after: true,
      },
    ],
    /* 强制可嵌套的块的最大深度 */
    'max-depth': [2, 4],
    /* 强制最大行数 */
    'max-lines': [
      2,
      {
        max: 1200,
        skipBlankLines: true,
        skipComments: true,
      },
    ],
    /* 强制回调函数最大嵌套深度 */
    'max-nested-callbacks': [1, 5],
    /* 强制函数定义中最多允许的参数数量 */
    'max-params': [2, 5],
    /* 强制函数块最多允许的的语句数量 */
    'max-statements': [2, 100],
    // 强制每一行中所允许的最大语句数量
    'max-statements-per-line': [1, { max: 1 }],
    /* 要求构造函数首字母大写 */
    'new-cap': [
      2,
      {
        newIsCap: true,
        capIsNew: false,
      },
    ],
    /* 强制或禁止调用无参构造函数时有圆括号 */
    'new-parens': 2,
    /* 要求方法链中每个调用都有一个换行符 */
    'newline-per-chained-call': [1, { ignoreChainWithDepth: 4 }],
    /* 禁用 Array 构造函数 */
    'no-array-constructor': 2,
    /* 禁用 arguments.caller 或 arguments.callee */
    'no-caller': 2,
    /* 禁止 if 语句中 return 语句之后有 else 块 */
    'no-else-return': [2, { allowElseIf: false }],
    /* 禁止出现空函数 */
    'no-empty-function': 1,
    /* 禁止扩展原生类型 */
    'no-extend-native': 2,
    /* 禁止不必要的 .bind() 调用 */
    'no-extra-bind': 2,
    /* 禁止不必要的括号 */
    'no-extra-parens': [2, 'functions'],
    /* 禁用 eval() */
    'no-eval': 2,
    /* 禁止数字字面量中使用前导和末尾小数点 */
    'no-floating-decimal': 2,
    /* 禁止使用类似 eval() 的方法 */
    'no-implied-eval': 2,
    /* 禁用 __iterator__ 属性 */
    'no-iterator': 2,
    /* 禁用标签语句 */
    'no-labels': [
      2,
      {
        allowLoop: false,
        allowSwitch: false,
      },
    ],
    /* 禁用不必要的嵌套块 */
    'no-lone-blocks': 2,
    /* 不允许标签与变量同名 */
    'no-label-var': 2,
    /* 禁止 if 作为唯一的语句出现在 else 语句中 */
    'no-lonely-if': 2,
    /* 禁止使用多个空格 */
    'no-multi-spaces': 2,
    /* 禁止使用多行字符串 */
    'no-multi-str': 2,
    /* 禁止出现多行空行 */
    'no-multiple-empty-lines': [
      2,
      {
        max: 1,
        maxBOF: 0,
        maxEOF: 1,
      },
    ],
    /* 禁用否定的表达式 */
    'no-negated-condition': 2,
    /* 禁用 Object 的构造函数 */
    'no-new-object': 2,
    /* 禁止调用 require 时使用 new 操作符 */
    'no-new-require': 2,
    /* 禁止对 String,Number 和 Boolean 使用 new 操作符 */
    'no-new-wrappers': 2,
    /* 禁止在字符串中使用八进制转义序列 */
    'no-octal-escape': 2,
    /* 禁止对 __dirname 和 __filename 进行字符串连接 */
    'no-path-concat': 2,
    /* 禁用 __proto__ 属性 */
    'no-proto': 2,
    /* 禁止在 return 语句中使用赋值语句,除非用圆括号括起来 */
    'no-return-assign': [2, 'except-parens'],
    /* 禁止自身比较 */
    'no-self-compare': 2,
    /* 禁用逗号操作符 */
    'no-sequences': 2,
    /* 禁止抛出异常字面量 */
    'no-throw-literal': 2,
    /* 禁用行尾空格 */
    'no-trailing-spaces': 2,
    /* 禁止将变量初始化为 undefined */
    'no-undef-init': 2,
    /* 禁止不必要的 .call() 和 .apply() */
    'no-useless-call': 2,
    /* 禁止在对象中使用不必要的计算属性 */
    'no-useless-computed-key': 2,
    /* 禁用不必要的构造函数 */
    'no-useless-constructor': 2,
    /* 禁用一成不变的循环条件 */
    'no-unmodified-loop-condition': 2,
    /* 禁止可以在有更简单的可替代的表达式时使用三元操作符 */
    'no-unneeded-ternary': [2, { defaultAssignment: false }],
    /* 禁止出现未使用过的变量 */
    'no-unused-vars': [2, {
      vars: 'all', args: 'none',
    }],
    /* 要求使用 let 或 const 而不是 var */
    'no-var': 2,
    /* 禁止属性前有空白 */
    'no-whitespace-before-property': 2,
    /* 强制大括号内换行符的一致性 */
    'object-curly-newline': [
      2,
      {
        ObjectExpression: {
          multiline: true,
          minProperties: 2,
        },
        ObjectPattern: { multiline: true },
      },
    ],
    /* 强制在大括号中使用一致的空格 */
    'object-curly-spacing': [
      2,
      'always',
      {
        objectsInObjects: false,
        arraysInObjects: false,
      },
    ],
    /* 强制操作符使用一致的换行符 */
    'operator-linebreak': [
      2,
      'after',
      {
        overrides: {
          '?': 'before',
          ':': 'before',
        },
      },
    ],
    /* 强制将对象的属性放在不同的行上 */
    // 'object-property-newline': 2,
    /* 强制函数中的变量要么一起声明要么分开声明 */
    'one-var': [2, { initialized: 'never' }],
    /* 要求或禁止块内填充 */
    'padded-blocks': [2, 'never'],
    /* 要求使用 const 声明那些声明后不再被修改的变量 */
    'prefer-const': 2,
    /* 要求对象字面量属性名称用引号括起来 */
    'quote-props': [2, 'as-needed'],
    /* 强制使用一致的反勾号、双引号或单引号 */
    quotes: [
      2,
      'single',
      {
        avoidEscape: true,
        allowTemplateLiterals: true,
      },
    ],
    /* 禁止使用分号代替 ASI */
    semi: [2, 'never'],
    /* 强制分号之前和之后使用一致的空格 */
    'semi-spacing': [
      2,
      {
        before: false,
        after: true,
      },
    ],
    /* 强制在块之前使用一致的空格 */
    'space-before-blocks': [2, { functions: 'never' }],
    /* 强制在 function的左括号之前使用一致的空格 */
    'space-before-function-paren': [2, 'never'],
    /* 强制在圆括号内使用一致的空格 */
    'space-in-parens': [2, 'never'],
    /* 要求操作符周围有空格 */
    'space-infix-ops': 2,
    /* 强制在一元操作符前后使用一致的空格 */
    'space-unary-ops': [
      2,
      {
        words: true,
        nonwords: false,
      },
    ],
    /* 强制在注释中 // 或 /* 使用一致的空格 */
    'spaced-comment': [2, 'always', { markers: ['global', 'globals', 'eslint', 'eslint-disable', '*package', '!', ',']}],
    /* 禁止模板字符串中的嵌入表达式周围空格的使用 */
    'template-curly-spacing': [2, 'never'],
    /* 要求 IIFE 使用括号括起来 */
    'wrap-iife': [2, 'any'],
    /* 强制在 yield* 表达式中 * 周围使用空格 */
    'yield-star-spacing': [2, 'both'],
    /* 禁止 “Yoda” 条件 */
    yoda: [2, 'never'],
  },
}

5. 仓库地址

Gitee https://gitee.com/liushuai1125/vite-eslint-app.git

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漠诽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值