Skip to content

Instantly share code, notes, and snippets.

@georges-gomes
Created March 20, 2020 10:03
Show Gist options
  • Save georges-gomes/6dc743addb90d2e7c5739bba00cf95ea to your computer and use it in GitHub Desktop.
Save georges-gomes/6dc743addb90d2e7c5739bba00cf95ea to your computer and use it in GitHub Desktop.
Hot module reload prototype
import inspector from "inspector";
const url2scriptid = new Map<string, string>();
const session = new inspector.Session();
session.connect();
setupDebugger();
function setupDebugger() {
session.post("Debugger.enable", () => {
subscribeToScriptId();
});
}
function subscribeToScriptId() {
session.on("Debugger.scriptParsed", message => {
console.log("scriptParsed", [message.params.url, message.params.scriptId]);
url2scriptid.set(message.params.url, message.params.scriptId);
});
}
function reloadModule(url: string) {
console.log("reload", url);
session.post(
"Debugger.setScriptSource",
{
scriptId: url2scriptid.get(url),
scriptSource: `<new code>`
},
(error, result) => {
if (!error) {
console.log("Hot reload successful", result);
} else {
console.log("Hot reload failed", result);
}
}
);
}
export { reloadModule };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment