What are Monorepo and Lerna?

Monorepo is a way to manage project code by managing multiple modules/packages in a project repository (REPO), as opposed to the usual repO for each module.

Lerna is a Monorepo solution

Lerna is a management tool that optimizes the workflow of a multi-package code base hosted on Git \ NPM. It allows you to manage multiple sub-projects under the main project, eliminating the problem of multiple packages being dependent on each other and having to manually maintain multiple packages at release time.

2. The advantages and disadvantages of

advantages

  • 1. Saves a lot of storage space.
    • For example, if multiple projects rely on Vue, vue-route, etc., node_modules will have a lot of redundancy in projects with multiple repositories.
  • 2. Convenient debugging
    • Under multiple warehouse projects, debugging depends on packages through NPM link, which is not required with Lerna
  • 3. Resource package upgrade problem
    • A project depends on multiple NPM packages, and when a subNPM package code is modified and upgraded, the main project package is updated and modified. (This problem is the most annoying, maybe a version number has to be updated and released)

Disadvantages:

  • Because the source code is together, warehouse changes are common, storage space becomes large, even a few gigabytes, and CI test runs can take longer

3. Usage

  • 1. Install lerna
npm install lerna -g
Copy the code
  • 2. Initialize a LERNA project
mkdir lerna-demo

cd lerna-demo

lerna init
Copy the code

Generate the following files:

- Packages - Lerna. json - package.json(project description file)Copy the code
  • 3. Common commands
    Lerna init // Initialize the Lerna project lerna create <name> // create a new package managed by Lerna lerna bootstrap // install all dependencies and connect all cross-dependencies lerna add axios // Add a module package to the outermost public node_modules lerna add a --scope=b Yarn start // Execute any command in the corresponding packages the following command is used to execute the yarn start command for project A in packages. Json. Instead of adding --scope a to the command, use the following command: This will run the rm -rf./node_modules lerna exec -- rm -rf./node_modules lerna list command in packages to display all installed packages. Equivalent to lerna ls lerna clean // Remove the node_modules directory from all packages. The outermost root node_modules lerna publish // sends packets in the current project. Lerna publish will never publish packages marked private (" private ": true in package.json)Copy the code

4. Application (Application Scenario)

  • Build a platform base component library project from scratch
  • Framework projects (e.g. vue3 source code)
  • Corporate component library project
  • Tool-based projects (e.g. Babel, Facebook/Jest, Alibaba/Rax)