-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor mergeSourceMaps to its own file
Reviewed By: davidaurelio Differential Revision: D10488231 fbshipit-source-id: 62be8989cbc31c3a824bb460b74dfb12b12a6831
- Loading branch information
1 parent
b99596c
commit df212a4
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |