UNPKG

@sentry/react-native

Version:
105 lines 5.37 kB
"use strict"; /* oxlint-disable typescript-eslint(no-unsafe-member-access) */ // Vendored / modified from @facebook/metro Object.defineProperty(exports, "__esModule", { value: true }); exports.createDefaultMetroSerializer = exports.getSortedModules = void 0; /** * This function ensures that modules in source maps are sorted in the same * order as in a plain JS bundle. * * https://github.com/facebook/metro/blob/9b85f83c9cc837d8cd897aa7723be7da5b296067/packages/metro/src/Server.js#L984 */ const getSortedModules = (graph, { createModuleId, }) => { const modules = [...graph.dependencies.values()]; // Sort by IDs return modules.sort((a, b) => createModuleId(a.path) - createModuleId(b.path)); }; exports.getSortedModules = getSortedModules; /** * Creates the default Metro plain bundle serializer. * Because Metro exports only the intermediate serializer functions, we need to * assemble the final serializer ourselves. We have to work with the modules the same as Metro does * to avoid unexpected changes in the final bundle. * * This is used when the user does not provide a custom serializer. * * https://github.com/facebook/metro/blob/9b85f83c9cc837d8cd897aa7723be7da5b296067/packages/metro/src/Server.js#L244-L277 */ const createDefaultMetroSerializer = () => { // Lazy-load Metro internals only when serializer is created // This defers requiring Metro modules until they're actually needed (during build), // avoiding import-time failures when Metro is only a transitive dependency var _a, _b; // oxlint-disable-next-line typescript-eslint(no-explicit-any) let baseJSBundleModule; try { baseJSBundleModule = require('metro/private/DeltaBundler/Serializers/baseJSBundle'); } catch (_c) { baseJSBundleModule = require('metro/src/DeltaBundler/Serializers/baseJSBundle'); } const baseJSBundle = typeof baseJSBundleModule === 'function' ? baseJSBundleModule : ((_a = baseJSBundleModule === null || baseJSBundleModule === void 0 ? void 0 : baseJSBundleModule.baseJSBundle) !== null && _a !== void 0 ? _a : baseJSBundleModule === null || baseJSBundleModule === void 0 ? void 0 : baseJSBundleModule.default); let sourceMapString; try { const sourceMapStringModule = require('metro/private/DeltaBundler/Serializers/sourceMapString'); sourceMapString = sourceMapStringModule.sourceMapString; } catch (e) { sourceMapString = require('metro/src/DeltaBundler/Serializers/sourceMapString'); if ('sourceMapString' in sourceMapString) { // Changed to named export in https://github.com/facebook/metro/commit/34148e61200a508923315fbe387b26d1da27bf4b // Metro 0.81.0 and 0.80.10 patch sourceMapString = sourceMapString.sourceMapString; } } // oxlint-disable-next-line typescript-eslint(no-explicit-any) let bundleToStringModule; try { bundleToStringModule = require('metro/private/lib/bundleToString'); } catch (_d) { bundleToStringModule = require('metro/src/lib/bundleToString'); } const bundleToString = typeof bundleToStringModule === 'function' ? bundleToStringModule : ((_b = bundleToStringModule === null || bundleToStringModule === void 0 ? void 0 : bundleToStringModule.bundleToString) !== null && _b !== void 0 ? _b : bundleToStringModule === null || bundleToStringModule === void 0 ? void 0 : bundleToStringModule.default); return (entryPoint, preModules, graph, options) => { // baseJSBundle assigns IDs to modules in a consistent order let bundle = baseJSBundle(entryPoint, preModules, graph, options); const isHot = 'hot' in graph.transformOptions ? graph.transformOptions.hot : graph.transformOptions.dev; if (options.sentryBundleCallback && !isHot) { bundle = options.sentryBundleCallback(bundle); } const { code } = bundleToString(bundle); if (isHot) { // Hot/dev means running in dev server, sourcemaps are generated on demand return code; } let sourceMapStringFunction; if (typeof sourceMapString === 'function') { sourceMapStringFunction = sourceMapString; } else if (typeof sourceMapString === 'object' && sourceMapString != null && 'sourceMapString' in sourceMapString && typeof sourceMapString['sourceMapString'] === 'function') { sourceMapStringFunction = sourceMapString.sourceMapString; } else { throw new Error(` [@sentry/react-native/metro] Cannot find sourceMapString function in 'metro/src/DeltaBundler/Serializers/sourceMapString'. Please check the version of Metro you are using and report the issue at http://www.github.com/getsentry/sentry-react-native/issues `); } // Always generate source maps, can't use Sentry without source maps const map = sourceMapStringFunction([...preModules, ...(0, exports.getSortedModules)(graph, options)], { processModuleFilter: options.processModuleFilter, shouldAddToIgnoreList: options.shouldAddToIgnoreList || (() => false), }); return { code, map }; }; }; exports.createDefaultMetroSerializer = createDefaultMetroSerializer; //# sourceMappingURL=utils.js.map