首页指南参考教程

构建生命周期钩子

了解如何将 EAS Build 生命周期钩子与 npm 结合使用来自定义构建过程。


EAS Build 生命周期 npm hooks 允许你通过在构建过程之前或之后运行脚本来自定义构建过程。

¥EAS Build lifecycle npm hooks allows you to customize your build process by running scripts before or after the build process.

为了更好地理解,请参阅 安卓构建流程iOS 构建过程

¥For better understanding, see the Android build process and the iOS build process.

EAS Build 生命周期钩子

¥EAS Build lifecycle hooks

有六个 EAS Build 生命周期 npm 钩子可用。要使用它们,你可以在 package.json 中设置它们。

¥There are six EAS Build lifecycle npm hooks available. To use, them, you can set them in your package.json.

构建生命周期 npm 钩子描述
eas-build-pre-install在 EAS Build 运行 npm install 之前执行。
eas-build-post-install行为取决于平台和项目类型。

对于 Android,在以下命令全部完成后运行一次:npm installnpx expo prebuild(如果需要)。

对于 iOS,在以下命令全部完成后运行一次:npm installnpx expo prebuild(如果需要)和 pod install
eas-build-on-success如果构建成功,则在构建过程结束时触发此钩子。
eas-build-on-error如果构建失败,则在构建过程结束时触发此钩子。
eas-build-on-complete此钩子在构建过程结束时触发。你可以使用 EAS_BUILD_STATUS 环境变量检查构建的状态。要么是 finished,要么是 errored
eas-build-on-cancel如果取消构建,则会触发此钩子。

使用一个或多个生命周期钩子时 package.json 的外观示例:

¥An example of how a package.json can look when using one or more lifecycle hooks:

package.json
{
  "name": "my-app",
  "scripts": {
    "eas-build-pre-install": "echo 123",
    "eas-build-post-install": "echo 456",
    "eas-build-on-success": "echo 789",
    "eas-build-on-error": "echo 012",
    "eas-build-on-cancel": "echo 345",
    "start": "expo start",
    "test": "jest"
  },
  "dependencies": {
    "expo": "51.0.0"
    %%placeholder-start%%... %%placeholder-end%%
  }
}

特定于平台的钩子行为

¥Platform-specific hook behavior

要仅为 Android 或 iOS 版本运行脚本(或脚本的某些部分),你可以根据脚本中的平台分叉行为。请参阅以下常见示例,通过 shell 脚本或 Node 脚本执行此操作。

¥To run a script (or some part of a script) only for Android or iOS builds, you can fork the behavior depending on the platform within the script. See the following common examples to do this through a shell script or a Node script.

示例

¥Examples

package.json 和 shell 脚本

¥package.json and shell script

package.json
{
  "name": "my-app",
  "scripts": {
    "eas-build-pre-install": "./pre-install",
    "start": "expo start"
    %%placeholder-start%%... %%placeholder-end%%
  },
  "dependencies": {
    %%placeholder-start%%... %%placeholder-end%%
  }
}
pre-install
#!/bin/bash

# This is a file called "pre-install" in the root of the project

if [[ "$EAS_BUILD_PLATFORM" == "android" ]]; then
  echo "Run commands for Android builds here"
elif [[ "$EAS_BUILD_PLATFORM" == "ios" ]]; then
  echo "Run commands for iOS builds here"
fi
Example: Pre-install script that installs git-lfs on macOS workers

如果尚未安装 git-lfs,以下脚本将安装它。这在需要 git-lfs 来安装某些 CocoaPods 的某些情况下很有用。

¥The following script installs git-lfs if it is not yet installed. This is useful in some cases where git-lfs is required to install certain CocoaPods.

pre-install
if [[ "$EAS_BUILD_PLATFORM" == "ios" ]]; then
  if brew list git-lfs > /dev/null 2>&1; then
    echo "=====> git-lfs is already installed."
  else
    echo "=====> Installing git-lfs"
    HOMEBREW_NO_AUTO_UPDATE=1 brew install git-lfs
    git lfs install
  fi
fi

package.json 和 Node 脚本

¥package.json and Node script

package.json
{
  "name": "my-app",
  "scripts": {
    "eas-build-pre-install": "node pre-install.js",
    "start": "expo start"
    // ...
  },
  "dependencies": {
    // ...
  }
}
pre-install.js
// Create a file called "pre-install.js" at the root of the project

if (process.env.EAS_BUILD_PLATFORM === 'android') {
  console.log('Run commands for Android builds here');
} else if (process.env.EAS_BUILD_PLATFORM === 'ios') {
  console.log('Run commands for iOS builds here');
}
Expo 中文网 - 粤ICP备13048890号