ariesate / engine

28 stars 9 forks source link

Replace Lerna with PNPM Workspaces #2

Open yingpengsha opened 2 years ago

yingpengsha commented 2 years ago

Intro

Whether we choose Lerna or Workspace, our goal is to choose a relatively mature solution to solve some side effects caused by Monorepo. So what do we hope to solve?

  1. Dependencies management
    1. The ability to manage packages dependencies uniformly
    2. Operations for normal dependencies are not too complicated with Monorepo.
    3. Automatically handles dependencies that exist between packages, rather then using link.
  2. Workflows
    1. Development Workflow
      1. Both rookies and veterans can get into development mode quickly (Half the work is done by tools, and the other half needs to be done by document).
    2. Release Workflow
      1. Publishing multiple packages together or publishing a single package can be done very easily.

In the end, we should choose a solution that solves more problems without incurring too much mental cost.

Solutions

Lerna

Lerna, one of the most popular Monorepo solutions, has three main features.

  1. Packages Management
    1. lerna init: Create a new Lerna repo or upgrade an existing repo to the current version of Lerna.
    2. lerna create: Create a new lerna-managed package.
    3. lerna import: Import a package into the monorepo with commit history.
    4. lerna list: List local packages.
  2. Dependencies Management
    1. lerna boostrap: Installs all dependencies and links any cross-dependencies, like npm install.
    2. lerna add: Add local or remote package as dependency to packages in the current Lerna repo, like npm install <package>.
    3. lerna link: Symlink together all packages that are dependencies of each other, link npm link.
    4. lerna clean: Remove the node_modules directory from all packages.
  3. Release Workflow
    1. lerna version: Bump version of packages changed since the last release.
    2. lerna publish: Publish packages in the current project.

Overall, Lerna is a solution with complete functionality coverage, but there is no silver bullet in software. Therefore, the existing defects of Lerna will be summarized later, especially for this project.

Workspaces

Workspaces is an inherent feature of mainstream PMT(Package Management Tool). It's mainly developed by the natural advantages of package management tools in dependency management.

It has an unparalleled advantage in dependency management, and because it's a inherent feature of PMT, it tends to have no additional mental cost.

This is a survey of Workspaces for mainstream PMT, ranked from top to bottom, good to bad, so it's obvious which one to choose :)

PMT How to use Unified Management of Dependencies Subpackages depend on each other Summary
PNPM Add pnpm-workspace.yml file βœ… Automatic support ⚠️ Need to use PNPM Workspace protocol πŸ‘ Stable and easy to use, and PNPM is the best PMT.
NPM Configure workspaces in packages.json ⚠️ Additional scripts are required to add dependencies to subpackages. βœ… Default to search for the package in the nearest workspace πŸ†• Need newer version of NPM, and it's still in the iterative stage, may be the best solution in the future.
Yarn ⬆️ ⚠️ Need to configure nohoist ⚠️ Cumbersome and unclear πŸ› Too many bugs, and it's no longer maintained
Yarn2 ⬆️ βœ… Automatic support ⚠️ Need to use Yarn Workspace protocol πŸ€” Too many strange features, It is not qualified as a PMT.

When we consider the Workspaces feature of a PMT as a solution, we also need to consider whether the solution is qualified or useful as a PMT. ​ Overall, Workspaces can solve all the existing problems in Dependencies Management, while workflow needs to be done in other ways.

Summary

As mentioned above, Lerna has complete functionality, Workspaces only solves the dependencies management problem well. So why do I still insist on replacing Lerna with PNPM Workspaces? ​ First, Lerna's Packages Management features are not needed. Second, Lerna's Dependencies Management features are completely covered by Workspaces and can do better. Third, Lerna's Release Workflow features are simple and crude. We wanted more flexibility, extensibility, stability, certainty from Release Workflow and Lerna didn't allow us to do it better.

In short, Lerna has some features that are not needed or are difficult to use and can impose additional mental cost, even if the PNPM Workspaces is not complete, it is sufficient for this project, and it is small and easy to use, with no additional burden.

Final Todo

References


RFC & PTAL: @sskyy

sskyy commented 2 years ago

Approved.