开发生产模式

了解如何在开发模式或生产模式下运行项目。


你的项目将始终以 开发 模式或 生产 模式运行。默认情况下,在本地使用 npx expo start 运行项目会以开发模式运行,而发布的项目(使用 eas update)或任何独立应用将以生产模式运行。

🌐 Your project will always run in either development or production mode. By default, running your project locally with npx expo start runs it in development mode, whereas a published project (with eas update), or any standalone app, will run in production mode.

开发模式 包含有用的警告,并让你可以访问使开发和调试更容易的工具。生产模式 压缩你的代码,更好地体现你的应用在终端用户设备上的性能。让我们更详细地了解这两种模式,并学习如何在它们之间切换。

开发模式

🌐 Development mode

React Native 包含一些非常有用的开发工具:在 Chrome 中进行远程 JavaScript 调试、实时重载、热重载,以及类似于你在 Chrome 中使用的受欢迎的元素检查器。如果你想了解如何使用这些工具,请参见 调试

🌐 React Native includes some very useful tools for development: remote JavaScript debugging in Chrome, live reload, hot reloading, and an element inspector similar to the beloved inspector you use in Chrome. If you want to see how to use those tools, see Debugging.

开发模式还会在应用运行时进行验证,以向你提供警告。例如,如果你正在使用已弃用的属性,或者忘记向组件传递必需的属性。下面的视频展示了元素检查器和性能监视器在 Android 模拟器和 iOS 模拟器上的实际操作:

🌐 Development mode also performs validations while your app is running to give you warnings. For example, if you're using a deprecated property or if you forgot to pass a required property into a component. The video below shows the Element Inspector and Performance Monitor in action on both Android Emulator and iOS Simulator:

警告 这会有代价。你的应用在开发模式下运行会更慢。
你可以通过 Expo CLI 开启或关闭它,参见 生产模式。切换后,请关闭并重新打开你的应用以使更改生效。每次测试应用性能时,请确保关闭开发模式

查看开发者菜单

🌐 View the developer menu

该菜单提供了许多功能,使开发和调试更加容易。有关如何在 Android 和 iOS 上打开它的更多信息,请参见开发者菜单

🌐 The menu gives access to a host of features that make development and debugging much easier. For more information on how to open it on Android and iOS, see Developer menu.

生产模式

🌐 Production mode

生产模式对于两件事最有用:

🌐 Production mode is most useful for two things:

  • 测试应用的性能,因为开发会大大减慢应用的速度。
  • 捕获仅在生产中出现的错误。

模拟项目在终端用户设备上运行方式的最简单方法是使用以下命令:

🌐 The easiest way to simulate how your project will run on end users' devices is with the command:

Terminal
npx expo start --no-dev --minify

它在生产模式下运行你应用的 JavaScript(这会告诉 Metro 打包器将 __DEV__ 环境变量设置为 false,以及其他一些操作)。--minify 标志会压缩你的应用。此标志还会删除不必要的数据,如注释、格式信息和未使用的代码。如果你的独立应用出现错误或崩溃,使用此命令运行项目可以节省大量查找根本原因的时间。

🌐 It runs the JavaScript of your app in production mode (which tells the Metro bundler to set the __DEV__ environment variable to false, among a few other things). The --minify flag minifies your app. This flag also eliminates unnecessary data such as comments, formatting, and unused code. If you are getting an error or crash in your standalone app, running your project with this command can save you a lot of time in finding the root cause.

要将你的应用完全编译用于生产,请参阅 编译 Android编译 iOS

🌐 To completely compile your app for production see Compiling Android and Compiling iOS.