React Vite 项目增加 eslint 和 prettier

ops/2025/2/12 5:21:07/

prettier_0">React Vite 项目增加 eslint 和 prettier

Eslint__8X_2">Eslint 版本为 8.X

1. 安装 8.X 版本的 eslint

pnpm i eslint@^8.57.0 -D    

2. 安装其他包

pnpm add -D eslint-plugin-import prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-prettier eslint-config-prettier @typescript-eslint/eslint-plugin @typescript-eslint/parser

3. 初始化 eslint

npx eslint --init 
3.1. 选择 eslint 的校验模式

选择第三个

3.2. 选择项目类型

选择第一个 ESM 规范

3.3. 选择项目框架

我们是 React,选择第一个

3.4. 是否使用 TS

项目中建议使用 TS

3.5. 运行在哪?

我这个在浏览器

3.6. 项目风格?

选择第二个

3.7. 选择 config 文件类型

我这边选择的是 JS

3.8. 缩进

tabs

3.9. 单引号双引号

3.10. 行尾?

3.11. 是否需要分号?

习惯了不要分号

3.12. 添加依赖

YES

3.13. 安装方式

pnpm 更快一些

4. .eslintrc.cjs 文件

把默认生成的替换为以下内容

javascript">module.exports = {root: true,globals: {chrome: true // 保留对 Chrome 扩展的支持},env: {browser: true,node: true,es2021: true},parser: '@typescript-eslint/parser', // 使用 TypeScript 解析器parserOptions: {ecmaVersion: 'latest',sourceType: 'module',ecmaFeatures: {jsx: true // 启用 JSX 支持}},extends: ['eslint:recommended', // 基本 ESLint 推荐配置'plugin:react/recommended', // React 推荐配置'plugin:react-hooks/recommended', // React Hooks 推荐配置'plugin:@typescript-eslint/recommended', // TypeScript 推荐配置'prettier', // 使用 Prettier'plugin:prettier/recommended' // 确保 Prettier 配置生效],plugins: ['react', 'react-hooks', '@typescript-eslint', 'import'], // 加载相关插件settings: {react: {version: 'detect' // 自动检测 React 版本}},rules: {'react/react-in-jsx-scope': 'off', // React 17+ 不需要显式导入 React'react/prop-types': 'off', // 如果使用 TypeScript,则不需要 PropTypes'@typescript-eslint/no-empty-function': 0,'@typescript-eslint/no-empty-interface': 0,'@typescript-eslint/no-explicit-any': 0,'@typescript-eslint/no-var-requires': 0,'@typescript-eslint/no-non-null-assertion': 0,'@typescript-eslint/no-unused-expressions': 'off','semi': ['error', 'never'], // 禁止分号'prettier/prettier': 'error', // 强制使用 Prettier 格式化// 导入排序规则'import/order': ['error',{'newlines-between': 'never',groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],pathGroups: [{pattern: 'react',group: 'external',position: 'before'},{pattern: 'react-dom',group: 'external',position: 'before'},{pattern: '@/components/**',group: 'internal',position: 'before'},{pattern: '@/utils/**',group: 'internal',position: 'before'}],pathGroupsExcludedImportTypes: ['react', 'react-dom']}]},overrides: [{files: ['*.tsx', '*.jsx'], // 对 .tsx 和 .jsx 文件的特殊规则rules: {'react/prop-types': 'off' // TS 文件通常不需要 PropTypes}}]
}

5. .eslintignore 文件

根据项目自己添加过滤文件

node_modules/
dist/
build/
.vscode
.idea
.husky
*.json
*.config.js

prettierrcjs_font_183">6. .prettierrc.js 文件

javascript">/*** @type {import('prettier').Options}*/
export default {printWidth: 80,tabWidth: 2,useTabs: false,semi: false,singleQuote: true,trailingComma: "none",bracketSpacing: true,bracketSameLine: true,plugins: ["@ianvs/prettier-plugin-sort-imports"],importOrder: ["<BUILTIN_MODULES>", // Node.js built-in modules"<THIRD_PARTY_MODULES>", // Imports not matched by other special words or groups."", // Empty line"^@plasmo/(.*)$","","^@plasmohq/(.*)$","","^~(.*)$","","^[./]"]
}

prettierrcjson_font_214">7. .prettierrc.json 文件

javascript">{"$schema": "https://json.schemastore.org/prettierrc","semi": false,"tabWidth": 2,"singleQuote": true,"printWidth": 100,"trailingComma": "none"
}

prettierignore_font_226">8. .prettierignore 文件

# 忽略格式化文件 (根据项目需要自行添加)
node_modules/
dist/
build/
*.config.js

9. .vscode/extensions.json 文件

