目前网上lerna的教程不是很分明, 所以我本人整顿并且实际的笔记
创立我的项目
$ mkdir lerna-repo && cd lerna-repo
$ npm init
// 本地目录装置
$ npm i lerna -D
$ npx lerna init
// or 全局装置
$ npm install --global lerna
$ lerna init
目录构造
lerna-repo/
packages/
core/
utils/
package.json
lerna.json
这里咱们将我的项目分成2个子包, 前面会教大家创立
一个core子项目, 一个utils子项目
lerna create
创立子包
lerna create <name> [loc(指定目录)]
// lerna create core
// lerna create utils
lerna add
装置依赖
lerna add <package>[@version] [--dev] [--exact] [--peer]
比方
lerna add fetch
package目录下的子项目都会装置到
指定package装置(utils名称的子项目)
lerna add fetch packages/utils/
命令
常用命令
lerna link
链接子包与子包之间的依赖(本地)
# core/ package.json
...
"dependencies": {
"fetch": "^1.1.0",
"@xxx/utils": "^1.0.4" // xxx为对应子包utils下面的package.json中的name
},
...
接下来运行lerna link
node_modules增加的包地址指向本地子包utils
lerna run
运行包的命令
将core和utils的package.json批改
...
"scripts": {
"test": "echo \"Run test from utils\""
},
...
运行lerna run test
echo "Run test from utils"
"Run test from utils"
lerna info run Ran npm script 'test' in '@xxx/core' in 0.9s:
> echo "Run test from core"
"Run test from core"
-
指定运行
lerna run --scope @xxx/utils test
lerna clean
清空package子项目的node_modules
lerna bootstrap
装置package子项目的依赖(node_modules)
补充命令
lerna exec
执行命令行
# 删除所有子包中的node_modules
lerna exec -- rm -rf node_modules/
# 删除指定utils子包中的node_modules
lerna exec --scope @xxx/utils rm -rf node_modules/
公布操作
git绑定近程仓库
npm login进行登录和创立Organization 仓库(Unlimited public packages)
lerna publish
公布一个版本
记得publish之前绑定近程仓库和npm的Organization
ps: 失常更新git push
, 版本替换的时候才应用lerna publish
>lerna publish
lerna notice cli v4.0.0
lerna info current version 1.0.7
lerna info Looking for changed packages since v1.0.7
? Select a new version (currently 1.0.7) (Use arrow keys)
> Patch (1.0.8)
Minor (1.1.0)
Major (2.0.0)
Prepatch (1.0.8-alpha.0)
Preminor (1.1.0-alpha.0)
Premajor (2.0.0-alpha.0)
Custom Prerelease
Custom Version
lerna publish
抉择Patch会更新的子包中的package.json的version更改为指定版本
lerna会主动生成git tag(版本标签)上传github上, 和主动上传npm的版本号(对应更新的子包)
lerna diff
查看包的本地批改
# core/ index.js
function core() {
// TODO
+ console.log("core")
}
发表回复