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.

231 lines
8.7 KiB

4 years ago
  1. Overview [![Build Status](https://travis-ci.org/lydell/source-map-resolve.svg?branch=master)](https://travis-ci.org/lydell/source-map-resolve)
  2. ========
  3. Resolve the source map and/or sources for a generated file.
  4. ```js
  5. var sourceMapResolve = require("source-map-resolve")
  6. var sourceMap = require("source-map")
  7. var code = [
  8. "!function(){...}();",
  9. "/*# sourceMappingURL=foo.js.map */"
  10. ].join("\n")
  11. sourceMapResolve.resolveSourceMap(code, "/js/foo.js", fs.readFile, function(error, result) {
  12. if (error) {
  13. return notifyFailure(error)
  14. }
  15. result
  16. // {
  17. // map: {file: "foo.js", mappings: "...", sources: ["/coffee/foo.coffee"], names: []},
  18. // url: "/js/foo.js.map",
  19. // sourcesRelativeTo: "/js/foo.js.map",
  20. // sourceMappingURL: "foo.js.map"
  21. // }
  22. sourceMapResolve.resolveSources(result.map, result.sourcesRelativeTo, fs.readFile, function(error, result) {
  23. if (error) {
  24. return notifyFailure(error)
  25. }
  26. result
  27. // {
  28. // sourcesResolved: ["/coffee/foo.coffee"],
  29. // sourcesContent: ["<contents of /coffee/foo.coffee>"]
  30. // }
  31. })
  32. })
  33. sourceMapResolve.resolve(code, "/js/foo.js", fs.readFile, function(error, result) {
  34. if (error) {
  35. return notifyFailure(error)
  36. }
  37. result
  38. // {
  39. // map: {file: "foo.js", mappings: "...", sources: ["/coffee/foo.coffee"], names: []},
  40. // url: "/js/foo.js.map",
  41. // sourcesRelativeTo: "/js/foo.js.map",
  42. // sourceMappingURL: "foo.js.map",
  43. // sourcesResolved: ["/coffee/foo.coffee"],
  44. // sourcesContent: ["<contents of /coffee/foo.coffee>"]
  45. // }
  46. result.map.sourcesContent = result.sourcesContent
  47. var map = new sourceMap.sourceMapConsumer(result.map)
  48. map.sourceContentFor("/coffee/foo.coffee")
  49. // "<contents of /coffee/foo.coffee>"
  50. })
  51. ```
  52. Installation
  53. ============
  54. - `npm install source-map-resolve`
  55. - `bower install source-map-resolve`
  56. - `component install lydell/source-map-resolve`
  57. Works with CommonJS, AMD and browser globals, through UMD.
  58. Note: This module requires `setImmediate` and `atob`.
  59. Use polyfills if needed, such as:
  60. - <https://github.com/NobleJS/setImmediate>
  61. - <https://github.com/davidchambers/Base64.js>
  62. Usage
  63. =====
  64. ### `sourceMapResolve.resolveSourceMap(code, codeUrl, read, callback)` ###
  65. - `code` is a string of code that may or may not contain a sourceMappingURL
  66. comment. Such a comment is used to resolve the source map.
  67. - `codeUrl` is the url to the file containing `code`. If the sourceMappingURL
  68. is relative, it is resolved against `codeUrl`.
  69. - `read(url, callback)` is a function that reads `url` and responds using
  70. `callback(error, content)`. In Node.js you might want to use `fs.readFile`,
  71. while in the browser you might want to use an asynchronus `XMLHttpRequest`.
  72. - `callback(error, result)` is a function that is invoked with either an error
  73. or `null` and the result.
  74. The result is an object with the following properties:
  75. - `map`: The source map for `code`, as an object (not a string).
  76. - `url`: The url to the source map. If the source map came from a data uri,
  77. this property is `null`, since then there is no url to it.
  78. - `sourcesRelativeTo`: The url that the sources of the source map are relative
  79. to. Since the sources are relative to the source map, and the url to the
  80. source map is provided as the `url` property, this property might seem
  81. superfluos. However, remember that the `url` property can be `null` if the
  82. source map came from a data uri. If so, the sources are relative to the file
  83. containing the data uri—`codeUrl`. This property will be identical to the
  84. `url` property or `codeUrl`, whichever is appropriate. This way you can
  85. conveniently resolve the sources without having to think about where the
  86. source map came from.
  87. - `sourceMappingURL`: The url of the sourceMappingURL comment in `code`.
  88. If `code` contains no sourceMappingURL, the result is `null`.
  89. ### `sourceMapResolve.resolveSources(map, mapUrl, read, [options], callback)` ###
  90. - `map` is a source map, as an object (not a string).
  91. - `mapUrl` is the url to the file containing `map`. Relative sources in the
  92. source map, if any, are resolved against `mapUrl`.
  93. - `read(url, callback)` is a function that reads `url` and responds using
  94. `callback(error, content)`. In Node.js you might want to use `fs.readFile`,
  95. while in the browser you might want to use an asynchronus `XMLHttpRequest`.
  96. - `options` is an optional object with any of the following properties:
  97. - `sourceRoot`: Override the `sourceRoot` property of the source map, which
  98. might only be relevant when resolving sources in the browser. This lets you
  99. bypass it when using the module outside of a browser, if needed. Pass a
  100. string to replace the `sourceRoot` property with, or `false` to ignore it.
  101. Defaults to `undefined`.
  102. - `callback(error, result)` is a function that is invoked with either an error
  103. or `null` and the result.
  104. The result is an object with the following properties:
  105. - `sourcesResolved`: The same as `map.sources`, except all the sources are
  106. fully resolved.
  107. - `sourcesContent`: An array with the contents of all sources in `map.sources`,
  108. in the same order as `map.sources`. If getting the contents of a source fails,
  109. an error object is put into the array instead.
  110. ### `sourceMapResolve.resolve(code, codeUrl, read, [options], callback)` ###
  111. The arguments are identical to `sourceMapResolve.resolveSourceMap`, except that
  112. you may also provide the same `options` as in `sourceMapResolve.resolveSources`.
  113. This is a convenience method that first resolves the source map and then its
  114. sources. You could also do this by first calling
  115. `sourceMapResolve.resolveSourceMap` and then `sourceMapResolve.resolveSources`.
  116. The result is identical to `sourceMapResolve.resolveSourceMap`, with the
  117. properties from `sourceMapResolve.resolveSources` merged into it.
  118. There is one extra feature available, though. If `code` is `null`, `codeUrl` is
  119. treated as a url to the source map instead of to `code`, and will be read. This
  120. is handy if you _sometimes_ get the source map url from the `SourceMap: <url>`
  121. header (see the [Notes] section). In this case, the `sourceMappingURL` property
  122. of the result is `null`.
  123. [Notes]: #notes
  124. ### `sourceMapResolve.*Sync()` ###
  125. There are also sync versions of the three previous functions. They are identical
  126. to the async versions, except:
  127. - They expect a sync reading function. In Node.js you might want to use
  128. `fs.readFileSync`, while in the browser you might want to use a synchronus
  129. `XMLHttpRequest`.
  130. - They throw errors and return the result instead of using a callback.
  131. `sourceMapResolve.resolveSourcesSync` also accepts `null` as the `read`
  132. parameter. The result is the same as when passing a function as the `read
  133. parameter`, except that the `sourcesContent` property of the result will be an
  134. empty array. In other words, the sources aren’t read. You only get the
  135. `sourcesResolved` property. (This only supported in the synchronus version, since
  136. there is no point doing it asynchronusly.)
  137. ### `sourceMapResolve.parseMapToJSON(string, [data])` ###
  138. The spec says that if a source map (as a string) starts with `)]}'`, it should
  139. be stripped off. This is to prevent XSSI attacks. This function does that and
  140. returns the result of `JSON.parse`ing what’s left.
  141. If this function throws `error`, `error.sourceMapData === data`.
  142. ### Errors
  143. All errors passed to callbacks or thrown by this module have a `sourceMapData`
  144. property that contain as much as possible of the intended result of the function
  145. up until the error occurred.
  146. Note that while the `map` property of result objects always is an object,
  147. `error.sourceMapData.map` will be a string if parsing that string fails.
  148. Note
  149. ====
  150. This module resolves the source map for a given generated file by looking for a
  151. sourceMappingURL comment. The spec defines yet a way to provide the URL to the
  152. source map: By sending the `SourceMap: <url>` header along with the generated
  153. file. Since this module doesn’t retrive the generated code for you (instead
  154. _you_ give the generated code to the module), it’s up to you to look for such a
  155. header when you retrieve the file (should the need arise).
  156. Development
  157. ===========
  158. Tests
  159. -----
  160. First off, run `npm install` to install testing modules and browser polyfills.
  161. `npm test` lints the code and runs the test suite in Node.js.
  162. x-package.json5
  163. ---------------
  164. package.json, component.json and bower.json are all generated from
  165. x-package.json5 by using [`xpkg`]. Only edit x-package.json5, and remember to
  166. run `xpkg` before commiting!
  167. [`xpkg`]: https://github.com/kof/node-xpkg
  168. Generating the browser version
  169. ------------------------------
  170. source-map-resolve.js is generated from source-map-resolve-node.js and
  171. source-map-resolve-template.js. Only edit the two latter files, _not_
  172. source-map-resolve.js! To generate it, run `npm run build`.
  173. License
  174. =======
  175. [The X11 (“MIT”) License](LICENSE).