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.

README.md 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # convert-source-map [![build status](https://secure.travis-ci.org/thlorenz/convert-source-map.svg?branch=master)](http://travis-ci.org/thlorenz/convert-source-map)
  2. <a href="https://www.patreon.com/bePatron?u=8663953"><img alt="become a patron" src="https://c5.patreon.com/external/logo/become_a_patron_button.png" height="35px"></a>
  3. Converts a source-map from/to different formats and allows adding/changing properties.
  4. ```js
  5. var convert = require('convert-source-map');
  6. var json = convert
  7. .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
  8. .toJSON();
  9. var modified = convert
  10. .fromComment('//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVpbGQvZm9vLm1pbi5qcyIsInNvdXJjZXMiOlsic3JjL2Zvby5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSIsInNvdXJjZVJvb3QiOiIvIn0=')
  11. .setProperty('sources', [ 'SRC/FOO.JS' ])
  12. .toJSON();
  13. console.log(json);
  14. console.log(modified);
  15. ```
  16. ```json
  17. {"version":3,"file":"build/foo.min.js","sources":["src/foo.js"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
  18. {"version":3,"file":"build/foo.min.js","sources":["SRC/FOO.JS"],"names":[],"mappings":"AAAA","sourceRoot":"/"}
  19. ```
  20. ## API
  21. ### fromObject(obj)
  22. Returns source map converter from given object.
  23. ### fromJSON(json)
  24. Returns source map converter from given json string.
  25. ### fromBase64(base64)
  26. Returns source map converter from given base64 encoded json string.
  27. ### fromComment(comment)
  28. Returns source map converter from given base64 encoded json string prefixed with `//# sourceMappingURL=...`.
  29. ### fromMapFileComment(comment, mapFileDir)
  30. Returns source map converter from given `filename` by parsing `//# sourceMappingURL=filename`.
  31. `filename` must point to a file that is found inside the `mapFileDir`. Most tools store this file right next to the
  32. generated file, i.e. the one containing the source map.
  33. ### fromSource(source)
  34. Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was found.
  35. ### fromMapFileSource(source, mapFileDir)
  36. Finds last sourcemap comment in file and returns source map converter or returns null if no source map comment was
  37. found.
  38. The sourcemap will be read from the map file found by parsing `# sourceMappingURL=file` comment. For more info see
  39. fromMapFileComment.
  40. ### toObject()
  41. Returns a copy of the underlying source map.
  42. ### toJSON([space])
  43. Converts source map to json string. If `space` is given (optional), this will be passed to
  44. [JSON.stringify](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify) when the
  45. JSON string is generated.
  46. ### toBase64()
  47. Converts source map to base64 encoded json string.
  48. ### toComment([options])
  49. Converts source map to an inline comment that can be appended to the source-file.
  50. By default, the comment is formatted like: `//# sourceMappingURL=...`, which you would
  51. normally see in a JS source file.
  52. When `options.multiline == true`, the comment is formatted like: `/*# sourceMappingURL=... */`, which you would find in a CSS source file.
  53. ### addProperty(key, value)
  54. Adds given property to the source map. Throws an error if property already exists.
  55. ### setProperty(key, value)
  56. Sets given property to the source map. If property doesn't exist it is added, otherwise its value is updated.
  57. ### getProperty(key)
  58. Gets given property of the source map.
  59. ### removeComments(src)
  60. Returns `src` with all source map comments removed
  61. ### removeMapFileComments(src)
  62. Returns `src` with all source map comments pointing to map files removed.
  63. ### commentRegex
  64. Provides __a fresh__ RegExp each time it is accessed. Can be used to find source map comments.
  65. ### mapFileCommentRegex
  66. Provides __a fresh__ RegExp each time it is accessed. Can be used to find source map comments pointing to map files.
  67. ### generateMapFileComment(file, [options])
  68. Returns a comment that links to an external source map via `file`.
  69. By default, the comment is formatted like: `//# sourceMappingURL=...`, which you would normally see in a JS source file.
  70. When `options.multiline == true`, the comment is formatted like: `/*# sourceMappingURL=... */`, which you would find in a CSS source file.
  71. [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/thlorenz/convert-source-map/trend.png)](https://bitdeli.com/free "Bitdeli Badge")