What is a lerna

Lerna is a multi-package management tool developed by Babel team. The previous development mode was one project and one warehouse, which was difficult to manage, especially when there were internal package calls in the system, it was difficult to release, so it was better to manage package management, so lernaJS was launched, using the concept of Monorepo. Now vue-CLI, create-React-app and so on all use Lerna

How to build LerNA

Install lerna

    yarn global add lerna
    or 
    npm install lerna -g
Copy the code

Create a project

Mkdir project name lerna initCopy the code

Description of project Structure

| -package package | -lerna. json configuration file | -package. jsonCopy the code

Advantages and disadvantages lerna

advantages

  • Saves a lot of storage space
    1. Node_modules can have a lot of redundancy if multiple projects rely on React
    2. Convenient debugging, multiple project warehouse cases to NPM link to global import, use Lerna is not required

disadvantages

  • With all the source code in one place, it can take a long time for a project to start testing

Lerna’s processing mechanism

  • Fixed mode
  • Stand-alone mode

Fixed mode

All packages are fixed versions and will be updated every time they are upgraded, regardless of whether changes are made

Independent mode

Each package has a fixed version number. Only the version number of the changed content is updated each time. The content version number of the unchanged package is not upgraded

use

Json file, replace the version content with independent {“version”: “independent”}

Lerna basic commands

create

 lerna create xxx 
Copy the code

This command creates a package named XXX

add

lerna add react --scope=xxx
Copy the code

My React dependencies are only installed under the XXX package name, but not all of them. Lerna add React is only installed under the XXX package name

list

lerna list
Copy the code

You can view the current package names and use lerna List to see if you can find our package names when we publish our packages

bootstrap

lerna bootstrap
Copy the code

Install dependencies to root node_modules = NPM install

publish

lerna publish
Copy the code

Publish our package to the NPM package. If we want to publish to our own private NPM, we can simply replace the address in the register in package.json

link

    lerna link
Copy the code

Debugging, the current package name in the form of dependency for local tuning, and does not affect the global package, type NPM link

run

lerna run <script>
Copy the code