1#!/usr/bin/env node 2/** 3 * Copyright (c) Meta Platforms, Inc. and 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 11'use strict'; 12 13const {composeSourceMaps} = require('metro-source-map'); 14const fs = require('fs'); 15 16const argv = process.argv.slice(2); 17let outputPath; 18for (let i = 0; i < argv.length; ) { 19 if (argv[i] === '-o') { 20 outputPath = argv[i + 1]; 21 argv.splice(i, 2); 22 continue; 23 } 24 ++i; 25} 26if (!argv.length) { 27 process.stderr.write( 28 'Usage: node compose-source-maps.js <packager_sourcemap> <compiler_sourcemap> [-o output_file]\n', 29 ); 30 process.exitCode = -1; 31} else { 32 const [packagerSourcemapPath, compilerSourcemapPath] = argv.splice(0, 2); 33 const packagerSourcemap = JSON.parse( 34 fs.readFileSync(packagerSourcemapPath, 'utf8'), 35 ); 36 const compilerSourcemap = JSON.parse( 37 fs.readFileSync(compilerSourcemapPath, 'utf8'), 38 ); 39 40 if ( 41 packagerSourcemap.x_facebook_offsets != null || 42 compilerSourcemap.x_facebook_offsets != null 43 ) { 44 throw new Error( 45 'Random Access Bundle (RAM) format is not supported by this tool; ' + 46 'it cannot process the `x_facebook_offsets` field provided ' + 47 'in the base and/or target source map(s)', 48 ); 49 } 50 51 if (compilerSourcemap.x_facebook_segments != null) { 52 throw new Error( 53 'This tool cannot process the `x_facebook_segments` field provided ' + 54 'in the target source map.', 55 ); 56 } 57 58 const composedMapJSON = JSON.stringify( 59 composeSourceMaps([packagerSourcemap, compilerSourcemap]), 60 ); 61 if (outputPath) { 62 fs.writeFileSync(outputPath, composedMapJSON, 'utf8'); 63 } else { 64 process.stdout.write(); 65 } 66} 67