惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
G
Google Developers Blog
B
Blog
Engineering at Meta
Engineering at Meta
阮一峰的网络日志
阮一峰的网络日志
The Register - Security
The Register - Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
The Cloudflare Blog
The Hacker News
The Hacker News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
雷峰网
雷峰网
F
Fortinet All Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
量子位
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
H
Help Net Security
Help Net Security
Help Net Security
P
Palo Alto Networks Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
W
WeLiveSecurity
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
Know Your Adversary
Know Your Adversary
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
N
News | PayPal Newsroom
AWS News Blog
AWS News Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
腾讯CDC
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
TaoSecurity Blog
TaoSecurity Blog
GbyAI
GbyAI
Y
Y Combinator Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker

ACCB

Markdown 告警组件 Astro 静态网站生成器入门指南 现代前端开发趋势:2025年值得关注的技术 零基础学 Python:从入门到实战 CSS Grid 与 Flexbox:选择合适的布局方案 为 Astro Cactus 添加网络提及功能 Markdown 元素展示 封面图片示例 这篇文章没有任何内容 这是一个非常长的标题用于测试页面布局和CSS样式的正确性 唯一标签验证 社交图片示例
现代前端开发工具链配置指南
lbb · 2025-01-08 · via ACCB

现代前端开发离不开强大的工具链支持。本文将详细介绍如何配置一套完整的前端开发环境,帮助你提升开发效率。

核心工具概览

包管理器

  • pnpm:比 npm 更快,节省磁盘空间
  • yarn:稳定可靠,适合大型项目
  • npm:官方工具,兼容性最好

构建工具

  • Vite:极速开发服务器,现代化构建
  • Webpack:成熟稳定,生态丰富
  • Turbopack:Vercel 出品,性能卓越

代码质量

  • ESLint:代码检查,发现潜在问题
  • Prettier:代码格式化,统一风格
  • TypeScript:类型检查,提升代码质量

项目初始化

1. 创建 React + TypeScript 项目

# 使用 Vite 创建项目

pnpm create vite my-app --template react-ts

# 或使用 Next.js

pnpx create-next-app@latest my-app --typescript --tailwind --eslint

# 进入项目目录

cd my-app

pnpm install

2. 配置 TypeScript

{

"compilerOptions": {

"target": "ES2020",

"lib": ["DOM", "DOM.Iterable", "ES6"],

"allowJs": false,

"skipLibCheck": true,

"esModuleInterop": false,

"allowSyntheticDefaultImports": true,

"strict": true,

"forceConsistentCasingInFileNames": true,

"module": "ESNext",

"moduleResolution": "Node",

"resolveJsonModule": true,

"isolatedModules": true,

"noEmit": true,

"jsx": "react-jsx",

"baseUrl": ".",

"paths": {

"@/*": ["src/*"],

"@/components/*": ["src/components/*"],

"@/utils/*": ["src/utils/*"]

}

},

"include": ["src", "**/*.ts", "**/*.tsx"],

"exclude": ["node_modules"]

}

3. 配置 ESLint

# 安装 ESLint 及相关插件

pnpm add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser

pnpm add -D eslint-plugin-react eslint-plugin-react-hooks

pnpm add -D eslint-plugin-import eslint-plugin-jsx-a11y

module.exports = {

env: {

browser: true,

es2021: true,

node: true,

},

extends: [

'eslint:recommended',

'@typescript-eslint/recommended',

'plugin:react/recommended',

'plugin:react-hooks/recommended',

'plugin:import/recommended',

'plugin:jsx-a11y/recommended',

],

parser: '@typescript-eslint/parser',

parserOptions: {

ecmaFeatures: {

jsx: true,

},

ecmaVersion: 12,

sourceType: 'module',

},

plugins: [

'react',

'@typescript-eslint',

'import',

'jsx-a11y',

],

rules: {

'react/react-in-jsx-scope': 'off',

'import/no-unresolved': 'off',

'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],

'react/prop-types': 'off',

},

settings: {

react: {

version: 'detect',

},

},

};

4. 配置 Prettier

# 安装 Prettier

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

{

"semi": true,

"trailingComma": "es5",

"singleQuote": true,

"printWidth": 80,

"tabWidth": 2,

"useTabs": false,

"bracketSpacing": true,

"arrowParens": "avoid",

"endOfLine": "lf"

}

node_modules

dist

build

