@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
153 lines (141 loc) • 4.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getDefaultExpoConfig = exports.createCountingSet = exports.convertSerializerOutput = void 0;
exports.getSortedModules = getSortedModules;
exports.metroSourceMapString = void 0;
var _sourceMapString = _interopRequireDefault(require("metro/src/DeltaBundler/Serializers/sourceMapString"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
// eslint-disable-next-line import/no-extraneous-dependencies
// eslint-disable-next-line import/no-extraneous-dependencies
/**
* Lazy-init reference for {@link createCountingSet} resolved function.
*/
let createCountingSetFunc;
/**
* Lazy-init reference for {@link metroSourceMapString} resolved function.
*/
let sourceMapStringFunc;
/**
* Lazy-init reference for {@link getDefaultExpoConfig} resolved function.
*/
let getDefaultExpoConfigFunc;
/**
* 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/76413561abb3757285e0cb0305f1f9f616fa2b6c/packages/metro/src/Server.js#L1086C1-L1095C7
*/
function getSortedModules(graph, {
createModuleId
}) {
const modules = [...graph.dependencies.values()];
// Assign IDs to modules in a consistent order
for (const module of modules) {
createModuleId(module.path);
}
// Sort by IDs
return modules.sort((a, b) => createModuleId(a.path) - createModuleId(b.path));
}
/**
* CountingSet was added in Metro 0.71.2 - a modified `Set` that only deletes items when the
* number of `delete(item)` calls matches the number of `add(item)` calls.
*
* https://github.com/facebook/metro/commit/fc29a1177f883144674cf85a813b58567f69d545
*/
const createCountingSet = () => {
if (createCountingSetFunc) {
return createCountingSetFunc();
}
try {
createCountingSetFunc = () => {
const {
default: MetroCountingSet
// eslint-disable-next-line global-require, import/no-extraneous-dependencies, @typescript-eslint/no-var-requires
} = require('metro/src/lib/CountingSet');
return new MetroCountingSet();
};
} catch (_) {
createCountingSetFunc = () => new Set();
}
return createCountingSetFunc();
};
/**
* In Metro versions prior to v0.80.10, `sourceMapString` was exported as a default export.
* Starting with v0.80.10, it became a named export.
*
* @returns The resolved `sourceMapString` function, of type {@link SourceMapString}
*/
exports.createCountingSet = createCountingSet;
const metroSourceMapString = (modules, options) => {
if (sourceMapStringFunc) {
return sourceMapStringFunc(modules, options);
}
if (typeof _sourceMapString.default === 'function') {
sourceMapStringFunc = _sourceMapString.default;
} else if (_sourceMapString.default && typeof _sourceMapString.default === 'object' && typeof _sourceMapString.default['sourceMapString'] === 'function') {
sourceMapStringFunc = _sourceMapString.default.sourceMapString;
}
if (!sourceMapStringFunc) {
throw new Error("[DATADOG METRO PLUGIN] Cannot find sourceMapString function in 'metro/src/DeltaBundler/Serializers/sourceMapString'");
}
return sourceMapStringFunc(modules, options);
};
/**
* Converts the serializer result of type {@link MetroSerializerOutput} to {@link MetroBundleWithMap}.
*/
exports.metroSourceMapString = metroSourceMapString;
const convertSerializerOutput = async output => {
const parse = obj => {
// Plain String
if (typeof obj === 'string') {
return {
code: obj,
map: '{}'
};
}
// Dictionary
if ('map' in obj) {
return {
code: obj.code,
map: obj.map
};
}
return undefined;
};
const value = parse(output);
if (!value) {
return parse(await output) ?? {
code: '',
map: '{}'
};
} else {
return value;
}
};
/**
* Gets the default Expo configuration options.
*/
exports.convertSerializerOutput = convertSerializerOutput;
const getDefaultExpoConfig = (projectRoot, options) => {
if (!getDefaultExpoConfigFunc) {
try {
// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const metroConfig = require('expo/metro-config');
getDefaultExpoConfigFunc = metroConfig.getDefaultConfig;
return metroConfig.getDefaultConfig(projectRoot, options);
} catch (e) {
throw new Error('Cannot load `expo/metro-config`. Make sure Expo is properly set up in your project.');
}
} else {
return getDefaultExpoConfigFunc(projectRoot, options);
}
};
exports.getDefaultExpoConfig = getDefaultExpoConfig;
//# sourceMappingURL=utils.js.map