Last active
August 29, 2015 14:08
-
-
Save lydell/857cba1b00cf8dd3c169 to your computer and use it in GitHub Desktop.
convert-source-map-simple
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
var sourceMap = require("source-map") | |
function convert(map) { | |
var simpleMap = shallowCopy(map) | |
// Prevent the source-map module from joining sources with the sourceRoot (if | |
// any), so that we can find the indexes of in the `sources` property. | |
delete simpleMap.sourceRoot | |
simpleMap.mappings = {} | |
new sourceMap.SourceMapConsumer(map).eachMapping(function(mapping) { | |
var newMapping = [] | |
if (mapping.source != null) { | |
newMapping.push( | |
simpleMap.sources.indexOf(mapping.source), | |
mapping.originalLine, | |
mapping.originalColumn | |
) | |
if (mapping.name != null) { | |
newMapping.push(simpleMap.names.indexOf(mapping.name)) | |
} | |
} | |
var line = simpleMap.mappings[mapping.generatedLine] | |
if (!line) { | |
line = simpleMap.mappings[mapping.generatedLine] = {} | |
} | |
line[mapping.generatedColumn] = newMapping | |
}) | |
// Re-add the sourceRoot if needed. | |
if ("sourceRoot" in map) { | |
simpleMap.sourceRoot = map.sourceRoot | |
} | |
return simpleMap | |
} | |
function shallowCopy(obj) { | |
var copy = {} | |
Object.keys(obj).forEach(function(key) { | |
copy[key] = obj[key] | |
}) | |
return copy | |
} | |
module.exports = convert |
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
{ | |
"name": "convert-source-map-simple", | |
"version": "0.0.1", | |
"dependencies": { | |
"source-map": "~0.1.40" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment