-
Notifications
You must be signed in to change notification settings - Fork 217
Proper source map support for development mode #145
Description
Hello. In order to speed up our development process, we have build a custom solution for the hypernova server which can load Javascript bundles over HTTP. With that, we can spin up a webpack dev server and reload the compiled bundle each time the source files change. That has been working wonders for the time being and we are really happy that we don't have to compile the files over and over again.
In order to make development even better, we wanted to add source map support for the generated stack traces. Our current implementation is the following.
- Client asks for component
Foo - We load the code for component
Fooover the webpack dev server - We evaluate its code with
vm.runInNewContextand we catch any errors generated - Should we catch any error, we load the source map file and we feed it into a
sourceMapConsumerfrom Mozilla's source-map. - We then map all the stack trace elements into new ones by querying the consumer.
- Finally we throw the modified error.
Code would look something like this
let scriptContent;
try {
const response = await axios.get(url);
scriptContent = response.data;
} catch (e) {
console.error('Error while getting remote file');
console.error(e);
throw e;
}
let module = require('module');
const wrappedCode = module.wrap(scriptContent);
const moduleRunner = vm.runInNewContext(wrappedCode, createContext(), {
displayErrors: true,
filename: 'ssr.node.js'
});
const { data } = await axios.get(`${url}.map`)
const sourceMapConsumer = await new sourceMap.SourceMapConsumer(data);
return () => {
const modExports = {};
const modContext = {};
try {
moduleRunner(modExports, require, modContext, __filename, __dirname);
} catch(error) {
// Get the trace lines
const trace = error.stack.split("\n").slice(1).map(l => l.trim());
const originalStackTrace = sourceMappedStackTrace(trace, sourceMapConsumer);
// Map them to the original files
// Construct the new stack trace
const newTrace = originalStackTrace.map((orig, index) => {
if(orig.source === null || orig.line === null || orig.column === null) {
// Something that we don't have a mapping for. Leave the original
return ` ${trace[index]}`;
}
let base = ' at ';
if(orig.name) {
base += `${orig.name} `;
}
base += `${orig.source}:${orig.line}:${orig.column}`;
return base;
})
.filter(e => e !== null)
// Throw the modified error
throw new Error(error.stack.split('\n')[0] + '\n' + newTrace.join('\n'))
}
return modContext.exports;Although this works great for the time being, we wanted to catch "runtime" errors as well. That is, errors generated when (e.g. ReactDOM.renderToString runs). To my understanding, there is no way to manipulate such errors from BatchManager.render as the exceptions are caught and modified there.
We could submit a patch upstream to enable custom error handling if there is no other elegant way of manipulating error traces.