Skip to content

Commit

Permalink
fix(metro): Fix sourceMapString is not a function, support `metro@0…
Browse files Browse the repository at this point in the history
….80.10`
  • Loading branch information
krystofwoldrich committed Aug 8, 2024
1 parent 764654b commit c0b4b96
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 218 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"jest-environment-jsdom": "^29.6.2",
"jest-extended": "^4.0.2",
"madge": "^6.1.0",
"metro": "0.76",
"metro": "0.80.10",
"prettier": "^2.0.5",
"react": "18.2.0",
"react-native": "0.73.9",
Expand Down
16 changes: 15 additions & 1 deletion src/js/tools/vendor/metro/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import * as bundleToString from 'metro/src/lib/bundleToString';

import type { MetroSerializer } from '../../utils';

type NewSourceMapStringExport = {
// Since Metro v0.80.10 https://github.com/facebook/metro/compare/v0.80.9...v0.80.10#diff-1b836d1729e527a725305eef0cec22e44605af2700fa413f4c2489ea1a03aebcL28
sourceMapString: typeof sourceMapString;
};

/**
* This function ensures that modules in source maps are sorted in the same
* order as in a plain JS bundle.
Expand Down Expand Up @@ -75,8 +80,17 @@ export const createDefaultMetroSerializer = (): MetroSerializer => {
return code;
}

let sourceMapStringFunction;
if (typeof sourceMapString === 'function') {
sourceMapStringFunction = sourceMapString;
} else if (typeof (sourceMapString as NewSourceMapStringExport).sourceMapString === 'function') {
sourceMapStringFunction = (sourceMapString as NewSourceMapStringExport).sourceMapString;
} else {
throw new Error('[@sentry/react-native/metro] Cannot find sourceMapString function in Metro');
}

// Always generate source maps, can't use Sentry without source maps
const map = sourceMapString([...preModules, ...getSortedModules(graph, options)], {
const map = sourceMapStringFunction([...preModules, ...getSortedModules(graph, options)], {
processModuleFilter: options.processModuleFilter,
shouldAddToIgnoreList: options.shouldAddToIgnoreList,
});
Expand Down
Loading

0 comments on commit c0b4b96

Please sign in to comment.