lerna开发

创建 npm 私服

  • verdaccio 是一个简单、零配置的本地私有化 npm 仓库
1
2
3
4
5
6
cnpm install verdaccio -g

verdaccio
http://localhost:4873
npm adduser --registry http://localhost:4873/
npm publish --registry http://localhost: 4873/

lerna 常用命令

项目初始化
| 命令 | 说明 |
| — | — |
| lerna init –independent | 初始化项目 |

创建包
| 命令 | 说明 |
| — | — |
| lerna create module-1 | 创建 package |
| lerna add | 安装依赖 |
| lerna link | 链接依赖 |

开发和测试
| 命令 | 说明 |
| — | — |
| lerna exec | 执行 shell 脚本 |
| lerna run | 执行 npm 命令 |
| lerna clean | 清空依赖|
| lerna bootstrap | 重新安装依赖 |

package 依赖

1
2
3
4
5
6
7
8
9
10
11
12
1.给指定package安装依赖
$ lerna add lodash packages/module-1
$ lerna add lodash --scope=module-1
$ lerna add lodash **/module-1
$yarn workspace module-1 add lodash

2.给所有package安装依赖
$ lerna add lodash

3.workspace之间的依赖
$ lerna add module-2 packages/module-1
$ lerna add module-2 --scope module-1

发布

1
$ lerna publish

lerna.json

  • version: 当前仓库的版本,independent mode 请设置为 independent.
    • lerna 对于包的管理有两种模式:固定模式 fixed、独立模式 independent。
    • 固定模式所有包是统一的版本号,每次升级所有包统一更新,无论这个包内容是否改变。 *具体体现在 lerna 的配置文件中 lerna.json 中永远会存在一个确定版本号:{“version”: “0.0.1”}
    • 独立模式,每个包都是单独的版本号,每次 lerna 触发发布命令,每个包的版本都会单独变化,具体体现在 lerna 的配置文件 lerna.json 中没有一个确定的版本号,而是{“version”:”independent”}
  • npmClient: 指定运行的客户端程序 默认为 npm
  • ignoreChanges: 一个不包含在lerna changed/publish的 glob 数组,使用这个阻止发布不必要的更新,比如修复README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"useWorkspaces": true, // 使用 workspaces 配置。此项为 true 的话,将使用 package.json 的 "workspaces",下面的 "packages" 字段将不生效
"version": "0.1.0", // 所有包版本号,独立模式-"independent"
"npmClient": "cnpm", // npm client,可设置为 cnpm、yarn 等
"packages": [ // 包所在目录,可指定多个
"packages/*"
],
"command": { // lerna 命令相关配置
"publish": { // 发布相关
"ignoreChanges": [ // 指定文件或目录的变更,不触发 publish
".gitignore",
"*.log",
"*.md"
]
},
"bootstrap": { // bootstrap 相关
"ignore": "npm-*", // 不受 bootstrap 影响的包
"npmClientArgs": [ // bootstr 执行参数
"--no-package-lock"
]
}
}
}

CRA + lerna + react + typescript 项目搭建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1.create-react-app demo --template typescript

2.cd demo

3.npm run eject

4.lerna init

5.lerna create builder-script

6.自定义builder-scripts脚本命令

7.自定义 eslintrc、tsconfig、webpack等配置项

评论