Appearance
Trae 项目规则教程
📚 什么是 Trae 项目规则
Trae 项目规则(.traerules 文件)是 Trae 编辑器的核心配置文件,用于定义 AI 的行为准则、编码规范和项目特定要求。它类似于 Cursor 的 .cursorrules 文件,但提供了更丰富的配置选项。
规则的作用
- 统一代码风格:确保 AI 生成的代码符合项目的编码规范
- 减少重复指令:避免每次对话都重复相同的要求
- 提高代码质量:引导 AI 遵循最佳实践
- 增强团队协作:确保团队成员使用一致的规则
🔧 创建和配置 .traerules 文件
1. 创建文件
在项目根目录创建一个名为 .traerules 的文件:
bash
# 在项目根目录执行
touch .traerules2. 配置格式
Trae 支持两种配置格式:
YAML 格式(推荐)
yaml
# 编码风格
codeStyle:
indentation: 2 spaces
naming:
variables: camelCase
functions: camelCase
classes: PascalCase
constants: UPPER_SNAKE_CASE
# 技术栈
techStack:
framework: React
language: TypeScript
styling: Tailwind CSS
# 输出要求
output:
comments: true
testing: true
documentation: trueMarkdown 格式
markdown
# Trae Project Rules
## 技术栈
- Framework: React
- Language: TypeScript
- Styling: Tailwind CSS
## 编码规范
- 缩进:2个空格
- 命名:camelCase (变量/函数), PascalCase (类), UPPER_SNAKE_CASE (常量)
- 注释:必须为复杂逻辑添加 JSDoc 注释
## 输出要求
- 必须包含单元测试
- 必须提供文档
- 必须遵循项目的 Git 提交规范🎯 规则的核心配置项
1. 编码风格 (codeStyle)
yaml
codeStyle:
indentation: 2 spaces # 缩进方式和大小
naming: # 命名规范
variables: camelCase
functions: camelCase
classes: PascalCase
constants: UPPER_SNAKE_CASE
formatting: # 代码格式
semicolons: true
singleQuotes: true
trailingCommas: all
bracketSpacing: true2. 技术栈 (techStack)
yaml
techStack:
framework: React # 前端框架
language: TypeScript # 编程语言
styling: Tailwind CSS # 样式方案
stateManagement: Zustand # 状态管理
api: React Query # API 处理
buildTool: Vite # 构建工具3. 目录结构 (directoryStructure)
yaml
directoryStructure:
components: src/components/ # 组件目录
pages: src/pages/ # 页面目录
hooks: src/hooks/ # 自定义 Hooks 目录
utils: src/utils/ # 工具函数目录
api: src/api/ # API 目录
types: src/types/ # 类型定义目录4. 输出要求 (output)
yaml
output:
comments: true # 是否需要注释
testing: true # 是否需要测试
documentation: true # 是否需要文档
gitCommit: true # 是否需要 Git 提交信息
errorHandling: true # 是否需要错误处理5. 禁止事项 (restrictions)
yaml
restrictions:
patterns: # 禁止的代码模式
- eval
- var
- console.log
libraries: # 禁止使用的库
- jQuery
- lodash
practices: # 禁止的实践
- inlineStyles
- anyType6. AI 行为 (aiBehavior)
yaml
aiBehavior:
adaptiveThinking: true # 是否启用自适应思考
planBeforeExecution: true # 是否在执行前制定计划
errorAnalysis: true # 是否分析错误
codeReview: true # 是否进行代码审查7. Vitepress 特定配置 (vitepress)
yaml
vitepress:
sidebar: true # 自动更新 sidebar 配置
examples: true # 教程文档尽量举例说明
规范: Vitepress # 按照 Vitepress 规范编写文档
version: 1.6.4 # Vitepress 版本🌐 规则的优先级
Trae 的规则优先级从高到低:
- 目录级规则:特定目录下的
.traerules文件 - 项目级规则:项目根目录的
.traerules文件 - 全局规则:Trae 的默认规则
规则继承
子目录的规则会继承父目录的规则,同时可以覆盖特定配置:
yaml
# 子目录的 .traerules
inherit: true # 继承父目录规则
# 覆盖特定配置
codeStyle:
indentation: 4 spaces # 覆盖父目录的 2 spaces📝 不同类型项目的规则模板
前端项目模板
yaml
# 前端项目 .traerules
codeStyle:
indentation: 2 spaces
naming:
variables: camelCase
functions: camelCase
classes: PascalCase
constants: UPPER_SNAKE_CASE
formatting:
semicolons: true
singleQuotes: true
trailingCommas: all
techStack:
framework: React
language: TypeScript
styling: Tailwind CSS
stateManagement: Zustand
api: React Query
buildTool: Vite
directoryStructure:
components: src/components/
pages: src/pages/
hooks: src/hooks/
utils: src/utils/
api: src/api/
types: src/types/
output:
comments: true
testing: true
documentation: true
gitCommit: true
restrictions:
patterns:
- eval
- var
- console.log
libraries:
- jQuery
practices:
- inlineStyles
- anyType
aiBehavior:
adaptiveThinking: true
planBeforeExecution: true
errorAnalysis: true
codeReview: true后端项目模板
yaml
# 后端项目 .traerules
codeStyle:
indentation: 4 spaces
naming:
variables: camelCase
functions: camelCase
classes: PascalCase
constants: UPPER_SNAKE_CASE
formatting:
semicolons: true
singleQuotes: false
trailingCommas: es5
techStack:
language: Node.js
framework: Express
database: MongoDB
orm: Mongoose
authentication: JWT
directoryStructure:
routes: src/routes/
controllers: src/controllers/
services: src/services/
models: src/models/
middlewares: src/middlewares/
utils: src/utils/
output:
comments: true
testing: true
documentation: true
gitCommit: true
restrictions:
patterns:
- eval
- var
- console.log
practices:
- hardcodedSecrets
- sqlInjection
aiBehavior:
adaptiveThinking: true
planBeforeExecution: true
errorAnalysis: true
codeReview: trueVitepress 项目模板
yaml
# Vitepress 项目 .traerules
codeStyle:
indentation: 2 spaces
naming:
variables: camelCase
functions: camelCase
classes: PascalCase
constants: UPPER_SNAKE_CASE
formatting:
semicolons: true
singleQuotes: true
trailingCommas: all
techStack:
framework: Vitepress
version: 1.6.4
vue: 3.5.13
typescript: 5.7.2
directoryStructure:
docs: docs/
pages: docs/pages/
config: docs/.vitepress/
output:
comments: true
documentation: true
gitCommit: true
vitepress: true
restrictions:
patterns:
- eval
- var
- console.log
practices:
- inlineStyles
aiBehavior:
adaptiveThinking: true
planBeforeExecution: true
errorAnalysis: true
codeReview: true
# Vitepress 特定规则
vitepress:
sidebar: true # 自动更新 sidebar 配置
examples: true # 教程文档尽量举例说明
规范: Vitepress # 按照 Vitepress 规范编写文档💡 规则的最佳实践
1. 逐步完善规则
- 从基础开始:先设置基本的编码规范和技术栈
- 逐步细化:根据项目需求逐步添加更详细的规则
- 团队共识:与团队成员共同制定和完善规则
2. 规则的使用技巧
- 引用规则:在对话中明确引用规则:"请根据 .traerules 的规范实现此功能"
- 规则更新:当项目需求变化时,及时更新规则
- 版本控制:将 .traerules 文件纳入版本控制系统
3. 规则的验证
- 代码审查:定期审查 AI 生成的代码是否符合规则
- 测试验证:确保生成的代码通过测试
- 性能检查:确保规则不会导致性能问题
4. Vitepress 项目特定实践
- sidebar 自动更新:确保新文档自动添加到 themeConfig 中的 sidebar 配置
- Vitepress 规范:按照 Vitepress 的规范编写文档,包括 frontmatter、目录结构等
- 示例说明:编写教程文档时,尽量提供具体示例
- 版本管理:明确指定 Vitepress、Vue 和 TypeScript 的版本
🔍 常见问题和解决方案
问题 1:规则不生效
解决方案:
- 检查文件路径:确保 .traerules 文件在正确的位置
- 检查文件格式:确保 YAML 或 Markdown 格式正确
- 检查规则语法:确保规则配置项正确
问题 2:规则冲突
解决方案:
- 检查规则优先级:了解目录级规则会覆盖项目级规则
- 明确规则继承:使用
inherit: true确保规则正确继承 - 统一规则管理:定期检查和统一项目中的规则
问题 3:规则过于严格
解决方案:
- 分层规则:为不同场景设置不同严格程度的规则
- 灵活配置:使用条件规则适应不同情况
- 渐进式实施:逐步实施严格规则,给团队适应时间
🚀 高级技巧
1. 条件规则
yaml
# 条件规则示例
rules:
- name: production
condition: env === 'production'
restrictions:
patterns:
- console.log
- name: development
condition: env === 'development'
output:
comments: false2. 规则模板
创建规则模板库,为不同类型的项目提供预设规则:
bash
# 创建规则模板目录
mkdir -p .traerules/templates
# 前端项目模板
cat > .traerules/templates/frontend.yaml << 'EOF'
# 前端项目规则模板
codeStyle:
indentation: 2 spaces
# ...
EOF
# 后端项目模板
cat > .traerules/templates/backend.yaml << 'EOF'
# 后端项目规则模板
codeStyle:
indentation: 4 spaces
# ...
EOF3. 规则自动化
使用脚本自动生成和更新规则:
javascript
// update-rules.js
const fs = require('fs');
const path = require('path');
// 读取 package.json 提取技术栈信息
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));
// 生成规则
const rules = {
techStack: {
framework: packageJson.dependencies.react ? 'React' : 'Unknown',
language: packageJson.devDependencies.typescript ? 'TypeScript' : 'JavaScript',
// ...
},
// ...
};
// 写入 .traerules 文件
fs.writeFileSync(
path.join(__dirname, '.traerules'),
JSON.stringify(rules, null, 2)
);
console.log('.traerules updated successfully!');📊 规则效果评估
评估指标
- 代码质量:ESLint 错误数量减少
- 开发效率:AI 生成代码的准确率提高
- 团队一致性:代码风格统一程度
- 维护成本:代码维护难度降低
评估方法
- 前后对比:实施规则前后的代码质量对比
- 团队反馈:收集团队成员对规则的反馈
- 自动化检查:使用工具检查代码是否符合规则
- 性能分析:评估规则对开发效率的影响
🎯 总结
Trae 项目规则是提升开发效率和代码质量的强大工具。通过合理配置和使用规则,可以:
- 统一代码风格:确保团队代码风格一致
- 减少重复指令:避免每次对话都重复相同的要求
- 提高代码质量:引导 AI 遵循最佳实践
- 增强团队协作:确保团队成员使用一致的标准
核心原则:
- 规则应该服务于项目需求,而不是限制创造力
- 规则应该是可维护和可扩展的
- 规则应该与团队的开发流程相适应
- 规则应该定期更新以适应项目的变化
通过本教程,你应该已经掌握了 Trae 项目规则的创建、配置和使用方法。现在,开始为你的项目创建定制化的规则,享受更高效、更优质的开发体验吧!