What is Yarn Workspace

Yarn workspace is a dependency management mechanism provided by Yarn under Monorepo. Manage dependencies of multiple packages in the repository: Hosting common dependencies. This prevents repeated installation of packages in the package. In the workspace mechanism, node_module is installed in the root directory and yarn.lock is generated. In a single package, yarn.lock does not need to be generated.

How do I use Yarn Workspace

  1. Yarn Enable work area
yarn config set workspaces-experimental true
Copy the code

Add workspaces-experimental true to the system global. Yarnrc.

2. Modify the package.json file in the root directory and add the following content:

{
 "private": true."workspaces": ["packages/*"]}Copy the code

The meanings of the configuration item are as follows: private – Disables publication of root directory contents workspaces – Specifies the path of the package in the workspace. The value is an array of strings that support Glob wildcards.

Lerna installs dependencies in a way

Lerna installs dependencies using the command Lerna bootstrap. This command is used to CD to each subfolder of package folder, run NPM install or YARN install, generate version lock file and node_module in the subfolder, and manage dependencies separately.

Yarn workspace combines with Lerna

Way to combine

  1. Configuration lerna. Json
{..."npmClient": "yarn"."useWorkspaces": true
}
Copy the code
  1. Configuration package. Json
{
 "private": true."workspaces": ["packages/*"]}Copy the code

Role allocation

Yarn manages dependencies. The workspace automatically references the package and sets the soft link (symlink). The soft link only takes effect in the current workspace. Lerna is responsible for the release of versions, the interdependence of packages and the version management.

benefits

  • Reduce the disk footprint of the project

    Yarn promotes co-dependencies in a project to the root directory for installation

  • Automatically set soft links for easy debugging

    The Yarn workspace will automatically reference the package and set symlink

  • Use the same yarn.lock for all packages, resulting in fewer conflicts and easier review

use

  • First installation dependencylerna bootstrap
  • The installationdependenciesDependency ‘yarn add -w [PKG]
  • The installationdevDependenciesRely onyarn add -D -W [pkg]

< span style = “font-size: 14px! Important; font-family: ‘devDependencies’