Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Last active June 12, 2019 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swissmanu/97e24a8b8e0ae29dc651f037b1a89a07 to your computer and use it in GitHub Desktop.
Save swissmanu/97e24a8b8e0ae29dc651f037b1a89a07 to your computer and use it in GitHub Desktop.
Revive a JavaScript stacktrace string with local available source maps.
const fs = require('fs');
const retrace = require('retrace'); // yarn add retrace
const path = require('path');
retrace.resolve = uri => {
// assumes that source maps are in the same directory as this script, but in
// a `maps` folder:
return readFile(path.join(__dirname, 'maps', `${path.basename(uri)}.map`));
};
function readFile(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, { encoding: 'utf-8' }, (err, content) => {
if (err) {
reject(err);
return;
}
resolve(content);
});
});
}
// Usage: Call exported function and pass the stacktrace string with proper new line
// characters.
// Caveat: Anonymous stack frames are omitted in the output.
module.exports = stack => {
const withoutAnonymousFrames = stack.replace(/(\n.*\<anonymous\>.*\n)/g, '');
return retrace.map(withoutAnonymousFrames)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment