Skip to content

Instantly share code, notes, and snippets.

@buhichan
Created May 21, 2021 10:34
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 buhichan/252f84beff31373262b8338b45d45f7b to your computer and use it in GitHub Desktop.
Save buhichan/252f84beff31373262b8338b45d45f7b to your computer and use it in GitHub Desktop.
(function (){
const moduleDef = {
"./a.tsx": {
code: function (module, exports, require){
module.exports = 1
},
parent: ["./b.tsx"]
},
"./b.tsx": {
code: function (module, exports, require){
function render(){
console.log(require("./a.tsx") + 3)
}
if(module.hot){
module.hot.accept("./a.tsx",()=>{
render()
})
}else{
render()
}
}
}
}
const moduleCache = {}
function resolve(url, hot){
if(hot || !(url in moduleCache)){
const exports = {}
let needBubble = !!hot
const module = {
exports,
hot: hot ? {
accept(url, callback){
needBubble = false
setTimeout(callback, 0)
}
} : false
}
moduleDef[url].code(module, exports, require)
moduleCache[url] = module
if(needBubble){
const parentModules = moduleDef[url].parent
if(!parentModules){
console.warn("hot bubble failed")
location.reload()
}else{
parentModules.forEach(url=>{
resolve(url, hot)
})
}
}
}
return moduleCache[url]
}
function require(url){
return resolve(url).exports
}
require("./b.tsx")
function hotUpdate(url, newCode){
moduleDef[url].code = newCode
resolve(url, true)
}
console.log('hot reloading...')
hotUpdate("./a.tsx", function(module, exports){
module.exports = 3
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment