注意
- 关于版本
Lerna 3.x 不支持使用官方 NPM 以外的 npm-cli 发布(即 npm-client 配置),因为其使用了开源的发布工具(对 NPM 进行了封装)进行发布
而 Lerna 2.x 是从每个包的目录执行 npmClient publish,所以如果是需要替换发布的客户端,只能使用 Lerna 2.x - 关于构建
Lerna 通过软链关联各个内部包的依赖,有些构建程序会把软链进来的依赖进行再次打包如 jsx2mp,需要注意 - 关于多 Git 仓库
Lerna 2.x 不支持 git submodule,发布的时候会阻塞,如果使用了 skip-git 命令,则无法自动打 tag,造成每次都和最后一次(旧的)tag 进行对比
普通用法
安装
npm i -g lerna
初始化
lerna init # 默认使用同一版本号lerna init --independent # 独立模式
创建一个 package
lerna create @mo-demo/cli
给 Package 安装依赖
# 为所有 package 增加 chalk 模块lerna add chalk# 为某个模块 A 增加依赖lerna add other --scope @mods/A# 为某个模块增加内部 package 依赖lerna add @mods/B --scope @mods/Alerna add @mods/B --scope @mods/A --dev (dev 依赖)
卸载依赖
# 将 npm-list 包下的 listr 卸载lerna exec --scope=npm-list npm remove listr# 将所有包下的 listr 卸载$ lerna exec -- yarn remove listr
查看包状态
# 查看所有包版本lerna ls# 查看哪些包更新过lerna updated# 查看所有包的修改lerna diff
高级用法
避免依赖冗余
# 将所有 packages 依赖安装在根目录 node_module 共用# 如有不同版本依赖报 warnlerna bootstrap --hoist# 将所有 packages 依赖 xxxx 组件安装在根目录共用lerna bootstrap --hoist xxxx# 禁止将 babel-* 依赖共用lerna bootstrap --hoist --nohoist=babel-*# 清除依赖(node_modules)lerna clean
运行各包的 npm script
# 在所有包下运行指定lerna run <script> -- [..args] # 例子lerna run testlerna run --scope my-component test
lerna.json 配置
{ "lerna": "2.0.0", "version": "1.1.3", "commands": { "publish": { "ignore": [ "ignored-file", "*.md" ] }, "bootstrap": { "ignore": "component-*" } }, "packages": ["packages/*"]}
- lerna: the current version of Lerna being used.
- version: the current version of the repository.
- commands.publish.ignore: an array of globs that won’t be included in lerna updated/publish. Use this to prevent publishing a new version unnecessarily for changes, such as fixing a README.md typo.
- commands.bootstrap.ignore: an array of globs that won’t be bootstrapped when running the lerna bootstrap command.
- commands.bootstrap.scope: an array of globs that restricts which packages will be bootstrapped when running the lerna bootstrap command.
- packages: Array of globs to use as package locations.
构建工具整合
相关连接
- 基于 Lerna 管理 packages 的 Monorepo 项目最佳实践
- Lerna 2.x 文档
- lerna管理前端packages的最佳实践