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

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

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

相关推荐
C澒9 小时前
React + TypeScript 编码规范|统一标准 & 高效维护
前端·react.js·typescript·团队开发·代码规范
nix.gnehc12 小时前
探索 OpenClaw:为什么现代AI助手青睐 TypeScript + Node.js?
typescript·node.js
行者-全栈开发15 小时前
43 篇系统实战:uni-app 从入门到架构师成长之路
前端·typescript·uni-app·vue3·最佳实践·企业级架构
阿诺木15 小时前
pnpm Workspaces Monorepo 生产环境踩坑实录
typescript
zhensherlock16 小时前
Protocol Launcher 系列:一键唤起 Windsurf 智能 IDE
javascript·ide·vscode·ai·typescript·github·ai编程
十步杀一人_千里不留行16 小时前
TypeScript 里的 Type Guard 是什么
javascript·ubuntu·typescript
We་ct17 小时前
LeetCode 211. 添加与搜索单词 - 数据结构设计:字典树+DFS解法详解
开发语言·前端·数据结构·算法·leetcode·typescript·深度优先
zhensherlock18 小时前
Protocol Launcher 系列:一键唤起 VSCodium 智能 IDE
javascript·ide·vscode·typescript·开源·编辑器·github
朱华飞Pro18 小时前
vue2 精细级别判断图片页面
vue·页面优化
阿懂在掘金18 小时前
Vue Asyncx 库三周年,回顾起源时的三十行代码
前端·typescript·开源