.next

.nuxt

.vscode

开发工具配置

VS Code 配置

{

"editor.formatOnSave": true,

"editor.defaultFormatter": "esbenp.prettier-vscode",

"editor.codeActionsOnSave": {

"source.fixAll.eslint": true,

"source.organizeImports": true

},

"typescript.preferences.importModuleSpecifier": "relative",

"emmet.includeLanguages": {

"typescript": "html",

"typescriptreact": "html"

}

}

{

"recommendations": [

"esbenp.prettier-vscode",

"dbaeumer.vscode-eslint",

"ms-vscode.vscode-typescript-next",

"bradlc.vscode-tailwindcss",

"ms-vscode.vscode-json",

"christian-kohler.path-intellisense"

]

}

Git 配置

node_modules/

dist/

build/

.next/

.nuxt/

.env.local

.env.development.local

.env.test.local

.env.production.local

.DS_Store

*.log

coverage/

Husky + lint-staged

# 安装 Husky 和 lint-staged

pnpm add -D husky lint-staged

# 初始化 Husky

npx husky install

npx husky add .husky/pre-commit "npx lint-staged"

{

"scripts": {

"prepare": "husky install"

},

"lint-staged": {

"*.{js,jsx,ts,tsx}": [

"eslint --fix",

"prettier --write",

"git add"

],

"*.{css,scss,less}": [

"prettier --write",

"git add"

],

"*.{json,md}": [

"prettier --write",

"git add"

]

}

}

性能优化工具

1. Bundle 分析

# 安装 bundle 分析工具

pnpm add -D @next/bundle-analyzer # Next.js

pnpm add -D rollup-plugin-visualizer # Vite

2. 图片优化

# 安装图片优化工具

pnpm add next-optimized-images # Next.js

pnpm add vite-plugin-imagemin # Vite

3. 代码分割配置

// React 懒加载

import { lazy, Suspense } from 'react';

const LazyComponent = lazy(() => import('./components/LazyComponent'));

function App() {

return (

<Suspense fallback={<div>加载中...</div>}>

<LazyComponent />

</Suspense>

);

}

调试工具

1. React DevTools

浏览器扩展,帮助调试 React 组件

2. Redux DevTools

调试状态管理

3. 网络调试

// 网络请求拦截

if (process.env.NODE_ENV === 'development') {

const { worker } = require('./mocks/browser');

worker.start();

}

测试环境

Jest + Testing Library

# 安装测试工具

pnpm add -D jest @testing-library/react @testing-library/jest-dom

pnpm add -D jest-environment-jsdom

module.exports = {

testEnvironment: 'jsdom',

setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],

moduleNameMapping: {

'^@/(.*)$': '<rootDir>/src/$1',

},

collectCoverageFrom: [

'src/**/*.{ts,tsx}',

'!src/**/*.d.ts',

],

};

部署配置

Docker 配置

# Dockerfile

FROM node:18-alpine AS deps

WORKDIR /app

COPY package.json pnpm-lock.yaml ./

RUN corepack enable pnpm && pnpm install --frozen-lockfile

FROM node:18-alpine AS builder

WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules

COPY . .

RUN corepack enable pnpm && pnpm build

FROM node:18-alpine AS runner

WORKDIR /app

COPY --from=builder /app/dist ./dist

COPY --from=builder /app/package.json ./package.json

EXPOSE 3000

CMD ["npm", "start"]

CI/CD 配置

name: CI

on: [push, pull_request]

jobs:

test:

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v3

- uses: pnpm/action-setup@v2

with:

version: 8

- uses: actions/setup-node@v3

with:

node-version: 18

cache: 'pnpm'

- run: pnpm install --frozen-lockfile

- run: pnpm lint

- run: pnpm test

- run: pnpm build

最佳实践

  1. 统一团队配置:使用 .editorconfig 统一编辑器配置
  2. 版本管理:使用 .nvmrc 锁定 Node.js 版本
  3. 依赖管理:定期更新依赖,关注安全漏洞
  4. 代码审查:建立代码审查流程
  5. 文档维护:保持 README 和技术文档更新

通过这套完整的工具链配置,你可以获得:

  • 更快的开发速度
  • 更好的代码质量
  • 更稳定的构建过程
  • 更高效的团队协作

现代前端开发工具在不断演进,建议定期关注新工具和最佳实践,持续优化开发环境。