This is my fourth day of the November Gwen Challenge. Check out the final Gwen Challenge 2021 for more details

preface

As the complexity of the project increases, the individual modules of the code are faced with the problem of maintaining multiple repositories or placing them in a single repository. The idea of Monorepo is to store multiple project code in one repository. The famous JS plug-in Babel uses Monorepo to manage their code.

The advantage of monorepo

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., can be used to manage these development configurations in a unified manner, because of the structure of Monorepo, all subpackages will use this configuration.

Lerna profile

The installation

NPM install lerna -g

Working mode

Lerna can work in two modes: Fixed/Locked Mode and Independent Mode, with the former Mode being the default (vue Babel et al are in this Mode).

Add a command line argument at initialization — the independent argument can be changed to the latter mode, depending on whether version is a version number or a “independent” string in lerna.json.

If it is the latter, the version number of each changed package (whether it is a patch, a minor change, or a major change) is selected sequentially at each publish.

Lerna. json Parameter description

  • Version Version of the current library. In independent mode, set this parameter to independent
  • NpmClient Specifies the client that can be used by commands. The default is NPM, which can be set to YARN
  • Command. The publish. IgnoreChanges can specify the change in the directory or file will not be publish
  • Command-publish. message specifies the format of the message to submit when publishing
  • Command-publish. registry sets the registry address for NPM package publishing
  • Command. Bootstrap. Ignore sets the package that will not be affected when lerna Bootstrap installs dependencies
  • Command. The bootstrap. NpmClientArgs specified in performing lerna bootstrap command is passed to the NPM install parameters
  • Scope specifies which packages will be affected by the lerna bootstrap command
  • Packages Specifies the directory where packages reside
  • You can set workspace for lerna projects by adding parameters in lerna.json. Projects under the same workspace can share a node_modules to avoid duplication of node_modules in the package: Set private and workspaces parameters for package.json files, and add useWorkspaces parameters to lerna.json files.

release

If you want to send NPM packets to the Internet, log in to the NPM account and execute lerna publish. If you want to publish packets only for the Intranet, you do not need to log in and publish. If you want to prevent it from being published to NPM, set “private”=true in its package.json;

Lerna command details

create:

  • NPM init -y; lerna create XXX -y; NPM init -y; Lerna create < name > [LOC] has more optional parameters as documented (workspaces parameters need to be configured);

add:

  • Install node_module module for one/some packages in LERNA; lerna add [@version] [–dev] [–exact]

The bootstrap:

  • bootstrap === [cd ${package} && npm run install for package in packages]

list:

  • The package.json attribute under the package is not listed.

Import:

  • Import local package, has not been successful;

Run:

  • lerna run xxx === [cd ${package} && npm run xxxFor package in packages]; The –scope argument can be added to control the scope of the run;

The exec:

  • lerna run xxx xx === [cd ${package} && xxx xxFor package in packages]; The –scope argument can be added to control the scope of the run;

The link:

  • Create soft link, similar to NPM link, not used;

clean:

  • Lerna clean === remove node_modules for all packages, can add –scope argument to control running scope;

Changed:

  • List the packages that lerna publishes to update next release. Git add and git commit Git diff –name-only v Not all packages are the same version as the Internet says.

version:

  • Analyze all packages managed by LERna and update to the next version, with different behavior (as if, uncertain) depending on the mode (fixed/independent); If fixed is used, it will be uniformly updated to a certain version, and subsequent parameters can be extracted or selected. For example, patch is to update a patch version.

The publish:

  • Can tag, upload Git, upload NPM. If your package name has scope e.g. “name”: “@gp0320/ gpWebpack “then add “publishConfig”: {“access”: “public”} in packes. json; Publish –access Public if the name of a publish package starts with @, such as @feu/tools, NPM defaults to a private publish and uses NPM publish –access public. Lerna publish does not support this parameter.

Other related links

Other lerNA best practices, automatic log generation, managing project dependencies, cleaning up the environment, and more: juejin.cn/post/684490… Juejin. Cn/post / 684490… Juejin. Cn/post / 684490…

Comparison with other Monorepo solutions (unified scripting commands, locking environments, testing using local NPM repositories, etc.) : juejin.cn/post/692485…

Practices for developing and managing UI component frameworks with LERNA: juejin.cn/post/684490…