Master monorepo management with Turborepo, Nx, and pnpm workspaces to build efficient, scalable multi-package repositories with optimized builds and dependency management. Use when setting up monorepos, optimizing builds, or managing shared dependencies.
构建高效、可扩展的单体仓库,实现跨多个包和应用程序的代码共享、一致的工具链和原子化变更。
优势:
挑战:
包管理器:
构建系统:
bash
json
// turbo.json
{
$schema: https://turbo.build/schema.json,
globalDependencies: [/.env.*local],
pipeline: {
build: {
dependsOn: [^build],
outputs: [dist/, .next/, !.next/cache/]
},
test: {
dependsOn: [build],
outputs: [coverage/]
},
lint: {
outputs: []
},
dev: {
cache: false,
persistent: true
},
type-check: {
dependsOn: [^build],
outputs: []
}
}
}
json
// package.json(根目录)
{
name: my-monorepo,
private: true,
workspaces: [apps/, packages/],
scripts: {
build: turbo run build,
dev: turbo run dev,
test: turbo run test,
lint: turbo run lint,
format: prettier --write \/*.{ts,tsx,md}\,
clean: turbo run clean && rm -rf node_modules
},
devDependencies: {
turbo: ^1.10.0,
prettier: ^3.0.0,
typescript: ^5.0.0
},
packageManager: pnpm@8.0.0
}
json
// packages/ui/package.json
{
name: @repo/ui,
version: 0.0.0,
private: true,
main: ./dist/index.js,
types: ./dist/index.d.ts,
exports: {
.: {
import: ./dist/index.js,
types: ./dist/index.d.ts
},
./button: {
import: ./dist/button.js,
types: ./dist/button.d.ts
}
},
scripts: {
build: tsup src/index.ts --format esm,cjs --dts,
dev: tsup src/index.ts --format esm,cjs --dts --watch,
lint: eslint src/,
type-check: tsc --noEmit
},
devDependencies: {
@repo/tsconfig: workspace:*,
tsup: ^7.0.0,
typescript: ^5.0.0
},
dependencies: {
react: ^18.2.0
}
}
yaml
json
// .npmrc
bash
bash
bash
json
// nx.json
{
extends: nx/presets/npm.json,
$schema: ./node_modules/nx/schemas/nx-schema.json,
targetDefaults: {
build: {
dependsOn: [^build],
inputs: [production, ^production],
cache: true
},
test: {
inputs: [default, ^production, {workspaceRoot}/jest.preset.js],
cache: true
},
lint: {
inputs: [default, {workspaceRoot}/.eslintrc.json],
cache: true
}
},
namedInputs: {
default: [{projectRoot}//*, sharedGlobals],
production: [
default,
!{projectRoot}//?(*.)+(spec|test).[jt]s?(x)?(.snap),
!{projectRoot}/tsconfig.spec.json
],
sharedGlobals: []
}
}
bash
json
// packages/tsconfig/base.json
{
compilerOptions: {
strict: true,
esModuleInterop: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
module: ESNext,
moduleResolution: bundler,
resolveJsonModule: true,
isolatedModules: true,
incremental: true,
declaration: true
},
exclude: [node_modules]
}
// packages/tsconfig/react.json
{
extends: ./base.json,
compilerOptions: {
jsx: react-jsx,
lib: [ES2022, DOM, DOM.Iterable]
}
}
// apps/web/tsconfig.json
{
extends: @repo/tsconfig/react.json,
compilerOptions: {
outDir: dist,
rootDir: src
},
include: [src],
exclude: [node_modules, dist]
}
javascript
// packages/config/eslint-preset.js
module.exports = {
extends: [
eslint:recommended,
plugin:@typescript-eslint/recommended,
plugin:react/re
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 monorepo-management-1776420088 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 monorepo-management-1776420088 技能
skillhub install monorepo-management-1776420088
文件大小: 5.04 KB | 发布时间: 2026-4-17 18:49