如何配置Vue插件以支持Vue 2和Vue 3

解读

国内项目普遍存在“渐进式升级”场景:同一仓库里既有 Vue 2 老模块,又有 Vue 3 新模块,甚至同一文件里混用 Options API 与 Composition API。面试官想确认两点:

  1. 你是否了解 ESLint 插件的“多实例”机制,能否让新旧语法同时过检;
  2. 你是否能把规则粒度控制到目录/文件级,避免“一刀切”导致误报或漏检。

回答时切忌只贴配置片段,要体现“版本识别 → 环境隔离 → 规则映射 → 性能兜底”的完整思路,并给出可落地的目录结构、脚本命令、CI 卡点方案。

知识点

  • parser 与 plugin 区别:parser 只负责 AST 生成,plugin 负责规则注册;Vue 2 用 vue-eslint-parser@7 + eslint-plugin-vue@7,Vue 3 用 vue-eslint-parser@9 + eslint-plugin-vue@9。
  • overrides 数组支持 files/excludedFiles 字段,可针对路径做解析器与规则双映射。
  • parserOptions.parser 的“二次委派”机制:外层 vue-eslint-parser 把 <script lang="ts"> 内容交给 @typescript-eslint/parser。
  • 全局变量注入差异:Vue 2 需手动声明 globals: { Vue: false },Vue 3 的 globalProperties 由 vue-eslint-parser@9 自动识别。
  • 性能陷阱:同一仓库同时装两套插件会重复遍历 node_modules,需用 eslint --cache --cache-strategy content 并配合 .eslintignore 把 dist、*.min.js 排除。
  • 国内常见工程化配套:husky + lint-staged 只做增量检查;GitLab CI 里用 eslint . --max-warnings 0 做全量卡点;若用 Monorepo,可在 pnpm-workspace.yaml 里给每个子包单独下 eslint 配置,避免幽灵依赖。

答案

  1. 安装依赖(以 pnpm 为例,npm/yarn 同理)
pnpm add -D \
  eslint@8 \
  vue-eslint-parser@9 \
  eslint-plugin-vue@9 \
  @typescript-eslint/parser@6 \
  @typescript-eslint/eslint-plugin@6
# 注意:vue-eslint-parser@9 向下兼容 Vue 2 模板,但规则集需要手动降级
  1. 目录约定(国内最常见的渐进式升级结构)
src/legacy/   # Vue 2 老代码
src/next/     # Vue 3 新代码
src/utils/    # 纯 JS/TS 与版本无关
  1. 根目录 .eslintrc.js(CommonJS 写法,兼容 IDE 自动提示)
module.exports = {
  root: true,
  env: { es2021: true, node: true },
  extends: ['eslint:recommended'],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
  },
  overrides: [
    /* --------- Vue 2 旧模块 --------- */
    {
      files: ['src/legacy/**/*.{js,vue}'],
      extends: [
        'plugin:vue/vue2-recommended', // 官方 Vue 2 规则集
        '@vue/prettier',               // 如团队用 Prettier
      ],
      parser: 'vue-eslint-parser',
      parserOptions: {
        ecmaVersion: 2021,
        sourceType: 'module',
        parser: '@babel/eslint-parser', // 仅 Vue 2 需要 babel parser
        requireConfigFile: false,
      },
      globals: { Vue: 'readonly' },     // Vue 2 全局
    },
    /* --------- Vue 3 新模块 --------- */
    {
      files: ['src/next/**/*.{js,ts,vue}'],
      extends: [
        'plugin:vue/vue3-recommended',
        '@vue/typescript/recommended',
        '@vue/prettier',
      ],
      parser: 'vue-eslint-parser',
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module',
        parser: '@typescript-eslint/parser', // 支持 <script lang="ts">
        project: './tsconfig.json',          // 开启规则需要类型信息
      },
      rules: {
        'vue/multi-word-component-names': 'off', // 国内习惯单词组件
        '@typescript-eslint/no-explicit-any': 'warn',
      },
    },
    /* --------- 纯 TS/JS 工具 --------- */
    {
      files: ['src/utils/**/*.{js,ts}'],
      excludedFiles: ['*.vue'],
      parser: '@typescript-eslint/parser',
      extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
    },
  ],
}
  1. 脚本与卡点
// package.json
{
  "scripts": {
    "lint": "eslint . --cache --max-warnings 0",
    "lint:fix": "eslint . --fix"
  },
  "lint-staged": {
    "*.{js,ts,vue}": "eslint --cache --fix"
  }
}

husky 的 .husky/pre-commit 仅执行 pnpm lint-staged,CI 阶段再跑一次全量 pnpm lint,保证合并前无增量警告。

  1. 常见坑位提示
  • 若老代码用 jsx,需在 Vue 2 的 overrides 里再加 parserOptions.ecmaFeatures.jsx: true。
  • 如果仓库用 Monorepo,把上述 .eslintrc.js 放到对应子包根目录,并在 parserOptions.project 指向子包自己的 tsconfig.eslint.json,避免跨包类型污染。
  • 国内镜像源安装失败时,锁定 vue-eslint-parser@9.3.0 与 eslint-plugin-vue@9.15.0 这对稳定组合,可绕过 pnpm 解析 bug。

拓展思考

  1. 自动化迁移:写一条自定义规则,扫描 <script setup> 出现次数,当某个 Vue 2 目录占比超过 80% 时,提示开发者“可整体升级到 Vue 3 规则集”,把配置演进从“人找配置”变成“配置找人”。
  2. 性能极限优化:把 vue-eslint-parser 的 template-body-indent 规则在 CI 环境关闭,本地 IDE 阶段保留,既保证卡点速度,又不牺牲开发体验。
  3. 与 SonarQube 双轨:国内金融、政务项目常要求 ESLint 结果同步到 SonarQube,可在 eslint 后加 eslint-formatter-sonarqube 插件,直接输出 sonar-generic-issue 格式,省去 Jenkins 二次解析。
  4. 未来可替换方案:若团队后续迁移到 Vite + Rolldown,可评估 oxc 生态的 oxlint,届时把同一套 overrides 路径映射迁移到 oxlint.config.json,实现“配置资产无损耗换引擎”。