news 2026/4/21 14:22:21

【前端】从零开始搭建现代前端框架:React 19、Vite、Tailwind CSS、ShadCN UI-第四章《统一的代码风格与严格的代码质量检查,ESLint 与 Prettier的配置 》

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【前端】从零开始搭建现代前端框架:React 19、Vite、Tailwind CSS、ShadCN UI-第四章《统一的代码风格与严格的代码质量检查,ESLint 与 Prettier的配置 》

第4章:统一的代码风格与严格的代码质量检查,为项目安装配置ESLint和Prettier

在现代前端项目中,ESLint 与 Prettier 的工程化整合非常关键,它决定了:

  • 团队代码是否统一

  • 自动化格式化是否生效

  • 是否能在 VSCode + Git Hooks 中自动检查

  • 是否能在持续集成(CI)中保证质量

本章将带你建立一套完全现代化的代码规范体系,基于:

  • ESLint 9(Flat Config)

  • Prettier 3

  • TypeScript

  • React 19

  • Tailwind CSS

  • ShadCN UI

最终项目具备:

✔ 统一代码风格
✔ VSCode 自动格式化
✔ Git 提交自动检查
✔ Tailwind class 排序
✔ import 顺序优化
✔ 生产工程可复用


4.1 为什么需要 ESLint 与 Prettier?

ESLint

用于检查代码中潜在的错误与不规范用法,例如:

  • 未使用的变量

  • React Hook 用法错误

  • 类型错误(TypeScript)

  • import 顺序问题

  • 逻辑潜在危险代码

Prettier

用于格式化代码,使团队代码风格统一,例如:

  • 换行策略

  • 引号单双

  • 尾随逗号

  • 缩进格式化

  • JSX 排版

为什么要一起使用?

ESLint 负责“正确性”,Prettier 负责“风格格式化”。
二者如果配置不当会冲突,需要通过插件让它们协同工作。


4.2 安装 ESLint 依赖

运行:

pnpm add -D eslint @eslint/js eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-react-refresh eslint-plugin-jsx-a11y @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-import eslint-import-resolver-typescript eslint-plugin-tailwindcss typescript

必要说明:

包名说明
eslint让 ESLint 能解析 TS/TSX
@eslint/jsTypeScript 规则集
typescript-eslint
eslint-plugin-reactReact 专用规则
eslint-plugin-react-hooksHook 规则(非常重要)
eslint-plugin-react-refreshVite react-refresh 热更新规则
eslint-plugin-react
eslint-plugin-importimport 语法检查
eslint-plugin-tailwindcssTailwind 类名排序与错误提示

4.3 安装 Prettier 及其插件

pnpm add -D prettier eslint-plugin-prettier eslint-config-prettier eslint-plugin-tailwindcss

说明:

包名功能
prettier代码格式化工具
eslint-plugin-prettier
eslint-config-prettier关闭 ESLint 中与 Prettier 冲突的规则
prettier-plugin-tailwindcssTailwind class 自动排序(强烈推荐)

4.4 修改eslint.config.js配置文件

在项目根目录找到:

.eslint.config.js

import js from '@eslint/js' import globals from 'globals' import reactHooks from 'eslint-plugin-react-hooks' import reactRefresh from 'eslint-plugin-react-refresh' import tseslint from 'typescript-eslint' import { defineConfig, globalIgnores } from 'eslint/config' export default defineConfig([ globalIgnores(['dist']), { files: ['**/*.{ts,tsx}'], extends: [ js.configs.recommended, tseslint.configs.recommended, reactHooks.configs.flat.recommended, reactRefresh.configs.vite, ], languageOptions: { ecmaVersion: 2020, globals: globals.browser, }, }, ])

改为:

