diff --git a/packages/plugin-vue/src/handleHotUpdate.ts b/packages/plugin-vue/src/handleHotUpdate.ts
index 5569f0dfbe02ae..d1dde364eb7f10 100644
--- a/packages/plugin-vue/src/handleHotUpdate.ts
+++ b/packages/plugin-vue/src/handleHotUpdate.ts
@@ -45,7 +45,8 @@ export async function handleHotUpdate(
})[0]
const templateModule = modules.find((m) => /type=template/.test(m.url))
- if (hasScriptChanged(prevDescriptor, descriptor)) {
+ const scriptChanged = hasScriptChanged(prevDescriptor, descriptor)
+ if (scriptChanged) {
let scriptModule: ModuleNode | undefined
if (
(descriptor.scriptSetup?.lang && !descriptor.scriptSetup.src) ||
@@ -66,7 +67,7 @@ export async function handleHotUpdate(
// binding metadata. However, when reloading the template alone the binding
// metadata will not be available since the script part isn't loaded.
// in this case, reuse the compiled script from previous descriptor.
- if (mainModule && !affectedModules.has(mainModule)) {
+ if (!scriptChanged) {
setResolvedScript(
descriptor,
getResolvedScript(prevDescriptor, false)!,
diff --git a/playground/vue/HmrTsx.vue b/playground/vue/HmrTsx.vue
new file mode 100644
index 00000000000000..c1a2331da6a6be
--- /dev/null
+++ b/playground/vue/HmrTsx.vue
@@ -0,0 +1,17 @@
+
+ Click the button then edit this message. The count should be preserved.HMR
+