-
-
Save bmeck/325253456b8c03125eafef797412dd6f to your computer and use it in GitHub Desktop.
Hot module reload prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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