Skip to content
Merge old source map and new source map in multi-transform flow
Branch: master
Clone or download
Latest commit 9877907 Dec 24, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Fix syntax for old node version Jun 19, 2017
.eslintignore Organize tests and more Jun 19, 2017
.gitignore Add tests Sep 23, 2016
LICENSE Add license file Sep 23, 2016 Update Sep 8, 2017
package.json 1.1.0 Dec 25, 2017


npm-version downloads travis-ci Coverage Status

Merge old source map and new source map in multi-transform flow


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


var esprima    = require('esprima'),
    estraverse = require('estraverse'),
    escodegen  = require('escodegen'),
    convert    = require('convert-source-map'),
    merge      = require('merge-source-map')

const CODE = 'a = 1',
      FILEPATH = 'a.js'

// create AST of original code
var ast = esprima.parse(CODE, {sourceType: 'module', loc: true})

// transform AST of original code
estraverse.replace(ast, {
  enter: 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(),
    newMap = JSON.parse(,
    mergedMap = merge(oldMap, newMap),
    mapComment = convert.fromObject(mergedMap).toComment()

// attach merge source map to transformed code
var transformed = gen.code + '\n' + mapComment



% npm install
% npm test


MIT (c) keik

You can’t perform that action at this time.