Multirepo mode

Single warehouse, that is, each package is managed by a separate warehouse. If the different package depend on each other, it will become more and more difficult to maintain.

Monorepo

All related package are placed in a warehouse for management.

What is lerna?

A tool for managing JavaScript projects with multiple packages. A tool for managing JavaScript projects with multiple packages, package .

A project managed by lerna, the usual structure is as follows:

- 📃 lerna.json
- 📃 package.json
- 📁 packages
  - 📁 packageA
    - 📃 package.json  
  - 📁 packageB
    - 📃 package.json

lerna Fixed/Locked mode (default mode)

The default mode, lerna init creates a default mode project. The fixed mode uses lerna.json to perform unified version management for all package package multi-project will cause all version numbers of package

lerna Independent mode

Independent mode, lerna init --independent Create independent mode projects. The independent mode allows each package individually modify the version number. At lerna publish , only the changed version number package

lerna.json

{
  "version": "1.1.3", // 版本号,Independent模式下设置为independent
  "npmClient": "npm", // 指定运行命令的客户端
  "command": {
    "publish": {
      "ignoreChanges": ["ignored-file", "*.md"], // 指定那些目录或者文件的变更不会被publish
      "message": "chore(release): publish", // 执行发布版本更新时的自定义提交消息
      "registry": "https://npm.pkg.github.com" // 设置npm包发布的注册地址
    },
  },
  "packages": ["packages/*"] // 指定包所在的目录
}

Use lerna

Install lerna

npm install --global lerna

Initialize lerna (use default mode)

lerna init

The project directory structure is as follows:

- 📁 packages3
- 📃 package.json
- 📃 lerna.json

Create three projects in the project directory

lerna-app.png

  • app depends on ui, utils
  • ui depends on utils
  • utils does not depend on any libraries and needs to be published on npm
lerna create app && lerna create ui && lerna create utils

The folder structure of the project at this time is as shown in the figure below:

项目目录.png

Process utils package

Simply add some sample code in utils.js

'use strict';

module.exports = { add };

function add(...args) {
    console.log('使用 utils 库的的 add 方法')
    let sum = 0
    for (let i = 0; i < args.length; i += 1) {
        sum += args[i]
    }
    return sum
}

Handle ui package

  1. private: true in the package.json file in the ui package, and npm will not publish this package.
  2. Add utils to the ui package. lerna add utils --scope=ui

Use utlis in ui.js

'use strict';

const { add } = require('utils');

module.exports = ui;

function ui(...args) {
  console.log('调用 ui 函数', ...args);
  add(...args)
}

Process app package

  1. private: true in the package.json file in the app package, and npm will not publish this package.
  2. Add ui and utils to the app. lerna add ui --scope=app , lerna add utils --scope=app

Use ui and utlis in app.js

'use strict';

const { add } = require('utils');
const ui = require('ui');

module.exports = app;

function app() {
    add(1, 2, 3)
    ui(1, 2, 3)
}

app()

Run the app, node app.js . Get the following log

使用 utils 库的的 add 方法
调用 ui 函数 1 2 3
使用 utils 库的的 add 方法

npm release

We need to publish utils to npm. If the project needs to be built. You need to use the build command to package the project in advance.

Next, call lerna publish to publish the project. Due to the Fixed/Locked mode used, the version numbers of all projects will be updated according to the version numbers in lerna.json.

发布1.png

After selecting the version, you can see the terminal page as follows:

发布2.png

The version numbers of the three packages are unified to 0.0.1, and app and ui are private and will not be published to npm.

lerna's command

lerna init

Initialize the lerna project


# 固定模式
lerna init

# 独立模式
lerna init ----independent

lerna bootstrap

Install all package dependencies. And connect the cross-dependency of local packages.

lerna create

Create a package in the lerna management project.

lerna import

lerna add

Add the local or remote package as a dependency to the package.

lerna add react --scope=app , add react to the app project

lerna clean

Delete the node_modules directory of all packages. You can also specify to delete node_modules under a specific package.

lerna clean --scope=ui , delete the node_modules directory under ui.

lerna ls

List all public packages (except private: true)

lerna changed

Check which packages have been updated since the last release.

lerna run

The command is executed in each package that contains the command, or it can be specified to be executed under a certain package.

lerna run build --scope=app , execute the build command in the app.

lerna publish

Publish the package that needs to be published

refer to


已注销
518 声望187 粉丝

想暴富