Metro
了解 Metro 打包器以及如何使用 Sentry React Native SDK 为你的应用程序配置它。
Sentry 的 React Native SDK 包含一个 Sentry Metro Serializer,它允许你为应用程序的 bundles 自动生成 Debug ID。这对于使源映射与 Sentry 正常工作至关重要。 本页面将指导你完成为应用程序设置 Metro 插件的过程。
- 注册一个账户
- 安装 Sentry React Native SDK 版本 5.17.0 或更新版本
- Expo 从 SDK 版本 5.16.0-alpha.1 开始支持
Sentry React Native SDK 提供了多种方式来配置 Sentry Metro Serializer,具体取决于你在 Metro 配置中是否使用了 customSerializer
。
以下示例展示了如果你没有使用任何 customSerializers
(默认配置)时如何使用 Sentry Metro Serializer。
metro.config.js
Copied
const { getDefaultConfig } = require("@react-native/metro-config");
const { withSentryConfig } = require("@sentry/react-native/metro");
const config = getDefaultConfig(__dirname);
module.exports = withSentryConfig(config);
如果你已经有一个自定义序列化器,你可以用 Sentry Metro Serializer 包裹它,并在序列化 bundle 内容之前调用 options.sentryBundleCallback
。
metro.config.js
Copied
const {
getDefaultConfig,
mergeConfig,
} = require("@react-native/metro-config");
const { withSentryConfig } = require("@sentry/react-native/metro");
const myCustomSerializer = (entryPoint, preModules, graph, options) => {
let bundle = prepareBundle(entryPoint, preModules, graph, options);
if (options.sentryBundleCallback) {
// Callback adds Sentry Debug IDs module to the bundle
bundle = options.sentryBundleCallback(bundle);
}
const code = createCode(bundle);
const map = createSourceMap();
return { code, map };
};
const config = {
serializer: {
customSerializer: myCustomSerializer,
},
};
module.exports = withSentryConfig(
mergeConfig(getDefaultConfig(__dirname), config),
);
预期的 bundle 中间结构:
Copied
export type Bundle = {
modules: Array<[id: number, code: string]>;
post: string;
pre: string;
};
- Sentry Metro Serializer 无法为 Hermes 组合源映射添加 Debug ID。请参阅 使用 Hermes 手动上传 指南,了解如何为 Hermes 组合源映射添加 Debug ID。
- 如果你看到
Debug ID was not found in the bundle.
错误消息,说明你的自定义序列化器没有调用sentryBundleCallback
。 - 如果你的 Expo 项目中缺少 Metro 配置,请使用
npx expo customize metro.config.js
创建它。了解更多关于 自定义 Expo Metro 配置 的信息。