了解如何在开发模式或生产模式下运行项目。
你的项目将始终在开发或生产模式下运行。默认情况下,使用 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 includes useful warnings and gives you access to tools that make development and debugging easier. Production mode minifies your code and better represents the performance your app will have on end users' devices. Let's look at each of these modes in more detail and learn how you can switch between them.
¥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.
开发模式还会在你的应用运行时执行验证,以便在你使用已弃用的属性或忘记将必需的属性传递到组件时向你发出警告。下面的视频展示了 Element Inspector 和 Performance Monitor 在 Android 和 iOS 模拟器上的运行情况:
¥Development mode also performs validations while your app is running to give you warnings if, for example, 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 and iOS simulators:
这是有代价的。你的应用在开发模式下运行速度较慢。 你可以使用 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:
测试应用的性能,因为开发会大大减慢应用的速度。
¥Testing your app's performance, as Development slows your app down considerably.
捕获仅在生产中出现的错误。
¥Catching bugs that only show up in production.
模拟项目在终端用户设备上运行方式的最简单方法是使用以下命令:
¥The easiest way to simulate how your project will run on end users' devices is with the command:
-
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.