-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split sourcemap merging logic into its own file.
- Loading branch information
1 parent
4da4728
commit e31e907
Showing
2 changed files
with
51 additions
and
44 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
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,50 @@ | ||
// @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.column != null) { | ||
mergedGenerator.addMapping({ | ||
source: mapping.source, | ||
|
||
original: | ||
mapping.source == null | ||
? null | ||
: { | ||
line: mapping.originalLine, | ||
column: mapping.originalColumn, | ||
}, | ||
|
||
generated: generatedPosition, | ||
|
||
name: mapping.name, | ||
}); | ||
} | ||
}); | ||
|
||
const mergedMap = mergedGenerator.toJSON(); | ||
inputMap.mappings = mergedMap.mappings; | ||
return inputMap; | ||
} |