To begin collecting source-maps, node must be run with --enable-source-maps
(or with NODE_V8_COVERAGE
set).
At which point, the new built-in module source_map
can be used to interact with the source-map cache:
// tooling has one interface into the source-map cache, URIs, any type of module that we
// wish to attach source-maps to (ESM, CJS, evals, data-urls) needs to have a methodlogy
// for setting a URI; Two different source-maps cannot be stored at the same URI.
const {cache} = require('source_map');
const sm1 = cache.sourceMapFromURI(`file://${require.resolve('./my-module')}`);
These methods return the same data-structure that is written to disk when
executing with NODE_V8_COVERAGE
:
{
"url": "data:abc123", // original source-map URL.
"data": {} // parsed source-map or null.
}
When executing with --enable-source-maps
, stack traces will be rewritten if source-maps are found corresponding
to files represented in the stack-trace.
The hope is to intitially engage folks rom the https://github.com/evanw/node-source-map-support project, to support stack-trace rewritting in Node.js. And, in the future, work with the V8 team to move more of this behavior into V8 itself.
Why not a single function, like
require
, that takes a path, resolves it, and then returns a Buffer to the source map contents (cached, of course) - regardless of whether it's inline or a file on disk. If there's concerns about fetching a URL, an alternative could be that it returns some kind of object that indicates the source, and lets you get the contents yourself if it's not inline or on disk. (a different function with the same semantics could apply for esm)