管理涉及多個相互依賴的套件的大型 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中文網其他相關文章!