TL;DR 如何为本地 yarn 工作区依赖项创建别名?
我之前尝试过 yarn workspaces 但从未成功,我正在再次尝试。
我在 package.json
中设置了 "workspaces": ["packages/*"]
。
对于每个包,我决定使用命名约定 @-/package-name
来防止命名冲突,并且不用担心内部包的命名空间。
当添加包作为依赖项时,我一直遵循一种使用接口(interface)名称进行解析的风格,但将其指向具体的实现。这是我在使用 yarn 工作区之前所做的:
"dependencies": {
"my-interface-name": "file:some/path/to/packages/some-concrete-implementation"
}
这基本上是为了允许我称之为编译时静态依赖注入(inject)。这也意味着每个包都可以根据需要单独命名它们的接口(interface)依赖项并防止命名冲突。
但是,我无法弄清楚如何使用 yarn 工作区来完成此操作。如何为名为 my-interface-name
的 yarn 工作区包 @-/some-concrete-implementation
创建别名?
我已经尝试过但没有成功:
"my-interface-name": "@-/some-concrete-implementation"}
这样的依赖 - 出于某种原因,这会导致 yarn 寻找 @-/some -具体实现
在 npm 注册表上而不是在本地工作区"my-interface-name": "workspace:@-/some-concrete-implementation"}
但它仍然在npm 注册表!我还没有尝试过但可以工作但首先消除了使用 yarn 工作空间的好处:
"dependencies": {"my-interface-name": "file:../../node_modules/@-/some-concrete-implementation"}"
最佳答案
你见过resolutions
package.json key吗? ?是您需要的吗?
我用它来别名/覆盖外部包,但文档中的示例显示它可以与本地包一起使用。
Resolutions
Allows you to override a version of a particular nested dependency. See the Selective Versions Resolutions RFC for the full spec.
{
"resolutions": {
"transitive-package-1": "0.0.29",
"transitive-package-2": "file:./local-forks/transitive-package-2",
"dependencies-package-1/transitive-package-3": "^2.1.1"
}
}
来自 RFC:
"**/a" denotes all the nested dependencies a of the project.
"a" is an alias for **/a (for retro-compatibility, see below, and because if it wasn't such an alias, it wouldn't mean anything as it would represent one of the non-nested project dependencies, which can't be overridden as explained below).
所以,我相信你需要的规则是:
"**/my-interface-name": "file:some/path/to/packages/some-concrete-implementation"
// OR equivalent
"my-interface-name": "file:some/path/to/packages/some-concrete-implementation"
我相信它在包的 package.json 中有效。最坏的情况下,您可以将其提升到工作区根目录并制定特定于工作区的规则,例如“a/b”。
https://stackoverflow.com/questions/61640230/
相关文章:
delphi - 如何在 Delphi 10.3 Rio 上安装 Indy?
c# - 如何使用 Sustainsys.Saml2 设置 NLog
node.js - 使用 nodeJs 从 AWS SQS 队列中获取待处理的消息数
python - 使用 zip() 对元组列表进行排序时,有时不支持“<”
python - 如何聚焦光线或如何在 pygame 中只绘制窗口的某些圆形部分?
javascript - AWS Elastic Beanstalk : How to change
c# - 为什么 Visual Studio 不警告我空引用异常?
arrays - 将 [MLMultiArray] 转换为 Float?