source: trip-planner-front/node_modules/source-map-resolve/readme.md@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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