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

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

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

相关推荐
今天不要写bug5 小时前
vue项目基于vue-cropper实现图片裁剪与图片压缩
前端·javascript·vue.js·typescript
ttod_qzstudio9 小时前
Vue 3 + TypeScript 严格模式下的 Performance.now() 实践:构建高性能前端应用
typescript·performance
是梦终空9 小时前
计算机毕业设计252—基于Java+Springboot+vue3+协同过滤推荐算法的农产品销售系统(源代码+数据库+2万字论文)
java·spring boot·vue·毕业设计·源代码·协同过滤算法·农产品销售系统
王林不想说话10 小时前
受控/非受控组件分析
前端·react.js·typescript
Beginner x_u14 小时前
从组件点击事件到业务统一入口:一次前端操作链的完整解耦实践
前端·javascript·vue·业务封装
by__csdn16 小时前
大前端:定义、演进与实践全景解析
前端·javascript·vue.js·react.js·typescript·ecmascript·动画
苏打水com18 小时前
第十篇:Day28-30 工程化优化与部署——从“能跑”到“好用”(对标职场“项目上线”需求)
前端·css·vue·html·js
武哥聊编程19 小时前
【从0带做】基于Springboot3+Vue3的生态养殖管理系统
java·学习·vue·毕业设计·springboot
by__csdn19 小时前
JavaScript性能优化实战:异步与延迟加载全方位攻略
开发语言·前端·javascript·vue.js·react.js·typescript·ecmascript
doupoa19 小时前
VitePressv2.0 + TailwindCSSv4.1 集成方案
typescript·前端框架·json·html5·postcss