当前位置:网站首页>加快 yarn install 的三个简单技巧
加快 yarn install 的三个简单技巧
2022-06-23 13:36:00 【码道人】

开发人员的生产力和生活质量的提高是我的热情所在。加速使用代码不仅很有趣,而且对于快速构建产品也是必不可少的。更快乐和更快的开发人员等于更快乐的客户,因为他们可以更快地获得功能和错误修复!
在处理基于 npm 的生态系统及其无数包时,依赖项的安装时间,尤其是在大型项目中,可能会过长。以下是三个简单的技巧,可以帮助你节省一两分钟,有时甚至可以减少yarn install一半的时间
臃肿的 yarn.lock 问题
如果你的项目在 yarn 上运行,那么你的某些依赖项很可能会被复制,即使它们满足 semver 条件。尽管yarn 承诺重复数据删除不是必需的,但这并不是事实。
想象一下这种情况:你正在将@awesome/tools库添加到依赖项中,这也依赖于[email protected]^1.0.0库,这是它的最新版本。安装@awesome/tools后,你将在yarn.lock文件中看到:
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"[email protected]^1.0.0":
version "1.0.0"几个月后,你想添加另一个依赖于这些实用程序的库,比如说@simple/button。Utils 库同时发布了一些错误修复和功能,它的最新版本是 1.5.1,并且@simple/button依赖于它。如果你只是运行yarn add @simple/button,那么你会在yarn.lock 中看到:
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/[email protected]":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"[email protected]^1.0.0":
version "1.0.0"
"[email protected]^1.5.1":
version "1.5.1"即使1.5.1和1.0.0版本是 semver 兼容的,yarn 也不会像你期望的那样将它们合并为一个,你最终会在 repo 中得到两个相同版本的 utils。
如果你有几个不同的库依赖于1.0.0,另外几个依赖于1.5.1,yarn 会将其中一个版本提升到文件夹的根node_modules目录,但另一个将无处可去(只有一个版本可以位于根目录) ,并且它们将作为副本安装在node_modules使用它们的库的文件夹中。你最终会得到这个文件夹结构:
/project
/node_modules
/ [email protected]
/@simple/button
/node_modules/ [email protected]
/@simple/form
/node_modules/ [email protected]
/@simple/select
/node_modules/ [email protected]虽然看起来你只有 2 个版本的utils库,但实际上,它可以是无限个存在于你的项目中的副本,所有这些都需要复制到它们的位置,所有这些都将偷走你的yarn install的时间。
解决方案
去重!要么手动确保所有与 semver 兼容的库都解析为一个版本,要么使用yarn-deduplicate等工具为你自动化。这就是你想要的yarn.lock:
"@awesome/[email protected]^1.2.3":
version "1.0.0"
dependencies:
"utils" "^1.0.0"
"@simple/[email protected]":
version "1.0.0"
dependencies:
"utils" "^1.5.1"
"[email protected]^1.0.0", "[email protected]^1.5.1":
version "1.5.1"打个比方,在一个项目中,我正在对所有依赖项进行彻底的重复数据删除,将yarn install时间从 3 分钟缩短到 1.5 分钟。
monorepo 和 workspaces 的阴暗面
如果你使用 monorepo 和 yarn 工作区(或Lerna,它使用下面的工作区)来管理你的包,你很容易发现自己处于本地包中的依赖项彼此不同步的情况。与上述情况类似,如果你的一个包依赖于[email protected]^1.0.0,其他包依赖于[email protected]^1.5.1,并且这些包没有在根目录中进行重复数据删除,yarn 将创建未提升到根目录的版本的多个副本,并将它们安装在你的工作区的node_modules文件夹中。在这种情况下,你不仅有可能最终在根 node_modules 中拥有相同依赖项的多个副本,而且还有分散在 repo 中的相同依赖项副本:
/project
/node_modules
/ [email protected]
/ packages
/one
/node_modules/ [email protected]
/two
/node_modules/ [email protected]在我正在处理的一个非常大的项目中,它有 600 多个工作区,如果我们允许只有 10% 的依赖项不同步,yarn install 会花费一个小时,而不是 5 分钟。
解决方案
如果迁移到 yarn2 或任何基于 pnpm 的东西不是一个好的选择(他们解决了这个特定问题),那么唯一的另一个选择是保持工作空间中的依赖项和根 package.json 严格同步。
Nuke node_modules
出于某种原因,当更新 yarn 中的依赖项后发生奇怪的事情时,每个人推荐的第一个解决方案是 nuke nodemodules 文件夹,并执行一次新的 yarn install。这通常也是最后一个建议,因为它神奇地修复了 90% 的怪异情况。一段时间后,你会养成一种习惯,每次检查主分支并开始开发新功能时都会执行 `rm -rf nodemodules && yarn install`。
要是能再提速一点就好了,至少提速50%……
解决方案
在你的根node_modules文件夹中有一个秘密文件.yarn-integrity,yarn 在其中写下了它需要知道的关于你的存储库及其安装的依赖项的所有信息。如果此文件的内容与 repo 中的情况不匹配,yarn 将更新它并刷新已安装的包。如果文件丢失,它将基于 yarn.lock 生成它,并更新node_modules 中的内容。所以如果大部分内容已经正确,那么它只是替换不正确的内容。
我们没有执行 rm -rf **node_modules** && yarn install,而是执行 rm -rf **node_modules/.yarn-integrity** && yarn install,它可以有效地减少一半用于 yarn install 的时间。
原文:https://adevnadia.medium.com/three-simple-tricks-to-speed-up-yarn-install-9cae57d159db
边栏推荐
- Drop down menu scenario of wechat applet
- Kali use
- Interrupt and polling
- Oracle进入sqlplus 报错
- Problems during MySQL uninstallation
- Technology sharing | do you understand the requirements of the tested project?
- Execute the sc.exe QC command to query some services. The data area passed to the system call is too small
- SQLserver2008r2安装dts组件不成功
- Short talk about community: how to build a game community?
- Test article
猜你喜欢

Vulnhub target os-hacknos-1

DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中

如何使用笔记软件 FlowUs、Notion 进行间隔重复?基于公式模版

MATLAB|时序数据中的稀疏辅助信号去噪和模式识别

微信小程序之在wx:for中绑定事件
![[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor

How do I turn on / off the timestamp when debugging the chrome console?

As a software testing practitioner, do you understand your development direction?

【深入理解TcaplusDB技术】TcaplusDB构造数据

Working for 7 years to develop my brother's career transition test: only by running hard can you get what you want~
随机推荐
2022 college entrance examination quarterly essay winners announced
系统设计与分析-技术报告-定时清理验证码的一种解决方案
Detailed explanation of serial port, com, UART, TTL, RS232 (485) differences
Vulnhub target os-hacknos-1
ICML 2022 | 上下文集成的基于transformer的拍卖设计神经网络
Sqlserver2008r2 failed to install DTS component
leetcode:242. 有效的字母异位词
微信小程序之input调整
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
Unity realizes the function of playing Ogg format video
Networknt:: JSON schema validator source code appreciation
leetcode:42. Rain water connection
渗透测试-提权专题
Soaring 2000+? The average salary of software testing in 2021 has come out, and I can't sit still
Tencent cloud tdsql-c heavy upgrade, leading the cloud native database market in terms of performance
2021-04-15
Kali use
Is flush a stock? Is it safe to open an account online now?
High quality coding - air quality map visualization
Oracle进入sqlplus 报错