Skip to content

Instantly share code, notes, and snippets.

@pinyugi
Created October 29, 2019 18:07
Show Gist options
  • Save pinyugi/a3faa0445e16d162dddad56e9e8d9073 to your computer and use it in GitHub Desktop.
Save pinyugi/a3faa0445e16d162dddad56e9e8d9073 to your computer and use it in GitHub Desktop.
repl-worker
self.importScripts('https://prepack.io/js/prepack.min.js');
function onlyWarnings(buffer) {
return buffer.every(function(error) {
return error.severity === "Warning" || error.severity === "Information";
});
}
onmessage = function(e) {
let buffer = [];
function errorHandler(error) {
// Syntax errors contain their location at the end, remove that
if (error.errorCode === "PP1004") {
let msg = error.message;
error.message = msg.substring(0, msg.lastIndexOf("("));
}
buffer.push({
severity: error.severity,
location: error.location,
message: error.message,
errorCode: error.errorCode,
});
return "Recover";
}
try {
let sources = [{ filePath: "dummy", fileContents: e.data.code }];
let options = {
compatibility: "browser",
filename: "repl",
timeout: 20000,
serialize: true,
heapGraphFormat: "VISJS",
errorHandler,
};
for (let property in e.data.options) {
if (e.data.options.hasOwnProperty(property)) {
options[property] = e.data.options[property];
}
}
let result = Prepack.prepackSources(sources, options);
let noErrors = onlyWarnings(buffer);
if (result && noErrors) {
postMessage({ type: 'success', data: result.code, graph: result.heapGraph, messages: buffer });
} else {
// A well-defined error occurred.
postMessage({ type: 'error', data: buffer });
}
} catch (err) {
buffer.push({
message: err.stack || 'An unknown error occurred'
});
postMessage({ type: 'error', data: buffer });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment