使用步骤
- 使用 npm 安装 @vue/cli-service 版本对应的 @vue/cli-plugin-eslint
- 例如:"@vue/cli-service": "~4.5.0" 对应的是 "@vue/cli-plugin-eslint": "^4"
npm install -D @vue/cli-plugin-eslint@^4
- 通过命令
vue invoke eslint
运行插件
- vue 命令是 @vue/cli 提供的
- 运行插件应该可以使用任意版本的 @vue/cli 提供的 vue 命令
- 运行插件会根据选择项更新适用依赖和修改配置文件
- 这种修改会因为插件安装顺序不同而不同,可能潜在错误,下文提供了不同版本的配置文件,可比较后进行修改
- eslint 插件运行时的可选项
- Pick an ESLint config:选择内置的eslint规则
- Error prevention only:仅仅显示错误
- Airbnb:爱彼迎的规则
- Standard:标准的规则
- Prettier:Prettier的规则(建议选这个,eslint 默认只能对 js 进行检查和格式化,Prettier提供了对 html 和 css 的支持)
- Pick additional lint features:选择特征,可多选
- Lint on save:保存时进行检查,似乎没有作用
- Lint and fix on commit:在提交代码时进行检查(建议勾选,强制提交的代码符合检查规则)
- 插件后续钩子函数
- 插件后续钩子会对所有文件根据 eslint 规则进行格式化
- 基于代码安全考虑,建议恢复被格式化的文件
- 在需要修改到某个文件时才对文件进行格式化,让测试人员能够对格式化后的代码提供一层保障
- 配置 vscode 保存时格式化代码
- 插件会生成 git 暂存时格式化代码的钩子
- 格式化程序由 eslint 提供,只能够对部分问题进行处理,且可能存在 bug
- 它通过 package.json 中 gitHooks、lint-staged 配置项实现
- 使用 eslint 插件是为帮助开发人员发现问题,并统一格式,方便查看每次提交的修改地方。
- 使用暂存时格式化将会削弱这种功能,建议关闭暂存时格式化功能,修改为保存时格式化
- 关闭暂存时格式化,只保留暂存强制检查
vue-cli-service lint
修改为vue-cli-service lint --no-fix --max-warnings=0
- --no-fix:声明 eslint 不进行格式化处理
- --max-warnings=0:
vue-cli-service lint
默认允许 warning 类型错误存在,为了严格限制提交的代码都被格式化,建议设置为 0
- 使 vscode 在保存时进行格式化
- 为 vsocde 安装插件 eslint
- 插件只是启动器,实际是运行 node_modules 中的 eslint 依赖作为处理程序
- 进行 vscode 的工作区配置,该配置可以随代码上传,被保存在 /.vscode/settings.json 中,内容如下
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"vetur.format.enable": false,
"typescript.format.enable": false
}
疑难杂症
- Delete ␍(eslintprettier/prettier)修复
- vue/cli 推荐的行尾序列为 LF
- 行尾序列有 CRLF 和 LF 两种,CRLF 是 window 系统历史遗留的换行符,LF目前更通用
- 点 vsocde 右下角的 CRLF 标志可进行当前文件的行尾序列切换
- git version 2.35.1.windows.2 在推送代码时会自动把行尾序列切换为 LF,当拉取代码到本地时会根据当前操作系统,自动把文件转换为 CRLF 或 LF。所以 windows 系统会出现 Delete ␍(eslintprettier/prettier) 无法修复的问题。
- 可以通过添加 .gitattributes 配置文件,声明在拉取到本地时使用的行尾序列为 LF。
- 但不建议这么做,实际运用中发现配置后 git 提交出现未修改得文件被识别为已修改,且无法提交
- 建议不对行尾序列做限定
相关文件
3.0
module.exports = {
root: true,
env: {
node: true
},
extends: ["plugin:vue/essential", "@vue/prettier"],
rules: {
"no-console": process.env.NODE_ENV === "production" ? "error" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
},
parserOptions: {
parser: "babel-eslint"
}
};
...
"devDependencies": {
"@vue/cli-plugin-babel": "^3.12.0",
"@vue/cli-service": "^3.12.0",
"sass": "^1.19.0",
"sass-loader": "^8.0.0",
"vue-template-compiler": "^2.6.10",
"@vue/cli-plugin-eslint": "^3.12.0",
"eslint": "^5.16.0",
"babel-eslint": "^10.0.1",
"prettier": "^1.18.2",
"lint-staged": "^8.1.5",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-vue": "^5.0.0",
"@vue/eslint-config-prettier": "^5.0.0"
},
...
4.0
module.exports = {
root: true,
env: {
node: true,
},
extends: ["plugin:vue/essential", "eslint:recommended", "@vue/prettier"],
parserOptions: {
parser: "babel-eslint",
},
rules: {
"no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
},
};
...
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.19",
"@vue/cli-plugin-router": "~4.5.19",
"@vue/cli-plugin-vuex": "~4.5.19",
"@vue/cli-service": "~4.5.19",
"sass": "^1.26.5",
"sass-loader": "^8.0.2",
"vue-template-compiler": "^2.6.11",
"@vue/cli-plugin-eslint": "~4.5.19",
"eslint": "^6.7.2",
"babel-eslint": "^10.1.0",
"prettier": "^2.2.1",
"lint-staged": "^9.5.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^6.2.2",
"@vue/eslint-config-prettier": "^6.0.0"
},
...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端