tetrisrepo主要想实现多包管理前端组件库,所以在此需求上尝试使用lerna实现
npx lerna init --independent
直接在该目录内运行上述命令会生成相应的lerna配置与目录结构,其初始化结构中较为关键的packages/
,lerna.json
。
使用create命令快速生成packages/name目录及其内相应文件与配置。
添加公共依赖,加上--scope=pkgName
用于单独依赖添加。
重新安装所有依赖。
通过在其他包中添加依赖本地开发的包,使用lerna link
命令来进行关联
使用yarn/workspaces实现lerna hoisting,可以在根目录管理依赖。
// lerna.json
{
"npmClient": "yarn",
"useWorkspaces": true
}
依赖托管后,lerna的packages将会被顶级package.json的workspaces覆盖
// package.json
{
// ...
"workspaces": [
"packages/*"
]
}
在lerna.json中配置publish,明确注册表配置,同时配置ignoreChanges避免不必要的版本升级。
{
"version": "1.1.3",
"npmClient": "npm",
"command": {
"publish": {
"ignoreChanges": ["ignored-file", "*.md"],
"message": "chore(release): publish",
"registry": "https://npm.pkg.github.com"
},
"bootstrap": {
"ignore": "component-*",
"npmClientArgs": ["--no-package-lock"]
}
},
"packages": ["packages/*"]
}
- version: the current version of the repository.
- npmClient: an option to specify a specific client to run commands with (this can also be specified on a per command basis). Change to "yarn" to run all commands with yarn. Defaults to "npm".
- command.publish.ignoreChanges: an array of globs that won't be included in lerna changed/publish. Use this to prevent publishing a new version unnecessarily for changes, such as fixing a README.md typo.
- command.publish.message: a custom commit message when performing version updates for publication. See @lerna/version for more details.
- command.publish.registry: use it to set a custom registry url to publish to instead of npmjs.org, you must already be authenticated if required.
- command.bootstrap.ignore: an array of globs that won't be bootstrapped when running the lerna bootstrap command.
- command.bootstrap.npmClientArgs: array of strings that will be passed as arguments directly to npm install during the lerna bootstrap command.
- command.bootstrap.scope: an array of globs that restricts which packages will be bootstrapped when running the lerna bootstrap command.
- packages: Array of globs to use as package locations.
除此之外,如果你的包名是带 scope 的,需要在那个包的 package.json 中设置 publishConfig.access 为 "public"。
某篇文章里提到包名写到scope的问题,这里还不是很理解,后期有用到再看看。