compose-source-maps.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env node
  2. /**
  3. * Copyright (c) Facebook, Inc. and its affiliates.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. * @format
  9. */
  10. 'use strict';
  11. const {composeSourceMaps} = require('metro-source-map');
  12. const fs = require('fs');
  13. const argv = process.argv.slice(2);
  14. let outputPath;
  15. for (let i = 0; i < argv.length;) {
  16. if (argv[i] === '-o') {
  17. outputPath = argv[i + 1];
  18. argv.splice(i, 2);
  19. continue;
  20. }
  21. ++i;
  22. }
  23. if (!argv.length) {
  24. process.stderr.write(
  25. 'Usage: node compose-source-maps.js <packager_sourcemap> <compiler_sourcemap> [-o output_file]\n'
  26. );
  27. process.exitCode = -1;
  28. } else {
  29. const [packagerSourcemapPath, compilerSourcemapPath] = argv.splice(0, 2);
  30. const packagerSourcemap = JSON.parse(fs.readFileSync(packagerSourcemapPath, 'utf8'));
  31. const compilerSourcemap = JSON.parse(
  32. fs.readFileSync(compilerSourcemapPath, 'utf8'),
  33. );
  34. if (
  35. packagerSourcemap.x_facebook_offsets != null ||
  36. compilerSourcemap.x_facebook_offsets != null
  37. ) {
  38. throw new Error(
  39. 'Random Access Bundle (RAM) format is not supported by this tool; ' +
  40. 'it cannot process the `x_facebook_offsets` field provided ' +
  41. 'in the base and/or target source map(s)',
  42. );
  43. }
  44. if (compilerSourcemap.x_facebook_segments != null) {
  45. throw new Error(
  46. 'This tool cannot process the `x_facebook_segments` field provided ' +
  47. 'in the target source map.',
  48. );
  49. }
  50. const composedMapJSON = JSON.stringify(composeSourceMaps([packagerSourcemap, compilerSourcemap]));
  51. if (outputPath) {
  52. fs.writeFileSync(outputPath, composedMapJSON, 'utf8');
  53. } else {
  54. process.stdout.write();
  55. }
  56. }