lerna开发流程(入坑和出坑)

16 篇文章 0 订阅

lerna常用api总结:

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发布

备注:可以使用lerna [command] -h 查找单个命令的使用方法和参数

lerna bootstrap 

执行该命令会做下面四件事情:

  1. 为每个包安装依赖
  2. 链接相互依赖的库到具体的目录
  3. 执行 npm run prepublish
  4. 执行 npm run prepare

参数有以下几个

-- --production --no-optional指定npm client的参数 

--hoist

把依赖安装到根目录的node_modules 

--ignore

忽略的包--ignore test-* 忽略名称以test开头的包
--scope 指定的包参数的含义是指包的名称

--ignore-scripts

不执行声明周期脚本命令, 比如 prepare 

--registry <url>

指定registry 

--npm-client

指定安装用的npm clientlerna 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,不打taglerna publish --canary

备注:npm ci 会删除node_modules文件夹,并且要有package-lock.json文件

——————————————————————————————————————————————————

lerna list 列举当前lerna 库包含的包

 

--json

显示为json格式 

--all

显示包含private的包 

--long

显示更多的扩展信息 

——————————————————————————————

lerna changed

显示自上次relase tag以来有修改的包, 选项通 list

——————————————————————————————————————————————————

lerna diff

显示自上次relase tag以来有修改的包的差异, 执行 git diff

——————————————————————————————————————————————————

lerna exec

在每个包目录下执行任意命令

--concurrency

默认命令时并行执行的, 我们可以设置并发量为1(全局参数)lerna exec --concurrency 1 -- ls -la

--scope

设置包含的packagelerna 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. 

lerna run 选项同lerna exec

执行每个包package.json中的脚本命令

——————————————————————————————————————————————————

lerna init  

创建一个新的lerna库或者是更新lerna版本

默认lerna有两种管理模式, 固定模式和独立模式

  • 固定模式  --exact

固定模式,通过lerna.json的版本进行版本管理。当你执行lerna publish命令时, 如果距离上次发布只修改了一个模块,将会更新对应模块的版本到新的版本号,然后你可以只发布修改的库。

这种模式也是Babel使用的方式。如果你希望所有的版本一起变更, 可以更新minor版本号,这样会导致所有的模块都更新版本。

  • 独立模式 --independent

独立模式,init的时候需要设置选项 --independent. 独立模式允许管理者对每个库单独改变版本号,每次发布的时候,你需要为每个改动的库指定版本号。这种情况下, lerna.json的版本号不会变化了, 默认为independent

——————————————————————————————————————————————————

lerna clean

删除各个包下的node_modules

——————————————————————————————————————————————————

lerna import

导入指定git仓库的包作为lerna管理的包

--flatten

如果有merge冲突, 用户可以使用这个选项所谓单独的commitlerna import ~/Product --flatten

--dest

可以指定导入的目录(lerna.json中设定的目录)lerna import ~/Product --dest=utilities

lerna add

添加一个包的版本为各个包的依赖

lerna add <package>[@version] [--dev] [--exact]

lerna link

链接互相引用的库

——————————————————————————————————————————————————

lerna create

新建包

——————————————————————————————————————————————————

lerna.json解析

{
  "version": "1.1.3",
  "npmClient": "npm",
  "command": {
    "publish": {
      "ignoreChanges": [
        "ignored-file",
        "*.md"
      ],
      "allowBranch": ["master", "feature/*"]
    },
    "bootstrap": {
      "ignore": "component-*",
      "npmClientArgs": ["--no-package-lock"]      
    }
  },
  "packages": ["packages/*"]
}

 

lerna发布相关

1,lerna publish做哪些事情

  • 运行lerna updated来决定哪一个包需要被publish
  • 如果有必要,将会更新lerna.json中的version
  • 将所有更新过的的包中的package.json的version字段更新
  • 将所有更新过的包中的依赖更新
  • 为新版本创建一个git commit或tag
  • 将包publish到npm上

2, lerna publish发布失败后怎样操作,如下:采用 from-package

Positionals

bump from-git

In addition to the semver keywords supported by lerna versionlerna 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.

bump 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

3,  symlink 的问题

如果我们的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 来解决这个问题,具体参见官方文档

4,指定cnpm源无效

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

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值