“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Why lerna

Splitting a large code repository into separate versioned packages is very useful for code sharing. However, some changes can become cumbersome and difficult to track if they span multiple code repositories, and testing across multiple code repositories can quickly become very complex.

To solve these (and many other) problems, some projects split the code repository into packages and store each package in a separate code repository. However, projects such as Babel, React, Angular, Ember, Meteor, Jest, and many others are developed with multiple packages in a code repository.

Lerna is a tool optimized for workflow management of multi-package code repositories using Git and NPM

NPM initialization

Create a new folder spring-breeze and type in the integration terminal

NPM init -y root generates package.json:

{" name ":" spring breeze - ", "version" : "1.0.0", "description" : ""," main ":" index. Js ", "scripts" : {" test ": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }Copy the code

Install lerna

Install LERNA globally

Run: NPM I lerna -g After the installation is complete run :lerna init After the execution is complete the directory structure is displayed

- Packages - Lerna. json - package.json(project description file)Copy the code

Initialize both packages

By default, package is placed in the Packages directory. Packages can be created using the command lerna create [loc]

lerna create core packages/core

lerna create core packages/tools

The packages/core/lib/core. Js is changed to:

'use strict'; Console. log(" I am core package ~~")Copy the code

The packages/tools/lib/tools. Js is changed to:

'use strict'; Console. log(" I'm tools package ~~")Copy the code

The directory structure is

-- Packages -- core... -- lib -- core.js -- package.json -- README.md - tools ... -- lib -- tools.js -- package.json -- readme.md-lerna. json(config file) -- package.json(project description file) -- lib -- tools.js -- package.json -- readme.md-lerna. json(config file) -- package.Copy the code

Modify the package name

To prevent duplication, the package name is usually changed to @organization/package, as in the current project core should be changed to @spring-breeze/core; Tools: @ spring breeze/tools

For example, core modules:

  • packages/core/package.json:
{"name": "@spring-breeze/core", "version": "0.0.1", "description": "> TODO: description", "author": "******", "homepage": "", "license": "ISC", "main": "lib/core.js", "directories": { "lib": "lib", "test": "__tests__" }, "files": [ "lib" ], "repository": { "type": "git", "url": "******" }, "scripts": { "test": "echo \"Error: run tests from root\" && exit 1" } }Copy the code

Install the third-party package Lerna Add

  • Adding public dependencies

    Run in the root directory (it doesn’t matter which directory you run in):lerna add lodash

You’ll notice that both core and Tools have LoDash installed

  • Add separate dependencies

    lerna add jquery –scope=core

Core will be installed with jquery

Remove dependency lerna Clean

Executing lerna clean removes node_modules from all packages

Install all dependencies on Lerna Bootstrap

Executing lerna Bootstrap reinstalls all dependencies

Testing local packages

If two packages developed locally are related, debugging with Lerna is very easy. Just write the version number of the local package to the dependency execution: Lerna link. For example, use the Tools module in the core module

  • Package adds dependencies: “@spring-breeze/tools”: “^0.0.1”
{"name": "@spring-breeze/core", "version": "0.0.1", "description": "> TODO: description", "author": "******", "homepage": "", "license": "ISC", "main": "lib/core.js", "directories": { "lib": "lib", "test": "__tests__" }, "files": [ "lib" ], "repository": { "type": "git", "url": "******" }, "dependencies": {"@spring-breeze/tools": "^0.0.1"}, "scripts": {"test": "echo "Error: run tests from root\" && exit 1"}}Copy the code

Execute lerna link after adding

  • Use: the core packages/core/lib/core. Js
'use strict';
require("@spring-breeze/tools")

Copy the code

Node executes core.js and the console prints: I’m tools package ~~. This allows for local debugging

Pre-release preparation

After debugging, it can be released. The release process is as follows:

  • Register an NPM account
  • New organization Spring-Breeze (depending on your project)
  • Perform NPM login
  • Configure in package.json under core and utils respectively
 "publishConfig": {
    "access": "public"
  }
Copy the code
  • New gitignore
*node_modules
Copy the code
  • Create a New Git repository, add your project git remote to “your Git repository “, and commit the project to your git repository
Git remote add https://gitee.com/geeksdidi/spring-breeze.git git add. Git commit -m "initialization" git push -u origin masterCopy the code
  • Create license. md(can be an empty file)
  • To modify a file, perform lerna publish

Note: Every time you release a new version, you commit your code to Git

View the published package

Log on NPM official website to see the package you released