当前位置:网站首页>How to use lerna to manage multiple packages

How to use lerna to manage multiple packages

2022-06-21 07:34:00 u012804784

High quality resource sharing

Learning route guidance ( Click unlock ) Knowledge orientation Crowd positioning
🧡 Python Actual wechat ordering applet 🧡 Progressive class This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system .
Python Quantitative trading practice beginner Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system

Why use lerna

Divide the large code warehouse into several independent versions software package (package) Very useful for code sharing . however , If something changes Across multiple code repositories, it's going to be very trouble And it's hard to track , also , Testing across multiple code repositories will quickly become very complex .

To solve these ( And many others ) problem , Some projects will The code warehouse is divided into multiple packages (package), And store each software package in a separate code warehouse . however , for example Babel、 React、Angular、Ember、Meteor、Jest And a lot of other projects are in A code repository contains multiple packages (package) And develop .

Lerna It's a tool , in the light of Use git and npm Optimize the workflow of managing multi package code warehouse

npm initialization

New folder spring-breeze, Input at integrated terminal
npm init -y
The root directory will generate 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"
}


install lerna

Global installation lerna
perform :npm i lerna -g
After installation :lerna init
After the execution, such a directory structure appears

- packages( Catalog )
- lerna.json( The configuration file )
- package.json( Project description file )

Initialize two package

By default ,package It's on the packages In the catalog . To create a package, you can use the command lerna create [loc]
lerna create core packages/core
lerna create core packages/tools

take packages/core/lib/core.js Change it to :

'use strict';
console.log(" I am a core package ~~")

take packages/tools/lib/tools.js Change it to :

'use strict';
console.log(" I am a tools package ~~")

At this point, the directory structure is

-- packages( Catalog )
  -- core
    ...
    -- lib
      -- core.js
    -- package.json 
    -- README.md
  - tools
    ...
    -- lib
      -- tools.js 
    -- package.json 
    -- README.md
- lerna.json( The configuration file )
- package.json( Project description file )
...

Modify package name

In order to prevent duplicate names , The package name is generally changed to @ organization / package , Such as the current project core Should be changed to :@spring-breeze/core;tools Change it to :@spring-breeze/tools

for example core modular :

  • 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"
  }
}


Install third party package lerna add

  • Add public dependencies

Execute at root ( It is the same in which directory ):lerna add lodash
You will find core and tools All installed lodash

  • Add a separate dependency

lerna add jquery --scope=core
core Will be installed jquery

Delete dependency lerna clean

perform lerna clean All packages will be deleted node_modules

Install all dependencies lerna bootstrap

perform lerna bootstrap All dependencies will be reinstalled

Test local package

If the two packages developed locally are related , use lerna Debugging is very simple , You only need to write the required local package version number to the dependent execution :lerna link That's all right. ; For example, in core Module USES tools modular

  • package Add dependency :“@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"
  }
}

After the addition is completed, execute lerna link

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


node perform core.js You will find that the console prints : I am a tools package ~~. In this way, you can perform local debugging

Prepare for release

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

  • register npm Account
  • New organization spring-breeze( It depends on your project )
  • perform npm login
  • Respectively in core and utils Under the package.json Middle configuration
 "publishConfig": {
    "access": "public"
  }

  • newly build gitignore
*node\_modules

  • newly build git Warehouse , Put the project git remote add “ Yours git Warehouse ”, And submit the project to git Warehouse
git remote add https://gitee.com/geeksdidi/spring-breeze.git 
git add .   
git commit -m " initialization "
git push -u origin master

  • newly build LICENSE.md( It can be an empty file )
  • Modify a file to execute lerna publish

Be careful : Each time a new version is released, the code is submitted to git On

View published package

land npm Official website You can see your own package

原网站

版权声明
本文为[u012804784]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210733276230.html