Skip to content

Commit 817bb8d

Browse files
feat(expo): Add withSentryExpoSerializers for easy metro configuration (#3454)
1 parent 6f95f45 commit 817bb8d

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Add `withSentryExpoSerializers` for easy configurable `metro.config.js` ([#3454](https://github.com/getsentry/sentry-react-native/pull/3454))
8+
9+
```js
10+
const { getDefaultConfig } = require('expo/metro-config');
11+
const { withSentryExpoSerializers } = require("@sentry/react-native/metro");
12+
13+
const config = getDefaultConfig(__dirname);
14+
module.exports = withSentryExpoSerializers(config);
15+
```
16+
17+
Note that this will remove any existing `customSerializer`. Guide for advanced setups [can be found here](https://docs.sentry.io/platforms/react-native/manual-setup/metro).
18+
519
### Fixes
620

721
- Expo SDK minimum version is 49 ([#3453](https://github.com/getsentry/sentry-react-native/pull/3453))

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
},
7373
"devDependencies": {
7474
"@babel/core": "^7.23.5",
75+
"@expo/metro-config": "^0.10.7",
7576
"@sentry-internal/eslint-config-sdk": "7.81.1",
7677
"@sentry-internal/eslint-plugin-sdk": "7.81.1",
7778
"@sentry-internal/typescript": "7.80.0",

src/js/tools/sentryMetroSerializer.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as crypto from 'crypto';
2-
import type { MixedOutput, Module } from 'metro';
2+
import type { MetroConfig, MixedOutput, Module } from 'metro';
3+
import { mergeConfig } from 'metro';
34
import * as countLines from 'metro/src/lib/countLines';
45

56
import type { Bundle, MetroSerializer, MetroSerializerOutput, SerializedBundle, VirtualJSOutput } from './utils';
@@ -14,6 +15,37 @@ const PRELUDE_MODULE_PATH = '__prelude__';
1415
const SOURCE_MAP_COMMENT = '//# sourceMappingURL=';
1516
const DEBUG_ID_COMMENT = '//# debugId=';
1617

18+
/**
19+
* This function will overwrite any existing custom serializer with default Expo and Sentry serializers.
20+
*
21+
* To use custom serializers, use `createSentryMetroSerializer(customSerializer)` instead.
22+
*/
23+
export function withSentryExpoSerializers(config: MetroConfig): MetroConfig {
24+
const { withExpoSerializers } = loadExpoSerializersModule();
25+
26+
const sentryConfig = {
27+
serializer: {
28+
customSerializer: createSentryMetroSerializer(),
29+
},
30+
} as MetroConfig;
31+
32+
const finalConfig = mergeConfig(config, sentryConfig);
33+
return withExpoSerializers(finalConfig);
34+
}
35+
36+
function loadExpoSerializersModule(): {
37+
withExpoSerializers: (config: MetroConfig) => MetroConfig;
38+
} {
39+
try {
40+
// eslint-disable-next-line @typescript-eslint/no-var-requires
41+
return require('@expo/metro-config/build/serializer/withExpoSerializers');
42+
} catch (e) {
43+
throw new Error(
44+
'Unable to load `withExpoSerializers` from `@expo/metro-config`. Make sure you have Expo installed.',
45+
);
46+
}
47+
}
48+
1749
/**
1850
* Creates a Metro serializer that adds Debug ID module to the plain bundle.
1951
* The Debug ID module is a virtual module that provides a debug ID in runtime.

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@
24612461
json5 "^2.2.2"
24622462
write-file-atomic "^2.3.0"
24632463

2464-
"@expo/metro-config@~0.10.0":
2464+
"@expo/metro-config@^0.10.7", "@expo/metro-config@~0.10.0":
24652465
version "0.10.7"
24662466
resolved "https://registry.yarnpkg.com/@expo/metro-config/-/metro-config-0.10.7.tgz#d1b91baffcb7feb52fc7e2e122450bfc5d01e7c1"
24672467
integrity sha512-uACymEiyX0447hI4unt+2cemLQkTZXKvTev936NhtsgVnql45EP0V0pzmo/0H0WlHaAGXgvOBZJl8wFqcJ3CbQ==

0 commit comments

Comments
 (0)