1 | # sourcemap-codec
|
---|
2 |
|
---|
3 | Encode/decode the `mappings` property of a [sourcemap](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit).
|
---|
4 |
|
---|
5 |
|
---|
6 | ## Why?
|
---|
7 |
|
---|
8 | Sourcemaps are difficult to generate and manipulate, because the `mappings` property – the part that actually links the generated code back to the original source – is encoded using an obscure method called [Variable-length quantity](https://en.wikipedia.org/wiki/Variable-length_quantity). On top of that, each segment in the mapping contains offsets rather than absolute indices, which means that you can't look at a segment in isolation – you have to understand the whole sourcemap.
|
---|
9 |
|
---|
10 | This package makes the process slightly easier.
|
---|
11 |
|
---|
12 |
|
---|
13 | ## Installation
|
---|
14 |
|
---|
15 | ```bash
|
---|
16 | npm install sourcemap-codec
|
---|
17 | ```
|
---|
18 |
|
---|
19 |
|
---|
20 | ## Usage
|
---|
21 |
|
---|
22 | ```js
|
---|
23 | import { encode, decode } from 'sourcemap-codec';
|
---|
24 |
|
---|
25 | var decoded = decode( ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );
|
---|
26 |
|
---|
27 | assert.deepEqual( decoded, [
|
---|
28 | // the first line (of the generated code) has no mappings,
|
---|
29 | // as shown by the starting semi-colon (which separates lines)
|
---|
30 | [],
|
---|
31 |
|
---|
32 | // the second line contains four (comma-separated) segments
|
---|
33 | [
|
---|
34 | // segments are encoded as you'd expect:
|
---|
35 | // [ generatedCodeColumn, sourceIndex, sourceCodeLine, sourceCodeColumn, nameIndex ]
|
---|
36 |
|
---|
37 | // i.e. the first segment begins at column 2, and maps back to the second column
|
---|
38 | // of the second line (both zero-based) of the 0th source, and uses the 0th
|
---|
39 | // name in the `map.names` array
|
---|
40 | [ 2, 0, 2, 2, 0 ],
|
---|
41 |
|
---|
42 | // the remaining segments are 4-length rather than 5-length,
|
---|
43 | // because they don't map a name
|
---|
44 | [ 4, 0, 2, 4 ],
|
---|
45 | [ 6, 0, 2, 5 ],
|
---|
46 | [ 7, 0, 2, 7 ]
|
---|
47 | ],
|
---|
48 |
|
---|
49 | // the final line contains two segments
|
---|
50 | [
|
---|
51 | [ 2, 1, 10, 19 ],
|
---|
52 | [ 12, 1, 11, 20 ]
|
---|
53 | ]
|
---|
54 | ]);
|
---|
55 |
|
---|
56 | var encoded = encode( decoded );
|
---|
57 | assert.equal( encoded, ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );
|
---|
58 | ```
|
---|
59 |
|
---|
60 |
|
---|
61 | # License
|
---|
62 |
|
---|
63 | MIT
|
---|