洛南网站建设黑龙江省公共资源

张小明 2026/1/3 5:25:42
洛南网站建设,黑龙江省公共资源,国内如何做国外网站的兼职项目,温州网站建设wmwl第4章#xff1a;统一的代码风格与严格的代码质量检查#xff0c;为项目安装配置ESLint和Prettier在现代前端项目中#xff0c;ESLint 与 Prettier 的工程化整合非常关键#xff0c;它决定了#xff1a;团队代码是否统一自动化格式化是否生效是否能在 VSCode Git Hooks 中…第4章统一的代码风格与严格的代码质量检查为项目安装配置ESLint和Prettier在现代前端项目中ESLint 与 Prettier 的工程化整合非常关键它决定了团队代码是否统一自动化格式化是否生效是否能在 VSCode Git Hooks 中自动检查是否能在持续集成CI中保证质量本章将带你建立一套完全现代化的代码规范体系基于ESLint 9Flat ConfigPrettier 3TypeScriptReact 19Tailwind CSSShadCN UI最终项目具备✔ 统一代码风格✔ VSCode 自动格式化✔ Git 提交自动检查✔ Tailwind class 排序✔ import 顺序优化✔ 生产工程可复用4.1 为什么需要 ESLint 与 PrettierESLint用于检查代码中潜在的错误与不规范用法例如未使用的变量React Hook 用法错误类型错误TypeScriptimport 顺序问题逻辑潜在危险代码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/TSXeslint/jsTypeScript 规则集typescript-eslinteslint-plugin-reactReact 专用规则eslint-plugin-react-hooksHook 规则非常重要eslint-plugin-react-refreshVite react-refresh 热更新规则eslint-plugin-reacteslint-plugin-importimport 语法检查eslint-plugin-tailwindcssTailwind 类名排序与错误提示4.3 安装 Prettier 及其插件pnpm add -D prettier eslint-plugin-prettier eslint-config-prettier eslint-plugin-tailwindcss说明包名功能prettier代码格式化工具eslint-plugin-prettiereslint-config-prettier关闭 ESLint 中与 Prettier 冲突的规则prettier-plugin-tailwindcssTailwind class 自动排序强烈推荐4.4 修改eslint.config.js配置文件在项目根目录找到.eslint.config.jsimport 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 创建.prettierignoredist node_modules pnpm-lock.yaml .env* *.png *.svg4.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-tailwindcsstailwindcss/classnames-order: warn,效果div classNamep-4 flex bg-red-500 text-center /会自动变成div classNameflex 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:check4.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 流程即日起你的项目将获得统一的代码风格即时错误提示自动排版含 Tailwindimport 顺序自动规范化React Hook 误用自动警告这套体系完全适用于企业级后台大型前端团队协作现代 SPA 工程现代 React包括 Server Components
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

html简单的网站WordPress将开发

TikZ科学绘图库完整使用教程:从零基础到专业图表制作 【免费下载链接】tikz Random collection of standalone TikZ images 项目地址: https://gitcode.com/gh_mirrors/tikz/tikz 在学术研究和技术文档创作中,高质量的科学图表是传达复杂概念的关…

张小明 2025/12/28 12:18:51 网站建设

网站网络广告如何建设北京梵客装饰

Driver.js 1.x终极升级指南:快速掌握新版API的完整教程 【免费下载链接】driver.js driver.js - 一个轻量级、无依赖的纯 JavaScript 库,用于控制用户在网页上的焦点移动,适用于需要实现网页交互和用户指引的前端开发者。 项目地址: https:…

张小明 2026/1/2 18:28:28 网站建设

新乡门户网站建设方案学动漫设计后悔死了

“被劫匪用枪抵着脑袋时,媳妇在电话里问的是‘那你什么时候能回家给我做饭?’”在饶晓志导演的黑色幽默话剧《你好,打劫!》中,钮宝平塑造的“妻管严”汉克斯,让观众在笑声中瞥见普通人生活的荒诞与真实。这…

张小明 2025/12/31 20:45:14 网站建设

wordpress报价网页优化方案

Android设备网页控制新体验:ws-scrcpy深度使用指南 【免费下载链接】ws-scrcpy Web client prototype for scrcpy. 项目地址: https://gitcode.com/gh_mirrors/ws/ws-scrcpy 🎯 开篇简介 在移动设备管理日益重要的今天,ws-scrcpy And…

张小明 2026/1/1 11:55:23 网站建设

商务网站建设推荐php网站服务器

原文链接:https://ai225.com/article/memvid-introduction 在AI技术快速发展的今天,如何高效存储和检索大量知识数据成为了一个重要挑战。Memvid作为一个创新的开源项目,提供了一种全新的解决方案:将数百万个文本块压缩为单个可搜…

张小明 2025/12/28 18:37:31 网站建设