gpt4 book ai didi

react-native - Yarn Workspaces 是提升只存在于一个工作空间中的模块

转载 作者:行者123 更新时间:2023-12-05 08:30:09 33 4
gpt4 key购买 nike

我正在尝试将 yarn workspaces + lerna 实现到我的项目中。我有一个简单的结构:

--package.json
--node_modules/
--@app/
|--client/
|--package.json
|--node_modules/
|--server/
|--package.json
|--node_modules/
|--db/
|--package.json
|--node_modules/

其中 client/是一个 React-Native 应用程序。我知道 react-native 中的陷阱,并修改了我的根 /package.json 如下:

  "workspaces": {
"packages": [
"@app/*"
],
"nohoist": [
"**/react-native",
"**/react-native/**",
]
},

据我了解,这应该可以防止 react native 本身以及 react-native 的任何依赖项被提升。但是,我遇到的是像 react-native-linear-gradientreact-native-reanimated 这样的 react-native 库(仅存在于 client/package.json) 被提升并安装在根级别 node_modules。它甚至不仅仅是被提升的原生模块。 Javascript 依赖项也被提升:

  • 原生基础
  • react 还原
  • @ Apollo
  • 样式组件

当然,nohoist 选项甚至不应该在这里发挥作用,因为这些模块只在 client/ 目录中使用。

那为什么要把它们吊到根呢?

为了更好地衡量,我删除了每个工作区中的 yarn.lock 文件,只保留根目录一级。在运行 lerna cleanrm -rf node_modules 之后,我在根级别运行了 yarn install

有趣的是,此安装非常长(3-5 分钟)。仅删除根级别 node_modules 甚至需要大约 20 秒。我完全不确定 yarn workspaces+lerna 是否正常。

其他详细信息

yarn 版本​​ - 1.22.10

最佳答案

what I am experiencing is that react-native libraries like react-native-linear-gradient and react-native-reanimated (which exist solely in client/package.json) are being hoisted and installed at the root level node_modules

这是预期的行为,因为它们不是 react-native 的依赖项。您必须指定相当多的模式,例如:

"nohoist": [
"**/react-native",
"**/react-native/**",
"**/react-native*",
"**/native-base",
"**/react-redux",
// OMG...
]

匹配所有...

我实际上会建议简单地添加这个:

"workspaces": { "nohoist": "**" } 

client/package.json 文件。

这将导致 client 包的 所有 依赖项安装在 client/node_modules 中——就好像它是一个独立的 React Native 项目一样在其父级没有工作区。

否则,在一般情况下添加新包(以及尤其是 native 模块包)时,您必须时刻保持警惕——您的nohoist 模式是否反射(reflect)了您拥有的所有库添加?

此外,自动链接和提升也存在问题。

换句话说,Yarn Workspaces 的重复数据删除功能被高估了。它仅略微减少了磁盘空间的使用,并且仅nohoist 一切并仅在真正有用的情况下使用 Workspaces 可能更简单——以便于本地包链接。

关于react-native - Yarn Workspaces 是提升只存在于一个工作空间中的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65984258/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com