管理涉及多个相互依赖的包的大型 JavaScript 或 TypeScript 项目对于开发人员和开发团队来说可能是一项重大挑战。通常,开发人员依赖每个包的多个存储库,这会导致代码维护、依赖项管理和协作方面的开销。
Lerna 是一个为管理 monorepos 开发的强大工具,它简化了这个过程。 Monorepos 使团队能够在单个存储库中托管多个包,从而简化依赖关系管理并使团队之间的协作更加顺畅。
这本电子书旨在提供使用 Lerna 有效管理您的 monorepos 的完整指南。无论您处理的是组件库还是具有多个互连包的大型项目,您都会发现有价值的见解,帮助您通过 Lerna 最大限度地提高生产力。
Lerna 是一个开源工具,有助于管理 monorepo 中的多个包。它提供了自动依赖管理、版本控制和发布等强大功能,使大规模维护 JavaScript 和 TypeScript 项目变得更加容易。
Monorepos 是许多大型项目的架构选择,因为它们提供了多种好处:
尽管有这些好处,管理单一存储库也会带来独特的挑战,特别是在管理依赖项和版本控制方面。 Lerna 旨在正面应对这些挑战,为 monorepos 提供优化的工作流程。
开始之前,请确保您已安装 Node.js 和 npm(或 Yarn)。 Lerna 与 npm 和 Yarn 兼容。
您可以通过 npm 在全局安装 Lerna:
npm install --global lerna
或者,您可以将 Lerna 添加为项目中的开发依赖项:
npm install --save-dev lerna
安装后,通过导航到项目目录并运行来初始化您的 monorepo:
lerna init
这将创建必要的配置文件,包括 lerna.json,并设置一个包文件夹来存放您的各个包。
在 Lerna 项目中,每个包都位于 packages 下自己的子文件夹中。每个包必须有自己的 package.json 文件用于依赖管理。
示例结构:
/my-project /packages /package-a /package-b lerna.json package.json
Managing dependencies across multiple packages is one of Lerna’s core strengths.
Lerna allows you to add dependencies to a specific package. For example, if only package-a needs lodash, you can run:
lerna add lodash --scope=package-a
When multiple packages share dependencies, you can hoist those dependencies to the root of your monorepo. This reduces redundancy and speeds up installations. To enable hoisting, add this to lerna.json:
{ "hoist": true }
To install dependencies and link packages that depend on one another, run:
lerna bootstrap
This ensures that all necessary external dependencies are installed and that packages can reference each other properly.
Lerna makes it easy to execute scripts (e.g., build, test, lint) across all packages in your monorepo.
To run a script like build across all packages, use:
lerna run build
If you only want to run a script in certain packages, use the --scope flag:
lerna run test --scope=package-a
This flexibility allows for more targeted execution, saving time during development.
Lerna provides robust versioning and publishing features, allowing you to easily version and release packages.
In fixed mode, all packages share the same version number. When any package is updated, the version number is incremented for all.
In independent mode, each package has its own version number. When a package is changed, only that package’s version is updated.
To switch to independent mode, modify lerna.json:
{ "version": "independent" }
To publish your packages to npm, run:
lerna publish
Lerna will handle versioning and publishing based on your configuration.
Combining Lerna with Yarn Workspaces can further optimize dependency management by hoisting even more shared dependencies.
To enable Yarn Workspaces, modify your lerna.json file:
{ "npmClient": "yarn", "useWorkspaces": true }
Then update your package.json:
{ "workspaces": ["packages/*"] }
This integration boosts performance and simplifies managing large-scale projects.
Lerna allows filtering to run commands for specific packages or to exclude certain packages.
Example for running on specific packages:
lerna run build --scope=package-a --scope=package-b
Example for excluding packages:
lerna run build --ignore=package-c
You can define custom Lerna commands within package.json for specialized workflows. These commands can then be run across your packages.
Lerna is an invaluable tool for managing monorepos, offering features that simplify complex workflows, from dependency management to versioning and publishing. By adopting Lerna, teams can reduce complexity, streamline processes, and improve collaboration, making it easier to maintain large-scale projects.
Whether you’re working on a simple component library or a multi-package ecosystem, Lerna provides the tools needed to manage your project effectively. Keep experimenting with Lerna’s advanced features to unlock its full potential.
快乐编码:)
以上是掌握 Lerna:管理 JavaScript Monorepos 的指南的详细内容。更多信息请关注PHP中文网其他相关文章!