Monorepo

Monorepo 实现方式在该链接已经分析的比较透彻, 一共有如下几个实现方式:
  1. Lerna + npm
    1. Lerna + yarn
      1. yarn workspace
        1. Lerna + yarn workspace
          1. yarn 3.0 + pnp

            lerna

            If we installed the lernal locally, we need run lerna with npx
            notion image

            Lerna 使用踩坑

            在安装项目的过程中, 如果使用 npm installhook(例如 postinstall) 直接触发 lerna run bootstrap --hoist 的话, 会导致 npm 包提升不彻底, 会造成重复引入类型,包导致构建失败, 目前有两个解决方案:
            • 手动分开运行 npm installnpm run bootstrap
              • 加一个新的命令npm run init, 主动触发 npm installnpm run bootstrap

                Yarn Workspace

                优点:
                • yarn workspace 的安装速度比较快
                  • 安装时不会临时更改 package.json
                    • 所有依赖都安装到根项目 node_modules, 非必要情况下不会在子包中生成 node_modules
                      • 安装/更新/删除单个 package 下的包的话支持也比较好
                        缺点:
                        • 因为所有的子项目都会被链接到 node_modules 里面, 所以会有加载多个库类型的问题, 解决方法在此

                          依赖管理

                          Reference

                          Why Lerna and Yarn Workspaces is a Perfect Match for Building Mono-Repos – A Close Look at Features and Performance

                          © Jiyu Shao 2018 - 2025