This is a library to generate and consume the source map format described here.
$ npm install source-map
<script src="https://unpkg.com/source-map@0.7.3/dist/source-map.js"></script>
<script>
sourceMap.SourceMapConsumer.initialize({
"lib/mappings.wasm": "https://unpkg.com/source-map@0.7.3/lib/mappings.wasm",
});
</script>
const rawSourceMap = {
version: 3,
file: "min.js",
names: ["bar", "baz", "n"],
sources: ["one.js", "two.js"],
sourceRoot: "http://example.com/www/js/",
mappings:
"CAAC,IAAI,IAAM,SAAUA,GAClB,OAAOC,IAAID;CCDb,IAAI,IAAM,SAAUE,GAClB,OAAOA",
};
const whatever = await SourceMapConsumer.with(rawSourceMap, null, consumer => {
console.log(consumer.sources);
// [ 'http://example.com/www/js/one.js',
// 'http://example.com/www/js/two.js' ]
console.log(
consumer.originalPositionFor({
line: 2,
column: 28,
})
);
// { source: 'http://example.com/www/js/two.js',
// line: 2,
// column: 10,
// name: 'n' }
console.log(
consumer.generatedPositionFor({
source: "http://example.com/www/js/two.js",
line: 2,
column: 10,
})
);
// { line: 2, column: 28 }
consumer.eachMapping(function (m) {
// ...
});
return computeWhatever();
});
In depth guide: Compiling to JavaScript, and Debugging with Source Maps
function compile(ast) {
switch (ast.type) {
case "BinaryExpression":
return new SourceNode(
ast.location.line,
ast.location.column,
ast.location.source,
[compile(ast.left), " + ", compile(ast.right)]
);
case "Literal":
return new SourceNode(
ast.location.line,
ast.location.column,
ast.location.source,
String(ast.value)
);
// ...
default:
throw new Error("Bad AST");
}
}
var ast = parse("40 + 2", "add.js");
console.log(
compile(ast).toStringWithSourceMap({
file: "add.js",
})
);
// { code: '40 + 2',
// map: [object SourceMapGenerator] }
var map = new SourceMapGenerator({
file: "source-mapped.js",
});
map.addMapping({
generated: {
line: 10,
column: 35,
},
source: "foo.js",
original: {
line: 33,
column: 2,
},
name: "christopher",
});
console.log(map.toString());
// '{"version":3,"file":"source-mapped.js","sources":["foo.js"],"names":["christopher"],"mappings":";;;;;;;;;mCAgCEA"}'
Get a reference to the module:
// Node.js
var sourceMap = require("source-map");
// Browser builds
var sourceMap = window.sourceMap;
// Inside Firefox
const sourceMap = require("devtools/toolkit/sourcemap/source-map.js");
A SourceMapConsumer instance represents a parsed source map which we can query
for information about the original file positions by giving it a file position
in the generated source.
When using SourceMapConsumer outside of node.js, for example on the Web, it
needs to know from what URL to load lib/mappings.wasm. You must inform it by
calling initialize before constructing any SourceMapConsumers.
The options object has the following properties:
"lib/mappings.wasm": A String containing the URL of the
lib/mappings.wasm file, or an ArrayBuffer with the contents of lib/mappings.wasm.sourceMap.SourceMapConsumer.initialize({
"lib/mappings.wasm": "https://example.com/source-map/lib/mappings.wasm",
});
The only parameter is the raw source map (either as a string which can be
JSON.parse'd, or an object). According to the spec, source maps have the
following attributes:
version: Which version of the source map spec this map is following.
sources: An array of URLs to the original source files.
names: An array of identifiers which can be referenced by individual
mappings.
sourceRoot: Optional. The URL root from which all sources are relative.
sourcesContent: Optional. An array of contents of the original source files.
mappings: A string of base64 VLQs which contain the actual mappings.
file: Optional. The generated filename this source map is associated with.
x_google_ignoreList: Optional. An additional extension field which is an array
of indices refering to urls in the sources array. This is used to identify third-party
sources, that the developer might want to avoid when debugging. Read more
The promise of the constructed souce map consumer is returned.
When the SourceMapConsumer will no longer be used anymore, you must call its
destroy method.
const consumer = await new sourceMap.SourceMapConsumer(rawSourceMapJsonData);
doStuffWith(consumer);
consumer.destroy();
Alternatively, you can use SourceMapConsumer.with to avoid needing to remember
to call destroy.
Construct a new SourceMapConsumer from rawSourceMap and sourceMapUrl
(see the SourceMapConsumer constructor for details. Then, invoke the async function f(SourceMapConsumer) -> T with the newly constructed consumer, wait
for f to complete, call destroy on the consumer, and return f's return
value.
You must not use the consumer after f completes!
By using with, you do not have to remember to manually call destroy on
the consumer, since it will be called automatically once f completes.
const xSquared = await SourceMapConsumer.with(
myRawSourceMap,
null,
async function (consumer) {
// Use `consumer` inside here and don't worry about remembering
// to call `destroy`.
const x = await whatever(consumer);
return x * x;
}
);
// You may not use that `consumer` anymore out here; it has
// been destroyed. But you can use `xSquared`.
console.log(xSquared);
Free this source map consumer's associated wasm data that is manually-managed.
consumer.destroy();
Alternatively, you can use SourceMapConsumer.with to avoid needing to remember
to call destroy.
Compute the last column for each generated mapping. The last column is inclusive.
// Before:
consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" });
// [ { line: 2,
// column: 1 },
// { line: 2,
// column: 10 },
// { line: 2,
// column: 20 } ]
consumer.computeColumnSpans();
// After:
consumer.allGeneratedPositionsFor({ line: 2, source: "foo.coffee" });
// [ { line: 2,
// column: 1,
// lastColumn: 9 },
// { line: 2,
// column: 10,
// lastColumn: 19 },
// { line: 2,
// column: 20,
// lastColumn: Infinity } ]
Returns the original source, line, and column information for the generated source's line and column positions provided. The only argument is an object with the following properties:
line: The line number in the generated source. Line numbers in
this library are 1-based (note that the underlying source map
specification uses 0-based line numbers -- this library handles the
translation).
column: The column number in the generated source. Column numbers
in this library are 0-based.
bias: Either SourceMapConsumer.GREATEST_LOWER_BOUND or
SourceMapConsumer.LEAST_UPPER_BOUND. Specifies whether to return the closest
element that is smaller than or greater than the one we are searching for,
respectively, if the exact element cannot be found. Defaults to
SourceMapConsumer.GREATEST_LOWER_BOUND.
and an object is returned with the following properties:
source: The original source file, or null if this information is not
available.
line: The line number in the original source, or null if this information is
not available. The line number is 1-based.
column: The column number in the original source, or null if this
information is not available. The column number is 0-based.
name: The original identifier, or null if this information is not available.
consumer.originalPositionFor({ line: 2, column: 10 });
// { source: 'foo.coffee',
// line: 2,
// column: 2,
// name: null }
consumer.originalPositionFor({
line: 99999999999999999,
column: 999999999999999,
});
// { source: null,
// line: null,
// column: null,
// name: null }
Returns the generated line and column information for the original source, line, and column positions provided. The only argument is an object with the following properties:
source: The filename of the original source.
line: The line number in the original source. The line number is
1-based.
column: The column number in the original source. The column
number is 0-based.
and an object is returned with the following properties:
line: The line number in the generated source, or null. The line
number is 1-based.
column: The column number in the generated source, or null. The
column number is 0-based.
consumer.generatedPositionFor({ source: "example.js", line: 2, column: 10 });
// { line: 1,
// column: 56 }
Returns all generated line and column information for the original source, line, and column provided. If no column is provided, returns all mappings corresponding to a either the line we are searching for or the next closest line that has any mappings. Otherwise, returns all mappings corresponding to the given line and either th
$ claude mcp add source-map \
-- python -m otcore.mcp_server <graph>