Encode/decode the mappings property of a sourcemap.
Why?
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. 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.
This package makes the process slightly easier.
Installation
npm install @jridgewell/sourcemap-codec
Usage
import { encode, decode } from '@jridgewell/sourcemap-codec';var decoded = decode( ';EAEEA,EAAE,EAAC,CAAE;ECQY,UACC' );assert.deepEqual( decoded, [ // the first line (of the generated code) has no mappings, // as shown by the starting semi-colon (which separates lines) [], // the second line contains four (comma-separated) segments [ // segments are encoded as you'd expect: // [ generatedCodeColumn, sourceIndex, sourceCodeLine, sourceCodeColumn, nameIndex ] // i.e. the first segment begins at column 2, and maps back to the second column // of the second line (both zero-based) of the 0th source, and uses the 0th // name in the `map.names` array [ 2, 0, 2, 2, 0 ], // the remaining segments are 4-length rather than 5-length, // because they don't map a name
Benchmarks
node v20.10.0amp.js.map - 45120 segmentsDecode Memory Usage:local code 5815135 bytes@jridgewell/sourcemap-codec 1.4.15 5868160 bytessourcemap-codec 5492584 bytessource-map-0.6.1 13569984 bytessource-map-0.8.0 6390584 byteschrome dev tools 8011136 bytesSmallest memory usage is sourcemap-codecDecode speed:decode: local code x 492 ops/sec ±1.22% (90 runs sampled)decode: @jridgewell/sourcemap-codec 1.4.15 x 499 ops/sec ±1.16% (89 runs sampled)decode: sourcemap-codec x 376 ops/sec ±1.66% (89 runs sampled)decode: source-map-0.6.1 x 34.99 ops/sec ±0.94% (48 runs sampled)decode: source-map-0.8.0 x 351 ops/sec ±0.07% (95 runs sampled)chrome dev tools x 165 ops/sec ±0.91% (86 runs sampled)Fastest is decode: @jridgewell/sourcemap-codec 1.4.15Encode Memory Usage:local code 444248 bytes@jridgewell/sourcemap-codec 1.4.15 623024 bytessourcemap-codec 8696280 bytessource-map-0.6.1 8745176 bytessource-map-0.8.0 8736624 bytesSmallest memory usage is local codeEncode speed: