Skip to main content

软件包修补

概述

¥Overview

有时你需要对依赖进行小的更改,只是为了解决一些小问题。建议的操作是进行 PR 上游,但你的更改可能需要一些时间才能通过审核并最终发布;与此同时该做什么?你有两个选项:

¥It sometimes happen that you need to make small changes to a dependency, just to workaround some small issue. The recommended action is to make a PR upstream, but it may take time until your changes get through review and end up in a release; what to do in the meantime? You have two options:

  • 你可以使用 git: 协议,只要正确设置,它就可以让你直接从其开发存储库安装项目。

    ¥You can use the git: protocol, which will let you install a project straight from its development repository, provided it was correctly setup.

  • 或者你可以使用 patch: 协议直接从你的项目中对依赖进行小的更改,同时将它们与原始代码分开。

    ¥Or you can use the patch: protocol to make small changes to the dependencies straight from your project, while keeping them separated from the original code.

不再需要等待合并和发布拉取请求,不再需要分叉存储库来修复阻止你的应用运行的一个小问题:内置补丁机制将始终让你自行解锁。

¥No more waiting around for pull requests to be merged and published, no more forking repos just to fix that one tiny thing preventing your app from working: the builtin patch mechanism will always let you unblock yourself.

制作补丁

¥Making patches

要创建补丁,请运行 yarn patch 命令并向其传递包名称,以使 Yarn 在临时文件夹中提取请求的包。然后你可以随意编辑补丁中的文件。

¥To create a patch, run the yarn patch command and pass it a package name to make Yarn extract the requested package in a temporary folder. You're then free to edit the files within the patch at your convenience.

完成更改后,使用临时文件夹作为参数运行 yarn patch-commit -s:补丁将在 .yarn/patches 中生成,并应用于你的项目。将其添加到 Git,你就可以开始了。

¥Once you're done with your changes, run yarn patch-commit -s with the temporary folder as parameter: the patch will be generated in .yarn/patches, and applied to your project. Add it to Git, and you're set to go.

维护补丁

¥Maintaining patches

默认情况下,yarn patch 将始终重置补丁。如果你希望添加新的更改,请使用 --update 标志并按照与之前相同的步骤操作 - 你的补丁将被重新生成。

¥By default, yarn patch will always reset the patch. If you wish to add new changes, use the --update flag and follow the same procedure as before - your patch will be regenerated.

限制

¥Limitations

  • 由于它们当前是在获取时而不是解析时计算的,因此包依赖已经解析,补丁将无法更改它们。相反,使用专门用于向包添加新运行时依赖的 packageExtensions 机制。

    ¥Because they're currently computed at fetch time rather than resolution time, the package dependencies have already been resolved and patches won't be able to alter them. Instead, use the packageExtensions mechanism which is specifically made to add new runtime dependencies to packages.

  • 补丁不适合修改二进制文件。最小化文件也有问题,尽管我们可以改进该功能以通过类似 Prettier 的工具自动处理此类文件。

    ¥Patches are ill-suited for modifying binary files. Minified files are problematic as well, although we could improve the feature to automatically process such files through a Prettier-like tool.