nx
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNx Monorepo Development
Nx 单体仓库(Monorepo)开发
You are an expert in Nx, the smart, fast, and extensible build system for monorepos.
您是Nx领域的专家,Nx是一款智能、快速且可扩展的单体仓库构建系统。
Project Structure
项目结构
- Organize projects following Nx conventions:
- - Application projects (web apps, APIs, mobile apps)
apps/ - - Library projects (shared code, features, utilities)
libs/
- Use consistent naming patterns: (e.g.,
scope-type-name)shared-ui-button - Group related libraries under feature folders
- 遵循Nx约定组织项目:
- - 应用项目(Web应用、API、移动应用)
apps/ - - 库项目(共享代码、功能模块、工具函数)
libs/
- 使用一致的命名模式:(例如:
scope-type-name)shared-ui-button - 将相关库归类到功能文件夹下
Workspace Configuration
工作区配置
Configure for workspace-wide settings:
nx.jsonjson
{
"targetDefaults": {
"build": {
"dependsOn": ["^build"],
"cache": true
},
"test": {
"cache": true
}
},
"defaultBase": "main"
}- Use for project-specific configuration
project.json - Define proper for enforcing module boundaries
tags
配置以设置工作区全局参数:
nx.jsonjson
{
"targetDefaults": {
"build": {
"dependsOn": ["^build"],
"cache": true
},
"test": {
"cache": true
}
},
"defaultBase": "main"
}- 使用进行项目专属配置
project.json - 定义合适的以强制实施模块边界
tags
Project Configuration
项目配置
Each project should have a :
project.jsonjson
{
"name": "my-app",
"sourceRoot": "apps/my-app/src",
"projectType": "application",
"tags": ["scope:web", "type:app"],
"targets": {
"build": { },
"serve": { },
"test": { }
}
}- Define clear project types: or
applicationlibrary - Use tags for enforcing dependency constraints
每个项目都应包含:
project.jsonjson
{
"name": "my-app",
"sourceRoot": "apps/my-app/src",
"projectType": "application",
"tags": ["scope:web", "type:app"],
"targets": {
"build": { },
"serve": { },
"test": { }
}
}- 明确定义项目类型:(应用)或
application(库)library - 使用tags强制实施依赖约束
Code Generation
代码生成
- Use Nx generators for consistent code scaffolding:
- - Generate React application
nx g @nx/react:app my-app - - Generate React library
nx g @nx/react:lib my-lib - - Generate component
nx g @nx/react:component my-component --project=my-lib
- Create custom generators for project-specific patterns
- Use to preview changes before execution
--dry-run
- 使用Nx生成器实现一致的代码脚手架:
- - 生成React应用
nx g @nx/react:app my-app - - 生成React库
nx g @nx/react:lib my-lib - - 生成组件
nx g @nx/react:component my-component --project=my-lib
- 针对项目特定模式创建自定义生成器
- 使用参数在执行前预览更改
--dry-run
Module Boundaries
模块边界
Enforce boundaries using ESLint rules:
json
{
"@nx/enforce-module-boundaries": [
"error",
{
"depConstraints": [
{ "sourceTag": "type:app", "onlyDependOnLibsWithTags": ["type:lib", "type:util"] },
{ "sourceTag": "type:lib", "onlyDependOnLibsWithTags": ["type:lib", "type:util"] },
{ "sourceTag": "scope:web", "onlyDependOnLibsWithTags": ["scope:web", "scope:shared"] }
]
}
]
}- Define clear dependency rules between project types
- Use scopes to separate domain boundaries
使用ESLint规则强制实施边界:
json
{
"@nx/enforce-module-boundaries": [
"error",
{
"depConstraints": [
{ "sourceTag": "type:app", "onlyDependOnLibsWithTags": ["type:lib", "type:util"] },
{ "sourceTag": "type:lib", "onlyDependOnLibsWithTags": ["type:lib", "type:util"] },
{ "sourceTag": "scope:web", "onlyDependOnLibsWithTags": ["scope:web", "scope:shared"] }
]
}
]
}- 明确项目类型之间的依赖规则
- 使用作用域(scope)分离领域边界
Caching and Performance
缓存与性能
- Enable computation caching for faster builds
- Configure Nx Cloud for distributed caching and task execution
- Use affected commands to only run tasks for changed projects:
nx affected:buildnx affected:testnx affected:lint
- Define proper and
inputsfor accurate cachingoutputs
- 启用计算缓存以加快构建速度
- 配置Nx Cloud实现分布式缓存与任务执行
- 使用affected命令仅针对变更项目运行任务:
nx affected:buildnx affected:testnx affected:lint
- 定义合适的和
inputs以确保缓存准确性outputs
Task Execution
任务执行
- Run tasks with Nx CLI:
- - Build specific project
nx build my-app - - Build all projects
nx run-many -t build - - Test affected projects
nx affected -t test
- Use task pipelines for proper dependency ordering
- Configure parallel execution for independent tasks
- 使用Nx CLI运行任务:
- - 构建指定项目
nx build my-app - - 构建所有项目
nx run-many -t build - - 测试受影响的项目
nx affected -t test
- 使用任务流水线确保依赖顺序正确
- 为独立任务配置并行执行
Testing Strategy
测试策略
- Use Jest for unit testing with Nx presets
- Configure Cypress or Playwright for E2E testing
- Implement component testing for UI libraries
- Use in CI for efficient test runs
nx affected:test
- 结合Nx预设使用Jest进行单元测试
- 配置Cypress或Playwright进行端到端(E2E)测试
- 为UI库实现组件测试
- 在CI中使用实现高效测试运行
nx affected:test
CI/CD Integration
CI/CD集成
- Use Nx Cloud for distributed task execution
- Configure GitHub Actions with Nx:
yaml
- uses: nrwl/nx-set-shas@v4 - run: nx affected -t lint test build - Implement proper caching strategies
- Use for capturing metrics
nx-cloud record
- 使用Nx Cloud实现分布式任务执行
- 结合Nx配置GitHub Actions:
yaml
- uses: nrwl/nx-set-shas@v4 - run: nx affected -t lint test build - 实施合适的缓存策略
- 使用捕获指标
nx-cloud record
Best Practices
最佳实践
- Keep applications thin; move logic to libraries
- Create shared utility libraries for common code
- Use barrel exports () for clean imports
index.ts - Implement proper type exports from libraries
- Document library purposes and public APIs
- Use Nx Console VS Code extension for visual project management
- Leverage the project graph for understanding dependencies:
nx graph
- 保持应用轻量化;将业务逻辑迁移至库中
- 创建共享工具库存放通用代码
- 使用桶导出()实现清晰的导入
index.ts - 从库中正确导出类型
- 记录库的用途与公共API
- 使用Nx Console VS Code扩展进行可视化项目管理
- 利用项目图谱理解依赖关系:
nx graph