[6a3a178] | 1 | # merge-source-map
|
---|
| 2 |
|
---|
| 3 | [![npm-version](https://img.shields.io/npm/v/merge-source-map.svg?style=flat-square)](https://npmjs.org/package/merge-source-map)
|
---|
| 4 | [![downloads](http://img.shields.io/npm/dm/merge-source-map.svg?style=flat-square)](https://npmjs.org/package/merge-source-map)
|
---|
| 5 | [![travis-ci](https://img.shields.io/travis/keik/merge-source-map.svg?style=flat-square)](https://travis-ci.org/keik/merge-source-map)
|
---|
| 6 | [![Coverage Status](https://img.shields.io/coveralls/keik/merge-source-map.svg?style=flat-square)](https://coveralls.io/github/keik/merge-source-map)
|
---|
| 7 |
|
---|
| 8 | Merge old source map and new source map in multi-transform flow
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | # API
|
---|
| 12 |
|
---|
| 13 | ```javascript
|
---|
| 14 | var merge = require('merge-source-map')
|
---|
| 15 | ```
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | ## `merge(oldMap, newMap)`
|
---|
| 19 |
|
---|
| 20 | Merge old source map and new source map and return merged.
|
---|
| 21 | If old or new source map value is falsy, return another one as it is.
|
---|
| 22 |
|
---|
| 23 | <dl>
|
---|
| 24 | <dt>
|
---|
| 25 | <code>oldMap</code> : <code>object|undefined</code>
|
---|
| 26 | </dt>
|
---|
| 27 | <dd>
|
---|
| 28 | old source map object
|
---|
| 29 | </dd>
|
---|
| 30 |
|
---|
| 31 | <dt>
|
---|
| 32 | <code>newmap</code> : <code>object|undefined</code>
|
---|
| 33 | </dt>
|
---|
| 34 | <dd>
|
---|
| 35 | new source map object
|
---|
| 36 | </dd>
|
---|
| 37 | </dl>
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | # Example
|
---|
| 41 |
|
---|
| 42 | ```javascript
|
---|
| 43 | var esprima = require('esprima'),
|
---|
| 44 | estraverse = require('estraverse'),
|
---|
| 45 | escodegen = require('escodegen'),
|
---|
| 46 | convert = require('convert-source-map'),
|
---|
| 47 | merge = require('merge-source-map')
|
---|
| 48 |
|
---|
| 49 | const CODE = 'a = 1',
|
---|
| 50 | FILEPATH = 'a.js'
|
---|
| 51 |
|
---|
| 52 | // create AST of original code
|
---|
| 53 | var ast = esprima.parse(CODE, {sourceType: 'module', loc: true})
|
---|
| 54 |
|
---|
| 55 | // transform AST of original code
|
---|
| 56 | estraverse.replace(ast, {
|
---|
| 57 | enter: function(node, parent) { /* change AST */ },
|
---|
| 58 | leave: function(node, parent) { /* change AST */ }
|
---|
| 59 | })
|
---|
| 60 |
|
---|
| 61 | // generate code and source map from transformed AST
|
---|
| 62 | var gen = escodegen.generate(ast, {
|
---|
| 63 | sourceMap: FILEPATH,
|
---|
| 64 | sourceMapWithCode: true,
|
---|
| 65 | sourceContent: CODE
|
---|
| 66 | })
|
---|
| 67 |
|
---|
| 68 | // merge old source map and new source map
|
---|
| 69 | var oldMap = convert.fromSource(CODE) && convert.fromSource(CODE).toObject(),
|
---|
| 70 | newMap = JSON.parse(gen.map.toString()),
|
---|
| 71 | mergedMap = merge(oldMap, newMap),
|
---|
| 72 | mapComment = convert.fromObject(mergedMap).toComment()
|
---|
| 73 |
|
---|
| 74 | // attach merge source map to transformed code
|
---|
| 75 | var transformed = gen.code + '\n' + mapComment
|
---|
| 76 |
|
---|
| 77 | console.log(transformed);
|
---|
| 78 | ```
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | # Test
|
---|
| 82 |
|
---|
| 83 | ```
|
---|
| 84 | % npm install
|
---|
| 85 | % npm test
|
---|
| 86 | ```
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | # License
|
---|
| 90 |
|
---|
| 91 | MIT (c) keik
|
---|