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) { if (module.hot) {
let hash = __webpack_require__.h(); let hash = __webpack_require__.h();
let hmrBootEmittedSymbol = Symbol.for('HMRBootEmitted'); let hmrBootEmittedSymbol = Symbol.for('HMRBootEmitted');
let originalLiveSyncSymbol = Symbol.for('OriginalLiveSync');
let hmrRuntimeLastLiveSyncSymbol = Symbol.for('HMRRuntimeLastLiveSync');
const logVerbose = (title: string, ...info: any) => { const logVerbose = (title: string, ...info: any) => {
if (__NS_ENV_VERBOSE__) { if (__NS_ENV_VERBOSE__) {
@ -108,8 +110,12 @@ if (module.hot) {
].some((path) => requireExists(path)); ].some((path) => requireExists(path));
}; };
const originalOnLiveSync = global.__onLiveSync; if (global.__onLiveSync !== global[hmrRuntimeLastLiveSyncSymbol]) {
global.__onLiveSync = async function () { // 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'); logVerbose('LiveSync');
if (!hasUpdate()) { if (!hasUpdate()) {
@ -120,9 +126,10 @@ if (module.hot) {
return false; return false;
} }
await originalOnLiveSync(); await global[originalLiveSyncSymbol]();
}; };
global.__onLiveSync = global[hmrRuntimeLastLiveSyncSymbol];
if (!global[hmrBootEmittedSymbol]) { if (!global[hmrBootEmittedSymbol]) {
global[hmrBootEmittedSymbol] = true; global[hmrBootEmittedSymbol] = true;
setStatus(hash, 'boot', 'HMR Enabled - waiting for changes...'); setStatus(hash, 'boot', 'HMR Enabled - waiting for changes...');