import js from '@eslint/js' import globals from 'globals' import reactHooks from 'eslint-plugin-react-hooks' import reactRefresh from 'eslint-plugin-react-refresh' import tseslint from 'typescript-eslint' import pluginReact from 'eslint-plugin-react' import jsxA11y from 'eslint-plugin-jsx-a11y' import importPlugin from 'eslint-plugin-import' export default [ { ignores: ['dist'] }, js.configs.recommended, ...tseslint.configs.recommended, { files: ['**/*.{ts,tsx}'], plugins: { react: pluginReact, 'react-hooks': reactHooks, 'react-refresh': reactRefresh, 'jsx-a11y': jsxA11y, import: importPlugin }, languageOptions: { ecmaVersion: 2020, sourceType: 'module', globals: globals.browser, parser: tseslint.parser, parserOptions: { project: './tsconfig.app.json', ecmaFeatures: { jsx: true } } }, settings: { react: { version: 'detect' }, 'import/resolver': { typescript: { project: './tsconfig.app.json' } } }, rules: { // React 19 不需要 React in scope 'react/react-in-jsx-scope': 'off', // React Hook 规则 'react-hooks/rules-of-hooks': 'error', 'react-hooks/exhaustive-deps': 'warn', // TypeScript '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }], // import 规则 'import/order': [ 'warn', { groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'], alphabetize: { order: 'asc', caseInsensitive: true } } ] } } ]

4.5 创建.prettierrc

{ "semi": true, "singleQuote": true, "printWidth": 100, "tabWidth": 2, "trailingComma": "all", "jsxSingleQuote": false, "arrowParens": "always" }

4.6 创建.prettierignore

dist node_modules pnpm-lock.yaml .env* *.png *.svg

4.7 配置 VS Code 自动格式化(关键)

.vscode/settings.json中配置:

{ // Editor settings "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll": "explicit", "source.fixAll.eslint": "explicit" }, "editor.defaultFormatter": "esbenp.prettier-vscode", // ESLint settings "eslint.enable": true, "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact"], "eslint.run": "onType", "eslint.workingDirectories": [{ "mode": "auto" }] }

4.8package.json 脚本(lint + format)

package.json

{ "scripts": { // Lint 检查 "lint": "eslint \"src/**/*.{ts,tsx}\"", "lint:fix": "eslint \"src/**/*.{ts,tsx}\" --fix", // Prettier 格式化 "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,css,md,html,json}\"", "format:check": "prettier --check \"src/**/*.{ts,tsx,js,jsx,css,md,html,json}\"" } }

4.9 Tailwind 与 ESLint 的联动(自动排序 classNames)

以下规则来自eslint-plugin-tailwindcss

"tailwindcss/classnames-order": "warn",

效果:

<div className="p-4 flex bg-red-500 text-center" />

会自动变成:

<div className="flex p-4 bg-red-500 text-center" />

对 ShadCN UI 组件场景尤为重要。


4.10 Git 提交强制检查(可选)

如果你的工程使用 Husky + lint-staged:

安装

pnpm add -D husky lint-staged npx husky init

建立.lintstagedrc

{ "src/**/*.{ts,tsx}": [ "eslint --fix", "prettier --write" ] }

现在 commit 时会自动 lint + 格式化。


4.11 CI 检查(可选,用于 GitHub Actions)

- name: Lint run: pnpm lint - name: Prettier Check run: pnpm format:check

4.12 最终文件结构(新增部分)

. ├── .vscode/ │ └── settings.json ├── src/ ├── .lintstagedrc ├── .prettierrc ├── .prettierignore ├── .eslint.config.js ├── package.json └── ...

4.12 本章总结

本章构建了完整的现代化 ESLint + Prettier 工程体系:

✔ ESLint Flat Config
✔ TypeScript 全量规则
✔ React 19 最佳实践
✔ React Hooks / Refresh
✔ Tailwind class 排序
✔ import 顺序优化
✔ Prettier 集成
✔ VSCode 格式化
✔ Git 提交检查
✔ CI 流程

即日起你的项目将获得:

  • 统一的代码风格

  • 即时错误提示

  • 自动排版(含 Tailwind)

  • import 顺序自动规范化

  • React Hook 误用自动警告

这套体系完全适用于:

  • 企业级后台

  • 大型前端团队协作

  • 现代 SPA 工程

  • 现代 React(包括 Server Components)

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/20 10:51:05

区块链核心知识点梳理(7)-Gas 机制与优化

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录7. Gas 机制与优化7.1 Gas 基础原理7.1.1 为什么需要 Gas&#xff1f;7.1.2 Gas 计算规则7.2 EIP-1559 详解7.2.1 动态 BaseFee 机制7.2.2 Priority Fee&#xff08;…

作者头像 李华
网站建设 2026/4/17 20:59:20

[网鼎杯 2020 朱雀组]Nmap 1

Nmap 相关参数 -iL 读取文件内容&#xff0c;以文件内容作为搜索目标 -o 输出到文件 举例 nmap -iL ip_target.txt -o result.txt 扫描ip_target.txt内包含的ip地址&#xff0c;输出扫描结果至result.txt 读取文件结果 # Nmap 6.47 scan initiated Fri Dec 24 06:48:23 202…

作者头像 李华
网站建设 2026/4/20 5:24:30

从0到1搭建智能分析OBS埋点数据的AI Agent:实战指南

在数据驱动业务决策的时代&#xff0c;OBS埋点数据作为用户行为分析的核心资产&#xff0c;其价值挖掘却常因技术门槛陷入困境。传统分析流程中&#xff0c;工程师需手动解析表结构、编写SQL查询、生成可视化图表&#xff0c;不仅效率低下且难以支持灵活的探索式分析。本文将结…

作者头像 李华
网站建设 2026/4/16 13:56:23

ABB机器人系统自带调试例行程序介绍

ABB机器人系统自带调试例行程序介绍1.AxisCalibration (轴校准)功能&#xff1a;用于校准机器人每个轴的“偏置”或“零点位置”。机器人在出厂时&#xff0c;每个关节轴都有一个机械的零点标记&#xff0c;此程序就是通过示教器操作&#xff0c;将软件中的零点位置与机械标记对…

作者头像 李华
网站建设 2026/4/21 11:25:21

Brian2终极指南:免费开源神经网络模拟器快速上手

Brian2终极指南&#xff1a;免费开源神经网络模拟器快速上手 【免费下载链接】brian2 Brian is a free, open source simulator for spiking neural networks. 项目地址: https://gitcode.com/gh_mirrors/br/brian2 你知道吗&#xff1f;在神经科学和人工智能研究领域&…

作者头像 李华
网站建设 2026/4/21 8:49:40

24、Linux系统设备管理与任务调度全解析

Linux系统设备管理与任务调度全解析 1. /proc文件系统与内核版本 在Linux系统中,/proc文件系统是一个特殊的文件系统,它提供了对内核数据的访问。通过 /proc/version ,可以查看内核版本号。你可以像操作其他目录和文件一样在 /proc 文件系统中导航,使用 more 或 c…

作者头像 李华