使用构建缓存提供程序
编辑页面
通过从提供程序缓存和复用构建来加速本地开发。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
构建缓存是一项功能,它通过基于项目 fingerprint 将构建远程缓存,从而加快 npx expo run:[android|ios] 的速度。
当你运行 npx expo run:[android|ios] 时,它会检查是否存在具有匹配 fingerprint 的构建,然后下载并启动它,而不是重新编译。否则,项目会照常编译,然后将生成的二进制文件上传到远程缓存,供以后运行时使用。
使用 EAS 作为构建提供程序
要使用 EAS Build provider 插件,请先将 eas-build-cache-provider 包作为开发依赖安装:
Terminal
- npx expo install eas-build-cache-provider --devTerminal
- npx expo install eas-build-cache-provider "--" --dev然后,更新你的 app.json,添加 buildCacheProvider 属性及其提供程序:
app.json
{ "expo": { "buildCacheProvider": "eas" %%placeholder-start%%... %%placeholder-end%% } }
你可以通过导出一个实现以下方法的插件来自定义自己的缓存提供程序:
type BuildCacheProviderPlugin<T = any> = { /** * 尝试获取一个已存在的构建。若缺失则返回其 URL 或 null。 */ resolveBuildCache(props: ResolveBuildCacheProps, options: T): Promise<string | null>; /** * 上传一个新的构建二进制文件。失败时返回其 URL 或 null。 */ uploadBuildCache(props: UploadBuildCacheProps, options: T): Promise<string | null>; /** * (可选)自定义 fingerprint 哈希算法。 */ calculateFingerprintHash?: ( props: CalculateFingerprintHashProps, options: T ) => Promise<string | null>; }; type ResolveBuildCacheProps = { projectRoot: string; platform: 'android' | 'ios'; runOptions: RunOptions; fingerprintHash: string; }; type UploadBuildCacheProps = { projectRoot: string; buildPath: string; runOptions: RunOptions; fingerprintHash: string; platform: 'android' | 'ios'; }; type CalculateFingerprintHashProps = { projectRoot: string; platform: 'android' | 'ios'; runOptions: RunOptions; };
一个使用 GitHub Releases 缓存构建的参考实现可以在 Build Cache Provider Example 中找到。
创建自定义构建提供程序
首先创建一个 provider 目录,用于以 TypeScript 编写提供程序插件,并在项目根目录添加一个 provider.plugin.js 文件,它将作为插件入口点。
1
2
创建一个 provider/src/index.ts 文件用于你的插件
provider/src/index.ts
import { type BuildCacheProviderPlugin } from '@expo/config'; const plugin: BuildCacheProviderPlugin = { resolveBuildCache: async () => { console.log('正在搜索远程构建...'); return null; }, uploadBuildCache: async () => { console.log('正在上传构建到远程...'); return null; }, }; export default plugin;
3
5
6
传递自定义选项
要向你的插件注入自定义选项,你可以使用 options 字段,它会作为你自定义函数的第二个参数传递进去。为此,请按如下所示修改 example/app.json 中的 buildCacheProvider 字段:
example/app.json
{ "expo": { %%placeholder-start%%... %%placeholder-end%% "buildCacheProvider": { "plugin": "./provider.plugin.js", "options": { "myCustomKey": "XXX-XXX-XXX" } } } }