fix(webpack): prevent hmr from patching __onLiveSync multiple times (#10103)

This commit is contained in:
Eduardo Speroni
2022-11-24 08:18:51 -03:00
committed by GitHub
parent cfaa8134b0
commit 42f5dc51c7

View File

@ -6,6 +6,8 @@
if (module.hot) {
let hash = __webpack_require__.h();
let hmrBootEmittedSymbol = Symbol.for('HMRBootEmitted');
let originalLiveSyncSymbol = Symbol.for('OriginalLiveSync');
let hmrRuntimeLastLiveSyncSymbol = Symbol.for('HMRRuntimeLastLiveSync');
const logVerbose = (title: string, ...info: any) => {
if (__NS_ENV_VERBOSE__) {
@ -108,8 +110,12 @@ if (module.hot) {
].some((path) => requireExists(path));
};
const originalOnLiveSync = global.__onLiveSync;
global.__onLiveSync = async function () {
if (global.__onLiveSync !== global[hmrRuntimeLastLiveSyncSymbol]) {
// we store the original liveSync here in case this code runs again
// which happens when you module.hot.accept() the main file
global[originalLiveSyncSymbol] = global.__onLiveSync;
}
global[hmrRuntimeLastLiveSyncSymbol] = async function () {
logVerbose('LiveSync');
if (!hasUpdate()) {
@ -120,9 +126,10 @@ if (module.hot) {
return false;
}
await originalOnLiveSync();
await global[originalLiveSyncSymbol]();
};
global.__onLiveSync = global[hmrRuntimeLastLiveSyncSymbol];
if (!global[hmrBootEmittedSymbol]) {
global[hmrBootEmittedSymbol] = true;
setStatus(hash, 'boot', 'HMR Enabled - waiting for changes...');