1 | # @jridgewell/trace-mapping
|
---|
2 |
|
---|
3 | > Trace the original position through a source map
|
---|
4 |
|
---|
5 | `trace-mapping` allows you to take the line and column of an output file and trace it to the
|
---|
6 | original location in the source file through a source map.
|
---|
7 |
|
---|
8 | You may already be familiar with the [`source-map`][source-map] package's `SourceMapConsumer`. This
|
---|
9 | provides the same `originalPositionFor` and `generatedPositionFor` API, without requiring WASM.
|
---|
10 |
|
---|
11 | ## Installation
|
---|
12 |
|
---|
13 | ```sh
|
---|
14 | npm install @jridgewell/trace-mapping
|
---|
15 | ```
|
---|
16 |
|
---|
17 | ## Usage
|
---|
18 |
|
---|
19 | ```typescript
|
---|
20 | import {
|
---|
21 | TraceMap,
|
---|
22 | originalPositionFor,
|
---|
23 | generatedPositionFor,
|
---|
24 | sourceContentFor,
|
---|
25 | isIgnored,
|
---|
26 | } from '@jridgewell/trace-mapping';
|
---|
27 |
|
---|
28 | const tracer = new TraceMap({
|
---|
29 | version: 3,
|
---|
30 | sources: ['input.js'],
|
---|
31 | sourcesContent: ['content of input.js'],
|
---|
32 | names: ['foo'],
|
---|
33 | mappings: 'KAyCIA',
|
---|
34 | ignoreList: [],
|
---|
35 | });
|
---|
36 |
|
---|
37 | // Lines start at line 1, columns at column 0.
|
---|
38 | const traced = originalPositionFor(tracer, { line: 1, column: 5 });
|
---|
39 | assert.deepEqual(traced, {
|
---|
40 | source: 'input.js',
|
---|
41 | line: 42,
|
---|
42 | column: 4,
|
---|
43 | name: 'foo',
|
---|
44 | });
|
---|
45 |
|
---|
46 | const content = sourceContentFor(tracer, traced.source);
|
---|
47 | assert.strictEqual(content, 'content for input.js');
|
---|
48 |
|
---|
49 | const generated = generatedPositionFor(tracer, {
|
---|
50 | source: 'input.js',
|
---|
51 | line: 42,
|
---|
52 | column: 4,
|
---|
53 | });
|
---|
54 | assert.deepEqual(generated, {
|
---|
55 | line: 1,
|
---|
56 | column: 5,
|
---|
57 | });
|
---|
58 |
|
---|
59 | const ignored = isIgnored(tracer, 'input.js');
|
---|
60 | assert.equal(ignored, false);
|
---|
61 | ```
|
---|
62 |
|
---|
63 | We also provide a lower level API to get the actual segment that matches our line and column. Unlike
|
---|
64 | `originalPositionFor`, `traceSegment` uses a 0-base for `line`:
|
---|
65 |
|
---|
66 | ```typescript
|
---|
67 | import { traceSegment } from '@jridgewell/trace-mapping';
|
---|
68 |
|
---|
69 | // line is 0-base.
|
---|
70 | const traced = traceSegment(tracer, /* line */ 0, /* column */ 5);
|
---|
71 |
|
---|
72 | // Segments are [outputColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
|
---|
73 | // Again, line is 0-base and so is sourceLine
|
---|
74 | assert.deepEqual(traced, [5, 0, 41, 4, 0]);
|
---|
75 | ```
|
---|
76 |
|
---|
77 | ### SectionedSourceMaps
|
---|
78 |
|
---|
79 | The sourcemap spec defines a special `sections` field that's designed to handle concatenation of
|
---|
80 | output code with associated sourcemaps. This type of sourcemap is rarely used (no major build tool
|
---|
81 | produces it), but if you are hand coding a concatenation you may need it. We provide an `AnyMap`
|
---|
82 | helper that can receive either a regular sourcemap or a `SectionedSourceMap` and returns a
|
---|
83 | `TraceMap` instance:
|
---|
84 |
|
---|
85 | ```typescript
|
---|
86 | import { AnyMap } from '@jridgewell/trace-mapping';
|
---|
87 | const fooOutput = 'foo';
|
---|
88 | const barOutput = 'bar';
|
---|
89 | const output = [fooOutput, barOutput].join('\n');
|
---|
90 |
|
---|
91 | const sectioned = new AnyMap({
|
---|
92 | version: 3,
|
---|
93 | sections: [
|
---|
94 | {
|
---|
95 | // 0-base line and column
|
---|
96 | offset: { line: 0, column: 0 },
|
---|
97 | // fooOutput's sourcemap
|
---|
98 | map: {
|
---|
99 | version: 3,
|
---|
100 | sources: ['foo.js'],
|
---|
101 | names: ['foo'],
|
---|
102 | mappings: 'AAAAA',
|
---|
103 | },
|
---|
104 | },
|
---|
105 | {
|
---|
106 | // barOutput's sourcemap will not affect the first line, only the second
|
---|
107 | offset: { line: 1, column: 0 },
|
---|
108 | map: {
|
---|
109 | version: 3,
|
---|
110 | sources: ['bar.js'],
|
---|
111 | names: ['bar'],
|
---|
112 | mappings: 'AAAAA',
|
---|
113 | },
|
---|
114 | },
|
---|
115 | ],
|
---|
116 | });
|
---|
117 |
|
---|
118 | const traced = originalPositionFor(sectioned, {
|
---|
119 | line: 2,
|
---|
120 | column: 0,
|
---|
121 | });
|
---|
122 |
|
---|
123 | assert.deepEqual(traced, {
|
---|
124 | source: 'bar.js',
|
---|
125 | line: 1,
|
---|
126 | column: 0,
|
---|
127 | name: 'bar',
|
---|
128 | });
|
---|
129 | ```
|
---|
130 |
|
---|
131 | ## Benchmarks
|
---|
132 |
|
---|
133 | ```
|
---|
134 | node v18.0.0
|
---|
135 |
|
---|
136 | amp.js.map - 45120 segments
|
---|
137 |
|
---|
138 | Memory Usage:
|
---|
139 | trace-mapping decoded 562400 bytes
|
---|
140 | trace-mapping encoded 5706544 bytes
|
---|
141 | source-map-js 10717664 bytes
|
---|
142 | source-map-0.6.1 17446384 bytes
|
---|
143 | source-map-0.8.0 9701757 bytes
|
---|
144 | Smallest memory usage is trace-mapping decoded
|
---|
145 |
|
---|
146 | Init speed:
|
---|
147 | trace-mapping: decoded JSON input x 180 ops/sec ±0.34% (85 runs sampled)
|
---|
148 | trace-mapping: encoded JSON input x 364 ops/sec ±1.77% (89 runs sampled)
|
---|
149 | trace-mapping: decoded Object input x 3,116 ops/sec ±0.50% (96 runs sampled)
|
---|
150 | trace-mapping: encoded Object input x 410 ops/sec ±2.62% (85 runs sampled)
|
---|
151 | source-map-js: encoded Object input x 84.23 ops/sec ±0.91% (73 runs sampled)
|
---|
152 | source-map-0.6.1: encoded Object input x 37.21 ops/sec ±2.08% (51 runs sampled)
|
---|
153 | Fastest is trace-mapping: decoded Object input
|
---|
154 |
|
---|
155 | Trace speed:
|
---|
156 | trace-mapping: decoded originalPositionFor x 3,952,212 ops/sec ±0.17% (98 runs sampled)
|
---|
157 | trace-mapping: encoded originalPositionFor x 3,487,468 ops/sec ±1.58% (90 runs sampled)
|
---|
158 | source-map-js: encoded originalPositionFor x 827,730 ops/sec ±0.78% (97 runs sampled)
|
---|
159 | source-map-0.6.1: encoded originalPositionFor x 748,991 ops/sec ±0.53% (94 runs sampled)
|
---|
160 | source-map-0.8.0: encoded originalPositionFor x 2,532,894 ops/sec ±0.57% (95 runs sampled)
|
---|
161 | Fastest is trace-mapping: decoded originalPositionFor
|
---|
162 |
|
---|
163 |
|
---|
164 | ***
|
---|
165 |
|
---|
166 |
|
---|
167 | babel.min.js.map - 347793 segments
|
---|
168 |
|
---|
169 | Memory Usage:
|
---|
170 | trace-mapping decoded 89832 bytes
|
---|
171 | trace-mapping encoded 35474640 bytes
|
---|
172 | source-map-js 51257176 bytes
|
---|
173 | source-map-0.6.1 63515664 bytes
|
---|
174 | source-map-0.8.0 42933752 bytes
|
---|
175 | Smallest memory usage is trace-mapping decoded
|
---|
176 |
|
---|
177 | Init speed:
|
---|
178 | trace-mapping: decoded JSON input x 15.41 ops/sec ±8.65% (34 runs sampled)
|
---|
179 | trace-mapping: encoded JSON input x 28.20 ops/sec ±12.87% (42 runs sampled)
|
---|
180 | trace-mapping: decoded Object input x 964 ops/sec ±0.36% (99 runs sampled)
|
---|
181 | trace-mapping: encoded Object input x 31.77 ops/sec ±13.79% (45 runs sampled)
|
---|
182 | source-map-js: encoded Object input x 6.45 ops/sec ±5.16% (21 runs sampled)
|
---|
183 | source-map-0.6.1: encoded Object input x 4.07 ops/sec ±5.24% (15 runs sampled)
|
---|
184 | Fastest is trace-mapping: decoded Object input
|
---|
185 |
|
---|
186 | Trace speed:
|
---|
187 | trace-mapping: decoded originalPositionFor x 7,183,038 ops/sec ±0.58% (95 runs sampled)
|
---|
188 | trace-mapping: encoded originalPositionFor x 5,192,185 ops/sec ±0.41% (100 runs sampled)
|
---|
189 | source-map-js: encoded originalPositionFor x 4,259,489 ops/sec ±0.79% (94 runs sampled)
|
---|
190 | source-map-0.6.1: encoded originalPositionFor x 3,742,629 ops/sec ±0.71% (95 runs sampled)
|
---|
191 | source-map-0.8.0: encoded originalPositionFor x 6,270,211 ops/sec ±0.64% (94 runs sampled)
|
---|
192 | Fastest is trace-mapping: decoded originalPositionFor
|
---|
193 |
|
---|
194 |
|
---|
195 | ***
|
---|
196 |
|
---|
197 |
|
---|
198 | preact.js.map - 1992 segments
|
---|
199 |
|
---|
200 | Memory Usage:
|
---|
201 | trace-mapping decoded 37128 bytes
|
---|
202 | trace-mapping encoded 247280 bytes
|
---|
203 | source-map-js 1143536 bytes
|
---|
204 | source-map-0.6.1 1290992 bytes
|
---|
205 | source-map-0.8.0 96544 bytes
|
---|
206 | Smallest memory usage is trace-mapping decoded
|
---|
207 |
|
---|
208 | Init speed:
|
---|
209 | trace-mapping: decoded JSON input x 3,483 ops/sec ±0.30% (98 runs sampled)
|
---|
210 | trace-mapping: encoded JSON input x 6,092 ops/sec ±0.18% (97 runs sampled)
|
---|
211 | trace-mapping: decoded Object input x 249,076 ops/sec ±0.24% (98 runs sampled)
|
---|
212 | trace-mapping: encoded Object input x 14,555 ops/sec ±0.48% (100 runs sampled)
|
---|
213 | source-map-js: encoded Object input x 2,447 ops/sec ±0.36% (99 runs sampled)
|
---|
214 | source-map-0.6.1: encoded Object input x 1,201 ops/sec ±0.57% (96 runs sampled)
|
---|
215 | Fastest is trace-mapping: decoded Object input
|
---|
216 |
|
---|
217 | Trace speed:
|
---|
218 | trace-mapping: decoded originalPositionFor x 7,620,192 ops/sec ±0.09% (99 runs sampled)
|
---|
219 | trace-mapping: encoded originalPositionFor x 6,872,554 ops/sec ±0.30% (97 runs sampled)
|
---|
220 | source-map-js: encoded originalPositionFor x 2,489,570 ops/sec ±0.35% (94 runs sampled)
|
---|
221 | source-map-0.6.1: encoded originalPositionFor x 1,698,633 ops/sec ±0.28% (98 runs sampled)
|
---|
222 | source-map-0.8.0: encoded originalPositionFor x 4,015,644 ops/sec ±0.22% (98 runs sampled)
|
---|
223 | Fastest is trace-mapping: decoded originalPositionFor
|
---|
224 |
|
---|
225 |
|
---|
226 | ***
|
---|
227 |
|
---|
228 |
|
---|
229 | react.js.map - 5726 segments
|
---|
230 |
|
---|
231 | Memory Usage:
|
---|
232 | trace-mapping decoded 16176 bytes
|
---|
233 | trace-mapping encoded 681552 bytes
|
---|
234 | source-map-js 2418352 bytes
|
---|
235 | source-map-0.6.1 2443672 bytes
|
---|
236 | source-map-0.8.0 111768 bytes
|
---|
237 | Smallest memory usage is trace-mapping decoded
|
---|
238 |
|
---|
239 | Init speed:
|
---|
240 | trace-mapping: decoded JSON input x 1,720 ops/sec ±0.34% (98 runs sampled)
|
---|
241 | trace-mapping: encoded JSON input x 4,406 ops/sec ±0.35% (100 runs sampled)
|
---|
242 | trace-mapping: decoded Object input x 92,122 ops/sec ±0.10% (99 runs sampled)
|
---|
243 | trace-mapping: encoded Object input x 5,385 ops/sec ±0.37% (99 runs sampled)
|
---|
244 | source-map-js: encoded Object input x 794 ops/sec ±0.40% (98 runs sampled)
|
---|
245 | source-map-0.6.1: encoded Object input x 416 ops/sec ±0.54% (91 runs sampled)
|
---|
246 | Fastest is trace-mapping: decoded Object input
|
---|
247 |
|
---|
248 | Trace speed:
|
---|
249 | trace-mapping: decoded originalPositionFor x 32,759,519 ops/sec ±0.33% (100 runs sampled)
|
---|
250 | trace-mapping: encoded originalPositionFor x 31,116,306 ops/sec ±0.33% (97 runs sampled)
|
---|
251 | source-map-js: encoded originalPositionFor x 17,458,435 ops/sec ±0.44% (97 runs sampled)
|
---|
252 | source-map-0.6.1: encoded originalPositionFor x 12,687,097 ops/sec ±0.43% (95 runs sampled)
|
---|
253 | source-map-0.8.0: encoded originalPositionFor x 23,538,275 ops/sec ±0.38% (95 runs sampled)
|
---|
254 | Fastest is trace-mapping: decoded originalPositionFor
|
---|
255 | ```
|
---|
256 |
|
---|
257 | [source-map]: https://www.npmjs.com/package/source-map
|
---|