Multirepo mode

Single warehouse, that is, each package is managed by a single warehouse. If different packages depend on each other, it becomes increasingly difficult to maintain.

Monorepo

All related packages are managed in a warehouse.

What is Lerna?

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

A project managed by LERNA usually has the following structure:

- 📃 lerna.json - 📃 package.json - 📁 packages - 📁 packageA - 📃 packageCopy the code

Lerna Fixed/Locked mode (default)

Default mode, lerna init creates default mode project. Fixed mode uses lerna.json for unified versioning of all packages. Changes to any package in multiple projects will result in changes to the version numbers of all packages.

Lerna Independent mode

Independent schema, lerna init –independent creates projects with independent schemas. Standalone mode allows each package to change its version number individually. When LERna publishes, only the version number of the changed package is updated.

lerna.json

{
  "version": 1.1.3 "".// Version number, set to Independent in Independent mode
  "npmClient": "npm".// Specify the client to run the command on
  "command": {
    "publish": {
      "ignoreChanges": ["ignored-file"."*.md"].// Specify which directory or file changes will not be published
      "message": "chore(release): publish".// A custom commit message when performing a release update
      "registry": "https://npm.pkg.github.com" // Set the registered address for NPM package publishing}},"packages": ["packages/*"] // Specify the directory where the package resides
}
Copy the code

Using lerna

Install lerna

npm install --global lerna
Copy the code

Initialize LERNA (using default mode)

lerna init
Copy the code

The project directory structure is as follows:

- 📁 packages3
- 📃 package.json
- 📃 lerna.json
Copy the code

Create three projects in the projects directory

  • App depends on UI, utils
  • UI relies on utils
  • Utils does not depend on any library and needs to be published on NPM
lerna create app && lerna create ui && lerna create utils
Copy the code

The folder structure of the project is shown below:

Processing utils package

Simply add some sample code to utils.js

'use strict';

module.exports = { add };

function add(. args) {
    console.log('Add method using utils library')
    let sum = 0
    for (let i = 0; i < args.length; i += 1) {
        sum += args[i]
    }
    return sum
}
Copy the code

Handle UI package

  1. Set in the package.json file in UI Packageprivate: trueNPM will not release 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('Call UI function'. args); add(... args) }Copy the code

Handle the app package

  1. Set in package.json file in app Packageprivate: trueNPM will not release this package.
  2. Add UI and utils to your 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()
Copy the code

Run the app, Node app.js. I get the following log

Use the utils library add method to call UI functions 1, 2, 3Copy the code

NPM release

We need to publish utils to NPM. If the project needs build. You need to package the project ahead of time using the build command.

Next, lerna publish is called. Due to Fixed/Locked mode, all project versions are updated according to the version numbers in lerna.json.

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

The version number of the three packages is 0.0.1, and APP and UI are private and will not be released to NPM.

Lerna command

lerna init

Initialize the LERNA project


#Fixed mode
lerna init

#Stand-alone mode
lerna init ----independent
Copy the code

lerna bootstrap

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

lerna create

Create a package in lerna management project.

lerna import

lerna add

Add local or remote packages as dependencies to the package.

Lerna add react –scope=app add react to app project

lerna clean

Delete the node_modules directories of all packages. You can also specify to remove 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

Execute the command in each package that contains it, or you can specify that it be executed under a package.

Lerna run build –scope=app

lerna publish

Publish the packages that need to be published

reference

  • package.json
  • lerna
  • lerna
  • Lerna Multi-package management practice