Monorepo中的对等依赖 [英] Peer Dependencies In A Monorepo

查看:85
本文介绍了Monorepo中的对等依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当monorepo中的程序包具有对等依赖关系时,在开发过程中应如何使这些依赖关系可用?

When packages in a monorepo have peer dependencies, how should these dependencies be made available to them during development?

例如,位于/packages/namespace/alpha/的软件包在其软件包中可能具有 styled-components devDependency .json .

For example a package at /packages/namespace/alpha/ might have a devDependency of styled-components in its package.json.

可能的选项:

  1. 也声明与开发程序依赖项相同的依赖项(不必要的复制和维护成本).

  1. Declare the same dependencies as dev dependencies as well (unnecessary duplication and maintenance cost).

将软件包安装在monorepo的路由 package.json 中(使用 yarn link 时模块解析的潜在问题.

Install the packages in the monorepo's route package.json (potential issues with module resolution when using yarn link.

我在纱线工作区中使用Lerna.

I'm using Lerna with yarn workspaces.

推荐答案

纱线工作区应依靠

Yarn workspaces should install almost everything in the root node_modules relying on node's module resolution algorithm. That is what you described in option 2.

因此,基本上,它在大多数情况下都应该起作用.当某些工具依赖自己的解析逻辑或存在具有依赖性的不同版本,等等.

So, basically it should work in most cases. The problems may occur when some tool relies on its own resolution logic or there are different versions of some dependency and so on.

选项1是一种很常见的方法,但是正如您所说的,它增加了维护成本.您可能需要跟踪此类依赖项并将其标记为外部,以避免将它们包括在lib的内置版本中.

Option 1 is a quite common approach but as you said it adds maintenance cost. You may need to keep track of such dependencies and mark them as external in order to avoid including them into the built version of the lib.

有可能的解决方法.例如, Angular建议使用TS paths 选项.您也可以在没有打字稿的情况下进行相同的操作,例如在Create React App 汇总插件的自动化工具基于 peerDependencies 添加外部元素,因此您也可以安全地将它们列为devDependies.

There are possible workarounds. For example, Angular suggests using TS paths option. And you can do pretty the same without typescript in, for example, Create React App using jsconfig.json. Or you can use something similar to this rollup plugin that automates adding externals basing on peerDependencies, so you can safely list them as devDependies as well.

这两个选项在此lerna问题中均被视为合法

另一种选择是安装您的对等依赖项,就目前而言,没有正式的"依赖项.解决方案.有安装对等cli 软件包,可与 npm yarn .对于 yarn ,有一个热门功能请求.有计划将此功能添加到 npm v7 中,实际上npm在v3之前就已实现

Another option is to install your peer dependencies and as for now, there is no "official" solution for that. There is install peers cli package that works with npm and yarn. There is a hot feature request for yarn. There is a plan to add this feature to npm v7, actually npm did it before v3.

总结起来,没有一种万能的解决方案,您需要了解您想要得到什么以及可以为此付出什么.

Wrapping up, there is no one-size-fits-all solution and you need to understand what you want to get and what you can sacrifice for that.

更新(2020年12月14日)— NPM 7将安装对等部门

正如我在初始答案中提到的那样,npm v7默认实现了安装对等项依赖关系.请参阅 RFC 进一步的细节.

As I mentioned in the initial answer npm v7 implemented installing peer dependencies by default. Please refer to the RFC for further details.

这篇关于Monorepo中的对等依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