我如何在 Yarn 2 中共享工作区中的公共依赖项?

How in Yarn 2 do I share common dependencies in workspaces?

基本相同的问题,但对于 yarn 2。我将共享依赖项放在层次结构的顶部。我相信我目前没有使用 PnP。

.yarnrc.yaml

nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-2.2.2.cjs

在最顶层,我在我想调用 tsc 的一个“工作区”中安装了 typescript(但我认为这可能是任何带有二进制文件的模块) command not found: tsc 我也注意到了一些警告,例如。 graph@workspace:app-lib/graph/packages/app doesn't provide jest@>=24 <25 requested by ts-jest@npm:24.3.0 在应用程序的父级中提供。

https://yarnpkg.com/advanced/qa#how-to-share-scripts-between-workspaces

Little-known Yarn feature: any script with a colon in its name (build:foo) can be called from any workspace. Another little-known feature: $INIT_CWD will always point to the directory running the script. Put together, you can write scripts that can be reused this way:

{
  "dependencies": {
    "typescript": "^3.8.0"
  },
  "scripts": {
    "g:tsc": "cd $INIT_CWD && tsc"
  }
}

Then, from any workspace that contains its own tsconfig.json, you'll be able to call TypeScript:

{
  "scripts": {
    "build": "yarn g:tsc"
  }
}

您可以将 -T,--top-level 标志与 yarn run 一起使用。它将查找根级依赖项。

{
  "scripts": {
    "tsc": "yarn run -T tsc"
  }
}

https://yarnpkg.com/cli/run