lerna

Lerna 是一个管理多个 npm 模块的工具,是 Babel 自己用来维护自己的 Monorepo 并开源出的一个项目。优化维护多包的工作流,解决多个包互相依赖,且发布需要手动维护多个包的问题。

1、安装

推荐全局安装,因为会经常用到 lerna 命令

npm i -g lerna

2、初始化项目

2.1 创建

lerna init

2.2 修改配置

node_modules提出至最顶层集中管理,避免共同依赖的重复安装

learn.json

{
  "packages": [
    "packages/*"
  ],
  "useWorkspaces": true,
  "npmClient": "yarn",  // 默认使用yarn,有利于扁平化依赖管理
  "version": "0.0.0" 
}

package.json

{
  "name": "B2B-IM",
  "private": true, // 主目录私有,确保主目录不会被发布到公网
  "devDependencies": {
    "lerna": "^4.0.0"
  },
  "workspaces": [
    "packages/*"
  ]
}

3、增加项目

lerna add <项目名>

项目整体目录:

~/project/lerna/test master*
❯ tree
.
├── lerna.json
├── package.json
└── packages
    ├── demo1
    │   ├── README.md
    │   ├── __tests__
    │   │   └── demo1.test.js
    │   ├── lib
    │   │   └── demo1.js
    │   └── package.json
    └── demo2
        ├── README.md
        ├── __tests__
        │   └── demo2.test.js
        ├── lib
        │   └── demo2.js
        └── package.json

4、配置依赖

承接上一章中的项目结构

4.1 给某个package安装依赖

yarn workspace <项目名> add <包名>
or
lerna add <包名> --scope=<项目名> (建议)

lerna add eslint packages/demo* :符合 package 的包会装上。

给demo1安装demo2
yarn workspace demo1 add demo2
lerna add demo2 --scope=demo1

4.2 全局增加依赖

yarn workspaces add <包名> 给所有应用都安装依赖
yarn add -W -D <包名> 给根应用安装依赖

lerna add <包名>

5、项目运行

yarn workspace @baidu/b2b-im-pc run serve
yarn workspaces run lib

lerna run test 则会执行所有子项目中的 test 
lerna run --scope package1 test 只执行 package1 中的 test 
lerna run --ignore package-* test 只执行除了匹配 package-* 外的项目中的 test

6.发布

lerna publish

~/project/lerna/demo/learn master
❯ lerna publish
info cli using local version of lerna
lerna notice cli v4.0.0
lerna info versioning independent
lerna info publish rooted leaf detected, skipping synthetic root lifecycles
lerna info Assuming all packages changed
? Select a new version for @baidu/b2b-im-pc-utils (currently 0.0.0) Patch (0.0.1)
? Select a new version for @baidu/b2b-im-pc (currently 0.0.0) Patch (0.0.1)
? Select a new version for @baidu/b2b-im-utils (currently 0.0.1) Patch (0.0.2)
? Select a new version for b2b-im (currently 0.0.0) Patch (0.0.1)

Changes:
 - @baidu/b2b-im-pc-utils: 0.0.0 => 0.0.1
 - @baidu/b2b-im-pc: 0.0.0 => 0.0.1
 - @baidu/b2b-im-utils: 0.0.1 => 0.0.2
 - b2b-im: 0.0.0 => 0.0.1 (private)

? Are you sure you want to publish these packages? Yes
lerna info execute Skipping releases
lerna info git Pushing tags...
lerna info publish Publishing packages to npm...
lerna notice Skipping all user and access validation due to third-party registry
lerna notice Make sure you're authenticated properly ¯\_(ツ)_/¯
lerna WARN ENOLICENSE Packages @baidu/b2b-im-pc-utils, @baidu/b2b-im-pc, and @baidu/b2b-im-utils are missing a license.
lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.
lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.
lerna http fetch PUT 201 http://registry.npm.baidu-int.com/@baidu%2fb2b-im-utils 161ms
lerna success published @baidu/b2b-im-utils 0.0.2
lerna notice
lerna notice 📦  @baidu/b2b-im-utils@0.0.2
lerna notice === Tarball Contents ===
lerna notice 1.7kB package.json
lerna notice 141B  README.md
lerna notice 94B   lib/index.d.ts
lerna notice === Tarball Details ===
lerna notice name:          @baidu/b2b-im-utils
lerna notice version:       0.0.2
lerna notice filename:      baidu-b2b-im-utils-0.0.2.tgz
lerna notice package size:  972 B
lerna notice unpacked size: 2.0 kB
lerna notice shasum:        489019f19245d1780fcb8b2ab86f433a5831c42a
lerna notice integrity:     sha512-z5GjxCl3akAnr[...]E/0WL2Lj3HdBg==
lerna notice total files:   3
lerna notice
lerna http fetch PUT 201 http://registry.npm.baidu-int.com/@baidu%2fb2b-im-pc-utils 578ms
lerna success published @baidu/b2b-im-pc-utils 0.0.1
lerna notice
lerna notice 📦  @baidu/b2b-im-pc-utils@0.0.1
lerna notice === Tarball Contents ===
lerna notice 1.2MB lib/index.js
lerna notice 1.9kB package.json
lerna notice 149B  README.md
lerna notice === Tarball Details ===
lerna notice name:          @baidu/b2b-im-pc-utils
lerna notice version:       0.0.1
lerna notice filename:      baidu-b2b-im-pc-utils-0.0.1.tgz
lerna notice package size:  365.3 kB
lerna notice unpacked size: 1.3 MB
lerna notice shasum:        250c9b3494707af2fa332fa0519d051269820ede
lerna notice integrity:     sha512-XBedAWcy5DJum[...]8AFIGVqvRdBTQ==
lerna notice total files:   3
lerna notice
lerna http fetch PUT 201 http://registry.npm.baidu-int.com/@baidu%2fb2b-im-pc 410ms
lerna success published @baidu/b2b-im-pc 0.0.1
lerna notice
lerna notice 📦  @baidu/b2b-im-pc@0.0.1
lerna notice === Tarball Contents ===
lerna notice 1.7kB package.json
lerna notice 1.2kB README.md
lerna notice === Tarball Details ===
lerna notice name:          @baidu/b2b-im-pc
lerna notice version:       0.0.1
lerna notice filename:      baidu-b2b-im-pc-0.0.1.tgz
lerna notice package size:  1.5 kB
lerna notice unpacked size: 2.8 kB
lerna notice shasum:        dff807ac612f8fa5534847b1a8655ab5d60b1e33
lerna notice integrity:     sha512-aAq6ym3qpwq18[...]rNY2GkPV/MViQ==
lerna notice total files:   2
lerna notice
Successfully published:
 - @baidu/b2b-im-pc-utils@0.0.1
 - @baidu/b2b-im-pc@0.0.1
 - @baidu/b2b-im-utils@0.0.2
