vue3+ts+vite搭建脚手架(二)配置eslint&prettier

我们用vite创建好的脚手架是十分纯净的,我们可以自由配置一些自己想要的东西

1.安装 eslint 依赖

javascript 复制代码
npm i eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin @vue/eslint-config-typescript -D
  • eslint:ESLint 的核心包。
  • eslint-plugin-vue:用于 Vue 文件的 ESLint 插件。
  • @typescript-eslint/parser:解析 TypeScript 文件。
  • @typescript-eslint/eslint-plugin:为 TypeScript 提供 ESLint 插件。
  • @vue/eslint-config-typescript

2.安装 prettier 依赖

javascript 复制代码
npm i prettier @vue/eslint-config-prettier eslint-plugin-prettier -D
  • prettier:代码格式化工具。
  • eslint-config-prettier:禁用所有与 Prettier 格式化规则相冲突的 ESLint 规则。
  • eslint-plugin-prettier:将 prettier 作为 ESLint 规范来使用。

3.配置eslint、prettier相关文件

.eslintrc.js

javascript 复制代码
module.exports = {
  root: true,
  parser: 'vue-eslint-parser',
  parserOptions: {
    parser: '@typescript-eslint/parser',
    sourceType: 'module'
  },
  extends: [
    'plugin:vue/vue3-recommended',
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier',
    'plugin:prettier/recommended'
  ],
  rules: {
    'no-var': 'error',
    semi: 'off',
    '@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
    '@typescript-eslint/no-explicit-any': 'off',
    '@typescript-eslint/no-var-requires': 0,
    '@typescript-eslint/no-require-imports': 'off',
    'vue/multi-word-component-names': 'off',
    'no-async-promise-executor': 'off',
    '@typescript-eslint/no-empty-object-type': 'off',
    'no-undef': 'off',
    '@typescript-eslint/no-unused-vars': [
      'error',
      {
        vars: 'all',
        args: 'after-used',
        ignoreRestSiblings: false
      }
    ],
    'vue/html-indent': [
      'error',
      2,
      {
        attribute: 1,
        baseIndent: 1,
        closeBracket: 0,
        alignAttributesVertically: true,
        ignores: []
      }
    ],
    'vue/max-attributes-per-line': ['off'],
    'vue/no-setup-props-destructure': 'off',
    camelcase: ['error', { properties: 'always' }]
  }
}

.prettierrc.json

javascript 复制代码
{
  "semi": false,
  "tabWidth": 2,
  "singleQuote": true,
  "printWidth": 100,
  "trailingComma": "none"
}

package.json文件添加两个脚本,用来在编译前检查一下代码

javascript 复制代码
 "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
 "prettier": "prettier --write src/"

package.json

javascript 复制代码
{
  "name": "vite-project",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc -b && vite build",
    "preview": "vite preview",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
    "prettier": "prettier --write src/"
  },
  "dependencies": {
    "axios": "^1.7.7",
    "lodash": "^4.17.21",
    "mockjs": "^1.1.0",
    "vue": "^3.4.37",
    "vue-router": "^4.4.3"
  },
  "devDependencies": {
    "@types/lodash": "^4.17.7",
    "@types/node": "^22.5.4",
    "@typescript-eslint/eslint-plugin": "^8.5.0",
    "@typescript-eslint/parser": "^8.5.0",
    "@vitejs/plugin-vue": "^5.1.2",
    "@vue/eslint-config-prettier": "^9.0.0",
    "@vue/eslint-config-typescript": "^13.0.0",
    "autoprefixer": "^10.4.20",
    "eslint": "8.57.0",
    "eslint-define-config": "^2.1.0",
    "eslint-plugin-prettier": "^5.2.1",
    "eslint-plugin-vue": "^9.28.0",
    "path": "^0.12.7",
    "postcss": "^8.4.45",
    "prettier": "^3.3.3",
    "tailwindcss": "^3.4.10",
    "typescript": "^5.5.3",
    "vite": "^5.4.1",
    "vite-plugin-mock": "^3.0.2",
    "vue-tsc": "^2.0.29"
  }
}
相关推荐
Mh4 小时前
我是如何设计品牌站组件库的?
前端·css·设计模式
程序员大阳4 小时前
副队长Vue3教程(2)--Vite 搭建 Vue 项目,认识目录结构
前端·javascript·vue.js·vite·创建项目·目录结构·项目结构
濮水大叔5 小时前
Cabloy/Zova 与 Next.js 的 SSR 水合差异
前端·typescript·next.js
SoaringHeart5 小时前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
Flynt6 小时前
Chrome给AI编程助手塞了203篇前端规范,我拆包实测:检索0.77秒是真快,扫描器有点挑食
前端·chrome·ai编程
北城笑笑7 小时前
Vue 110 ,若依SSE流式接口限次空500排查与解决
前端·javascript·vue.js·后端·vue
程序员韩星7 小时前
OpenAI 兼容接口返回 404,先查请求地址,再查模型名
前端·人工智能·http
Patrick_Wilson7 小时前
构建通过了,为什么有些浏览器还会白屏?
前端·javascript·浏览器
其实防守也摸鱼8 小时前
用 Claude Code 重构遗留系统:从评估到落地的完整指南
前端·网络·人工智能·chrome·安全·重构
七仔啊8 小时前
CNC计算机数控加工模拟器
前端