From 3e5874431fadef69336f0c7c1b75e834c458df31 Mon Sep 17 00:00:00 2001 From: daiwei Date: Thu, 28 Aug 2025 10:43:16 +0800 Subject: [PATCH 1/2] fix(hmr): prevent __VUE_HMR_RUNTIME__ from being overwritten by third-party libraries --- packages/runtime-core/src/hmr.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/runtime-core/src/hmr.ts b/packages/runtime-core/src/hmr.ts index 2c46ea73b50..c4af6d2625a 100644 --- a/packages/runtime-core/src/hmr.ts +++ b/packages/runtime-core/src/hmr.ts @@ -31,11 +31,17 @@ export interface HMRRuntime { // Note: for a component to be eligible for HMR it also needs the __hmrId option // to be set so that its instances can be registered / removed. if (__DEV__) { - getGlobalThis().__VUE_HMR_RUNTIME__ = { - createRecord: tryWrap(createRecord), - rerender: tryWrap(rerender), - reload: tryWrap(reload), - } as HMRRuntime + const globalThis = getGlobalThis() + // vite-plugin-vue/issues/644, #13202 + // custom-element libraries bundle Vue to simplify usage outside Vue projects but + // it overwrite __VUE_HMR_RUNTIME__, causing HMR to break. + if (!globalThis.__VUE_HMR_RUNTIME__) { + globalThis.__VUE_HMR_RUNTIME__ = { + createRecord: tryWrap(createRecord), + rerender: tryWrap(rerender), + reload: tryWrap(reload), + } as HMRRuntime + } } const map: Map< From 7113d9c53c4581638749123265938dba27e26573 Mon Sep 17 00:00:00 2001 From: daiwei Date: Tue, 2 Sep 2025 16:58:17 +0800 Subject: [PATCH 2/2] chore: tweaks --- packages/runtime-core/src/hmr.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/hmr.ts b/packages/runtime-core/src/hmr.ts index c4af6d2625a..4cc5fcfc76c 100644 --- a/packages/runtime-core/src/hmr.ts +++ b/packages/runtime-core/src/hmr.ts @@ -31,12 +31,12 @@ export interface HMRRuntime { // Note: for a component to be eligible for HMR it also needs the __hmrId option // to be set so that its instances can be registered / removed. if (__DEV__) { - const globalThis = getGlobalThis() + const g = getGlobalThis() // vite-plugin-vue/issues/644, #13202 // custom-element libraries bundle Vue to simplify usage outside Vue projects but // it overwrite __VUE_HMR_RUNTIME__, causing HMR to break. - if (!globalThis.__VUE_HMR_RUNTIME__) { - globalThis.__VUE_HMR_RUNTIME__ = { + if (!g.__VUE_HMR_RUNTIME__) { + g.__VUE_HMR_RUNTIME__ = { createRecord: tryWrap(createRecord), rerender: tryWrap(rerender), reload: tryWrap(reload),