Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.