vue3+TS项目配置Eslint+prettier+husky语法校验

vue3+TS项目配置Eslint+prettier+husky语法校验

本文配置了Eslint+prettier+husky。其中ESLint 负责代码质量检查,Prettier 负责代码格式统一,Husky 通过 Git 钩子在提交时自动执行检查与格式化,三者结合确保代码规范、风格一致且无低级错误。

1. Eslint

(1)在项目中执行npx eslint --init

你可能看见的问题

shell 复制代码
What do you want to lint?	 >> JavaScript
How would you like to use ESLint?	>> To check syntax and find problems
What type of modules does your project use?		>> JavaScript modules (import/export)
Which framework does your project use? 		 >> Vue.js
Does your project use TypeScript? >>  Yes
Where does your code run? 	>> √ Browser  √ Node	(空格多选)
Which language do you want your configuration file be written in? 	>>JavaScript
Would you like to install them now? >> Yes
Which package manager do you want to use?	>> npm

(2)修改生成的eslint.config.js

ts 复制代码
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
import { defineConfig } from "eslint/config";

export default defineConfig([
  {
    // 需要校验的文件
    files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
    plugins: { js },
    extends: ["js/recommended"],
    languageOptions: { globals: { ...globals.browser, ...globals.node } }
  },
  tseslint.configs.recommended,
  pluginVue.configs["flat/essential"],
  {
    // vue 文件中的ts语法校验
    files: ["**/*.vue"],
    languageOptions: { parserOptions: { parser: tseslint.parser } }
  },
  {
    // 忽略校验
    ignores: [".css", "*.d.ts", "**/node_modules/**"]
  },
  {
    // 自定义eslint校验规则
    rules: {
      "no-console": "warn"
    }
  }
]);

(3)修改package.json中的scripts

json 复制代码
{
	"scripts": {
	    "dev": "vite",
	    "build": "vue-tsc -b && vite build",
	    "preview": "vite preview",
	    "lint": "eslint",
	    "lint:fix": "eslint --fix --quiet",
	  }
}

(4)vscode中下载ESlint插件

2. Prettier

(1)安装依赖

sh 复制代码
npm install prettier eslint-plugin-prettier eslint-config-prettier -D

(2)项目根目录(与vite.config.ts同级)新建文件prettier.config.js

js 复制代码
export default {
  singleQuote: false, // 使用单引号
  semi: true, // 末尾使用分号
  trailingComma: "none", // 使用尾随逗号
  printWidth: 120, // 每行代码的长度
  tabWidth: 2, // 缩进宽度
  useTabs: false, // 使用制表符缩进
  endOfLine: "auto"
};

(3)修改eslint.config.js

js 复制代码
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
import { defineConfig } from "eslint/config";
import prettierRecommended from "eslint-plugin-prettier/recommended";

export default defineConfig([
  {
    // 需要校验的文件
    files: ["**/*.{js,mjs,cjs,ts,mts,cts,vue}"],
    plugins: { js },
    extends: ["js/recommended"],
    languageOptions: { globals: { ...globals.browser, ...globals.node } }
  },
  tseslint.configs.recommended,
  pluginVue.configs["flat/essential"],
  {
    // vue 文件中的ts语法校验
    files: ["**/*.vue"],
    languageOptions: { parserOptions: { parser: tseslint.parser } }
  },
  {
    // 忽略校验
    ignores: [".css", "*.d.ts", "**/node_modules/**"]
  },
  {
    // 自定义eslint校验规则
    rules: {
      "no-console": "warn"
    }
  },
  // 集成prettier
  prettierRecommended
]);

(4)下载vscode插件Prettier - Code formatter

(5)vscode中设置保存自动修复代码

3. husky

(1)安装依赖

sh 复制代码
npm install husky lint-staged -D

(2)修改package.json

json 复制代码
{
	"scripts":{...},
	"lint-staged": {
        "src/**/*.{js,cjs,ts,vue}": [
            "npm run lint:fix"
        ],
        "src/**/*.{html,json,css,scss}": [
            "npx prettier --write"
        ]
    }
}

(3)初始化husky,会生成一个.husky的文件夹

sh 复制代码
npx husky init

修改文件夹中的pre-commit文件

复制代码
npx lint-staged

(4)新建文件commitlint.config.cjs(与vite.config.ts同级)

cjs 复制代码
module.exports = {
  extends: ["@commitlint/config-conventional"]
};

(5)配置commit信息校验

sh 复制代码
npm install @commitlint/cli @commitlint/config-conventional -D

(6)在pre-commit同级创建commit-msg文件,并修改文件内容

复制代码
npx commitlint --edit $1
相关推荐
marsh02064 分钟前
49 openclaw故障排查:系统异常时的诊断方法
服务器·前端·青少年编程·ai·php·技术美术
Maimai108085 分钟前
前端如何落地 SSE:从实时评论到可复用的实时数据 Hook
前端·javascript·react.js·前端框架·web3·状态模式·webassembly
冴羽17 分钟前
JavaScript 9 个先有库再有 API 的故事
前端·javascript
欧阳天风19 分钟前
vue+vite生产环境更新提示
前端·javascript·vue.js
靠谱品牌推荐官23 分钟前
【架构实战】如何设计一套原生支持 GEO 大模型爬虫语义索引的 HTML5/CSS3 纯净白盒前端架构?
前端·爬虫·架构
谢小飞44 分钟前
Three.js三球轮播沉浸式落地页开发
前端·three.js
之歆1 小时前
DAY_14JavaScript DOM 进阶:HTML DOM 接口、事件监听与经典交互实战
开发语言·前端·javascript·html·ecmascript·交互
江南十四行1 小时前
从Web开发到网络通信的知识梳理
前端
肖老师xy1 小时前
Vue3+OpenStreetMap实现地理围栏
前端
KaMeidebaby1 小时前
卡梅德生物技术快报|Fab 抗体文库构建标准化实验流程与数据复盘
服务器·前端·数据库·人工智能·算法