Skip to content

Commit

Permalink
Refactor mergeSourceMaps to its own file
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D10488231

fbshipit-source-id: 62be8989cbc31c3a824bb460b74dfb12b12a6831
  • Loading branch information
Miguel Jimenez Esun authored and facebook-github-bot committed Oct 22, 2018
1 parent b99596c commit df212a4
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/metro/src/ModuleGraph/worker/mergeSourceMaps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

/**
* 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
* @flow strict-local
*/

'use strict';

// eslint-disable-next-line lint/flow-no-fixme
// $FlowFixMe: too hard to type, and they only have a .ts file.
const sourceMap = require('source-map');

import type {BabelSourceMap} from '@babel/core';
import type {MetroSourceMap} from 'metro-source-map';

function mergeSourceMaps(
file: string,
originalMap: MetroSourceMap,
secondMap: MetroSourceMap,
): BabelSourceMap {
const merged = new sourceMap.SourceMapGenerator();
const inputMap = new sourceMap.SourceMapConsumer(originalMap);
new sourceMap.SourceMapConsumer(secondMap).eachMapping(mapping => {
const original = inputMap.originalPositionFor({
line: mapping.originalLine,
column: mapping.originalColumn,
});
if (original.line == null) {
return;
}

merged.addMapping({
generated: {line: mapping.generatedLine, column: mapping.generatedColumn},
original: {line: original.line, column: original.column || 0},
source: file,
name: original.name || mapping.name,
});
});
return merged.toJSON();
}

module.exports = mergeSourceMaps;

0 comments on commit df212a4

Please sign in to comment.