Created
May 21, 2021 10:34
-
-
Save buhichan/252f84beff31373262b8338b45d45f7b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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