如何用 Windsurf 搭建一个完整的前端项目?
本文通过一个可运行的 React + TypeScript 任务管理面板,演示如何使用 Windsurf 完成需求拆解、项目初始化、组件开发、状态管理、表单验证、测试、构建和部署。全文附带可复制提示词、项目结构和实测数据。
AI编程工具最容易制造的一种错觉是:
输入一句“帮我做个网站”,几分钟后看到页面,就等于完成了一个项目。实际上,一个完整前端项目至少还需要解决:
- 技术栈是否合适;
- 目录结构是否可维护;
- 状态和数据如何管理;
- 表单是否校验;
- 页面是否响应式;
- 是否支持键盘和读屏器;
- 是否存在类型错误和Lint问题;
- 是否有测试;
- 生产构建是否成功;
- 环境变量和密钥是否安全;
- 部署后能否继续维护。
Windsurf的真正价值,不是替你生成几百行代码,而是让AI在编辑器、代码库和终端之间连续工作:读取现有文件、制定计划、修改多个模块、运行命令、分析报错并继续修复。
产品名称说明:Windsurf在2025年被Cognition收购后,2026年的官方文档逐步使用“Devin Desktop”名称。本文仍沿用用户更熟悉的“Windsurf”,并以其中的Cascade工作流为主。不同版本中按钮名称可能略有变化。
一、本文要完成什么项目?
示例项目名为 TaskFlow,是一个响应式任务管理面板。
它包含:
- 仪表盘首页;
- 任务列表;
- 新建任务表单;
- 任务状态和优先级;
- 简单数据分析;
- 深色模式;
- 本地数据持久化;
- 桌面、平板和手机布局;
- 表单校验;
- 单元测试;
- Playwright端到端测试脚本;
- 生产构建和部署配置。
技术栈
| 模块 | 选择 |
|---|---|
| 框架 | React 19 |
| 语言 | TypeScript |
| 构建工具 | Vite 8 |
| 路由 | React Router |
| 状态管理 | Zustand |
| 表单 | React Hook Form |
| 校验 | Zod |
| 图标 | Lucide React |
| 单元测试 | Vitest + Testing Library |
| 端到端测试 | Playwright |
| 样式 | 原生CSS变量与响应式布局 |
这不是唯一正确的技术栈,但它适合演示一个现代、可维护且不依赖大型UI框架的前端项目。
二、可复现实测结果
为了避免把教程写成“看起来能运行”,本文实际创建并验证了示例项目。
测试环境
| 项目 | 版本或条件 |
|---|---|
| Node.js | 22.16.0 |
| npm | 10.9.2 |
| React | 19.2.7 |
| TypeScript | 6.0.3 |
| Vite | 8.1.0 |
| Vitest | 4.1.9 |
| 操作系统 | Linux容器环境 |
验证结果
| 检查项目 | 实测结果 |
|---|---|
| 源码文件 | 21个 |
| Vite转换模块 | 138个 |
| Oxlint | 0个警告,0个错误 |
| TypeScript类型检查 | 0个错误 |
| 单元测试 | 4/4通过 |
| Vite生产构建阶段 | 约0.75秒 |
| 完整`npm run build`命令 | 约3.4秒 |
| 主JavaScript | 337.51 KB |
| 主JavaScript Gzip | 105.79 KB |
| CSS | 7.48 KB |
| CSS Gzip | 2.26 KB |
| `dist`目录 | 约353 KB |
| 生产依赖安全审计 | 0项已知漏洞 |
这些数字衡量的是示例项目的构建结果,不是Windsurf的“编程速度”。不同电脑、依赖版本和网络环境会产生不同数据。
项目同时编写了3项Playwright端到端测试,覆盖:
- 新建并完成任务;
- 深色模式持久化;
- 手机端导航。
由于本次受管Chromium环境禁止访问本地页面,本文没有把这3项脚本包装成“已执行通过”。在自己的电脑上安装Playwright浏览器后,可以运行:
```bash
npx playwright install
npm run test:e2e
```
三、安装Windsurf并准备环境
Windsurf当前支持macOS、Windows和Linux,并可以导入VS Code或Cursor设置。
在开始前,建议安装:
- Node.js 22 LTS或兼容版本;
- Git;
- Windsurf;
- Chrome或Chromium;
- GitHub账号。
Vite 8要求Node.js 20.19以上或22.12以上。如果Node版本过低,项目初始化或构建可能直接失败。
检查环境:
```bash
node -v
npm -v
git --version
```
免费版够不够?
截至2026年6月,Windsurf公开方案包括:
| 方案 | 公开价格 |
|---|---|
| Free | 0美元/月 |
| Pro | 20美元/月 |
| Max | 200美元/月 |
| Team | 80美元/月起,团队席位结构以结算页为准 |
免费版可以完成本教程,但Agent配额和模型选择更有限。Pro更适合连续开发和较大代码库。
价格和配额经常调整,购买前应以官方结算页面为准。
四、不要先写代码:先写清楚需求
打开一个空目录后,不要立即输入:
帮我做一个任务管理网站。
这种提示词会让AI自行猜测框架、页面、状态和视觉风格,最终得到一个“能展示但不可维护”的Demo。
先创建一个需求文件:
```text
docs/requirements.md
```
内容至少包括:
```markdown
TaskFlow requirements
Goal
Build a responsive personal task dashboard.
Pages
- Dashboard
- Tasks
- Analytics
- Settings
- 404
Core features
- Create and delete tasks
- Change task status
- Priority and due date
- Persist data locally
- Dark mode
- Mobile navigation
Quality requirements
- TypeScript strict mode
- Accessible labels and keyboard focus
- Form validation
- Unit tests
- End-to-end test scripts
- Production build must pass
Non-goals
- No backend
- No authentication
- No external database
- No large UI component library
```
需求文件的意义不是形式主义,而是把“完成标准”从聊天记录移到代码库中。
五、用AGENTS.md约束Windsurf
AI生成代码最常见的问题不是不会写,而是每次写法不一致。
在项目根目录创建:
```text
AGENTS.md
```
示例:
```markdown
Project instructions
- Package manager: npm.
- Framework: React + TypeScript + Vite.
- Use functional components.
- State management: Zustand.
- Forms: React Hook Form + Zod.
- Unit tests: Vitest + Testing Library.
- E2E tests: Playwright.
- All interactive controls require accessible names.
- Do not add a dependency when a small local implementation is enough.
- Run npm run check before marking a task complete.
- Never place secrets in frontend source code.
```
Windsurf官方当前建议:
- 简单、按目录生效的规范使用`AGENTS.md`;
- 跨目录或需要激活条件的约束使用Rules;
- 复杂重复流程使用Skills;
- 不要依赖自动Memory保存关键项目规则。
为什么这一步重要?
如果没有规则,AI可能:
- 一会儿使用npm,一会儿使用pnpm;
- 同一项目混用CSS Modules、Tailwind和内联样式;
- 新增多个功能重复的依赖;
- 忘记写测试;
- 为了解决一个错误修改不相关文件。
六、第一步:只让Cascade制定计划
打开Cascade后,先使用Chat或只读模式,让它分析需求,不要立即写代码。
可以复制这个提示词:
```text
Read docs/requirements.md and AGENTS.md.
Do not write code yet.
Create an implementation plan that includes:
1. Technology choices and reasons
2. Project directory structure
3. Data model
4. Route design
5. Component boundaries
6. State-management plan
7. Validation strategy
8. Unit and E2E test plan
9. Accessibility checklist
10. Build and deployment checklist
Identify ambiguous requirements and choose the simplest maintainable solution.
```
一个合格计划应该回答什么?
- Task类型有哪些字段?
- 哪些状态需要全局管理?
- 页面和组件如何分层?
- 路由是否需要懒加载?
- 数据如何持久化?
- 测试哪些业务行为?
- 哪些功能明确不做?
如果计划只是“创建首页、添加样式、完成项目”,不要让AI继续。要求它细化。
七、第二步:初始化Vite项目
确认计划后,在Cascade中输入:
```text
Initialize the project with the latest Vite React TypeScript template.
Use npm.
Do not install application dependencies yet.
After scaffolding:
1. Run npm install
2. Start the development server
3. Report any errors
4. Stop and wait for review
```
对应命令是:
```bash
npm create vite@latest taskflow -- --template react-ts
cd taskflow
npm install
npm run dev
```
Vite官方支持React TypeScript模板。Vite 8默认项目已经包含现代React构建配置,不需要让AI从零手写Webpack。
安全设置
Windsurf终端具有四种自动执行级别:
- Disabled;
- Allowlist Only;
- Auto;
- Turbo。
初学者建议使用 Allowlist Only 或 Auto。
不要一开始就开启Turbo。至少将以下危险命令加入拒绝或人工确认范围:
```text
rm
sudo
chmod
chown
curl | sh
git push --force
git reset --hard
npm publish
```
AI可以运行命令,不代表每条命令都应该自动批准。
八、第三步:安装项目依赖
让Cascade根据已确认的方案安装依赖:
```text
Install only the dependencies required by the approved architecture:
Runtime:
- react-router-dom
- zustand
- zod
- react-hook-form
- @hookform/resolvers
- lucide-react
Development:
- vitest
- jsdom
- @testing-library/react
- @testing-library/jest-dom
- @testing-library/user-event
- @playwright/test
Before installing:
1. Explain why each package is needed
2. Check whether an existing dependency already solves the problem
3. Use exact package names
4. Do not add a UI framework
```
对应命令:
```bash
npm install react-router-dom zustand zod react-hook-form \
@hookform/resolvers lucide-react
npm install -D vitest jsdom \
@testing-library/react \
@testing-library/jest-dom \
@testing-library/user-event \
@playwright/test
```
为什么不让AI自由选择依赖?
AI很容易为了一个简单功能加入大型库。
例如:
- 为三个统计条安装完整图表库;
- 为一个日期字段安装日期工具包;
- 为简单动画安装大型动画库;
- 同时安装多个状态管理方案。
每个依赖都会增加:
- Bundle体积;
- 安全风险;
- 升级成本;
- AI理解代码库的负担。
九、第四步:让Windsurf按模块生成,而不是一次生成全部
不要使用:
按照计划把整个项目完成。
更稳定的方法是分阶段提交任务。
阶段一:数据模型与状态
```text
Implement only the data layer.
Requirements:
- Create Task, TaskStatus, and TaskPriority TypeScript types
- Create a Zustand store
- Include addTask, updateStatus, removeTask, and resetTasks
- Persist tasks to localStorage
- Add three seed tasks
- Do not create UI components yet
After editing:
- Run TypeScript type checking
- Explain the public store API
```
阶段二:路由和布局
```text
Implement the application shell and routing.
Routes:
- /
- /tasks
- /analytics
- /settings
- fallback 404
Requirements:
- Responsive sidebar on desktop
- Bottom navigation on mobile
- Active-route state
- Semantic navigation
- Dark-mode toggle persisted to localStorage
- Do not implement page-specific business logic yet
```
阶段三:表单
```text
Implement the task form using React Hook Form and Zod.
Validation:
- title: 3 to 80 characters
- description: maximum 240 characters
- priority: low, medium, or high
- due date: required
Accessibility:
- Every field has a visible label
- Validation errors are readable
- Submit is keyboard accessible
Add focused unit tests for invalid and valid submissions.
```
阶段四:任务列表与分析页
```text
Implement:
1. Task list
2. Status selector
3. Delete action
4. Priority labels
5. Dashboard summary cards
6. A dependency-free CSS bar chart for task status
Do not add a chart library.
Preserve the existing store API.
Add tests for status updates and deletion.
```
阶段五:响应式和视觉完善
```text
Polish the interface without changing business logic.
Requirements:
- CSS custom properties for light and dark themes
- Desktop, tablet, and 390px mobile layouts
- Visible focus states
- No horizontal overflow
- Touch targets approximately 44px where possible
- Respect semantic heading order
- Avoid low-contrast text
```
分阶段的优势是,每次改动更小,Diff更容易检查,出错后也更容易回退。
十、如何审查Windsurf生成的代码?
不要只看页面是否漂亮。
每次修改后,至少检查五件事。
1. Diff是否只包含本次任务?
如果只是添加表单,AI却修改了路由、主题和Store,应暂停并询问原因。
2. 是否重复造轮子?
检查是否出现:
- 两个Task类型;
- 两套状态管理;
- 重复CSS变量;
- 同功能组件;
- 无必要的工具函数。
3. 是否有静默错误?
特别关注:
- `any`;
- 非空断言;
- 空的`catch`;
- 未处理Promise;
- 直接修改状态;
- 缺少依赖的`useEffect`;
- 把密钥写进前端。
4. 无障碍是否真实可用?
图标按钮必须有`aria-label`,不能只有垃圾桶图标。
表单错误不能只用红色表示。
导航、按钮和下拉框必须能够通过键盘操作。
5. 是否方便以后修改?
一个组件超过300行时,不一定必须拆,但应该让AI解释边界是否合理。
十一、第五步:用终端形成验证闭环
在`package.json`中增加:
```json
{
"scripts": {
"dev": "vite",
"lint": "oxlint",
"typecheck": "tsc -b --pretty false",
"test": "vitest run",
"test:e2e": "playwright test",
"build": "tsc -b && vite build",
"check": "npm run lint && npm run typecheck && npm run test && npm run build"
}
}
```
然后对Cascade说:
```text
Run npm run check.
If a command fails:
1. Read the complete error
2. Identify the root cause
3. Make the smallest safe fix
4. Re-run only the failed check first
5. Run npm run check again after it passes
Do not silence TypeScript or lint errors.
Do not remove failing tests unless the requirement is wrong.
```
本文实测结果
```text
Lint: 0 warnings, 0 errors
Typecheck: 0 errors
Unit tests: 4 passed
Build: passed
Modules: 138 transformed
JS gzip: 105.79 KB
CSS gzip: 2.26 KB
```
这一步是“AI写代码”和“AI交付项目”的分界线。
没有验证命令的项目,只能证明AI生成过文件,不能证明项目可交付。
十二、第六步:让Windsurf生成测试
单元测试应测试行为
不要要求:
给所有组件写测试。
这种提示容易生成大量只验证“组件能渲染”的低价值测试。
使用更明确的任务:
```text
Add unit tests for business behavior only.
Test cases:
1. Empty title shows a validation error
2. A valid form submission adds one task
3. Changing the status updates the store
4. Deleting a task removes it
Use Testing Library queries based on roles and accessible names.
Do not test implementation details.
```
Playwright端到端测试
```text
Add Playwright tests for three critical flows:
1. Create a task and mark it done
2. Switch to dark mode and verify persistence after reload
3. Use mobile navigation at 390x844
Use stable role- and label-based selectors.
Do not use arbitrary timeouts.
```
Playwright官方提供测试运行器、断言、隔离、并行执行和移动设备模拟,适合验证真实用户流程。
十三、第七步:性能和Bundle检查
构建成功后,让Cascade分析输出:
```text
Run a production build and analyze the output.
Report:
- Build time
- Number of transformed modules
- Raw and gzip sizes for JS and CSS
- Any chunk above 200 KB gzip
- Dependencies contributing most to the bundle
- Three optimization opportunities, ranked by impact
Do not optimize before measuring.
```
本文示例主JavaScript为:
- 原始体积:337.51 KB;
- Gzip:105.79 KB。
对于一个包含路由、状态、表单校验和图标的单页应用,这个体积可以接受,但仍可以继续优化:
- 路由懒加载;
- 只导入实际使用的图标;
- 避免引入大型图表和日期库;
- 将非首屏页面拆分为独立Chunk。
不要把Lighthouse分数写死
Lighthouse结果受电脑、浏览器、网络和运行模式影响。
建议在本机执行:
```bash
npm run build
npm run preview
npx lighthouse http://localhost:4173 \
--only-categories=performance,accessibility,best-practices,seo \
--view
```
正确做法是记录测试环境和多次运行中位数,而不是只挑最好的一次。
十四、第八步:处理环境变量和API
前端项目需要后端接口时,创建:
```text
.env.example
```
示例:
```bash
VITE_API_BASE_URL=https://api.example.com
```
代码中读取:
```ts
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL
```
必须记住:
所有以`VITE_`开头的变量都会进入前端构建,用户可以在浏览器中看到。
因此,不能放入:
- OpenAI API Key;
- 数据库密码;
- 私钥;
- 管理员令牌;
- 支付平台Secret;
- 服务账号凭证。
正确架构是:
```text
Browser -> Your backend/serverless function -> Third-party API
```
让Windsurf生成项目时,可以加入规则:
```text
Never place secrets in frontend code.
If a feature requires a secret, create a server-side boundary or explain why it cannot be implemented safely in a frontend-only project.
```
十五、第九步:Git提交不要交给AI随意决定
建议每完成一个阶段就提交:
```bash
git add .
git commit -m "feat: add task state and persistence"
```
合理的提交顺序:
```text
chore: scaffold Vite React TypeScript project
feat: add application shell and routes
feat: add task store and persistence
feat: add validated task form
feat: add task list and analytics
test: add unit and end-to-end coverage
style: add responsive themes
chore: add production checks
```
可以让Cascade生成提交信息,但提交前必须自己看Diff。
不要允许AI默认执行:
```bash
git push --force
git reset --hard
git clean -fd
```
Named Checkpoints可以帮助回退Cascade修改,但Git仍然是项目的正式历史记录。
十六、第十步:部署项目
Windsurf的App Deploys可以通过Cascade将React、Vue、Svelte和Next.js项目部署到Netlify,并生成公开预览地址。
可以输入:
```text
Before deployment:
1. Run npm run check
2. Run the production build
3. Verify that no .env files or secrets are included
4. Summarize the build output
If all checks pass, deploy this project to Netlify as a preview.
```
官方提醒:
- App Deploys仍是Beta;
- 当前主要支持Netlify;
- 预览项目会上传到Windsurf/Cognition的部署流程;
- 敏感生产项目应该认领到自己的Netlify账号;
- 未认领的部署可能被删除。
因此,更稳妥的正式部署方式是:
1. 将项目推送到自己的GitHub;
2. 在Netlify、Vercel或Cloudflare Pages连接仓库;
3. 配置构建命令和环境变量;
4. 开启分支预览;
5. 保留部署日志和回滚能力。
十七、五个最实用的Windsurf提示词模式
1. 先计划,不写代码
```text
Do not edit files yet. Inspect the codebase and propose a plan with affected files, risks, and validation steps.
```
2. 限制改动范围
```text
Only modify the task-form feature. Do not refactor routing, theme, or the store unless required. Explain every out-of-scope change before making it.
```
3. 让AI自己验证
```text
After implementation, run lint, typecheck, focused tests, and production build. Fix root causes instead of suppressing errors.
```
4. 要求最小修改
```text
Make the smallest safe change that satisfies the requirement. Preserve public APIs and existing behavior.
```
5. 要求审查而不是继续生成
```text
Review the current implementation as a senior frontend engineer. List correctness, accessibility, performance, security, and maintainability issues. Do not edit files until I approve the findings.
```
十八、常见失败方式
失败一:一次生成整个项目
结果通常是:
- 文件多但结构混乱;
- 功能看似完整,测试无法运行;
- 依赖过多;
- 修改一个功能导致多个页面损坏。
解决方法:分阶段生成,每阶段都运行检查。
失败二:只说“做得更好看”
“更好看”没有验收标准。
改成:
```text
Use a neutral productivity-dashboard style.
Use one primary accent color.
Maintain at least 4.5:1 text contrast.
Keep content width below 1120px.
Use 16–24px spacing.
Create a mobile bottom navigation below 640px.
```
失败三:报错后让AI连续乱改
如果AI连续修改多个配置文件,应先回退,然后让它解释根因。
使用:
```text
Stop editing. Explain why the previous fix failed, which assumption was wrong, and what the minimal next experiment should be.
```
失败四:让AI“修复”测试
AI可能删除断言、扩大选择器或跳过测试。
明确要求:
```text
Do not weaken, skip, or delete tests unless the documented requirement changed.
```
失败五:把预览部署当生产环境
预览链接适合分享,不等于拥有:
- 自定义域名;
- 监控;
-备份;
- 安全头;
- 隐私合规;
- 稳定SLA;
- 正式回滚流程。
十九、Windsurf适合不会编程的人吗?
它可以显著降低入门门槛,但不会消除工程判断。
完全没有前端基础时,至少需要理解:
- 组件是什么;
- 状态和Props的区别;
- 浏览器与服务器的边界;
- API Key为什么不能放前端;
- npm依赖的作用;
- 类型检查、Lint和测试分别解决什么问题;
- Git提交和部署的基本流程。
AI最适合替你完成:
- 样板代码;
- 重复修改;
- 代码解释;
- 错误定位;
- 测试初稿;
- 文档和README;
- 小范围重构。
人仍然应该负责:
- 产品需求;
- 架构边界;
- 安全;
- 验收标准;
- 关键代码审查;
- 最终发布。
二十、完整项目交付清单
在认为项目完成前,逐项确认:
功能
- [ ] 所有页面可以访问
- [ ] 表单校验正常
- [ ] 状态更新和删除正常
- [ ] 刷新后数据仍存在
- [ ] 404页面存在
- [ ] 深色模式持久化
质量
- [ ] Lint通过
- [ ] TypeScript通过
- [ ] 单元测试通过
- [ ] 关键E2E流程通过
- [ ] 生产构建通过
- [ ] 无生产依赖高危漏洞
用户体验
- [ ] 手机端无横向滚动
- [ ] 键盘可以完成主要操作
- [ ] 图标按钮有可访问名称
- [ ] 错误信息清晰
- [ ] 加载和空状态合理
- [ ] 颜色对比度可接受
安全与部署
- [ ] `.env`已加入`.gitignore`
- [ ] 仓库没有密钥
- [ ] API通过后端代理
- [ ] 正式部署属于自己的账号
- [ ] 环境变量在部署平台配置
- [ ] 有日志和回滚方案
二十一、最终结论
Windsurf能够把前端项目开发从“不断在编辑器、浏览器、终端和文档之间切换”,变成一个连续的AI协作流程。
但高质量使用方式不是:
帮我一次性做完一个网站。
而是:
1. 把需求写进仓库;
2. 用`AGENTS.md`固定规则;
3. 先让AI做计划;
4. 按数据、路由、组件、样式和测试分阶段实现;
5. 每阶段检查Diff;
6. 用Lint、类型检查、测试和构建形成闭环;
7. 最后才部署。
本次可复现实测项目最终达到:
- 0个Lint问题;
- 0个TypeScript错误;
- 4项单元测试全部通过;
- 生产构建成功;
- 主JavaScript Gzip约105.79 KB;
- 生产依赖安全审计0项漏洞。
这组数据并不证明AI可以取代前端工程师。
它证明的是:
当需求、规则和验证命令足够明确时,Windsurf可以把AI从“代码生成器”变成真正参与交付流程的开发代理。
参考资料
1. [Windsurf / Devin Desktop Getting Started](https://docs.devin.ai/desktop/getting-started)
2. [Cascade Overview](https://docs.devin.ai/desktop/cascade/cascade)
3. [Memories, Rules and AGENTS.md](https://docs.devin.ai/desktop/cascade/memories)
4. [Windsurf Terminal and Auto-Execution Levels](https://docs.devin.ai/desktop/terminal)
5. [Windsurf App Deploys](https://docs.devin.ai/desktop/cascade/app-deploys)
6. [Windsurf Plans and Usage](https://docs.devin.ai/desktop/accounts/usage)
7. [Windsurf Pricing Update, March 2026](https://devin.ai/blog/windsurf-pricing-plans)
8. [Vite Getting Started](https://vite.dev/guide/)
9. [Playwright Installation and Overview](https://playwright.dev/docs/intro)