Nx
概述
Nx 是一个针对 monorepo 的构建系统。它提供计算缓存(本地 + 远程)、任务编排、依赖关系图、代码生成器以及受影响的命令。支持 React、Angular、Node.js、Next.js 和任何语言。
说明
步骤 1:创建工作区
npx create-nx-workspace@latest my-org --preset=ts
cd my-org
步骤 2:添加项目
nx g @nx/react:app my-app
nx g @nx/js:lib shared-utils
nx g @nx/node:app api
步骤 3:运行任务
nx serve my-app
nx build api
nx test shared-utils
nx affected -t test # 仅运行受影响的测试
nx run-many -t build test # 运行所有构建和测试
步骤 4:nx.json 配置
// nx.json — 工作区配置
{
"targetDefaults": {
"build": { "dependsOn": ["^build"], "cache": true },
"test": { "cache": true },
"lint": { "cache": true }
}
}
指导原则
- Nx 会缓存任务结果 — 如果输入没有变化,第二次运行将瞬间完成。
- 在 CI 中使用
nx affected仅构建/测试 PR 中受影响的代码。 - Nx Cloud 提供远程缓存 — 在团队或 CI 之间共享缓存。
- Nx 无需插件即可工作 — 使用
npx nx init将其添加到任何仓库中。