# 项目优化实施计划 > **面向 AI 代理的工作者:** 必需子技能:使用 superpowers:subagent-driven-development(推荐)或 superpowers:executing-plans 逐任务实现此计划。步骤使用复选框(`- [ ]`)语法来跟踪进度。 **目标:** 根据代码审查报告,修复所有必须修复的问题,完成建议的优化项,提升项目整体质量。 **架构:** 纯静态 Next.js 网站,修复 TypeScript 配置问题,清理无效配置,优化测试环境,改进代码组织结构。 **技术栈:** Next.js 16, React 19, TypeScript, Jest, Playwright --- ## 文件结构 ### 将要修改的文件 | 文件 | 职责 | 变更类型 | |------|------|----------| | `tsconfig.json` | TypeScript 配置 | 修改 - 添加 Jest 类型支持 | | `next.config.ts` | Next.js 配置 | 修改 - 移除无效 headers 配置 | | `src/contexts/theme-context.tsx` | 主题上下文 | 修改 - 简化为纯静态版本 | | `src/app/layout.tsx` | 根布局 | 修改 - 移除暗色主题脚本 | | `package.json` | 项目配置 | 修改 - 清理无效脚本 | | `e2e/website-acceptance.spec.ts` | E2E 测试 | 修改 - 支持环境变量 URL | | `e2e/playwright.config.ts` | Playwright 配置 | 修改 - 添加 baseURL 配置 | | `nginx-static.conf` | Nginx 配置 | 修改 - 添加安全头 | ### 将要创建的文件 | 文件 | 职责 | |------|------| | `src/lib/constants/index.ts` | 常量导出入口 | | `src/lib/constants/company.ts` | 公司信息常量 | | `src/lib/constants/navigation.ts` | 导航配置 | | `src/lib/constants/services.ts` | 服务数据 | | `src/lib/constants/products.ts` | 产品数据 | | `src/lib/constants/news.ts` | 新闻数据 | | `src/lib/constants/stats.ts` | 统计数据 | ### 将要删除的文件 | 文件 | 原因 | |------|------| | 无 | 本次优化不删除文件 | --- ## 阶段一:P0 必须修复(立即处理) ### 任务 1:修复 TypeScript 测试类型错误 **文件:** - 修改:`tsconfig.json` **问题:** 测试文件中 `describe`、`it`、`expect`、`beforeEach` 等全局类型未定义,导致 `npm run type-check` 失败。 - [ ] **步骤 1:修改 tsconfig.json 添加 Jest 类型支持** ```json { "compilerOptions": { "target": "ES2017", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "strict": true, "noUncheckedIndexedAccess": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "noUnusedLocals": true, "noUnusedParameters": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "bundler", "resolveJsonModule": true, "isolatedModules": true, "jsx": "react-jsx", "incremental": true, "types": ["jest", "node"], "plugins": [ { "name": "next" } ], "paths": { "@/*": [ "./src/*" ] } }, "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", ".next/dev/types/**/*.ts", "**/*.mts", "dist/types/**/*.ts", "dist/dev/types/**/*.ts" ], "exclude": [ "node_modules", "e2e" ] } ``` - [ ] **步骤 2:运行类型检查验证修复** 运行:`npm run type-check` 预期:无测试相关的类型错误 - [ ] **步骤 3:Commit** ```bash git add tsconfig.json git commit -m "fix: 添加 Jest 类型支持,修复测试文件类型错误" ``` --- ## 阶段二:P1 建议修改(短期优化) ### 任务 2:移除无效的 headers 配置 **文件:** - 修改:`next.config.ts` - 修改:`nginx-static.conf` **问题:** `output: 'export'` 静态导出模式下,`headers` 配置不会生效。应将安全头配置移至 nginx。 - [ ] **步骤 1:简化 next.config.ts,移除 headers 配置** ```typescript import type { NextConfig } from "next"; const cdnDomain = process.env.CDN_DOMAIN || ''; const nextConfig: NextConfig = { distDir: 'dist', output: 'export', assetPrefix: cdnDomain || undefined, images: { unoptimized: true, }, compress: true, poweredByHeader: false, reactStrictMode: true, experimental: { optimizePackageImports: ['lucide-react'], }, compiler: { removeConsole: process.env.NODE_ENV === 'production', }, }; export default nextConfig; ``` - [ ] **步骤 2:更新 nginx-static.conf 添加安全头** 读取当前 nginx-static.conf 内容后追加安全头配置。 - [ ] **步骤 3:运行构建验证配置正确** 运行:`npm run build` 预期:构建成功,无错误 - [ ] **步骤 4:Commit** ```bash git add next.config.ts nginx-static.conf git commit -m "refactor: 移除无效的 headers 配置,安全头移至 nginx" ``` --- ### 任务 3:简化 ThemeContext 为纯静态版本 **文件:** - 修改:`src/contexts/theme-context.tsx` - 修改:`src/app/layout.tsx` **问题:** ThemeProvider 硬编码只返回 'light',但 layout.tsx 中还有暗色主题切换脚本,两者不一致。纯静态网站不需要主题切换。 - [ ] **步骤 1:简化 theme-context.tsx** ```typescript 'use client'; import { createContext, useContext, type ReactNode } from 'react'; interface ThemeContextType { theme: 'light'; resolvedTheme: 'light'; } const ThemeContext = createContext(undefined); export function ThemeProvider({ children }: { children: ReactNode }) { return ( {children} ); } export function useTheme() { const context = useContext(ThemeContext); if (context === undefined) { throw new Error('useTheme must be used within a ThemeProvider'); } return context; } ``` - [ ] **步骤 2:修改 layout.tsx 移除暗色主题脚本** 移除 `` 中的暗色主题检测脚本: ```typescript // 移除以下代码块: