-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
merge-map.js
55 lines (45 loc) 路 1.5 KB
/
merge-map.js
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
// @flow
import type { SourceMap } from "convert-source-map";
import sourceMap from "source-map";
export default function mergeSourceMap(
inputMap: SourceMap,
map: SourceMap,
): SourceMap {
const inputMapConsumer = new sourceMap.SourceMapConsumer(inputMap);
const outputMapConsumer = new sourceMap.SourceMapConsumer(map);
const mergedGenerator = new sourceMap.SourceMapGenerator({
file: inputMapConsumer.file,
sourceRoot: inputMapConsumer.sourceRoot,
});
// This assumes the output map always has a single source, since Babel always compiles a
// single source file to a single output file.
const source = outputMapConsumer.sources[0];
inputMapConsumer.eachMapping(function(mapping) {
const generatedPosition = outputMapConsumer.generatedPositionFor({
line: mapping.generatedLine,
column: mapping.generatedColumn,
source: source,
});
if (generatedPosition.line != null && generatedPosition.column != null) {
const generated = {
line: generatedPosition.line,
column: generatedPosition.column,
};
mergedGenerator.addMapping({
source: mapping.source,
original:
mapping.source == null
? null
: {
line: mapping.originalLine,
column: mapping.originalColumn,
},
generated,
name: mapping.name,
});
}
});
const mergedMap = mergedGenerator.toJSON();
inputMap.mappings = mergedMap.mappings;
return inputMap;
}