@srcmap/remapping
v0.6.0
Published
The @jridgewell/remapping and @ampproject/remapping API on the srcmap Rust remapping code, compiled to WebAssembly
Maintainers
Readme
@srcmap/remapping
The API of @jridgewell/remapping and @ampproject/remapping on the srcmap Rust remapping code, compiled to WebAssembly.
remapping(maps, loader) takes the arguments of @jridgewell/remapping and returns a SourceMap with the same fields.
On the maps of four recorded Vite 8 runs, it gives the same output as @jridgewell/remapping. On the three larger runs it is 1.3 to 1.7 times faster on the first pass, and 1.5 to 1.9 times faster on later passes. benchmarks/vite-workload.mjs in the repository reproduces this result.
The wrapper gives each map to WASM as parts, not as JSON. A map that it cannot take that way with certainty (for example a map with an unknown key, or a string with a lone surrogate) takes a slower JSON path, with the same result.
It also fits a Node.js tool that must compose maps the same way as a Rust tool with srcmap-remapping.
Install
npm install @srcmap/remappingUsage
// Before:
// import remapping from '@jridgewell/remapping'
// import remapping from '@ampproject/remapping'
// After:
import remapping from '@srcmap/remapping'
// Remap a single source map through a loader
const composed = remapping(minifiedMap, (sourcefile) => {
// Return the upstream source map for this file, or null
return upstreamMaps[sourcefile] ?? null
})
console.log(composed.sources) // Original sources
console.log(composed.mappings) // Composed VLQ mappings
console.log(composed.toString()) // JSON stringCompose an array of source maps
// When you have the full chain: [minified map, intermediate map, original map]
const composed = remapping([minifiedMap, intermediateMap], (sourcefile) => {
return upstreamMaps[sourcefile] ?? null
})API compatibility
| Export | Description |
|--------|-------------|
| remapping(input, loader, options?) | Remap a single source map through a loader |
| remapping(inputs[], loader, options?) | Compose an array of source maps |
| SourceMap | Result class with version, file, mappings, sources, sourcesContent, names, ignoreList |
The loader function receives a source filename and a LoaderContext, and should return the upstream source map (as a JSON string, parsed object, or decoded source map), or null if no upstream map exists. As in @jridgewell/remapping, the loader is called again for the sources of each map that it returns, and it can set ctx.source, ctx.content and ctx.ignore for a source without an upstream map.
options takes excludeContent and decodedMappings. true means { excludeContent: true }.
SourceMap result
| Property / Method | Type | Description |
|--------|------|-------------|
| version | number | Source map version (always 3) |
| file | string \| undefined | Output filename |
| mappings | string \| number[][][] | VLQ-encoded mappings, or decoded segments with decodedMappings |
| sources | (string \| null)[] | Source filenames |
| sourcesContent | (string \| null)[] \| undefined | Inline source contents, left out with excludeContent |
| names | string[] | Name strings |
| ignoreList | number[] | Indices of ignored sources |
| toString() | string | Serialize as JSON string |
| toJSON() | RawSourceMap | Return as parsed object |
Differences from @jridgewell/remapping
- WASM-powered: Composition runs in Rust via WebAssembly
- Indexed source maps: Handled natively (no JS-side flattening)
ctx.sourcethat is not a string: As in@jridgewell/remapping, a falsy value gives segments without a source, and an other value stays insourcesas it is. Values with the same string form (for example42and"42") share one entry, and the value of the first segment that uses that entry goes insources. One difference stays: when the loader ignores falsy sources of more than one kind in one call (for example""andnull), the result has one entry for them, and jridgewell can have one entry for each kind.
Part of srcmap
Source map tooling written in Rust. See also:
@srcmap/trace-mapping- The@jridgewell/trace-mappingAPI (consumer)@srcmap/gen-mapping- The@jridgewell/gen-mappingAPI (generator)@srcmap/remapping-wasm- Lower-level WASM remapping API
License
MIT
