简体   繁体   English

纱线链接所有包/工作区

[英]Yarn Link All Packages/Workspaces

Yarn's link allows you to register a local package for symlinking into another local package. Yarn 的链接允许您注册一个本地包以符号链接到另一个本地包。 To use link you cd into the package directory and run yarn link .要使用link ,您 cd 进入包目录并运行yarn link This works fine on individual packages, however I have a monorepo, using Yarn's workspaces alongside Lerna.这适用于单个包,但是我有一个 monorepo,使用 Yarn 的工作区和 Lerna。 There are many packages, and I would like a simple way of linking all the packages within the monorepo.有很多包,我想要一种简单的方法来链接 monorepo 中的所有包。

My package.json :我的package.json

…
  "workspaces": [
    "packages/*"
  ],
…

My lerna.json :我的lerna.json

{
  "npmClient": "yarn",
  "useWorkspaces": true,
  "packages": [
    "packages/*"
  ],
  …
}

Is there a simple way to run yarn link (and yarn unlink ) on each package?是否有一种简单的方法可以在每个包上运行yarn link (和yarn unlink )?

Lerna provides the exec command to 'Run an arbitrary command in each package': Lerna 提供exec命令来“在每个包中运行任意命令”:

link.sh链接.sh

lerna exec -- yarn link

unlink.sh取消链接.sh

lerna exec -- yarn unlink

If I may offer a pure Yarn solution.如果我可以提供纯纱线解决方案。

Using the Yarn workspace-tools plugin you can make use of the Yarn foreach command to run a command on all workspaces.使用Yarn 工作区工具插件,您可以使用Yarn foreach 命令在所有工作区上运行命令。

To add the plugin run:添加插件运行:

yarn plugin import workspace-tools

Usage:用法:

yarn workspaces foreach <commandName> ...

Example:例子:

unlink:取消链接:

yarn workspaces foreach run unlink

link:关联:

yarn workspaces foreach run link

I would suggest adding 1 command in each workspace with the same name which unlinks and links the required local dependencies then calling them all with one foreach command.我建议在每个工作区中添加 1 个具有相同名称的命令,以取消链接并链接所需的本地依赖项,然后使用一个 foreach 命令将它们全部调用。

Edited *已编辑*

After re-reading your inital question I realise you're using Yarn Workspaces which means you don't even need to use Yarn Link at all to have one local package be a dependency in another package.重新阅读您的初始问题后,我意识到您正在使用 Yarn Workspaces,这意味着您甚至根本不需要使用 Yarn Link 就可以让一个本地包成为另一个包中的依赖项。

From the directory of the package which requires the local package run:从需要本地包运行的包目录:

yarn add <PACKAGE NAME>@*

Where <PACKAGE NAME> is the name of the required package.其中<PACKAGE NAME>是所需包的名称。

To confirm the dependency was successfully added run:要确认依赖项已成功添加,请运行:

yarn workspaces info

And confirm your dependency was added it should look like this:并确认您的依赖项已添加,它应该如下所示:

{
  // Rest of the workspace info
  "<DEPENDANT PACKAGE>": {
    "location": "packages/<DEPENDANT PACKAGE>",
    "workspaceDependencies": [
      "<DEPENDENCY PACKAGE>"
    ],
    "mismatchedWorkspaceDependencies": []
  },
}

If your IDE complains about "Cannot find module or its corresponding type declarations" be sure to re-build the package.如果您的 IDE 抱怨"Cannot find module or its corresponding type declarations" ,请务必重新构建包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM