1 | # Source Map
|
---|
2 |
|
---|
3 | [![Build Status](https://travis-ci.org/mozilla/source-map.png?branch=master)](https://travis-ci.org/mozilla/source-map)
|
---|
4 |
|
---|
5 | [![Coverage Status](https://coveralls.io/repos/github/mozilla/source-map/badge.svg)](https://coveralls.io/github/mozilla/source-map)
|
---|
6 |
|
---|
7 | [![NPM](https://nodei.co/npm/source-map.png?downloads=true&downloadRank=true)](https://www.npmjs.com/package/source-map)
|
---|
8 |
|
---|
9 | This is a library to generate and consume the source map format
|
---|
10 | [described here][format].
|
---|
11 |
|
---|
12 | [format]: https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
|
---|
13 |
|
---|
14 | ## Use with Node
|
---|
15 |
|
---|
16 | $ npm install source-map
|
---|
17 |
|
---|
18 | ## Use on the Web
|
---|
19 |
|
---|
20 | <script src="https://unpkg.com/source-map@0.7.3/dist/source-map.js"></script>
|
---|
21 | <script>
|
---|
22 | sourceMap.SourceMapConsumer.initialize({
|
---|
23 | "lib/mappings.wasm": "https://unpkg.com/source-map@0.7.3/lib/mappings.wasm"
|
---|
24 | });
|
---|
25 | </script>
|
---|
26 |
|
---|
27 | --------------------------------------------------------------------------------
|
---|
28 |
|
---|
29 | <!-- `npm run toc` to regenerate the Table of Contents -->
|
---|
30 |
|
---|
31 | <!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
---|
32 | <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
---|
33 | ## Table of Contents
|
---|
34 |
|
---|
35 | - [Examples](#examples)
|
---|
36 | - [Consuming a source map](#consuming-a-source-map)
|
---|
37 | - [Generating a source map](#generating-a-source-map)
|
---|
38 | - [With SourceNode (high level API)](#with-sourcenode-high-level-api)
|
---|
39 | - [With SourceMapGenerator (low level API)](#with-sourcemapgenerator-low-level-api)
|
---|
40 | - [API](#api)
|
---|
41 | - [SourceMapConsumer](#sourcemapconsumer)
|
---|
42 | - [SourceMapConsumer.initialize(options)](#sourcemapconsumerinitializeoptions)
|
---|
43 | - [new SourceMapConsumer(rawSourceMap)](#new-sourcemapconsumerrawsourcemap)
|
---|
44 | - [SourceMapConsumer.with](#sourcemapconsumerwith)
|
---|
45 | - [SourceMapConsumer.prototype.destroy()](#sourcemapconsumerprototypedestroy)
|
---|
46 | - [SourceMapConsumer.prototype.computeColumnSpans()](#sourcemapconsumerprototypecomputecolumnspans)
|
---|
47 | - [SourceMapConsumer.prototype.originalPositionFor(generatedPosition)](#sourcemapconsumerprototypeoriginalpositionforgeneratedposition)
|
---|
48 | - [SourceMapConsumer.prototype.generatedPositionFor(originalPosition)](#sourcemapconsumerprototypegeneratedpositionfororiginalposition)
|
---|
49 | - [SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)](#sourcemapconsumerprototypeallgeneratedpositionsfororiginalposition)
|
---|
50 | - [SourceMapConsumer.prototype.hasContentsOfAllSources()](#sourcemapconsumerprototypehascontentsofallsources)
|
---|
51 | - [SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing])](#sourcemapconsumerprototypesourcecontentforsource-returnnullonmissing)
|
---|
52 | - [SourceMapConsumer.prototype.eachMapping(callback, context, order)](#sourcemapconsumerprototypeeachmappingcallback-context-order)
|
---|
53 | - [SourceMapGenerator](#sourcemapgenerator)
|
---|
54 | - [new SourceMapGenerator([startOfSourceMap])](#new-sourcemapgeneratorstartofsourcemap)
|
---|
55 | - [SourceMapGenerator.fromSourceMap(sourceMapConsumer)](#sourcemapgeneratorfromsourcemapsourcemapconsumer)
|
---|
56 | - [SourceMapGenerator.prototype.addMapping(mapping)](#sourcemapgeneratorprototypeaddmappingmapping)
|
---|
57 | - [SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)](#sourcemapgeneratorprototypesetsourcecontentsourcefile-sourcecontent)
|
---|
58 | - [SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]])](#sourcemapgeneratorprototypeapplysourcemapsourcemapconsumer-sourcefile-sourcemappath)
|
---|
59 | - [SourceMapGenerator.prototype.toString()](#sourcemapgeneratorprototypetostring)
|
---|
60 | - [SourceNode](#sourcenode)
|
---|
61 | - [new SourceNode([line, column, source[, chunk[, name]]])](#new-sourcenodeline-column-source-chunk-name)
|
---|
62 | - [SourceNode.fromStringWithSourceMap(code, sourceMapConsumer[, relativePath])](#sourcenodefromstringwithsourcemapcode-sourcemapconsumer-relativepath)
|
---|
63 | - [SourceNode.prototype.add(chunk)](#sourcenodeprototypeaddchunk)
|
---|
64 | - [SourceNode.prototype.prepend(chunk)](#sourcenodeprototypeprependchunk)
|
---|
65 | - [SourceNode.prototype.setSourceContent(sourceFile, sourceContent)](#sourcenodeprototypesetsourcecontentsourcefile-sourcecontent)
|
---|
66 | - [SourceNode.prototype.walk(fn)](#sourcenodeprototypewalkfn)
|
---|
67 | - [SourceNode.prototype.walkSourceContents(fn)](#sourcenodeprototypewalksourcecontentsfn)
|
---|
68 | - [SourceNode.prototype.join(sep)](#sourcenodeprototypejoinsep)
|
---|
69 | - [SourceNode.prototype.replaceRight(pattern, replacement)](#sourcenodeprototypereplacerightpattern-replacement)
|
---|
70 | - [SourceNode.prototype.toString()](#sourcenodeprototypetostring)
|
---|
71 | - [SourceNode.prototype.toStringWithSourceMap([startOfSourceMap])](#sourcenodeprototypetostringwithsourcemapstartofsourcemap)
|
---|
72 |
|
---|
73 | <!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
---|
74 |
|
---|
75 | ## Examples
|
---|
76 |
|
---|
77 | ### Consuming a source map
|
---|
78 |
|
---|
79 | ```js
|
---|
80 | const rawSourceMap = {
|
---|
81 | version: 3,
|
---|
82 | file: 'min.js',
|
---|
83 | names: ['bar', 'baz', 'n'],
|
---|
84 | sources: ['one.js', 'two.js'],
|
---|
85 | sourceRoot: 'http://example.com/www/js/',
|
---|
86 | mappings: 'CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA'
|
---|
87 | };
|
---|
88 |
|
---|
89 | const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
|
---|
90 |
|
---|
91 | console.log(consumer.sources);
|
---|
92 | // [ 'http://example.com/www/js/one.js',
|
---|
93 | // 'http://example.com/www/js/two.js' ]
|
---|
94 |
|
---|
95 | console.log(consumer.originalPositionFor({
|
---|
96 | line: 2,
|
---|
97 | column: 28
|
---|
98 | }));
|
---|
99 | // { source: 'http://example.com/www/js/two.js',
|
---|
100 | // line: 2,
|
---|
101 | // column: 10,
|
---|
102 | // name: 'n' }
|
---|
103 |
|
---|
104 | console.log(consumer.generatedPositionFor({
|
---|
105 | source: 'http://example.com/www/js/two.js',
|
---|
106 | line: 2,
|
---|
107 | column: 10
|
---|
108 | }));
|
---|
109 | // { line: 2, column: 28 }
|
---|
110 |
|
---|
111 | consumer.eachMapping(function (m) {
|
---|
112 | // ...
|
---|
113 | });
|
---|
114 |
|
---|
115 | return computeWhatever();
|
---|
116 | });
|
---|
117 | ```
|
---|
118 |
|
---|
119 | ### Generating a source map
|
---|
120 |
|
---|
121 | In depth guide:
|
---|
122 | [**Compiling to JavaScript, and Debugging with Source Maps**](https://hacks.mozilla.org/2013/05/compiling-to-javascript-and-debugging-with-source-maps/)
|
---|
123 |
|
---|
124 | #### With SourceNode (high level API)
|
---|
125 |
|
---|
126 | ```js
|
---|
127 | function compile(ast) {
|
---|
128 | switch (ast.type) {
|
---|
129 | case 'BinaryExpression':
|
---|
130 | return new SourceNode(
|
---|
131 | ast.location.line,
|
---|
132 | ast.location.column,
|
---|
133 | ast.location.source,
|
---|
134 | [compile(ast.left), " + ", compile(ast.right)]
|
---|
135 | );
|
---|
136 | case 'Literal':
|
---|
137 | return new SourceNode(
|
---|
138 | ast.location.line,
|
---|
139 | ast.location.column,
|
---|
140 | ast.location.source,
|
---|
141 | String(ast.value)
|
---|
142 | );
|
---|
143 | // ...
|
---|
144 | default:
|
---|
145 | throw new Error("Bad AST");
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | var ast = parse("40 + 2", "add.js");
|
---|
150 | console.log(compile(ast).toStringWithSourceMap({
|
---|
151 | file: 'add.js'
|
---|
152 | }));
|
---|
153 | // { code: '40 + 2',
|
---|
154 | // map: [object SourceMapGenerator] }
|
---|
155 | ```
|
---|
156 |
|
---|
157 | #### With SourceMapGenerator (low level API)
|
---|
158 |
|
---|
159 | ```js
|
---|
160 | var map = new SourceMapGenerator({
|
---|
161 | file: "source-mapped.js"
|
---|
162 | });
|
---|
163 |
|
---|
164 | map.addMapping({
|
---|
165 | generated: {
|
---|
166 | line: 10,
|
---|
167 | column: 35
|
---|
168 | },
|
---|
169 | source: "foo.js",
|
---|
170 | original: {
|
---|
171 | line: 33,
|
---|
172 | column: 2
|
---|
173 | },
|
---|
174 | name: "christopher"
|
---|
175 | });
|
---|
176 |
|
---|
177 | console.log(map.toString());
|
---|
178 | // '{"version":3,"file":"source-mapped.js","sources":["foo.js"],"names":["christopher"],"mappings":";;;;;;;;;mCAgCEA"}'
|
---|
179 | ```
|
---|
180 |
|
---|
181 | ## API
|
---|
182 |
|
---|
183 | Get a reference to the module:
|
---|
184 |
|
---|
185 | ```js
|
---|
186 | // Node.js
|
---|
187 | var sourceMap = require('source-map');
|
---|
188 |
|
---|
189 | // Browser builds
|
---|
190 | var sourceMap = window.sourceMap;
|
---|
191 |
|
---|
192 | // Inside Firefox
|
---|
193 | const sourceMap = require("devtools/toolkit/sourcemap/source-map.js");
|
---|
194 | ```
|
---|
195 |
|
---|
196 | ### SourceMapConsumer
|
---|
197 |
|
---|
198 | A `SourceMapConsumer` instance represents a parsed source map which we can query
|
---|
199 | for information about the original file positions by giving it a file position
|
---|
200 | in the generated source.
|
---|
201 |
|
---|
202 | #### SourceMapConsumer.initialize(options)
|
---|
203 |
|
---|
204 | When using `SourceMapConsumer` outside of node.js, for example on the Web, it
|
---|
205 | needs to know from what URL to load `lib/mappings.wasm`. You must inform it by
|
---|
206 | calling `initialize` before constructing any `SourceMapConsumer`s.
|
---|
207 |
|
---|
208 | The options object has the following properties:
|
---|
209 |
|
---|
210 | * `"lib/mappings.wasm"`: A `String` containing the URL of the
|
---|
211 | `lib/mappings.wasm` file.
|
---|
212 |
|
---|
213 | ```js
|
---|
214 | sourceMap.SourceMapConsumer.initialize({
|
---|
215 | "lib/mappings.wasm": "https://example.com/source-map/lib/mappings.wasm"
|
---|
216 | });
|
---|
217 | ```
|
---|
218 |
|
---|
219 | #### new SourceMapConsumer(rawSourceMap)
|
---|
220 |
|
---|
221 | The only parameter is the raw source map (either as a string which can be
|
---|
222 | `JSON.parse`'d, or an object). According to the spec, source maps have the
|
---|
223 | following attributes:
|
---|
224 |
|
---|
225 | * `version`: Which version of the source map spec this map is following.
|
---|
226 |
|
---|
227 | * `sources`: An array of URLs to the original source files.
|
---|
228 |
|
---|
229 | * `names`: An array of identifiers which can be referenced by individual
|
---|
230 | mappings.
|
---|
231 |
|
---|
232 | * `sourceRoot`: Optional. The URL root from which all sources are relative.
|
---|
233 |
|
---|
234 | * `sourcesContent`: Optional. An array of contents of the original source files.
|
---|
235 |
|
---|
236 | * `mappings`: A string of base64 VLQs which contain the actual mappings.
|
---|
237 |
|
---|
238 | * `file`: Optional. The generated filename this source map is associated with.
|
---|
239 |
|
---|
240 | The promise of the constructed souce map consumer is returned.
|
---|
241 |
|
---|
242 | When the `SourceMapConsumer` will no longer be used anymore, you must call its
|
---|
243 | `destroy` method.
|
---|
244 |
|
---|
245 | ```js
|
---|
246 | const consumer = await new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
|
---|
247 | doStuffWith(consumer);
|
---|
248 | consumer.destroy();
|
---|
249 | ```
|
---|
250 |
|
---|
251 | Alternatively, you can use `SourceMapConsumer.with` to avoid needing to remember
|
---|
252 | to call `destroy`.
|
---|
253 |
|
---|
254 | #### SourceMapConsumer.with
|
---|
255 |
|
---|
256 | Construct a new `SourceMapConsumer` from `rawSourceMap` and `sourceMapUrl`
|
---|
257 | (see the `SourceMapConsumer` constructor for details. Then, invoke the `async
|
---|
258 | function f(SourceMapConsumer) -> T` with the newly constructed consumer, wait
|
---|
259 | for `f` to complete, call `destroy` on the consumer, and return `f`'s return
|
---|
260 | value.
|
---|
261 |
|
---|
262 | You must not use the consumer after `f` completes!
|
---|
263 |
|
---|
264 | By using `with`, you do not have to remember to manually call `destroy` on
|
---|
265 | the consumer, since it will be called automatically once `f` completes.
|
---|
266 |
|
---|
267 | ```js
|
---|
268 | const xSquared = await SourceMapConsumer.with(
|
---|
269 | myRawSourceMap,
|
---|
270 | null,
|
---|
271 | async function (consumer) {
|
---|
272 | // Use `consumer` inside here and don't worry about remembering
|
---|
273 | // to call `destroy`.
|
---|
274 |
|
---|
275 | const x = await whatever(consumer);
|
---|
276 | return x * x;
|
---|
277 | }
|
---|
278 | );
|
---|
279 |
|
---|
280 | // You may not use that `consumer` anymore out here; it has
|
---|
281 | // been destroyed. But you can use `xSquared`.
|
---|
282 | console.log(xSquared);
|
---|
283 | ```
|
---|
284 |
|
---|
285 | #### SourceMapConsumer.prototype.destroy()
|
---|
286 |
|
---|
287 | Free this source map consumer's associated wasm data that is manually-managed.
|
---|
288 |
|
---|
289 | ```js
|
---|
290 | consumer.destroy();
|
---|
291 | ```
|
---|
292 |
|
---|
293 | Alternatively, you can use `SourceMapConsumer.with` to avoid needing to remember
|
---|
294 | to call `destroy`.
|
---|
295 |
|
---|
296 | #### SourceMapConsumer.prototype.computeColumnSpans()
|
---|
297 |
|
---|
298 | Compute the last column for each generated mapping. The last column is
|
---|
299 | inclusive.
|
---|
300 |
|
---|
301 | ```js
|
---|
302 | // Before:
|
---|
303 | consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" })
|
---|
304 | // [ { line: 2,
|
---|
305 | // column: 1 },
|
---|
306 | // { line: 2,
|
---|
307 | // column: 10 },
|
---|
308 | // { line: 2,
|
---|
309 | // column: 20 } ]
|
---|
310 |
|
---|
311 | consumer.computeColumnSpans();
|
---|
312 |
|
---|
313 | // After:
|
---|
314 | consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" })
|
---|
315 | // [ { line: 2,
|
---|
316 | // column: 1,
|
---|
317 | // lastColumn: 9 },
|
---|
318 | // { line: 2,
|
---|
319 | // column: 10,
|
---|
320 | // lastColumn: 19 },
|
---|
321 | // { line: 2,
|
---|
322 | // column: 20,
|
---|
323 | // lastColumn: Infinity } ]
|
---|
324 | ```
|
---|
325 |
|
---|
326 | #### SourceMapConsumer.prototype.originalPositionFor(generatedPosition)
|
---|
327 |
|
---|
328 | Returns the original source, line, and column information for the generated
|
---|
329 | source's line and column positions provided. The only argument is an object with
|
---|
330 | the following properties:
|
---|
331 |
|
---|
332 | * `line`: The line number in the generated source. Line numbers in
|
---|
333 | this library are 1-based (note that the underlying source map
|
---|
334 | specification uses 0-based line numbers -- this library handles the
|
---|
335 | translation).
|
---|
336 |
|
---|
337 | * `column`: The column number in the generated source. Column numbers
|
---|
338 | in this library are 0-based.
|
---|
339 |
|
---|
340 | * `bias`: Either `SourceMapConsumer.GREATEST_LOWER_BOUND` or
|
---|
341 | `SourceMapConsumer.LEAST_UPPER_BOUND`. Specifies whether to return the closest
|
---|
342 | element that is smaller than or greater than the one we are searching for,
|
---|
343 | respectively, if the exact element cannot be found. Defaults to
|
---|
344 | `SourceMapConsumer.GREATEST_LOWER_BOUND`.
|
---|
345 |
|
---|
346 | and an object is returned with the following properties:
|
---|
347 |
|
---|
348 | * `source`: The original source file, or null if this information is not
|
---|
349 | available.
|
---|
350 |
|
---|
351 | * `line`: The line number in the original source, or null if this information is
|
---|
352 | not available. The line number is 1-based.
|
---|
353 |
|
---|
354 | * `column`: The column number in the original source, or null if this
|
---|
355 | information is not available. The column number is 0-based.
|
---|
356 |
|
---|
357 | * `name`: The original identifier, or null if this information is not available.
|
---|
358 |
|
---|
359 | ```js
|
---|
360 | consumer.originalPositionFor({ line: 2, column: 10 })
|
---|
361 | // { source: 'foo.coffee',
|
---|
362 | // line: 2,
|
---|
363 | // column: 2,
|
---|
364 | // name: null }
|
---|
365 |
|
---|
366 | consumer.originalPositionFor({ line: 99999999999999999, column: 999999999999999 })
|
---|
367 | // { source: null,
|
---|
368 | // line: null,
|
---|
369 | // column: null,
|
---|
370 | // name: null }
|
---|
371 | ```
|
---|
372 |
|
---|
373 | #### SourceMapConsumer.prototype.generatedPositionFor(originalPosition)
|
---|
374 |
|
---|
375 | Returns the generated line and column information for the original source,
|
---|
376 | line, and column positions provided. The only argument is an object with
|
---|
377 | the following properties:
|
---|
378 |
|
---|
379 | * `source`: The filename of the original source.
|
---|
380 |
|
---|
381 | * `line`: The line number in the original source. The line number is
|
---|
382 | 1-based.
|
---|
383 |
|
---|
384 | * `column`: The column number in the original source. The column
|
---|
385 | number is 0-based.
|
---|
386 |
|
---|
387 | and an object is returned with the following properties:
|
---|
388 |
|
---|
389 | * `line`: The line number in the generated source, or null. The line
|
---|
390 | number is 1-based.
|
---|
391 |
|
---|
392 | * `column`: The column number in the generated source, or null. The
|
---|
393 | column number is 0-based.
|
---|
394 |
|
---|
395 | ```js
|
---|
396 | consumer.generatedPositionFor({ source: "example.js", line: 2, column: 10 })
|
---|
397 | // { line: 1,
|
---|
398 | // column: 56 }
|
---|
399 | ```
|
---|
400 |
|
---|
401 | #### SourceMapConsumer.prototype.allGeneratedPositionsFor(originalPosition)
|
---|
402 |
|
---|
403 | Returns all generated line and column information for the original source, line,
|
---|
404 | and column provided. If no column is provided, returns all mappings
|
---|
405 | corresponding to a either the line we are searching for or the next closest line
|
---|
406 | that has any mappings. Otherwise, returns all mappings corresponding to the
|
---|
407 | given line and either the column we are searching for or the next closest column
|
---|
408 | that has any offsets.
|
---|
409 |
|
---|
410 | The only argument is an object with the following properties:
|
---|
411 |
|
---|
412 | * `source`: The filename of the original source.
|
---|
413 |
|
---|
414 | * `line`: The line number in the original source. The line number is
|
---|
415 | 1-based.
|
---|
416 |
|
---|
417 | * `column`: Optional. The column number in the original source. The
|
---|
418 | column number is 0-based.
|
---|
419 |
|
---|
420 | and an array of objects is returned, each with the following properties:
|
---|
421 |
|
---|
422 | * `line`: The line number in the generated source, or null. The line
|
---|
423 | number is 1-based.
|
---|
424 |
|
---|
425 | * `column`: The column number in the generated source, or null. The
|
---|
426 | column number is 0-based.
|
---|
427 |
|
---|
428 | ```js
|
---|
429 | consumer.allGeneratedpositionsfor({ line: 2, source: "foo.coffee" })
|
---|
430 | // [ { line: 2,
|
---|
431 | // column: 1 },
|
---|
432 | // { line: 2,
|
---|
433 | // column: 10 },
|
---|
434 | // { line: 2,
|
---|
435 | // column: 20 } ]
|
---|
436 | ```
|
---|
437 |
|
---|
438 | #### SourceMapConsumer.prototype.hasContentsOfAllSources()
|
---|
439 |
|
---|
440 | Return true if we have the embedded source content for every source listed in
|
---|
441 | the source map, false otherwise.
|
---|
442 |
|
---|
443 | In other words, if this method returns `true`, then
|
---|
444 | `consumer.sourceContentFor(s)` will succeed for every source `s` in
|
---|
445 | `consumer.sources`.
|
---|
446 |
|
---|
447 | ```js
|
---|
448 | // ...
|
---|
449 | if (consumer.hasContentsOfAllSources()) {
|
---|
450 | consumerReadyCallback(consumer);
|
---|
451 | } else {
|
---|
452 | fetchSources(consumer, consumerReadyCallback);
|
---|
453 | }
|
---|
454 | // ...
|
---|
455 | ```
|
---|
456 |
|
---|
457 | #### SourceMapConsumer.prototype.sourceContentFor(source[, returnNullOnMissing])
|
---|
458 |
|
---|
459 | Returns the original source content for the source provided. The only
|
---|
460 | argument is the URL of the original source file.
|
---|
461 |
|
---|
462 | If the source content for the given source is not found, then an error is
|
---|
463 | thrown. Optionally, pass `true` as the second param to have `null` returned
|
---|
464 | instead.
|
---|
465 |
|
---|
466 | ```js
|
---|
467 | consumer.sources
|
---|
468 | // [ "my-cool-lib.clj" ]
|
---|
469 |
|
---|
470 | consumer.sourceContentFor("my-cool-lib.clj")
|
---|
471 | // "..."
|
---|
472 |
|
---|
473 | consumer.sourceContentFor("this is not in the source map");
|
---|
474 | // Error: "this is not in the source map" is not in the source map
|
---|
475 |
|
---|
476 | consumer.sourceContentFor("this is not in the source map", true);
|
---|
477 | // null
|
---|
478 | ```
|
---|
479 |
|
---|
480 | #### SourceMapConsumer.prototype.eachMapping(callback, context, order)
|
---|
481 |
|
---|
482 | Iterate over each mapping between an original source/line/column and a
|
---|
483 | generated line/column in this source map.
|
---|
484 |
|
---|
485 | * `callback`: The function that is called with each mapping. Mappings have the
|
---|
486 | form `{ source, generatedLine, generatedColumn, originalLine, originalColumn,
|
---|
487 | name }`
|
---|
488 |
|
---|
489 | * `context`: Optional. If specified, this object will be the value of `this`
|
---|
490 | every time that `callback` is called.
|
---|
491 |
|
---|
492 | * `order`: Either `SourceMapConsumer.GENERATED_ORDER` or
|
---|
493 | `SourceMapConsumer.ORIGINAL_ORDER`. Specifies whether you want to iterate over
|
---|
494 | the mappings sorted by the generated file's line/column order or the
|
---|
495 | original's source/line/column order, respectively. Defaults to
|
---|
496 | `SourceMapConsumer.GENERATED_ORDER`.
|
---|
497 |
|
---|
498 | ```js
|
---|
499 | consumer.eachMapping(function (m) { console.log(m); })
|
---|
500 | // ...
|
---|
501 | // { source: 'illmatic.js',
|
---|
502 | // generatedLine: 1,
|
---|
503 | // generatedColumn: 0,
|
---|
504 | // originalLine: 1,
|
---|
505 | // originalColumn: 0,
|
---|
506 | // name: null }
|
---|
507 | // { source: 'illmatic.js',
|
---|
508 | // generatedLine: 2,
|
---|
509 | // generatedColumn: 0,
|
---|
510 | // originalLine: 2,
|
---|
511 | // originalColumn: 0,
|
---|
512 | // name: null }
|
---|
513 | // ...
|
---|
514 | ```
|
---|
515 | ### SourceMapGenerator
|
---|
516 |
|
---|
517 | An instance of the SourceMapGenerator represents a source map which is being
|
---|
518 | built incrementally.
|
---|
519 |
|
---|
520 | #### new SourceMapGenerator([startOfSourceMap])
|
---|
521 |
|
---|
522 | You may pass an object with the following properties:
|
---|
523 |
|
---|
524 | * `file`: The filename of the generated source that this source map is
|
---|
525 | associated with.
|
---|
526 |
|
---|
527 | * `sourceRoot`: A root for all relative URLs in this source map.
|
---|
528 |
|
---|
529 | * `skipValidation`: Optional. When `true`, disables validation of mappings as
|
---|
530 | they are added. This can improve performance but should be used with
|
---|
531 | discretion, as a last resort. Even then, one should avoid using this flag when
|
---|
532 | running tests, if possible.
|
---|
533 |
|
---|
534 | ```js
|
---|
535 | var generator = new sourceMap.SourceMapGenerator({
|
---|
536 | file: "my-generated-javascript-file.js",
|
---|
537 | sourceRoot: "http://example.com/app/js/"
|
---|
538 | });
|
---|
539 | ```
|
---|
540 |
|
---|
541 | #### SourceMapGenerator.fromSourceMap(sourceMapConsumer)
|
---|
542 |
|
---|
543 | Creates a new `SourceMapGenerator` from an existing `SourceMapConsumer` instance.
|
---|
544 |
|
---|
545 | * `sourceMapConsumer` The SourceMap.
|
---|
546 |
|
---|
547 | ```js
|
---|
548 | var generator = sourceMap.SourceMapGenerator.fromSourceMap(consumer);
|
---|
549 | ```
|
---|
550 |
|
---|
551 | #### SourceMapGenerator.prototype.addMapping(mapping)
|
---|
552 |
|
---|
553 | Add a single mapping from original source line and column to the generated
|
---|
554 | source's line and column for this source map being created. The mapping object
|
---|
555 | should have the following properties:
|
---|
556 |
|
---|
557 | * `generated`: An object with the generated line and column positions.
|
---|
558 |
|
---|
559 | * `original`: An object with the original line and column positions.
|
---|
560 |
|
---|
561 | * `source`: The original source file (relative to the sourceRoot).
|
---|
562 |
|
---|
563 | * `name`: An optional original token name for this mapping.
|
---|
564 |
|
---|
565 | ```js
|
---|
566 | generator.addMapping({
|
---|
567 | source: "module-one.scm",
|
---|
568 | original: { line: 128, column: 0 },
|
---|
569 | generated: { line: 3, column: 456 }
|
---|
570 | })
|
---|
571 | ```
|
---|
572 |
|
---|
573 | #### SourceMapGenerator.prototype.setSourceContent(sourceFile, sourceContent)
|
---|
574 |
|
---|
575 | Set the source content for an original source file.
|
---|
576 |
|
---|
577 | * `sourceFile` the URL of the original source file.
|
---|
578 |
|
---|
579 | * `sourceContent` the content of the source file.
|
---|
580 |
|
---|
581 | ```js
|
---|
582 | generator.setSourceContent("module-one.scm",
|
---|
583 | fs.readFileSync("path/to/module-one.scm"))
|
---|
584 | ```
|
---|
585 |
|
---|
586 | #### SourceMapGenerator.prototype.applySourceMap(sourceMapConsumer[, sourceFile[, sourceMapPath]])
|
---|
587 |
|
---|
588 | Applies a SourceMap for a source file to the SourceMap.
|
---|
589 | Each mapping to the supplied source file is rewritten using the
|
---|
590 | supplied SourceMap. Note: The resolution for the resulting mappings
|
---|
591 | is the minimum of this map and the supplied map.
|
---|
592 |
|
---|
593 | * `sourceMapConsumer`: The SourceMap to be applied.
|
---|
594 |
|
---|
595 | * `sourceFile`: Optional. The filename of the source file.
|
---|
596 | If omitted, sourceMapConsumer.file will be used, if it exists.
|
---|
597 | Otherwise an error will be thrown.
|
---|
598 |
|
---|
599 | * `sourceMapPath`: Optional. The dirname of the path to the SourceMap
|
---|
600 | to be applied. If relative, it is relative to the SourceMap.
|
---|
601 |
|
---|
602 | This parameter is needed when the two SourceMaps aren't in the same
|
---|
603 | directory, and the SourceMap to be applied contains relative source
|
---|
604 | paths. If so, those relative source paths need to be rewritten
|
---|
605 | relative to the SourceMap.
|
---|
606 |
|
---|
607 | If omitted, it is assumed that both SourceMaps are in the same directory,
|
---|
608 | thus not needing any rewriting. (Supplying `'.'` has the same effect.)
|
---|
609 |
|
---|
610 | #### SourceMapGenerator.prototype.toString()
|
---|
611 |
|
---|
612 | Renders the source map being generated to a string.
|
---|
613 |
|
---|
614 | ```js
|
---|
615 | generator.toString()
|
---|
616 | // '{"version":3,"sources":["module-one.scm"],"names":[],"mappings":"...snip...","file":"my-generated-javascript-file.js","sourceRoot":"http://example.com/app/js/"}'
|
---|
617 | ```
|
---|
618 |
|
---|
619 | ### SourceNode
|
---|
620 |
|
---|
621 | SourceNodes provide a way to abstract over interpolating and/or concatenating
|
---|
622 | snippets of generated JavaScript source code, while maintaining the line and
|
---|
623 | column information associated between those snippets and the original source
|
---|
624 | code. This is useful as the final intermediate representation a compiler might
|
---|
625 | use before outputting the generated JS and source map.
|
---|
626 |
|
---|
627 | #### new SourceNode([line, column, source[, chunk[, name]]])
|
---|
628 |
|
---|
629 | * `line`: The original line number associated with this source node, or null if
|
---|
630 | it isn't associated with an original line. The line number is 1-based.
|
---|
631 |
|
---|
632 | * `column`: The original column number associated with this source node, or null
|
---|
633 | if it isn't associated with an original column. The column number
|
---|
634 | is 0-based.
|
---|
635 |
|
---|
636 | * `source`: The original source's filename; null if no filename is provided.
|
---|
637 |
|
---|
638 | * `chunk`: Optional. Is immediately passed to `SourceNode.prototype.add`, see
|
---|
639 | below.
|
---|
640 |
|
---|
641 | * `name`: Optional. The original identifier.
|
---|
642 |
|
---|
643 | ```js
|
---|
644 | var node = new SourceNode(1, 2, "a.cpp", [
|
---|
645 | new SourceNode(3, 4, "b.cpp", "extern int status;\n"),
|
---|
646 | new SourceNode(5, 6, "c.cpp", "std::string* make_string(size_t n);\n"),
|
---|
647 | new SourceNode(7, 8, "d.cpp", "int main(int argc, char** argv) {}\n"),
|
---|
648 | ]);
|
---|
649 | ```
|
---|
650 |
|
---|
651 | #### SourceNode.fromStringWithSourceMap(code, sourceMapConsumer[, relativePath])
|
---|
652 |
|
---|
653 | Creates a SourceNode from generated code and a SourceMapConsumer.
|
---|
654 |
|
---|
655 | * `code`: The generated code
|
---|
656 |
|
---|
657 | * `sourceMapConsumer` The SourceMap for the generated code
|
---|
658 |
|
---|
659 | * `relativePath` The optional path that relative sources in `sourceMapConsumer`
|
---|
660 | should be relative to.
|
---|
661 |
|
---|
662 | ```js
|
---|
663 | const consumer = await new SourceMapConsumer(fs.readFileSync("path/to/my-file.js.map", "utf8"));
|
---|
664 | const node = SourceNode.fromStringWithSourceMap(fs.readFileSync("path/to/my-file.js"), consumer);
|
---|
665 | ```
|
---|
666 |
|
---|
667 | #### SourceNode.prototype.add(chunk)
|
---|
668 |
|
---|
669 | Add a chunk of generated JS to this source node.
|
---|
670 |
|
---|
671 | * `chunk`: A string snippet of generated JS code, another instance of
|
---|
672 | `SourceNode`, or an array where each member is one of those things.
|
---|
673 |
|
---|
674 | ```js
|
---|
675 | node.add(" + ");
|
---|
676 | node.add(otherNode);
|
---|
677 | node.add([leftHandOperandNode, " + ", rightHandOperandNode]);
|
---|
678 | ```
|
---|
679 |
|
---|
680 | #### SourceNode.prototype.prepend(chunk)
|
---|
681 |
|
---|
682 | Prepend a chunk of generated JS to this source node.
|
---|
683 |
|
---|
684 | * `chunk`: A string snippet of generated JS code, another instance of
|
---|
685 | `SourceNode`, or an array where each member is one of those things.
|
---|
686 |
|
---|
687 | ```js
|
---|
688 | node.prepend("/** Build Id: f783haef86324gf **/\n\n");
|
---|
689 | ```
|
---|
690 |
|
---|
691 | #### SourceNode.prototype.setSourceContent(sourceFile, sourceContent)
|
---|
692 |
|
---|
693 | Set the source content for a source file. This will be added to the
|
---|
694 | `SourceMap` in the `sourcesContent` field.
|
---|
695 |
|
---|
696 | * `sourceFile`: The filename of the source file
|
---|
697 |
|
---|
698 | * `sourceContent`: The content of the source file
|
---|
699 |
|
---|
700 | ```js
|
---|
701 | node.setSourceContent("module-one.scm",
|
---|
702 | fs.readFileSync("path/to/module-one.scm"))
|
---|
703 | ```
|
---|
704 |
|
---|
705 | #### SourceNode.prototype.walk(fn)
|
---|
706 |
|
---|
707 | Walk over the tree of JS snippets in this node and its children. The walking
|
---|
708 | function is called once for each snippet of JS and is passed that snippet and
|
---|
709 | the its original associated source's line/column location.
|
---|
710 |
|
---|
711 | * `fn`: The traversal function.
|
---|
712 |
|
---|
713 | ```js
|
---|
714 | var node = new SourceNode(1, 2, "a.js", [
|
---|
715 | new SourceNode(3, 4, "b.js", "uno"),
|
---|
716 | "dos",
|
---|
717 | [
|
---|
718 | "tres",
|
---|
719 | new SourceNode(5, 6, "c.js", "quatro")
|
---|
720 | ]
|
---|
721 | ]);
|
---|
722 |
|
---|
723 | node.walk(function (code, loc) { console.log("WALK:", code, loc); })
|
---|
724 | // WALK: uno { source: 'b.js', line: 3, column: 4, name: null }
|
---|
725 | // WALK: dos { source: 'a.js', line: 1, column: 2, name: null }
|
---|
726 | // WALK: tres { source: 'a.js', line: 1, column: 2, name: null }
|
---|
727 | // WALK: quatro { source: 'c.js', line: 5, column: 6, name: null }
|
---|
728 | ```
|
---|
729 |
|
---|
730 | #### SourceNode.prototype.walkSourceContents(fn)
|
---|
731 |
|
---|
732 | Walk over the tree of SourceNodes. The walking function is called for each
|
---|
733 | source file content and is passed the filename and source content.
|
---|
734 |
|
---|
735 | * `fn`: The traversal function.
|
---|
736 |
|
---|
737 | ```js
|
---|
738 | var a = new SourceNode(1, 2, "a.js", "generated from a");
|
---|
739 | a.setSourceContent("a.js", "original a");
|
---|
740 | var b = new SourceNode(1, 2, "b.js", "generated from b");
|
---|
741 | b.setSourceContent("b.js", "original b");
|
---|
742 | var c = new SourceNode(1, 2, "c.js", "generated from c");
|
---|
743 | c.setSourceContent("c.js", "original c");
|
---|
744 |
|
---|
745 | var node = new SourceNode(null, null, null, [a, b, c]);
|
---|
746 | node.walkSourceContents(function (source, contents) { console.log("WALK:", source, ":", contents); })
|
---|
747 | // WALK: a.js : original a
|
---|
748 | // WALK: b.js : original b
|
---|
749 | // WALK: c.js : original c
|
---|
750 | ```
|
---|
751 |
|
---|
752 | #### SourceNode.prototype.join(sep)
|
---|
753 |
|
---|
754 | Like `Array.prototype.join` except for SourceNodes. Inserts the separator
|
---|
755 | between each of this source node's children.
|
---|
756 |
|
---|
757 | * `sep`: The separator.
|
---|
758 |
|
---|
759 | ```js
|
---|
760 | var lhs = new SourceNode(1, 2, "a.rs", "my_copy");
|
---|
761 | var operand = new SourceNode(3, 4, "a.rs", "=");
|
---|
762 | var rhs = new SourceNode(5, 6, "a.rs", "orig.clone()");
|
---|
763 |
|
---|
764 | var node = new SourceNode(null, null, null, [ lhs, operand, rhs ]);
|
---|
765 | var joinedNode = node.join(" ");
|
---|
766 | ```
|
---|
767 |
|
---|
768 | #### SourceNode.prototype.replaceRight(pattern, replacement)
|
---|
769 |
|
---|
770 | Call `String.prototype.replace` on the very right-most source snippet. Useful
|
---|
771 | for trimming white space from the end of a source node, etc.
|
---|
772 |
|
---|
773 | * `pattern`: The pattern to replace.
|
---|
774 |
|
---|
775 | * `replacement`: The thing to replace the pattern with.
|
---|
776 |
|
---|
777 | ```js
|
---|
778 | // Trim trailing white space.
|
---|
779 | node.replaceRight(/\s*$/, "");
|
---|
780 | ```
|
---|
781 |
|
---|
782 | #### SourceNode.prototype.toString()
|
---|
783 |
|
---|
784 | Return the string representation of this source node. Walks over the tree and
|
---|
785 | concatenates all the various snippets together to one string.
|
---|
786 |
|
---|
787 | ```js
|
---|
788 | var node = new SourceNode(1, 2, "a.js", [
|
---|
789 | new SourceNode(3, 4, "b.js", "uno"),
|
---|
790 | "dos",
|
---|
791 | [
|
---|
792 | "tres",
|
---|
793 | new SourceNode(5, 6, "c.js", "quatro")
|
---|
794 | ]
|
---|
795 | ]);
|
---|
796 |
|
---|
797 | node.toString()
|
---|
798 | // 'unodostresquatro'
|
---|
799 | ```
|
---|
800 |
|
---|
801 | #### SourceNode.prototype.toStringWithSourceMap([startOfSourceMap])
|
---|
802 |
|
---|
803 | Returns the string representation of this tree of source nodes, plus a
|
---|
804 | SourceMapGenerator which contains all the mappings between the generated and
|
---|
805 | original sources.
|
---|
806 |
|
---|
807 | The arguments are the same as those to `new SourceMapGenerator`.
|
---|
808 |
|
---|
809 | ```js
|
---|
810 | var node = new SourceNode(1, 2, "a.js", [
|
---|
811 | new SourceNode(3, 4, "b.js", "uno"),
|
---|
812 | "dos",
|
---|
813 | [
|
---|
814 | "tres",
|
---|
815 | new SourceNode(5, 6, "c.js", "quatro")
|
---|
816 | ]
|
---|
817 | ]);
|
---|
818 |
|
---|
819 | node.toStringWithSourceMap({ file: "my-output-file.js" })
|
---|
820 | // { code: 'unodostresquatro',
|
---|
821 | // map: [object SourceMapGenerator] }
|
---|
822 | ```
|
---|