.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}pre{font-size: 24px; The line – height: 1.8}

introduce

Monorepo: 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 tool for managing multiple NPM modules. It is a project that Babel uses to maintain its own Monorepo and open source it. Optimize the workflow of maintaining multiple packages to solve the problem that multiple packages depend on each other and need to be manually maintained for release.

steps

1. Global installationlernaandyarn

$ npm i -g lerna yarn
Copy the code

2. Initialize the project

$ mkdir ndogkit && cd ndogkit

$ lerna init

$ tree -a -C --dirsfirst -L 3 -I 'node_modules|.git'.├ ── ├─ ├─ download.jsonCopy the code

3. Add yarn workspace

(1) package.json

{..."name": "root"./* Private in the project root must be set to true, otherwise workspace will not be enabled */
  "private": true./* Specify the module to manage, see vuepress */ for package organization
  "workspaces": [
    /* All module packages are in this folder */
    "packages/@ndogkit/*"./* Documentation */
    "packages/doc"./* Scaffolding related */
    "packages/ndogkit"]... }Copy the code

(2) lerna.json

{.../* [doc](https://github.com/lerna/lerna/tree/master/commands) */
  "command": {
    "bootstrap": {
      /* This mode: when multiple packages have the same dependencies, they will be promoted to the top-level node_modules to avoid repeated installation and reduce the volume */
      "hoist": true./* During bootstrap, pass the parameters to NPM install */
      "npmClientArgs": ["--no-package-lock"."--no-ci"]},"version": {
      "allowBranch": ["master"]./* Commit all current changes instead of adding new commits, skipping 'git commit' and 'git push' */
      "amend": false./ * automatically generated changelog. Md, see [about stereotypes submit] (https://www.conventionalcommits.org/zh-hans/v1.0.0-beta.4/) * /
      "conventionalCommits": true
    },
    "publish": {
      /* Skip NPM package access */
      "verifyAccess": false./* Release address */
      "registry": "http://npm.jsany.org"}},/* Run all commands */ using YARN
  "npmClient": "yarn"./* Workspaces to use YARN (specify workspaces in package.json) */
  "useWorkspaces": true./* Standalone mode: In this mode only packages that have changed will be released, and each release will prompt you for each package that has changed to specify whether it is a patch, minor, major or custom change. Note: after using the contracted commit, the corresponding version will be generated according to the COMMIT, so there is no need to manually specify */
  "version": "independent". }Copy the code

4. Add modules

$ tree -a -C --dirsfirst -L 3 -I 'node_modules|.git|package-lock.json'. / packages ├ ─ ─ @ ndogkit │ ├ ─ ─ the core │ ├ ─ ─ an egg - tools │ └ ─ ─ Shared - utils ├ ─ ─ docs └ ─ ─ ndogkit ├ ─ ─ CMDS ├ ─ ─ utils ├ ─ ─ cli. Js ├ ─ ─ index. Js └ ─ ─ package. The jsonPackage. json if "private": true is set, it will not be displayed
$ lerna ls
info cli using localVersion of Lerna Lerna notice CLI v3.19.0 LERna info versioning independent @ndogkit/core @ndogkit/egg-tools @ndogkit/shared-utils ndogkit lerna success found 4 packagesCopy the code

Note: Here lerna gets the module name as the name attribute from package.json

5. Install dependencies (four types)

Install all dependencies (bootstrap)

# --npm-client=yarn --hoist conflicts
Replace Lerna Bootstrap with the YARN Workspace feature
$ yarn
Copy the code

(2) The root project installs dependencies

# yarn If the NPM package is installed in workspace mode, the -w parameter must be added
$ yarn add -D -W [...pkg]
Copy the code

(3) Install external dependencies on package

$ yarn workspace @ndogkit/share-utils add chalk debug globby fs-extra mime
Copy the code

(4) Install internal dependencies for package

You must specify the correct version number, otherwise you will find the package in NPM$YARN workspace @ndogkit/egg-tools add @ndogkit/[email protected]Copy the code

6. Remove dependencies

$ lerna clean && rm -rf ./node_modules
Copy the code

7. Run commands

# lerna run [...cmd] --scope @scope
$ lerna run compile --scope @ndogkit/shared-utils
Copy the code

Release 8.

Json package with "private": true is not displayed
$ lerna changed
Each release automatically updates the version number of the relevant package and updates other package dependencies that reference this package
$ lerna publish
Copy the code