使用 Yarn 1 (Classic) 的 npm 缓存

了解如何通过覆盖 Yarn 1 (Classic) 中的注册表来使用 npm 缓存。


默认情况下,EAS npm 缓存无法与 Yarn 1(经典版)配合使用,因为 yarn.lock 文件包含每个库的注册表 URL。Yarn 1 不提供任何方式来覆盖这一点,并且 Yarn 团队也不打算在 Yarn 1 中支持它。不过,这个问题在 Yarn 2 及以上版本中已经修复。

🌐 By default, the EAS npm cache won't work with Yarn 1 (Classic) because yarn.lock files contain URLs to registries for every library. Yarn 1 does not provide any way to override it and Yarn team does not plan to support it in Yarn 1. However, this issue is fixed in Yarn 2+.

如果你想在使用 Yarn 1 时利用 npm 缓存,可以在 package.json 中添加 eas-build-pre-install npm 钩子 来覆盖 yarn.lock 中的注册表:

🌐 If you want to take advantage of the npm cache with Yarn 1, add the eas-build-pre-install npm hook in package.json to override the registry in the yarn.lock:

package.json
{ "scripts": { "eas-build-pre-install": "bash -c \"[ ! -z \\\"$EAS_BUILD_NPM_CACHE_URL\\\" ] && sed -i -e \\\"s#https://registry.yarnpkg.com#$EAS_BUILD_NPM_CACHE_URL#g\\\" yarn.lock\" || true" } }