Skip to content

Instantly share code, notes, and snippets.

@lydell
Last active August 29, 2015 14:08
Show Gist options
  • Save lydell/857cba1b00cf8dd3c169 to your computer and use it in GitHub Desktop.
Save lydell/857cba1b00cf8dd3c169 to your computer and use it in GitHub Desktop.
convert-source-map-simple
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
{
"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