|
| 1 | +import type { MetroConfig, MixedOutput, Module, ReadOnlyGraph } from 'metro'; |
| 2 | + |
| 3 | +import { createSentryMetroSerializer, unstable_beforeAssetSerializationPlugin } from './sentryMetroSerializer'; |
| 4 | +import type { DefaultConfigOptions } from './vendor/expo/expoconfig'; |
| 5 | + |
| 6 | +export * from './sentryMetroSerializer'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Adds Sentry to the Metro config. |
| 10 | + * |
| 11 | + * Adds Debug ID to the output bundle and source maps. |
| 12 | + * Collapses Sentry frames from the stack trace view in LogBox. |
| 13 | + */ |
| 14 | +export function withSentryConfig(config: MetroConfig): MetroConfig { |
| 15 | + let newConfig = config; |
| 16 | + |
| 17 | + newConfig = withSentryDebugId(newConfig); |
| 18 | + newConfig = withSentryFramesCollapsed(newConfig); |
| 19 | + |
| 20 | + return newConfig; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * This function returns Default Expo configuration with Sentry plugins. |
| 25 | + */ |
| 26 | +export function getSentryExpoConfig(projectRoot: string, options: DefaultConfigOptions = {}): MetroConfig { |
| 27 | + const { getDefaultConfig } = loadExpoMetroConfigModule(); |
| 28 | + const config = getDefaultConfig(projectRoot, { |
| 29 | + ...options, |
| 30 | + unstable_beforeAssetSerializationPlugins: [ |
| 31 | + ...(options.unstable_beforeAssetSerializationPlugins || []), |
| 32 | + unstable_beforeAssetSerializationPlugin, |
| 33 | + ], |
| 34 | + }); |
| 35 | + |
| 36 | + return withSentryFramesCollapsed(config); |
| 37 | +} |
| 38 | + |
| 39 | +function loadExpoMetroConfigModule(): { |
| 40 | + getDefaultConfig: ( |
| 41 | + projectRoot: string, |
| 42 | + options: { |
| 43 | + unstable_beforeAssetSerializationPlugins?: ((serializationInput: { |
| 44 | + graph: ReadOnlyGraph<MixedOutput>; |
| 45 | + premodules: Module[]; |
| 46 | + debugId?: string; |
| 47 | + }) => Module[])[]; |
| 48 | + }, |
| 49 | + ) => MetroConfig; |
| 50 | +} { |
| 51 | + try { |
| 52 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 53 | + return require('expo/metro-config'); |
| 54 | + } catch (e) { |
| 55 | + throw new Error('Unable to load `expo/metro-config`. Make sure you have Expo installed.'); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +type MetroCustomSerializer = Required<Required<MetroConfig>['serializer']>['customSerializer'] | undefined; |
| 60 | + |
| 61 | +function withSentryDebugId(config: MetroConfig): MetroConfig { |
| 62 | + const customSerializer = createSentryMetroSerializer( |
| 63 | + config.serializer?.customSerializer || undefined, |
| 64 | + ) as MetroCustomSerializer; |
| 65 | + // MetroConfig types customSerializers as async only, but sync returns are also supported |
| 66 | + // The default serializer is sync |
| 67 | + |
| 68 | + return { |
| 69 | + ...config, |
| 70 | + serializer: { |
| 71 | + ...config.serializer, |
| 72 | + customSerializer, |
| 73 | + }, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +type MetroFrame = Parameters<Required<Required<MetroConfig>['symbolicator']>['customizeFrame']>[0]; |
| 78 | +type MetroCustomizeFrame = { readonly collapse?: boolean }; |
| 79 | +type MetroCustomizeFrameReturnValue = |
| 80 | + | ReturnType<Required<Required<MetroConfig>['symbolicator']>['customizeFrame']> |
| 81 | + | undefined; |
| 82 | + |
| 83 | +/** |
| 84 | + * Collapses Sentry internal frames from the stack trace view in LogBox. |
| 85 | + */ |
| 86 | +export function withSentryFramesCollapsed(config: MetroConfig): MetroConfig { |
| 87 | + const originalCustomizeFrame = config.symbolicator?.customizeFrame; |
| 88 | + const collapseSentryInternalFrames = (frame: MetroFrame): boolean => |
| 89 | + typeof frame.file === 'string' && |
| 90 | + (frame.file.includes('node_modules/@sentry/utils/cjs/instrument.js') || |
| 91 | + frame.file.includes('node_modules/@sentry/utils/cjs/logger.js')); |
| 92 | + |
| 93 | + const customizeFrame = (frame: MetroFrame): MetroCustomizeFrameReturnValue => { |
| 94 | + const originalOrSentryCustomizeFrame = ( |
| 95 | + originalCustomization: MetroCustomizeFrame | undefined, |
| 96 | + ): MetroCustomizeFrame => ({ |
| 97 | + ...originalCustomization, |
| 98 | + collapse: (originalCustomization && originalCustomization.collapse) || collapseSentryInternalFrames(frame), |
| 99 | + }); |
| 100 | + |
| 101 | + const maybePromiseCustomization = (originalCustomizeFrame && originalCustomizeFrame(frame)) || undefined; |
| 102 | + |
| 103 | + if (maybePromiseCustomization !== undefined && 'then' in maybePromiseCustomization) { |
| 104 | + return maybePromiseCustomization.then<MetroCustomizeFrame>(originalCustomization => |
| 105 | + originalOrSentryCustomizeFrame(originalCustomization), |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + return originalOrSentryCustomizeFrame(maybePromiseCustomization); |
| 110 | + }; |
| 111 | + |
| 112 | + return { |
| 113 | + ...config, |
| 114 | + symbolicator: { |
| 115 | + ...config.symbolicator, |
| 116 | + customizeFrame, |
| 117 | + }, |
| 118 | + }; |
| 119 | +} |
0 commit comments