lerna 项目搭建

项目结构

fip-tools
├── commands
|  ├── lint
|  |  ├── command.js
|  |  ├── package.json
|  |  ├── README.md
|  |  └── __tests__
|  |     └── lint.test.js
|  └── version
├── core
|  ├── cli
|  |  ├── lib
|  |  |  └── cli.js
|  |  ├── package-lock.json
|  |  ├── package.json
|  |  ├── README.md
|  |  └── __tests__
|  |     └── cli.test.js
|  └── test
├── lerna.json
├── package-lock.json
├── package.json
└── packages

文件详情

// commands/lint/command.js
exports.command = "lint";
exports.describe = "Create a new Lerna repo or upgrade an existing repo to the current version of Lerna.";
exports.builder = {
    exact: {
        describe: "Specify lerna dependency version in package.json without a caret (^)",
        type: "boolean",
    },
    independent: {
        describe: "Version packages independently",
        alias: "i",
        type: "boolean",
    },
};
exports.handler = function handler(argv) {
    console.log('lint789...');
};
// core/cli/lib/cli.js
#!/usr/bin/env node
const yargs = require("yargs/yargs");
const lintCmd = require('lint/command')
function cli() {
    yargs(process.argv.slice(2)).command(lintCmd).argv;
}
cli()
  • 全局安装lerna
yarn global add lerna
  • 初始化项目
// 在项目根目录执行
lerna init
  • 修改配置文件
// lerna.json
{
  "useWorkspaces": true,
  "version": "0.0.10"
}

// package.json
{
  "name": "root",
  "private": true,
  "workspaces": [
    "commands/*",
    "core/*"
  ],
  "devDependencies": {
    "lerna": "^4.0.0"
  }
}
  • 新建包
// 在core 下目录下建立cli 包
lerna create cli core
lerna create commands lint
  • 模块之间的链接
// 将lint包添加为cli的依赖
lerna add lint  --scope=cli
lerna add module-1 --scope=module-2
  • commit 更改
    只有commit了更改,在publish时才会检测到更新。
  • 发布
lerna publish
  • 在其他项目使用
npm i cli
npx fip lint => lint789...

总结:
1、各个包之间的版本管理:初始化时使用lerna init --independent,生成的lerna.json中的是'version':'independent',这表示各个包之间的版本互相独立。
2、自定义存放包的位置:自定义lerna.json中的packages字段,如packages: [ "commands/*", "core/*" ],.。
3、搭配yarn使用workspaces环境,需指定npmClient:'yarn'

// 查看工作区
yarn workspaces info
// 给B包安装依赖A
yarn workspace packageB add packageA 

4、lerna 命令

lerna init
lerna create
lerna add
lerna bootstrop
lerna publish
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容