compose-source-maps.js 1.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/env node
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @format
 */

'use strict';

const {composeSourceMaps} = require('metro-source-map');
const fs = require('fs');

const argv = process.argv.slice(2);
let outputPath;
for (let i = 0; i < argv.length;) {
    if (argv[i] === '-o') {
      outputPath = argv[i + 1];
      argv.splice(i, 2);
      continue;
    }
    ++i;
}
if (!argv.length) {
  process.stderr.write(
    'Usage: node compose-source-maps.js <packager_sourcemap> <compiler_sourcemap> [-o output_file]\n'
  );
  process.exitCode = -1;
} else {
  const [packagerSourcemapPath, compilerSourcemapPath] = argv.splice(0, 2);
  const packagerSourcemap = JSON.parse(fs.readFileSync(packagerSourcemapPath, 'utf8'));
  const compilerSourcemap = JSON.parse(
    fs.readFileSync(compilerSourcemapPath, 'utf8'),
  );

  if (
    packagerSourcemap.x_facebook_offsets != null ||
    compilerSourcemap.x_facebook_offsets != null
  ) {
    throw new Error(
      'Random Access Bundle (RAM) format is not supported by this tool; ' +
        'it cannot process the `x_facebook_offsets` field provided ' +
        'in the base and/or target source map(s)',
    );
  }

  if (compilerSourcemap.x_facebook_segments != null) {
    throw new Error(
      'This tool cannot process the `x_facebook_segments` field provided ' +
        'in the target source map.',
    );
  }

  const composedMapJSON = JSON.stringify(composeSourceMaps([packagerSourcemap, compilerSourcemap]));
  if (outputPath) {
    fs.writeFileSync(outputPath, composedMapJSON, 'utf8');
  } else {
    process.stdout.write();
  }
}