Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rn): Add React Native Metro Bundler page #8662

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions src/platforms/react-native/manual-setup/metro.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
title: Metro
sidebar_order: 3
redirect_from:
- /platforms/react-native/advanced-setup/
description: "Learn about the Metro bundler and how to configure it for your your application with Sentry React Native SDK."
---

Sentry React Native SDK package ships with a Sentry Metro Serializer which allows you to automatically generated Debug IDs for your applications bundles. This is crucial for source maps to work properly with Sentry.
This page will guide you through the process of setting up the Metro Plugin for your application.
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

## Prerequisities

- [Sign up for an account](https://sentry.io/signup/)
- [Install Sentry React Native SDK](/platforms/react-native) version 5.11.0 or newer
- Expo is supported from the SDK version 5.16.0-alpha.1
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

## Configuration

The Sentry React Native SDK allows multiple ways to configure the Sentry Metro Serializer depending on your current use of `customeSerializer` in your Metro configuration.
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

### Use Sentry Metro Serializer

The example below shows how to use the Sentry Metro Serializer if you don't have any `customSerializers` (the default configuration).

```javascript {tabTitle:React Native}
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
const {
createSentryMetroSerializer,
} = require("@sentry/react-native/dist/js/tools/sentryMetroSerializer");

const config = {
serializer: {
customSerializer: createSentryMetroSerializer(),
},
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);
```

```javascript {tabTitle:Expo}
const { mergeConfig } = require("metro");
const { getDefaultConfig } = require("expo/metro-config");
const { createSentryMetroSerializer } = require("@sentry/react-native/metro");
const {
withExpoSerializers,
} = require("@expo/metro-config/build/serializer/withExpoSerializers");

const config = getDefaultConfig(__dirname);

const sentryConfig = {
serializer: {
customSerializer: createSentryMetroSerializer(),
},
};

const finalConfig = mergeConfig(config, sentryConfig);
module.exports = withExpoSerializers(finalConfig);
```

### Wrap your custom serializer
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

If you already have a custom serializer, you can wrap it with the Sentry Metro Serializer and call `options.sentryBundleCallback` before serializing the bundle content.

```javascript {tabTitle:React Native}
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
const {
createSentryMetroSerializer,
} = require("@sentry/react-native/dist/js/tools/sentryMetroSerializer");

const myCustomSerializer = (entryPoint, preModules, graph, options) => {
let bundle = perapreBundle(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: createSentryMetroSerializer(myCustomSerializer),
},
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);
```

Expected bundle intermediate structure:

```typescript
export type Bundle = {
modules: Array<[id: number, code: string]>;
post: string;
pre: string;
};
```

## Throubleshooting
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

- Sentry Metro Serializer can't add Debug ID to the Hermes Composed Source Maps. Please see [Manual Upload With Hermes](/platforms/react-native/sourcemaps#manual-upload-with-hermes) guide on how to add Debug ID to the Hermes Composed Source Maps.
- If you see `Debug ID was not found in the bundle.` error message the `sentryBundleCallback` was not called by your custom serializer.