.. | ||
index.js | ||
LICENSE | ||
package.json | ||
README.md |
merge-source-map
Merge old source map and new source map in multi-transform flow
API
var merge = require('merge-source-map')
merge(oldMap, newMap)
Merge old source map and new source map and return merged. If old or new source map value is falsy, return another one as it is.
-
oldMap
:object|undefined
- old source map object
-
newmap
:object|undefined
- new source map object
Example
var esprima = require('esprima'),
= require('estraverse'),
estraverse = require('escodegen'),
escodegen = require('convert-source-map'),
convert = require('merge-source-map')
merge
const CODE = 'a = 1',
= 'a.js'
FILEPATH
// create AST of original code
var ast = esprima.parse(CODE, {sourceType: 'module', loc: true})
// transform AST of original code
.replace(ast, {
estraverseenter: function(node, parent) { /* change AST */ },
leave: function(node, parent) { /* change AST */ }
})
// generate code and source map from transformed AST
var gen = escodegen.generate(ast, {
sourceMap: FILEPATH,
sourceMapWithCode: true,
sourceContent: CODE
})
// merge old source map and new source map
var oldMap = convert.fromSource(CODE) && convert.fromSource(CODE).toObject(),
= JSON.parse(gen.map.toString()),
newMap = merge(oldMap, newMap),
mergedMap = convert.fromObject(mergedMap).toComment()
mapComment
// attach merge source map to transformed code
var transformed = gen.code + '\n' + mapComment
console.log(transformed);
Test
% npm install
% npm test
License
MIT (c) keik