使用 Expo Atlas 和 Lighthouse 分析 JavaScript 包

了解如何使用 Expo Atlas 和 Lighthouse 改善 Expo 应用和网站的生产 JavaScript 包大小。


不同平台上的打包性能各不相同。例如,网页浏览器不支持预编译字节码,因此 JavaScript 打包包的大小对于提高启动时间和性能非常重要。打包包越小,下载和解析的速度就越快。

🌐 Bundle performance varies for different platforms. For example, web browsers don't support precompiled bytecode, so the JavaScript bundle size is important for improving startup time and performance. The smaller the bundle, the faster it can be downloaded and parsed.

使用 Expo Atlas 分析包大小

🌐 Analyzing bundle size with Expo Atlas

项目中使用的库会影响生产环境 JavaScript 包的大小。你可以使用 Expo Atlas 可视化生产包,并确定哪些库会增加包的大小。

🌐 The libraries used in a project influence the size of the production JavaScript bundle. You can use Expo Atlas to visualize the production bundle and identify which libraries contribute to the bundle size.

npx expo start 中使用 Atlas

🌐 Using Atlas with npx expo start

你可以在本地开发服务器上使用 Expo Atlas。这种方法允许 Atlas 在你更改项目中的任何代码时进行更新。

🌐 You can use Expo Atlas with the local development server. This method allows Atlas to update whenever you change any code in your project.

一旦你的应用在 Android、iOS 和/或 Web 上使用本地开发服务器运行,你可以通过开发工具插件菜单打开 Atlas,使用 Shift + M

Terminal
# Start the local development server with Atlas
EXPO_ATLAS=true npx expo start

将开发模式更改为生产模式

🌐 Changing development mode to production

默认情况下,Expo 会以开发模式启动本地开发服务器。开发模式会禁用在生产模式中启用的一些优化。你也可以以生产模式启动本地开发服务器,以更准确地了解生产包的大小:

🌐 By default, Expo starts the local development server in development mode. Development mode disables some optimizations that are enabled in production mode. You can also start the local development server in production mode to get a more accurate representation of the production bundle size:

Terminal
# Run the local development server in production mode
EXPO_ATLAS=true npx expo start --no-dev

npx expo export 上使用 Expo Atlas

🌐 Using Expo Atlas with npx expo export

在为应用生成生产包或 EAS 更新时,你也可以使用 Expo Atlas。Atlas 会在导出期间生成一个 .expo/atlas.jsonl 文件,你可以共享并打开它,即使没有项目访问权限。

🌐 You can also use Expo Atlas when generating a production bundle for your app or EAS Update. Atlas generates a .expo/atlas.jsonl file during export, which you can share and open without having access to the project.

Terminal
# Export your app for all platforms
EXPO_ATLAS=true npx expo export

# Open the generated Expo Atlas file
npx expo-atlas .expo/atlas.jsonl

你也可以使用 --platform 选项指定你想分析的平台。Expo Atlas 只会收集已导出平台的数据。

🌐 You can also specify the platforms you want to analyze using the --platform option. Expo Atlas will gather the data for the exported platforms only.

分析转换后的模块

🌐 Analyzing transformed modules

在 Atlas 中,你可以按住 ⌘ Cmd 并点击图表节点以查看已转换的模块详细信息。此功能可帮助你了解模块如何被 Babel 转换、它导入了哪些模块以及哪些模块导入了它。这可以用于追踪依赖图中模块的来源。

使用 source-map-explorer 分析包大小

🌐 Analyzing bundle size with source-map-explorer

适用于 SDK 50 及更早版本 的替代方法。

如果你使用的是 SDK 50 或以下版本,可以使用 source-map-explorer 库来可视化和分析生产环境的 JavaScript 包。

🌐 If you are using SDK 50 or below, you can use the source-map-explorer library to visualize and analyze the production JavaScript bundle.

1

要使用 source map explorer,请运行以下命令进行安装:

🌐 To use source map explorer, run the following command to install it:

Terminal
npm i --save-dev source-map-explorer

2

package.json 中添加一个脚本来运行它。根据你使用的平台或 SDK,可能需要调整输入路径。为了简洁,以下示例假设项目是 Expo SDK 50,并且不使用 Expo Router server 输出。

