关于lerna:lerna-基本使用创建-发布-命令

4次阅读

共计 1778 个字符,预计需要花费 5 分钟才能阅读完成。

目前网上 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")
}
正文完
 0