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"
  }
}
相关推荐
程序员大阳8 分钟前
副队长JavaScript教程(52)-DOM操作元素的内容
javascript·dom·innertext·innerhtml·value·内容操作
山雀~1 小时前
CSP 安全漏洞修复
前端·网络协议
裕波1 小时前
苏千|从 cnpm 到 Vite+:AI 时代的开发者体验演进
javascript
console.log('npc')2 小时前
周期任务功能完整实现:CRON 编辑器 + 模板变量 + 执行历史
javascript·vue.js·编辑器
程序员大阳2 小时前
副队长JavaScript教程(58)-基于DOM的网页留言板开发
javascript·实例·dom·案例·留言板
陌离2 小时前
Go-Gui:一套 无限接近 现代化UI的 GPU绘制界面组件框架
前端
咚咚咚小柒2 小时前
【前端】LangChain.js学习
前端·javascript·人工智能·typescript·langchain·vue·ai编程
2401_885885042 小时前
巴林语音通知接口如何对接?系统告警语音API接入教程
android·前端·网络·python·语音识别
java1234_小锋2 小时前
【技术专题】CSS3 网页技术 - CSS3 盒模型
前端·css·css3
计算机魔术师2 小时前
手握芯片、又要吞下开源分发入口,英伟达被盯上了
前端