web 3d图形渲染器
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.

193 lines
7.7 KiB

  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. Usage
  56. =====
  57. ### `sourceMapResolve.resolveSourceMap(code, codeUrl, read, callback)` ###
  58. - `code` is a string of code that may or may not contain a sourceMappingURL
  59. comment. Such a comment is used to resolve the source map.
  60. - `codeUrl` is the url to the file containing `code`. If the sourceMappingURL
  61. is relative, it is resolved against `codeUrl`.
  62. - `read(url, callback)` is a function that reads `url` and responds using
  63. `callback(error, content)`. In Node.js you might want to use `fs.readFile`,
  64. while in the browser you might want to use an asynchronus `XMLHttpRequest`.
  65. - `callback(error, result)` is a function that is invoked with either an error
  66. or `null` and the result.
  67. The result is an object with the following properties:
  68. - `map`: The source map for `code`, as an object (not a string).
  69. - `url`: The url to the source map. If the source map came from a data uri,
  70. this property is `null`, since then there is no url to it.
  71. - `sourcesRelativeTo`: The url that the sources of the source map are relative
  72. to. Since the sources are relative to the source map, and the url to the
  73. source map is provided as the `url` property, this property might seem
  74. superfluos. However, remember that the `url` property can be `null` if the
  75. source map came from a data uri. If so, the sources are relative to the file
  76. containing the data uri—`codeUrl`. This property will be identical to the
  77. `url` property or `codeUrl`, whichever is appropriate. This way you can
  78. conveniently resolve the sources without having to think about where the
  79. source map came from.
  80. - `sourceMappingURL`: The url of the sourceMappingURL comment in `code`.
  81. If `code` contains no sourceMappingURL, the result is `null`.
  82. ### `sourceMapResolve.resolveSources(map, mapUrl, read, [options], callback)` ###
  83. - `map` is a source map, as an object (not a string).
  84. - `mapUrl` is the url to the file containing `map`. Relative sources in the
  85. source map, if any, are resolved against `mapUrl`.
  86. - `read(url, callback)` is a function that reads `url` and responds using
  87. `callback(error, content)`. In Node.js you might want to use `fs.readFile`,
  88. while in the browser you might want to use an asynchronus `XMLHttpRequest`.
  89. - `options` is an optional object with any of the following properties:
  90. - `sourceRoot`: Override the `sourceRoot` property of the source map, which
  91. might only be relevant when resolving sources in the browser. This lets you
  92. bypass it when using the module outside of a browser, if needed. Pass a
  93. string to replace the `sourceRoot` property with, or `false` to ignore it.
  94. Defaults to `undefined`.
  95. - `callback(error, result)` is a function that is invoked with either an error
  96. or `null` and the result.
  97. The result is an object with the following properties:
  98. - `sourcesResolved`: The same as `map.sources`, except all the sources are
  99. fully resolved.
  100. - `sourcesContent`: An array with the contents of all sources in `map.sources`,
  101. in the same order as `map.sources`. If getting the contents of a source fails,
  102. an error object is put into the array instead.
  103. ### `sourceMapResolve.resolve(code, codeUrl, read, [options], callback)` ###
  104. The arguments are identical to `sourceMapResolve.resolveSourceMap`, except that
  105. you may also provide the same `options` as in `sourceMapResolve.resolveSources`.
  106. This is a convenience method that first resolves the source map and then its
  107. sources. You could also do this by first calling
  108. `sourceMapResolve.resolveSourceMap` and then `sourceMapResolve.resolveSources`.
  109. The result is identical to `sourceMapResolve.resolveSourceMap`, with the
  110. properties from `sourceMapResolve.resolveSources` merged into it.
  111. There is one extra feature available, though. If `code` is `null`, `codeUrl` is
  112. treated as a url to the source map instead of to `code`, and will be read. This
  113. is handy if you _sometimes_ get the source map url from the `SourceMap: <url>`
  114. header (see the [Notes] section). In this case, the `sourceMappingURL` property
  115. of the result is `null`.
  116. [Notes]: #notes
  117. ### `sourceMapResolve.*Sync()` ###
  118. There are also sync versions of the three previous functions. They are identical
  119. to the async versions, except:
  120. - They expect a sync reading function. In Node.js you might want to use
  121. `fs.readFileSync`, while in the browser you might want to use a synchronus
  122. `XMLHttpRequest`.
  123. - They throw errors and return the result instead of using a callback.
  124. `sourceMapResolve.resolveSourcesSync` also accepts `null` as the `read`
  125. parameter. The result is the same as when passing a function as the `read
  126. parameter`, except that the `sourcesContent` property of the result will be an
  127. empty array. In other words, the sources aren’t read. You only get the
  128. `sourcesResolved` property. (This only supported in the synchronus version, since
  129. there is no point doing it asynchronusly.)
  130. ### `sourceMapResolve.parseMapToJSON(string, [data])` ###
  131. The spec says that if a source map (as a string) starts with `)]}'`, it should
  132. be stripped off. This is to prevent XSSI attacks. This function does that and
  133. returns the result of `JSON.parse`ing what’s left.
  134. If this function throws `error`, `error.sourceMapData === data`.
  135. ### Errors
  136. All errors passed to callbacks or thrown by this module have a `sourceMapData`
  137. property that contain as much as possible of the intended result of the function
  138. up until the error occurred.
  139. Note that while the `map` property of result objects always is an object,
  140. `error.sourceMapData.map` will be a string if parsing that string fails.
  141. Note
  142. ====
  143. This module resolves the source map for a given generated file by looking for a
  144. sourceMappingURL comment. The spec defines yet a way to provide the URL to the
  145. source map: By sending the `SourceMap: <url>` header along with the generated
  146. file. Since this module doesn’t retrive the generated code for you (instead
  147. _you_ give the generated code to the module), it’s up to you to look for such a
  148. header when you retrieve the file (should the need arise).
  149. License
  150. =======
  151. [MIT](LICENSE).