如何配置ESLint正确解析.vue单文件组件
解读
在国内前端团队面试中,Vue 技术栈占比极高,而 .vue 单文件组件(SFC)同时包含 <template>、<script>、<style> 三种语言块,ESLint 默认只能识别纯 JS/TS。如果配置不到位,会出现“Parsing error: Unexpected token <”或“定义了但未使用”的误报,直接拉低代码质量门禁的通过率。面试官想确认候选人是否真正落地过 Vue 项目的规范工程化,而不仅仅停留在“装过插件”层面。因此,回答必须覆盖:解析器链、共享配置、IDE 联动、CI 卡点、Monorepo 兼容、性能优化、以及 Vue3 与 Vue2 的差异。
知识点
- 解析器链:ESLint 默认使用
espree,无法解析.vue,需要eslint-plugin-vue提供的vue-eslint-parser作为顶层解析器,再把<script lang="ts">的内容交给@typescript-eslint/parser。 - 共享配置层级:
eslint:recommended→@vue/eslint-config-standard/@vue/eslint-config-typescript→ 团队自定义rules,覆盖顺序必须正确。 - 文件扩展名与
overrides:国内项目常混用.js、.ts、.vue,需通过overrides区分解析器与规则集,避免对普通 JS 文件重复解析 AST。 - 全局变量与环境:Vue SFC 编译后自动注入
defineProps、defineEmits等宏,需在env或globals中声明,否则报no-undef。 - IDE 实时反馈:VS Code 需同时安装
ESLint与Volar插件,并在settings.json中关闭 Vetur 以避免双解析冲突;保存时editor.codeActionsOnSave触发eslint --fix,保证本地提前修复。 - Git Hooks 与 CI:国内公司普遍使用 Husky + lint-staged,仅对
git diff中的.vue文件执行eslint --cache --ext .vue,.js,.ts src/,缓存策略可缩短 60% 耗时;CI 阶段再全量校验并生成eslint-formatter-junit报告,对接禅道或飞书机器人。 - Monorepo 场景:pnpm workspace 下,ESLint 配置需放在根目录,并在
.eslintignore中排除dist、node_modules/.pnpm;子包若使用不同 Vue 版本,可通过root: true与parserOptions.ecmaVersion隔离。 - 性能优化:开启
eslint --cache-location node_modules/.cache/eslint并把缓存目录纳入 GitLab Runner 的cache:key;对大型老旧项目,可临时关闭vue/no-unused-vars等耗时规则,渐进式治理。 - Vue2 与 Vue3 差异:Vue3 推荐
plugin:vue/vue3-recommended,Vue2 用plugin:vue/essential;Composition API 项目需额外开启vue/script-setup-uses-vars规则,防止<script setup>变量被误删。
答案
-
安装依赖(以 Vue3 + TypeScript + pnpm 为例,国内镜像源已配置
.npmrc)pnpm add -D eslint @eslint/js eslint-plugin-vue @vue/eslint-config-typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin -
在项目根目录新建
eslint.config.js(ESLint 9 Flat Config,国内新版脚手架已默认启用)import js from '@eslint/js' import vue from 'eslint-plugin-vue' import ts from '@typescript-eslint/eslint-plugin' import tsParser from '@typescript-eslint/parser' import vueParser from 'vue-eslint-parser' export default [ js.configs.recommended, ...vue.configs['vue3-recommended'], { files: ['**/*.{js,ts,vue}'], languageOptions: { parser: vueParser, parserOptions: { parser: tsParser, // 处理 <script lang="ts"> ecmaVersion: 'latest', sourceType: 'module', project: './tsconfig.eslint.json', // 单独给 ESLint 用的 tsconfig,排除 dist }, globals: { defineProps: 'readonly', defineEmits: 'readonly', defineExpose: 'readonly', withDefaults: 'readonly', }, }, plugins: { '@typescript-eslint': ts, }, rules: { // 团队统一风格示例 'vue/multi-word-component-names': 'off', // 允许 index.vue '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }], }, }, { files: ['**/*.vue'], rules: { // 模板特定规则 'vue/html-self-closing': ['error', { html: { void: 'never' } }], }, }, ] -
配置
.eslintignoredist node_modules *.d.ts -
VS Code
settings.json(关闭 Vetur,启用 Volar + ESLint){ "eslint.validate": ["javascript", "typescript", "vue"], "volar.takeOverMode.enabled": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": true } } -
Git Hooks(Husky v9,国内镜像)
pnpm add -D husky lint-staged echo "export default { '*.{js,ts,vue}': 'eslint --cache --fix' }" > lint-staged.config.js husky install echo "pnpm lint-staged" > .husky/pre-commit -
CI 阶段(GitHub Actions / GitLab CI 同理)
script: - pnpm eslint . --ext .vue,.js,.ts --max-warnings 0 --format junit > eslint-report.xml artifacts: reports: junit: eslint-report.xml -
验证
在任意.vue文件写入<template><div>{{ a }}</div></template><script setup lang="ts">const a = 1</script>,保存后应无红色波浪线;git commit触发 Husky,若删除const a则提交被拦截并提示no-undef。
拓展思考
-
大型存量 Vue2 项目如何“灰度”升级到 Vue3 规则集?
可先对新增文件使用overrides匹配**/views/next-gen/**并应用plugin:vue/vue3-recommended,老模块保持plugin:vue/essential,通过eslint --report-unused-disable-directive逐步清理旧禁用注释,实现“增量规范”。 -
微前端场景下,主应用使用 Vue2,子应用使用 Vue3,CI 如何一次校验?
在根目录维护两份eslint.config.legacy.js与eslint.config.modern.js,CI 阶段分别执行ESLINT_USE_FLAT_CONFIG=true eslint -c eslint.config.legacy.js legacy/src与eslint -c eslint.config.modern.js modern/src,最后合并两份junit报告上传,确保门禁统一。 -
当项目同时包含 JSX 与 Vue SFC,如何防止解析器冲突?
对**/*.tsx文件单独写overrides,指定parser: '@typescript-eslint/parser',并关闭vueParser;同时开启eslint-plugin-react的react/jsx-uses-vars,避免no-unused-vars误报 JSX 组件。