🌐 Add a script to package.json to run it. You might have to adjust the input path depending on the platform or SDK you are using. For brevity, the following example assumes the project is Expo SDK 50 and does not use Expo Router server output.

package.json
{ "scripts": { "analyze:web": "source-map-explorer 'dist/_expo/static/js/web/*.js' 'dist/_expo/static/js/web/*.js.map'", "analyze:ios": "source-map-explorer 'dist/_expo/static/js/ios/*.js' 'dist/_expo/static/js/ios/*.js.map'", "analyze:android": "source-map-explorer 'dist/_expo/static/js/android/*.js' 'dist/_expo/static/js/android/*.js.map'" } }

如果你在网页上使用 SDK 50 server 输出,则使用以下内容映射Webpack:

🌐 If you are using the SDK 50 server output for web, then use the following to map web bundles:

Terminal
npx source-map-explorer 'dist/client/_expo/static/js/web/*.js' 'dist/client/_expo/static/js/web/*.js.map'

Web 打包包输出到 dist/client 子目录,以防止将服务器代码暴露给客户端。

🌐 Web bundles are output to the dist/client subdirectory to prevent exposing server code to the client.

3

导出你的生产 JavaScript 包,并包含 --source-maps 标志,以便 source map explorer 能够读取源映射。对于使用 Hermes 的原生应用,你可以使用 --no-bytecode 选项来禁用字节码生成。

🌐 Export your production JavaScript bundle and include the --source-maps flag so that the source map explorer can read the source maps. For native apps using Hermes, you can use the --no-bytecode option to disable bytecode generation.

Terminal
npx expo export --source-maps --platform web

# Native apps using Hermes can disable bytecode for analyzing the JavaScript bundle.
npx expo export --source-maps --platform ios --no-bytecode

此命令显示输出中的 JavaScript 包和源映射路径。在下一步中,你将把这些路径传递给源映射浏览器。

🌐 This command shows the JavaScript bundle and source map paths in the output. In the next step, you will pass these paths to the source map explorer.

避免将源映射发布到生产环境,因为它们可能导致安全问题和性能问题(浏览器会下载这些大型映射文件)。

4

运行脚本以分析你的包:

🌐 Run the script to analyze your bundle:

Terminal
npm run analyze:web

运行此命令时,你可能会看到以下错误:

🌐 On running this command, you might see the following error:

You must provide the URL of lib/mappings.wasm by calling SourceMapConsumer.initialize({ 'lib/mappings.wasm': ... }) before using SourceMapConsumer

这可能是由于 Node.js 18 及以上版本中 source-map-explorer 的一个 已知问题 引起的。为了解决此问题,请在运行分析脚本之前设置环境变量 NODE_OPTIONS=--no-experimental-fetch

🌐 This is probably due to a known issue in source-map-explorer in Node.js 18 and above. To resolve this, set the environment variable NODE_OPTIONS=--no-experimental-fetch before running the analyze script.

你可能会遇到类似 Unable to map 809/13787 bytes (5.87%) 的警告。这是因为源映射通常会排除打包器运行时的定义(例如,__d(() => {}, []))。这个值是固定的,不必担心。

🌐 You might encounter a warning such as Unable to map 809/13787 bytes (5.87%). This occurs because source maps often exclude bundler runtime definitions (for example, __d(() => {}, [])). This value is consistent and not a reason for concern.

灯塔

🌐 Lighthouse

Lighthouse 是一个查看你的网站速度、可访问性和性能的好工具。你可以在 Chrome 的 审计 标签页中测试你的项目,或者使用 Lighthouse CLI

🌐 Lighthouse is a great way to see how fast, accessible, and performant your website is. You can test your project with the Audit tab in Chrome, or with the Lighthouse CLI.

在使用 npx expo export -p web 创建生产构建并进行部署(可以使用 npx serve dist、生产环境部署或自定义服务器)后,使用你网站的托管 URL 运行 Lighthouse。

🌐 After creating a production build with npx expo export -p web and serving it (using either npx serve dist, or production deployment, or custom server), run Lighthouse with the URL your site is hosted at.

Terminal
# Install the lighthouse CLI
npm install -g lighthouse

# Run the lighthouse CLI for your site
npx lighthouse <url> --view