lerna bootstrap | 安装依赖 |
lerna clean | 删除各个包下的node_modules |
lerna init | 创建新的lerna库 |
lerna list | 显示package列表 |
lerna changed | 显示自上次relase tag以来有修改的包, 选项通 list |
lerna diff | 显示自上次relase tag以来有修改的包的差异, 执行 git diff |
lerna exec | 在每个包目录下执行任意命令 |
lerna run | 执行每个包package.json中的脚本命令 |
lerna add |
添加一个包的版本为各个包的依赖 |
lerna import | 引入package |
lerna link | 链接互相引用的库 |
lerna create | 新建package |
lerna publish | 发布 |
执行该命令会做下面四件事情:
参数有以下几个
-- --production --no-optional | 指定npm client的参数 | |
--hoist |
把依赖安装到根目录的node_modules | |
--ignore |
忽略的包 | --ignore test-* 忽略名称以test开头的包 |
--scope | 指定的包 | 参数的含义是指包的名称 |
--ignore-scripts |
不执行声明周期脚本命令, 比如 prepare | |
--registry |
指定registry | |
--npm-client |
指定安装用的npm client | lerna bootstrap --npm-client=yarn |
--use-workspace |
使用yarn workspace, 没用过 | |
--no-ci |
默认调用 npm ci 替换 npm install , 使用选项修改设置 | npm ci 类似于 npm-install ,但它旨在用于自动化环境,如测试平台,持续集成和部署。 |
--skip-git | 将不会创建git commit或tag | |
--skip-npm | 将不会把包publish到npm上 | |
--canary | 可以用来独立发布每个commit,不打tag | lerna publish --canary |
备注:npm ci 会删除node_modules文件夹,并且要有package-lock.json文件
——————————————————————————————————————————————————
--json |
显示为json格式 | |
--all |
显示包含private的包 | |
--long |
显示更多的扩展信息 |
显示自上次relase tag以来有修改的包, 选项通 list
——————————————————————————————————————————————————
显示自上次relase tag以来有修改的包的差异, 执行 git diff
——————————————————————————————————————————————————
在每个包目录下执行任意命令
--concurrency |
默认命令时并行执行的, 我们可以设置并发量为1(全局参数) | lerna exec --concurrency 1 -- ls -la |
--scope |
设置包含的package | lerna exec --scope my-component -- ls -la |
--stream |
交叉并行输出结果 | lerna exec --stream -- babel src -d lib |
--parallel |
Execute command with unlimited concurrency, streaming prefixed output. | |
--no-bail |
Continue executing command despite non-zero exit in a given package. |
执行每个包package.json中的脚本命令
——————————————————————————————————————————————————
创建一个新的lerna库或者是更新lerna版本
默认lerna有两种管理模式, 固定模式和独立模式
固定模式,通过lerna.json的版本进行版本管理。当你执行lerna publish
命令时, 如果距离上次发布只修改了一个模块,将会更新对应模块的版本到新的版本号,然后你可以只发布修改的库。
这种模式也是Babel使用的方式。如果你希望所有的版本一起变更, 可以更新minor版本号,这样会导致所有的模块都更新版本。
独立模式,init的时候需要设置选项 --independent
. 独立模式允许管理者对每个库单独改变版本号,每次发布的时候,你需要为每个改动的库指定版本号。这种情况下, lerna.json
的版本号不会变化了, 默认为independent
。
——————————————————————————————————————————————————
删除各个包下的node_modules
——————————————————————————————————————————————————
导入指定git仓库的包作为lerna管理的包
--flatten |
如果有merge冲突, 用户可以使用这个选项所谓单独的commit | lerna import ~/Product --flatten |
--dest |
可以指定导入的目录(lerna.json中设定的目录) | lerna import ~/Product --dest=utilities |
添加一个包的版本为各个包的依赖
lerna add [@version] [--dev] [--exact]
链接互相引用的库
——————————————————————————————————————————————————
新建包
——————————————————————————————————————————————————
{
"version": "1.1.3",
"npmClient": "npm",
"command": {
"publish": {
"ignoreChanges": [
"ignored-file",
"*.md"
],
"allowBranch": ["master", "feature/*"]
},
"bootstrap": {
"ignore": "component-*",
"npmClientArgs": ["--no-package-lock"]
}
},
"packages": ["packages/*"]
}
from-git
In addition to the semver keywords supported by lerna version
, lerna publish
also supports the from-git
keyword. This will identify packages tagged by lerna version
and publish them to npm. This is useful in CI scenarios where you wish to manually increment versions, but have the package contents themselves consistently published by an automated process.
from-package
Similar to the from-git
keyword except the list of packages to publish is determined by inspecting each package.json
and determining if any package version is not present in the registry. Any versions not present in the registry will be published. This is useful when a previous lerna publish
failed to publish all packages to the registry.
具体参见:https://github.com/lerna/lerna/tree/master/commands/publish#readme
如果我们的package中有webpack,那么其中的loader很有可能会出问题。
假设 package 下面有一个包 pkg1 ,依赖 package 下面的另一个包 pkg2 。
运行 lerna bootstrap 之后, pkg1/node_modules 下就会出现 pkg2 的 symlink 。
我们遇到的问题是在pkg2中有一个TS文件,export出去。pkg1中去引入,但是发现总是没有命中真实的loader。
如果使用 webpack 系列工具来编译运行 pkg1 ,由于 webpack loader 判断路径默认是按照真实路径来的,所以 pkg2 对应到的路径是 [project root]/package/pkg2
,而不是 [project root]/package/pkg1/node_modules/pkg2
。
这样一来,如果需要 pkg2 中的源码过 pkg1 的 loader (比如 pkg2 中的 TS 通过 pkg1 的 ts-loader),就需要特殊配置。这和不涉及 symlink 的真实场景存在较大差异。
同时,很多配置(比如 postcssrc 、 babelrc 、 eslintrc 等)是以 resolve 到的文件去解析的。
所以此时其实很希望 webpack loader 基于 symlink 的路径去解析判断 include / exclude 等配置,而不是按照真实文件的路径。
所以需要配置webpack 的 resolve.symlinks
来解决这个问题,具体参见官方文档。
据gitlab issue publish时不接受参数,只能使用npm仓库。
但是旧版本 2.x 的支持,本人测试,截止到3.3.2
,3.x的lerna指定cnpm源publish均无效。
参考:https://www.jianshu.com/p/2f9c05b119c9
https://www.jianshu.com/p/8b7e6025354b