Skip to content

Instantly share code, notes, and snippets.

@bmeck
Forked from georges-gomes/hmr.js
Created March 20, 2020 13:56
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 bmeck/325253456b8c03125eafef797412dd6f to your computer and use it in GitHub Desktop.
Save bmeck/325253456b8c03125eafef797412dd6f 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