You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.2 KiB

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