sourcemapUtils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.generateSourcemap = generateSourcemap;
  6. exports.findSourcemap = findSourcemap;
  7. function _cliTools() {
  8. const data = require("@react-native-community/cli-tools");
  9. _cliTools = function () {
  10. return data;
  11. };
  12. return data;
  13. }
  14. function _fs() {
  15. const data = _interopRequireDefault(require("fs"));
  16. _fs = function () {
  17. return data;
  18. };
  19. return data;
  20. }
  21. function _path() {
  22. const data = _interopRequireDefault(require("path"));
  23. _path = function () {
  24. return data;
  25. };
  26. return data;
  27. }
  28. function _os() {
  29. const data = _interopRequireDefault(require("os"));
  30. _os = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. function _ip() {
  36. const data = _interopRequireDefault(require("ip"));
  37. _ip = function () {
  38. return data;
  39. };
  40. return data;
  41. }
  42. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  43. function getTempFilePath(filename) {
  44. return _path().default.join(_os().default.tmpdir(), filename);
  45. }
  46. function writeJsonSync(targetPath, data) {
  47. let json;
  48. try {
  49. json = JSON.stringify(data);
  50. } catch (e) {
  51. throw new (_cliTools().CLIError)(`Failed to serialize data to json before writing to ${targetPath}`, e);
  52. }
  53. try {
  54. _fs().default.writeFileSync(targetPath, json, 'utf-8');
  55. } catch (e) {
  56. throw new (_cliTools().CLIError)(`Failed to write json to ${targetPath}`, e);
  57. }
  58. }
  59. async function getSourcemapFromServer(port) {
  60. _cliTools().logger.debug('Getting source maps from Metro packager server');
  61. const DEBUG_SERVER_PORT = port || '8081';
  62. const IP_ADDRESS = _ip().default.address();
  63. const PLATFORM = 'android';
  64. const requestURL = `http://${IP_ADDRESS}:${DEBUG_SERVER_PORT}/index.map?platform=${PLATFORM}&dev=true`;
  65. try {
  66. const {
  67. data
  68. } = await (0, _cliTools().fetch)(requestURL);
  69. return data;
  70. } catch (e) {
  71. _cliTools().logger.debug(`Failed to fetch source map from "${requestURL}"`);
  72. return undefined;
  73. }
  74. }
  75. /**
  76. * Generate a sourcemap by fetching it from a running metro server
  77. */
  78. async function generateSourcemap(port) {
  79. // Fetch the source map to a temp directory
  80. const sourceMapPath = getTempFilePath('index.map');
  81. const sourceMapResult = await getSourcemapFromServer(port);
  82. if (sourceMapResult) {
  83. _cliTools().logger.debug('Using source maps from Metro packager server');
  84. writeJsonSync(sourceMapPath, sourceMapResult);
  85. _cliTools().logger.debug(`Successfully obtained the source map and stored it in ${sourceMapPath}`);
  86. return sourceMapPath;
  87. } else {
  88. _cliTools().logger.error('Cannot obtain source maps from Metro packager server');
  89. return undefined;
  90. }
  91. }
  92. /**
  93. *
  94. * @param ctx
  95. */
  96. async function findSourcemap(ctx, port) {
  97. const intermediateBuildPath = _path().default.join(ctx.root, 'android', 'app', 'build', 'intermediates', 'sourcemaps', 'react', 'debug', 'index.android.bundle.packager.map');
  98. const generatedBuildPath = _path().default.join(ctx.root, 'android', 'app', 'build', 'generated', 'sourcemaps', 'react', 'debug', 'index.android.bundle.map');
  99. if (_fs().default.existsSync(generatedBuildPath)) {
  100. _cliTools().logger.debug(`Getting the source map from ${generateSourcemap}`);
  101. return generatedBuildPath;
  102. } else if (_fs().default.existsSync(intermediateBuildPath)) {
  103. _cliTools().logger.debug(`Getting the source map from ${intermediateBuildPath}`);
  104. return intermediateBuildPath;
  105. } else {
  106. return generateSourcemap(port);
  107. }
  108. }
  109. //# sourceMappingURL=sourcemapUtils.js.map