Skip to content

Commit d1fd647

Browse files
authored
fix(metro): Handle missing shouldAddToIgnoreList callback in Metro (#5260)
* fix(metro): Handle missing shouldAddToIgnoreList callback in Metro serializer * Adds changelog
1 parent 161947d commit d1fd647

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- Fix compatibility with `react-native-legal` ([#5253](https://github.com/getsentry/sentry-react-native/pull/5253))
1818
- The licenses json file is correctly generated and placed into the `res/` folder now
19+
- Handle missing shouldAddToIgnoreList callback in Metro ([#5260](https://github.com/getsentry/sentry-react-native/pull/5260))
1920

2021
### Dependencies
2122

packages/core/src/js/tools/vendor/metro/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Please check the version of Metro you are using and report the issue at http://w
140140
// Always generate source maps, can't use Sentry without source maps
141141
const map = sourceMapStringFunction([...preModules, ...getSortedModules(graph, options)], {
142142
processModuleFilter: options.processModuleFilter,
143-
shouldAddToIgnoreList: options.shouldAddToIgnoreList,
143+
shouldAddToIgnoreList: options.shouldAddToIgnoreList || (() => false),
144144
});
145145
return { code, map };
146146
};

packages/core/test/tools/sentryMetroSerializer.test.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,48 @@ describe('Sentry Metro Serializer', () => {
5050
expect(bundle.code).toEqual(fs.readFileSync(`${__dirname}/fixtures/bundleWithPrelude.js.fixture`, 'utf8'));
5151
expect(bundle.map).toEqual(fs.readFileSync(`${__dirname}/fixtures/bundleWithPrelude.js.fixture.map`, 'utf8'));
5252
});
53+
54+
test('works when shouldAddToIgnoreList is undefined', async () => {
55+
const serializer = createSentryMetroSerializer();
56+
const args = mockMinSerializerArgs({ shouldAddToIgnoreList: undefined });
57+
58+
const bundle = await serializer(...args);
59+
60+
expect(bundle).toBeDefined();
61+
if (typeof bundle !== 'string') {
62+
expect(bundle.code).toBeDefined();
63+
expect(bundle.map).toBeDefined();
64+
const debugId = determineDebugIdFromBundleSource(bundle.code);
65+
expect(debugId).toMatch(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/);
66+
}
67+
});
5368
});
5469

55-
function mockMinSerializerArgs(): Parameters<MetroSerializer> {
70+
function mockMinSerializerArgs(options?: {
71+
shouldAddToIgnoreList?: ((module: Module<MixedOutput>) => boolean) | undefined;
72+
}): Parameters<MetroSerializer> {
5673
let modulesCounter = 0;
74+
75+
const baseOptions: Record<string, any> = {
76+
asyncRequireModulePath: 'asyncRequire',
77+
createModuleId: (_filePath: string): number => modulesCounter++,
78+
dev: false,
79+
getRunModuleStatement: (_moduleId: string | number): string => '',
80+
includeAsyncPaths: false,
81+
modulesOnly: false,
82+
processModuleFilter: (_module: Module<MixedOutput>) => true,
83+
projectRoot: '/project/root',
84+
runBeforeMainModule: [],
85+
runModule: false,
86+
serverRoot: '/server/root',
87+
};
88+
89+
if (options && 'shouldAddToIgnoreList' in options) {
90+
baseOptions.shouldAddToIgnoreList = options.shouldAddToIgnoreList;
91+
} else {
92+
baseOptions.shouldAddToIgnoreList = (_module: Module<MixedOutput>) => false;
93+
}
94+
5795
return [
5896
'index.js',
5997
[],
@@ -68,20 +106,7 @@ function mockMinSerializerArgs(): Parameters<MetroSerializer> {
68106
unstable_transformProfile: 'hermes-stable',
69107
},
70108
},
71-
{
72-
asyncRequireModulePath: 'asyncRequire',
73-
createModuleId: (_filePath: string): number => modulesCounter++,
74-
dev: false,
75-
getRunModuleStatement: (_moduleId: string | number): string => '',
76-
includeAsyncPaths: false,
77-
modulesOnly: false,
78-
processModuleFilter: (_module: Module<MixedOutput>) => true,
79-
projectRoot: '/project/root',
80-
runBeforeMainModule: [],
81-
runModule: false,
82-
serverRoot: '/server/root',
83-
shouldAddToIgnoreList: (_module: Module<MixedOutput>) => false,
84-
},
109+
baseOptions as any,
85110
];
86111
}
87112

0 commit comments

Comments
 (0)