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即可加入 Nx。