{"recommendations": ["Vue.vscode-typescript-vue-plugin", "antfu.iconify", // iconify 图标显示"antfu.unocss", // unocss 代码提示"christian-kohler.path-intellisense", // 文件路径提示/补全"dbaeumer.vscode-eslint", // eslint"esbenp.prettier-vscode", // prettier"eamodio.gitlens", // git"editorconfig.editorconfig", // editorconfig"mikestead.dotenv", // .env支持"naumovs.color-highlight", // 颜色高亮"steoates.autoimport","vue.volar" // vue3]
}

10. .vscode/settings.json 文件

{"editor.codeActionsOnSave": {"source.fixAll.eslint": "explicit"},"editor.fontLigatures": true,"editor.formatOnSave": false,"editor.guides.bracketPairs": "active","editor.quickSuggestions": {"strings": true},"eslint.enable": true,"editor.tabSize": 2,"eslint.validate": ["javascript","javascriptreact","typescript","typescriptreact","vue","html","json","jsonc","json5","yaml","yml","markdown"],"files.associations": {"*.env.*": "dotenv"},"files.eol": "\n","path-intellisense.mappings": {"@": "${workspaceFolder}/src","~@": "${workspaceFolder}/src"},"[html]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[json]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[jsonc]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascript]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[javascriptreact]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[markdown]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[typescript]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[typescriptreact]": {"editor.defaultFormatter": "esbenp.prettier-vscode"},"[vue]": {"editor.defaultFormatter": "Vue.volar"}
}

http://www.ppmy.cn/ops/157699.html

相关文章

数据库约束(2)

数据库约束(2) 1.检查约束 检查约束时用来检查数据表中字段值有效性的一种手段&#xff0c;可以通过create table或者alter table语句实现。设置检查约束时要根据实际情况进行设置&#xff0c;这样能够减少无效数据的输入。 CHECK 表达式在更新表数据的时候&#xff0c;系统…

从零构建高可用MySQL集群:Percona XtraDB Cluster 实战部署

实战指南&#xff1a;基于Percona XtraDB Cluster 构建高可用 MySQL 集群架构 引言&#xff1a;为什么选择PXC&#xff1f; Percona XtraDB Cluster&#xff08;PXC&#xff09;是基于Galera协议的MySQL高可用解决方案&#xff0c;提供同步多主复制、数据强一致性等关键特性&…

光伏设计软件分类:无人机、Unity3D引擎齐上阵

无人机3D设计 无人机可搭载高分辨率光学相机、激光雷达等测绘设备&#xff0c;对目标区域进行全方位、多角度的航拍作业。通过对采集到的影像数据进行导入处理&#xff0c;运用复杂的图像识别算法与三维重建技术&#xff0c;构建出云端实景3D模型&#xff0c;在实景3D模型中进…

Linux:线程的互斥与同步

一、买票的线程安全 大部分情况&#xff0c;线程使用的数据都是局部变量&#xff0c;变量的地址空间在线程栈空间内&#xff0c;这种情况&#xff0c;变量归属单个线程&#xff0c;其他线程无法获得这种变量。 但有时候&#xff0c;很多变量都需要在线程间共享&#xff0c;这样…

tomcat如何配置保存7天滚动日志

在 Tomcat 中&#xff0c;logging.properties 文件是用于配置 Java 日志框架&#xff08;java.util.logging&#xff09;的。若要实现 catalina.out 日志保存 7 天&#xff0c;且每天的日志文件名带有时间戳&#xff0c;可以按以下步骤进行配置&#xff1a; 1. 备份原配置 在修…

数据可视化基本套路总结

首先从维基百科上搬出数据可视化的概念&#xff1a; 数据可视化是关于数据之视觉表现形式的研究&#xff1b;其中&#xff0c;这种数据的视觉表现形式被定义为一种以某种概要形式抽提出来的信息&#xff0c;包括相应信息单位的各种属性和变量。 用人话简单来说&#xff0c;数据…

SQL自学,mysql从入门到精通 --- 第 15天,数据导入、导出

数据的导入、导出 -- 查看当前设置的目录路径&#xff0c;限制从数据库服务器读取和写入文件的操作只能在指定的目录中进行,在安全性和文件操作限制方面具有重要意义。rootmysqldb 14:19: [(none)]> SHOW VARIABLES LIKE "secure_file_priv"; -----------------…

【JVM详解四】执行引擎

一、概述 Java程序运行时&#xff0c;JVM会加载.class字节码文件&#xff0c;但是字节码并不能直接运行在操作系统之上&#xff0c;而JVM中的执行引擎就是负责将字节码转化为对应平台的机器码让CPU运行的组件。 执行引擎是JVM核心的组成部分之一。可以把JVM架构分成三部分&am…