在vscode编辑器当中使用vite框架,配置eslint:
首先在vite项目中,安装eslint-plugin-vue
依赖
npm install --save-dev eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue eslint-plugin-html prettier @babel/plugin-syntax-dynamic-import babel-eslint
or
yarn add eslint eslint-config-prettier eslint-plugin-prettier eslint-plugin-vue eslint-plugin-html prettier --dev
在根目录下创建.eslintrc.js
.eslintrc.js配置如下
module.exports = {
root: true,
env: {
node: true,
'vue/setup-compiler-macros': true
},
extends: ['eslint:recommended', 'plugin:vue/vue3-recommended', 'prettier'],
plugins: ['vue', 'html', 'prettier'],
parserOptions: {
ecmaVersion: 8
},
rules: {
'prettier/prettier': 'error',
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'vue/no-multiple-template-root': 'off',
'vue/multi-word-component-names': 'off',
'no-mutating-props': 'off',
'vue/no-v-html': 'off'
},
overrides: [
{
files: [
'**/__tests__/*.{j,t}s?(x)',
'**/tests/unit/**/*.spec.{j,t}s?(x)'
],
env: {
mocha: true
}
}
]
};
在根目录下创建.prettierrc
{
"semi": true,
"endOfLine": "auto",
"singleQuote": true,
"trailingComma": "none"
}
vscode编辑器配置
{
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.fixAll.eslint": true
},
"eslint.alwaysShowStatus": true,
"eslint.validate": [
"vue",
"javascript",
"javascriptreact",
"html",
],
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"eslint.options": {
"overrideConfig": {
"env": {
"browser": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"no-debugger": "off"
}
}
},
}
完成以上配置后在vite框架下就可以使用eslint+prettier进行格式化校验了,但是如果创建路由进行懒加载的话
会出现这么一个错误。
许多网上教程说安装babel-eslint插件并且,在 .eslintrc.js配置中加入

这是虽然解决了路由懒加载那里的错误,但是在出现import的地方都会出现新的错误
所以,干脆直接忽略掉路由文件夹下的校验,具体做法是在根目录下创建 .eslintignore文件,在该文件中加入router,忽略掉router文件夹下的所有校验。
重启vscode编辑器。
浏览器界面显示 ESLint的 错误
对于大多数开发人员和团队来说,我们目前的设置可能足以提高生产力。但是,如果您想更进一步,您可以安装 Vite ESLint 插件以在浏览器中查看覆盖在您的应用程序上的 ESLint 问题。
使用 Vite 在浏览器中覆盖 ESLint 错误
这使得那些无法自动修复的 ESLint 错误无法被忽略。我知道一些喜欢这个的开发人员和其他人觉得它超级烦人,所以如果你愿意就安装它,否则只要确保你特别注意你的 IDE 中的那些红色曲线。
vite-plugin-eslint
通过运行安装
npm install vite-plugin-eslint --save-dev
然后通过导入插件注册插件并将其添加为插件vite.config.js
import { defineConfig } from 'vite';
import eslintPlugin from 'vite-plugin-eslint';
export default defineConfig({
plugins: [eslintPlugin()],
});
就是这样,任何 ESLint 错误现在都会在浏览器中报告!如果它不适合您,请尝试重新启动development server。