This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/merge-source-map
2019-08-11 20:48:02 +02:00
..
index.js Update Framework 2019-08-11 20:48:02 +02:00
LICENSE Update Framework 2019-08-11 20:48:02 +02:00
package.json Update Framework 2019-08-11 20:48:02 +02:00
README.md Update Framework 2019-08-11 20:48:02 +02:00

merge-source-map

npm-version downloads travis-ci Coverage Status

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'),
    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(gen.map.toString()),
    mergedMap = merge(oldMap, newMap),
    mapComment = convert.fromObject(mergedMap).toComment()

// 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