脚手架开发:Lerna简介

quote

I think it's hard winning a war with words.

Gone with the wind

Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm.

原生脚手架开发痛点分析

  • 痛点一:重复操作

    • 多Package本地link
    • 多Package依赖安装
    • 多Package单元测试
    • 多Package代码提交
    • 多Package代码发布
  • 痛点二:版本一致性

    • 发布时版本一致性
    • 发布后相互依赖版本升级

Lerna简介

Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm.

优势

  • 大幅减少重复操作
  • 提升操作的标准化

官网

https://lerna.js.org/

案例

使用 Lerna 管理的大型项目:

lerna 开发脚手架流程(划重点)

基于 Lerna 创建项目

安装 Lerna

npm install -g lerna

创建项目

git init jun-cli && cd jun-cli

初始化 Lerna 项目

lerna init 

创建 Package

lerna create @jun-cli/core packages

安装依赖

lerna add mocha packages/core --dev

删除依赖

lerna clean

执行单元测试

lerna run test

执行特定包的单元测试

lerna run test @jun-cli/core
lerna link

发布项目

lerna publish

lerna-使用细节(划重点)

lerna init

会自动完成 git 初始化,但不会创建.gitignore,,这个必须要手动添加,否则会将node_modules 目录都上传到git,如果node_modules 已经加入git stage,可使用:

git reset HEAD <file>

执行 unstage 操作,如果文件已经被 git 监听到变更,可使用:

git checkout -- <file>

将变更作废,记得在执行操作之前将文件加入.gitignore

lerna add

  • 第一个参数:添加 npm 包名
  • 第二个参数:本地 package 的路径
  • 选项:

    • --dev:将依赖安装到devDependencies,不加时安装到dependencies
lerna add <package> [loc] --dev

如果未发布上线,需要手动将依赖添加到package.json, 再执行lerna link

lerna clean

只会删除node_modules,不会删除package.json中的依赖。

lerna exec 和 lerna run

--scope属性后添加的是包名,而不是 package 的路径,这点和lerna add用法不同。

lerna publish

  • 发布时会自动执行:git add package-lock.json,所以package-lock.json不要加入 .gitignore
  • 先创建远程仓库,并且同步一次 master 分支。
  • 执行lerna publish前先完成npm login
  • 如果发布的npm包名为:@xxx/yyy的格式,需要先在npm注册名为:xxx 的 organization,否则可能会提交不成功。
  • 发布到npm group时默认为private,所以我们需要手动在package.json 中添加如下配置:
"publishConfig": {
    "access": "public"
  }

评论没有加载,检查你的局域网

Cannot load comments. Check you network.

eat();

sleep();

code();

repeat();