Monorepo
Monorepo 实现方式在该链接已经分析的比较透彻, 一共有如下几个实现方式:
- Lerna + npm
- Lerna + yarn
- yarn workspace
- Lerna + yarn workspace
- yarn 3.0 + pnp
lerna
If we installed the lernal locally, we need run
lerna
with npx

Lerna 使用踩坑
在安装项目的过程中, 如果使用
npm install
的 hook
(例如 postinstall
) 直接触发 lerna run bootstrap --hoist
的话, 会导致 npm 包提升不彻底, 会造成重复引入类型,包导致构建失败, 目前有两个解决方案:- 手动分开运行
npm install
和npm run bootstrap
- 加一个新的命令
npm run init
, 主动触发npm install
和npm run bootstrap
Yarn Workspace
优点:
yarn workspace
的安装速度比较快
- 安装时不会临时更改
package.json
- 所有依赖都安装到根项目
node_modules
, 非必要情况下不会在子包中生成node_modules
- 安装/更新/删除单个
package
下的包的话支持也比较好
缺点:
- 因为所有的子项目都会被链接到
node_modules
里面, 所以会有加载多个库类型的问题, 解决方法在此
依赖管理
Reference
Why Lerna and Yarn Workspaces is a Perfect Match for Building Mono-Repos – A Close Look at Features and Performance