问题描述
所以我有一棵像
的树├── package.json
├── tsconfig.json
└── packages
├── lib1
│ ├── package.json
│ ├── src
│ │ ├── index.ts
│ └── tsconfig.json
├── lib2
│ ├── package.json
│ ├── src
│ │ ├── index.ts
│ └── tsconfig.json
├── graph
│ ├── package.json
│ ├── src
│ │ ├── index.ts
│ └── tsconfig.json
└── peer
├── package.json
├── src
│ └── index.ts
└── tsconfig.json
其中图依赖于lib2,而依赖于lib1。
{
"compilerOptions": {
"target": "es2018","module": "commonjs","lib": ["es2018"],"moduleResolution": "node","declaration": true,"strict": true,"esModuleInterop": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"sourceMap": true,"resolveJsonModule": true,"outDir": "build"
},"exclude": ["**/node_modules","**/build","**/dist"]
}
{
"extends": "../tsconfig-build.json","compilerOptions": {
"rootDir": "src","outDir": "build"
}
}
当我使用它构建时,在编译时一切都很好
"scripts": {
":g:tsc": "cd $INIT_CWD && tsc --project tsconfig-build.json",
但是如果我尝试使用@vercel/ncc
,则会遇到编译时错误,例如'rootDir' is expected to contain all source files.
"build": "ncc build src/index.ts",
我尝试在paths
中使用references
和tsconfig.json
,但是它们都不符合我的目的,而且打字稿似乎无法正确地查找不同的模块。当我将其指向对等点的index.ts时,它可以正常工作,但是没有工作空间依赖性。
解决方法
我们使用 Lerna 来管理我们的 Monorepo。
https://github.com/lerna/lerna
它允许拥有公共和私人包。公共包可以发布到注册中心,任何依赖于来自同一个 Monorepo 的其他包的包都必须作为依赖项添加到 package.json 中。 Lerna 可以管理依赖包中最终发生的任何更改的版本增量。
我们通过使用 GitHub 操作实现了自动部署,让 Lerna 将公共包发布到 GitHub 注册表,并将所有包包括私有和提交更改增加到主分支。
lerna.json
{
"version": "independent","packages": ["packages/*"],"npmClient": "yarn","ignoreChanges": ["**/*.md"],"useWorkspaces": true,"command": {
"version": {
"conventionalCommits": true,"createRelease": "github","exact": true,"message": "chore(release): publish","preid": "next"
},"publish": {
"distTag": "latest","preDistTag": "next","registry": "https://npm.pkg.github.com/"
}
}
}