What is @cspotcode/source-map-consumer?
@cspotcode/source-map-consumer is a JavaScript library that allows you to work with source maps. Source maps are files that map from the transformed source to the original source, enabling better debugging and error reporting by showing the original source code.
What are @cspotcode/source-map-consumer's main functionalities?
Basic Usage
This code demonstrates how to create a SourceMapConsumer instance with a raw source map object and log the sources.
const { SourceMapConsumer } = require('@cspotcode/source-map-consumer');
const rawSourceMap = { /* raw source map object */ };
SourceMapConsumer.with(rawSourceMap, null, consumer => {
console.log(consumer.sources);
});
Mapping a Generated Position to an Original Position
This code shows how to map a position in the generated code back to the original source position.
const { SourceMapConsumer } = require('@cspotcode/source-map-consumer');
const rawSourceMap = { /* raw source map object */ };
SourceMapConsumer.with(rawSourceMap, null, consumer => {
const pos = consumer.originalPositionFor({ line: 2, column: 10 });
console.log(pos);
});
Mapping an Original Position to a Generated Position
This code demonstrates how to map a position in the original source code to the corresponding position in the generated code.
const { SourceMapConsumer } = require('@cspotcode/source-map-consumer');
const rawSourceMap = { /* raw source map object */ };
SourceMapConsumer.with(rawSourceMap, null, consumer => {
const pos = consumer.generatedPositionFor({ source: 'source.js', line: 2, column: 10 });
console.log(pos);
});
Iterating Over Mappings
This code shows how to iterate over all mappings in the source map.
const { SourceMapConsumer } = require('@cspotcode/source-map-consumer');
const rawSourceMap = { /* raw source map object */ };
SourceMapConsumer.with(rawSourceMap, null, consumer => {
consumer.eachMapping(mapping => {
console.log(mapping);
});
});
Other packages similar to @cspotcode/source-map-consumer
source-map
The 'source-map' package is a popular library for working with source maps. It provides similar functionalities to @cspotcode/source-map-consumer, such as parsing source maps, mapping positions, and iterating over mappings. It is widely used and well-documented.
source-map-support
The 'source-map-support' package provides source map support for stack traces in Node.js and browsers. It allows you to map stack traces from transpiled code back to the original source code, making debugging easier. While it focuses more on stack trace mapping, it complements the functionalities provided by @cspotcode/source-map-consumer.
A smaller version of @cspotcode/source-map which includes only the consumer, not the generator.
NOTE: the type declarations are incorrect. They include declarations for things from @cspotcode/source-map
which are omitted from this package.
This is a delibrate, pragmatic choice. The declarations for things which are included -- the consumer -- should
be correct.