如何使用 Yarn 工作區將共享依賴項添加到 monorepo? (How do I add shared dependencies to a monorepo using Yarn workspaces?)


問題描述

如何使用 Yarn 工作區將共享依賴項添加到 monorepo? (How do I add shared dependencies to a monorepo using Yarn workspaces?)

我正在使用 Yarn 的 Workspaces 功能來管理 monorepo。一些單獨的工作區在他們自己的 package.json 中定義了自己的依賴項,但是我也有一些共享依賴項(一些用於測試整個 repo,一些用於所有工作區)。如何以正確的方式聲明這些?

如果我嘗試將它們添加到根 package.json,Yarn 會給我一個警告。但是在升級依賴項時向所有 n 工作區添加相同的依賴項(有很多並且可能會增長)似乎很乏味且難以維護。

是正確的做法添加共享依賴項(例如,typescript,或 jestserverless


參考解法

方法 1:

After a half‑day search and trying, I found multiple ways to manage shared dependencies

1. Sync dependency version with syncpack

https://github.com/JamieMason/syncpack/

Given the fact that yarn workspaces will share the dependencies if their versions are the same (see https://classic.yarnpkg.com/lang/en/docs/workspaces/), all we have to do is to keep the dependency versions in sync to make dependencies sharing, which is exactly what syncpack does.

I found this is the least harmful way to make sharing dependencies works, while other methods require some degree of twisting package.json.

2. Use peer dependencies

For each package, set sharing dependencies as peerDependencies with version “*”, and set root workspace set the real dependencies. For example:

# ./package.json
{   
    ...
    "workspaces": ["packages/*"],
    "dependencies": [
        "some‑lib": "^1.0.0",
    ]
}

# ./packages/pkg‑a/package.json
{
    ...
    "peerDependencies": [
        "some‑lib": "*",
    ]
}

and yarn install at workspace root.

3. Share script (require yarn v2)

See https://yarnpkg.com/getting‑started/qa#how‑to‑share‑scripts‑between‑workspaces

Miscellaneous

yarn provides workspace cli to add/remove package, see https://classic.yarnpkg.com/lang/en/docs/cli/workspace/

yarn workspace awesome‑package add react react‑dom ‑‑dev

yarn workspace web‑project remove some‑package

(by old gregWcW)

參考文件

  1. How do I add shared dependencies to a monorepo using Yarn workspaces? (CC BY‑SA 2.5/3.0/4.0)

#yarn-workspaces #yarnpkg






相關問題

如何使用帶有打字稿和輸出文件夾的紗線工作區? (How to use yarn workspaces with typescript and out folders?)

如何在 Monorepo 中運行多個開發包 (How to run multiple packages in development inside a Monorepo)

帶有許多 npm 腳本的 Monorepo (Monorepo with Many npm-scripts)

使用紗線工作區導出枚舉時出現意外的令牌錯誤 (Unexpected token error exporting enums using yarn workspaces)

如何在 Expo 應用示例嵌套文件夾中熱重載開發包? (How to hot reload a development package in an Expo app example nested folder?)

將 monorepo 包放在包文件夾下是一種約定,還是 yarn 工作區期望這樣? (Is putting monorepo packages under a package folder a convention, or do yarn workspace expect that?)

通過創建具有循環依賴關係的 monorepo 導致錯誤 (Causing an error by creating a monorepo with circular dependencies)

Monorepo – Yarn 工作區 Typescript Node.JS 項目 – 運行 nodemon 時找不到模塊 (Monorepo – Yarn workspaces Typescript Node.JS project – cannot find module when running nodemon)

紗線工作區為每個運行 shell 命令 (Yarn workspaces run shell command for each)

紗線工作區共享包未在 create-react-app 中加載 (yarn workspaces shared package does not load in create-react-app)

如何使用 Yarn 工作區將共享依賴項添加到 monorepo? (How do I add shared dependencies to a monorepo using Yarn workspaces?)

使用紗線工作區在monorepo中跨項目共享配置變量? (Sharing config variables across projects in a monorepo using yarn workspaces?)







留言討論