lerna 項目搭建

項目結(jié)構(gòu)

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
  • 初始化項目
// 在項目根目錄執(zhí)行
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時才會檢測到更新。
  • 發(fā)布
lerna publish
  • 在其他項目使用
npm i cli
npx fip lint => lint789...

總結(jié):
1、各個包之間的版本管理:初始化時使用lerna init --independent,生成的lerna.json中的是'version':'independent',這表示各個包之間的版本互相獨立。
2、自定義存放包的位置:自定義lerna.json中的packages字段,如packages: [ "commands/*", "core/*" ],.。
3、搭配yarn使用workspaces環(huán)境,需指定npmClient:'yarn'

// 查看工作區(qū)
yarn workspaces info
// 給B包安裝依賴A
yarn workspace packageB add packageA 

4、lerna 命令

lerna init
lerna create
lerna add
lerna bootstrop
lerna publish
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容