Nx

mindrally/skills/nx

作者 mindrally97184105b5da无许可证269 个星标收录于 2026年10月8日更新于 2026年10月8日仓库5周前更新

Best practices for Nx monorepo development, project configuration, and code generation

AI 生成的概览

关于 Nx 单体仓库结构、项目配置、代码生成、模块边界、缓存与 CI 的实践指导。

功能
该技能提供在 Nx 单体仓库中工作的最佳实践指导。内容涵盖工作区与项目配置文件、用于生成应用、库和组件的生成器命令、ESLint 模块边界规则、缓存与受影响任务命令、测试配置以及 CI/CD 集成。它仅是指令型参考,本身不产出文件或脚本。
适用场景
适用于搭建、配置或维护 Nx 单体仓库时,或在决定如何组织应用与库、约束依赖边界,以及通过缓存和受影响命令加速构建与测试时使用。
运行要求
不包含脚本。该指导假定已有 Nx 工作区,并需要 Nx CLI、Node.js 工具链,以及可选的 Nx Cloud、ESLint、Jest、Cypress 或 Playwright,和 GitHub Actions 等 CI 服务。

Nx Monorepo Development

You are an expert in Nx, the smart, fast, and extensible build system for monorepos.

Project Structure

  • Organize projects following Nx conventions:
    • apps/ - Application projects (web apps, APIs, mobile apps)
    • libs/ - Library projects (shared code, features, utilities)
  • Use consistent naming patterns: scope-type-name (e.g., shared-ui-button)
  • Group related libraries under feature folders

Workspace Configuration

Configure nx.json for workspace-wide settings:

json
{  "targetDefaults": {    "build": {      "dependsOn": ["^build"],      "cache": true    },    "test": {      "cache": true    }  },  "defaultBase": "main"}
  • Use project.json for project-specific configuration
  • Define proper tags for enforcing module boundaries

Project Configuration

Each project should have a project.json:

json
{  "name": "my-app",  "sourceRoot": "apps/my-app/src",  "projectType": "application",  "tags": ["scope:web", "type:app"],  "targets": {    "build": { },    "serve": { },    "test": { }  }}
  • Define clear project types: application or library
  • Use tags for enforcing dependency constraints

Code Generation

  • Use Nx generators for consistent code scaffolding:
    • nx g @nx/react:app my-app - Generate React application
    • nx g @nx/react:lib my-lib - Generate React library
    • nx g @nx/react:component my-component --project=my-lib - Generate component
  • Create custom generators for project-specific patterns
  • Use --dry-run to preview changes before execution

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

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:build
    • nx affected:test
    • nx affected:lint
  • Define proper inputs and outputs for accurate caching

Task Execution

  • Run tasks with Nx CLI:
    • nx build my-app - Build specific project
    • nx run-many -t build - Build all projects
    • nx affected -t test - Test affected projects
  • Use task pipelines for proper dependency ordering
  • Configure parallel execution for independent tasks

Testing Strategy

  • Use Jest for unit testing with Nx presets
  • Configure Cypress or Playwright for E2E testing
  • Implement component testing for UI libraries
  • Use nx affected:test in CI for efficient test runs

CI/CD Integration

  • 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 nx-cloud record for capturing metrics

Best Practices

  • Keep applications thin; move logic to libraries
  • Create shared utility libraries for common code
  • Use barrel exports (index.ts) for clean imports
  • 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

来源与署名

来源:mindrally/skills位于nx提交9718410

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架