Vue typescript项目配置eslint+prettier

前言

本文基于

  • "vite": "^5.0.0"

1.安装依赖

安装 eslint

javascript 复制代码
yarn add eslint --dev

安装 eslint-plugin-vue

javascript 复制代码
yarn add eslint-plugin-vue --dev

主要用于检查 Vue 文件语法

安装 prettier 及相关插件

javascript 复制代码
yarn add prettier eslint-config-prettier eslint-plugin-prettier --dev

安装 typescript 解析器、规则补充

javascript 复制代码
yarn add @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev

2.根目录创建 .eslintrc.cjs

javascript 复制代码
module.exports = {
  env: { browser: true, es2020: true },
  extends: [
    'eslint:recommended',
    'plugin:vue/vue3-recommended',
    'plugin:@typescript-eslint/recommended',
    'eslint-config-prettier',
    'plugin:prettier/recommended'
  ],
  parser: 'vue-eslint-parser',
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    parser: '@typescript-eslint/parser'
  },
  plugins: ['vue', 'prettier'],
  rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
  }
};

3.根目录创建 .prettierrc.cjs

javascript 复制代码
module.exports = {
  // 字符串是否使用单引号
  singleQuote: true,
  // 大括号首尾是否需要空格
  bracketSpacing: true,
  // 对象末尾是否需要逗号
  trailingComma: 'none',
  // 箭头函数参数括号(1个参数不需要, 1个以上需要)
  arrowParens: 'avoid',
  // 折行标准(默认)
  proseWrap: 'preserve',
  // 根据显示样式决定html是否折行
  htmlWhitespaceSensitivity: 'css',
  // 换行符(crlf/lf/auto)
  endOfLine: 'auto'
};

4.配置 package.json 的 scripts 字段

javascript 复制代码
"scripts": {
   ...
   "lint": "eslint . --ext vue,ts --report-unused-disable-directives --max-warnings 0"
}

5.测试配置

javascript 复制代码
yarn lint

如果本篇文章对你有帮助的话,很高兴能够帮助上你。

当然,如果你觉得文章有什么让你觉得不合理、或者有更简单的实现方法又或者有理解不来的地方,希望你在看到之后能够在评论里指出来,我会在看到之后尽快的回复你。

相关推荐
FungLeo7 小时前
成为全栈·Vue 管理后台篇·Biome、类型检查、构建和API 代理如何组成质量门禁
前端·javascript·vue.js·typescript·vite·前端工程化·biome
泯泷1 天前
改一个字,整段都红了?——中文内容差异对比的算法与协议设计
前端·javascript·typescript
4cos901 天前
pi学习笔记-AgentLoop
typescript
afumu2 天前
让本机 Agent 指挥浏览器里的多个网页 AI:一套 127.0.0.1 控制协议的设计取舍
chrome·typescript
濮水大叔3 天前
Cabloy/Vona 为什么不需要 NestJS 的 Dynamic Module
typescript·node.js·nestjs
AI源码3 天前
高校查分系统
vue
浪遏4 天前
recruitment-cli:让 AI 接手求职招聘,从架构到一次发送超时
架构·typescript·node.js
律宏阔4 天前
微信小程序 + Taro 从 ESLint 迁移到 Oxlint 让项目代码检查速度提升近 12 倍
前端·typescript·eslint
瓦瓦gogo4 天前
从一个 TypeScript 函数开始理解 MCP 协议
linux·ubuntu·typescript
ZhengEnCi5 天前
Hono 4 多库多模型服务的企业级 Monorepo 目录结构:npm workspaces 与 Turborepo 该怎么分层
typescript