create

Install lerna and YARN and initialize the project

npm i lerna yarn -g
git init mono-repos && cd mono-repos && yarn add lerna -D && lerna init
Copy the code

Configure the root directory package.json

/* /package.json */ {"name": "root", "private": true, // "devDependencies": {"lerna": { "^4.0.0"}, "workSpaces ": [// enable Yarn workspaces" packages/*"]}Copy the code

Configuration lerna. Json

/* /lerna.json */ { "packages": [ "packages/*" ], "version": "Independent ", // Independent version number mode, each package maintains its own version number, the default is fix mode "npmClient": "yarn", // dependency management tool, the default is NPM "useWorkspaces": True // Enable workspaces mode}Copy the code

Dependency management

  • You are advised to assign dependency management to YARN for professional management.
  • yarn:classic.yarnpkg.com/en/

Common commands

/ / global installation depend on the yarn install / / for a package to add, delete, depend on the yarn workspace < package - name > [add | remove] < library > [-d | -s] / / other please consult websiteCopy the code

Hoist the function

  • When lerna + YARN workspace is used, lerna’s –hoist is disabled. We can use Yarn install to implement the same function.
mkdir -p packages/sub-package-1/src/index.js && cd packages/sub-package-1 && npm init -y
cd -
mkdir -p packages/sub-package-2/src/index.js && cd packages/sub-package-2 && npm init -y
Copy the code
  • Install pretty-format in sub-package-1 and sub-package-1, and the dependency will be placed in node_modules of the corresponding package.
  • After yarn install is executed, the same pretty-format in both packages will be promoted to node_modules of root.
yarn workspace sub-package-1 add -D pretty-format
yarn workspace sub-package-2 add -D pretty-format
yarn install
Copy the code

release

  • Version publishing is completely managed by LERna. In addition, LerNA also supplements some of the limitations of YARN dependency management.
  • lerna:github.com/lerna/lerna

Common commands

Lerna exec // Executes arbitrary commands in each package lerna changed // checks which packages have been updated since the last release lerna diff // Since the last release, Lerna publish // Release version lerna clean // Clear all node_modules lerna init // Initialize the project lerna create // Create the subpackage lerna run in the project // Run the NPM script in the package containing the scriptCopy the code

Try to publish a package

  • First, submit the code to the repository and log in to the NPM library.
npm login
Copy the code
  • Configure the subproject package.json
/* /packages/subsub-package-1/package.json */ /* /packages/subsub-package-2/package.json */ { "name": "Sub - package - 1", / / or sub - package - 2 "version" : "1.0.0", "main" : "SRC/index. Js", "scripts" : {" test ", "echo" Error: No test specified" && exit 1"}, "repository": {// specified repository" type": "git", "url": "Https://xxx.com/xxx/xxx.git"}, "publishConfig" : {" access ":" public "/ / not to spend money to release the package of begin with @ when you need to set up public}}Copy the code
  • release
lerna publish
Copy the code
  • The above is the basic use of mono-Ropos project built by LERna + YARN workspace, and it will be updated if there are new discoveries.

Possible problems

  • When the uncommitted code release fails, the commit code will not be updated to the NPM repository for the next release, which can be committed as follows.
Git git git git git git git git git git git Package.json will not be updatedCopy the code

reference

zhuanlan.zhihu.com/p/108118011

zhuanlan.zhihu.com/p/372889162