Skip to main content

加速 CI 构建

你或你的公司可能已经设置了持续集成 (CI) 环境来测试你的 React Native 应用。

¥You or your company may have set up a Continuous Integration (CI) environment to test your React Native application.

快速的 CI 服务很重要,原因有两个:

¥A fast CI service is important for 2 reasons:

  • CI 机器运行的时间越长,成本就越高。

    ¥The more time CI machines are running, the more they cost.

  • CI 作业运行的时间越长,开发周期就越长。

    ¥The longer the CI jobs take to run, the longer the development loop.

因此,尝试尽量减少 CI 环境构建 React Native 的时间非常重要。

¥It is therefore important to try and minimize the time the CI environment spends building React Native.

禁用 iOS 版 Flipper

¥Disable Flipper for iOS

Flipper 是 React Native 默认附带的调试工具,可帮助开发者调试和分析其 React Native 应用。然而,CI 中不需要 Flipper:你或你的一位同事不太可能需要调试在 CI 环境中构建的应用。

¥Flipper is a debugging tool shipped by default with React Native, to help developers debug and profile their React Native applications. However, Flipper is not required in CI: it is very unlikely that you or one of your collegue would have to debug the app built in the CI environment.

对于 iOS 应用,每次构建 React Native 框架时都会构建 Flipper,并且可能需要一些时间来构建,而这是你可以节省的时间。

¥For iOS apps, Flipper is built every time the React Native framework is built and it may require some time to build, and this is time you can save.

从 React Native 0.71 开始,我们在模板的 Podfile 中引入了一个新标志:NO_FLIPPER

¥Starting from React Native 0.71, we introduced a new flag in the template's Podfile: the NO_FLIPPER flag.

默认情况下,未设置 NO_FLIPPER 标志,因此 Flipper 将默认包含在你的应用中。

¥By default, the NO_FLIPPER flag is not set, therefore Flipper will be included by default in your app.

你可以在安装 iOS pod 时指定 NO_FLIPPER=1,以指示 React Native 不安装 Flipper。通常,该命令如下所示:

¥You can specify NO_FLIPPER=1 when installing your iOS pods, to instruct React Native not to install Flipper. Typically, the command would look like this:

# from the root folder of the react native project
NO_FLIPPER=1 bundle exec pod install --project-directory=ios

在 CI 环境中添加此命令可以跳过 Flipper 依赖的安装,从而节省时间和金钱。

¥Add this command in your CI environment to skip the installation of Flipper dependencies and thus saving time and money.

处理传递依赖

¥Handle Transitive Dependencies

你的应用可能正在使用一些依赖于 Flipper pod 的库。如果是这种情况,使用 NO_FLIPPER 标志禁用 Flipper 可能还不够:在这种情况下,你的应用可能无法构建。

¥Your app may be using some libraries which depends on the Flipper pods. If that's your case, disabling flipper with the NO_FLIPPER flag may not be enough: your app may fail to build in this case.

处理这种情况的正确方法是为 React Native 添加自定义配置,指示应用正确安装传递依赖。为了实现这一目标:

¥The proper way to handle this case is to add a custom configuration for react native, instructing the app to properly install the transitive dependency. To achieve that:

  1. 如果你还没有该文件,请创建一个名为 react-native.config.js.txt 的新文件。

    ¥If you don't have it already, create a new file called react-native.config.js.

  2. 当标志打开时,从 dependency 中显式排除传递依赖。

    ¥Exclude explicitly the transitive dependency from the dependency when the flag is turned on.

例如,react-native-flipper 库是依赖于 Flipper 的附加库。如果你的应用使用它,你需要将其从依赖中排除。你的 react-native.config.js 看起来像这样:

¥For example, the react-native-flipper library is an additional library that depends on Flipper. If your app uses that, you need to exclude it from the dependencies. Your react-native.config.js would look like this:

react-native.config.js
module.exports = {
// other fields
dependency: {
...(process.env.NO_FLIPPER
? {'react-native-flipper': {platforms: {ios: null}}}
: {}),
},
};