lerna success published 3 packages

lerna changed

列出下次发版lerna publish 要更新的包。

原理: 需要先git add,git commit 提交。 然后内部会运行git diff --name-only v版本号,搜集改动的包,就是下次要发布的

~/project/lerna/demo/learn master
❯ lerna changed
info cli using local version of lerna
lerna notice cli v4.0.0
lerna info versioning independent
lerna info Looking for changed packages since @baidu/b2b-im-pc-utils@0.0.2-alpha.0
@baidu/b2b-im-pc
lerna success found 1 package ready to publish
❯ lerna publish
info cli using local version of lerna
lerna notice cli v4.0.0
lerna info versioning independent
lerna info publish rooted leaf detected, skipping synthetic root lifecycles
lerna info Looking for changed packages since @baidu/b2b-im-pc-utils@0.0.2-alpha.0
? Select a new version for @baidu/b2b-im-pc (currently 0.0.2-alpha.0) (Use arrow keys)
❯ Patch (0.0.2)
  Minor (0.1.0)
  Major (1.0.0)
  Prepatch (0.0.3-alpha.0)
  Preminor (0.1.0-alpha.0)
  Premajor (1.0.0-alpha.0)
  Custom Prerelease
  Custom Version
  
  
  
? Are you sure you want to publish these packages? Yes
lerna info execute Skipping releases
lerna info git Pushing tags...
lerna info publish Publishing packages to npm...
lerna notice Skipping all user and access validation due to third-party registry
lerna notice Make sure you're authenticated properly ¯\_(ツ)_/¯
lerna WARN ENOLICENSE Package @baidu/b2b-im-pc is missing a license.
lerna WARN ENOLICENSE One way to fix this is to add a LICENSE.md file to the root of this repository.
lerna WARN ENOLICENSE See https://choosealicense.com for additional guidance.
lerna http fetch PUT 201 http://registry.npm.baidu-int.com/@baidu%2fb2b-im-pc 475ms
lerna success published @baidu/b2b-im-pc 0.0.2
lerna notice
lerna notice 📦  @baidu/b2b-im-pc@0.0.2
lerna notice === Tarball Contents ===
lerna notice 28.3kB  lib/b2b-im-pc.css
lerna notice 18.2kB  lib/static/fonts/iconfont.f26d2881.eot
lerna notice 188B    lib/demo.html
lerna notice 792.8kB lib/b2b-im-pc.common.js
lerna notice 793.9kB lib/b2b-im-pc.umd.js
lerna notice 183.9kB lib/b2b-im-pc.umd.min.js
lerna notice 346.0kB lib/components/helloWorld.js
lerna notice 42.6kB  lib/components/icon.js
lerna notice 294.5kB lib/components/notification.js
lerna notice 1.7kB   package.json
lerna notice 1.2kB   README.md
lerna notice 77.6kB  lib/static/img/iconfont.aae0f63e.svg
lerna notice 18.0kB  lib/static/fonts/iconfont.1a078e52.ttf
lerna notice 10.8kB  lib/static/fonts/iconfont.866d3af0.woff
lerna notice === Tarball Details ===
lerna notice name:          @baidu/b2b-im-pc
lerna notice version:       0.0.2
lerna notice filename:      baidu-b2b-im-pc-0.0.2.tgz
lerna notice package size:  682.5 kB
lerna notice unpacked size: 2.6 MB
lerna notice shasum:        8d5612b8c391b184704fd14bd4bce3528e6fa977
lerna notice integrity:     sha512-HE1Bsf/+Olzgf[...]OSKg7cpENHd0g==
lerna notice total files:   14
lerna notice
Successfully published:
 - @baidu/b2b-im-pc@0.0.2
lerna success published 1 package

7、注意事项

7.1 请勿使用lerna create 后,先 add 本地包再使用vue-cli 创建项目

add 本地包后由于该包在npm上无法找到,会阻断vue-cli的初始化进程导致创建失败

Subscribe to ionantha

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe