What is Lernajs?

LernaJs is a multi-package management tool developed by the Babel team. Because Babel contains many sub-packages, which used to be stored in multiple repositories, it is difficult to manage, especially when there is a call to the system package, which is difficult to publish. So in order to manage packages better and faster,Babel introduced lernaJs, which uses monorepo concepts. Now React,Babel,Angular, and Jest all use this tool to manage packages.

What is monorepo?

Monorepo is compared to multi-package. Multi-package is the creation of multiple repositories, one for each package. Monorepo sets up a repository where multiple packages are managed, which has two benefits

  • The communication between various packages is more convenient. For multi-package, if one of the packages in the system is modified, the version needs to be issued separately, and other packages referencing this package need to be issued. With Lerna you can automatically manage the release of these packages, which is very convenient.
  • Some common configurations, such as ESLint, Babel,rollup, etc., manage these development configurations uniformly

Quick start

Install and initialize the project

Install lernajs
npm install lerna -g or yarn global add lerna
Copy the code
Initialize the LERNA project
lerna init
Copy the code

After execution, a MonorePO managed by Lerna is automatically generated. Its file structure is as follows:

Package. json // Lerna JS configuration fileCopy the code
Add child package

After the environment is initialized, you need to add a subpackage first. The command to add a subpackage is as follows:

lerna create <pkgName>
Copy the code

After executing this command, the package will ask the same questions about the package name, version and so on as NPM init. After filling in these questions, the package will automatically create a subpackage. The default directory structure for subpackages is as follows:

<packName> __tests__ // Test file lib // The entry file for the package is in the package.json readme.md directory by defaultCopy the code
How do I add dependencies to subpackages

The command to add a dependency is:

Lerna add <moduleName> --scope = <pkgName> --scope = <pkgName2> // Add pkgName1 to pkgName2. Cross-references within packages copy pkgName1 to pkgName2Copy the code
All subpackages update dependencies
lerna bootstrap
Copy the code
How to package

Packaging is specific to the packaged plug-in configuration within the respective package, and rollup is recommended for JS or component plug-ins, which is not covered here.

How to publish
lerna publish
Copy the code

You can set rules for version numbers in lerna.json

// lerna.json

"version": "0.0.3"// If it is a number, all subpackages are of this version. Set to 'independent' to manage the version of each package independentlyCopy the code

Lernajs compares package changes and automatically publishes the subpackages that need to be published.

conclusion

This allows us to implement the structure of a multi-package Monorepo, each of which can be developed, packaged, and tested by command. The configuration of ESLint, Babel, and Jest is the same for all subpackages. You can easily issue a command, update a command and add dependencies.

Final file structure

docs
|---README.md
packages
|---packageOne
|---packageTwo
      .
      .
      .
script
|---build.js
|---test.js
.commitlintrc.js
.eslintignore
.eslintrc
.gitignore
babel.config.js
lerna.json
package.json

Copy the code

Using the command

Commands about packages

Create a new package:

lerna creat <packname>
Copy the code

release

lerna publish
Copy the code

All packages update node_modules

lerna bootstrap
Copy the code

Add package B to package A

lerna add <packageA> --scope <packageB>
Copy the code