Skip to content

Commit 9d0b9d8

Browse files
author
sun0day
authored
fix(plugin-vue): invalidate script module cache when it changed in hot update (#11059)
1 parent 72f63c5 commit 9d0b9d8

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

packages/plugin-vue/src/handleHotUpdate.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export async function handleHotUpdate(
4545
})[0]
4646
const templateModule = modules.find((m) => /type=template/.test(m.url))
4747

48-
if (hasScriptChanged(prevDescriptor, descriptor)) {
48+
const scriptChanged = hasScriptChanged(prevDescriptor, descriptor)
49+
if (scriptChanged) {
4950
let scriptModule: ModuleNode | undefined
5051
if (
5152
(descriptor.scriptSetup?.lang && !descriptor.scriptSetup.src) ||
@@ -66,7 +67,7 @@ export async function handleHotUpdate(
6667
// binding metadata. However, when reloading the template alone the binding
6768
// metadata will not be available since the script part isn't loaded.
6869
// in this case, reuse the compiled script from previous descriptor.
69-
if (mainModule && !affectedModules.has(mainModule)) {
70+
if (!scriptChanged) {
7071
setResolvedScript(
7172
descriptor,
7273
getResolvedScript(prevDescriptor, false)!,

playground/vue/HmrTsx.vue

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<h2 class="hmr-tsx">HMR</h2>
3+
<p>Click the button then edit this message. The count should be preserved.</p>
4+
<button class="hmr-tsx-inc" @click="count++">count is {{ count }}</button>
5+
</template>
6+
7+
<script setup lang="tsx">
8+
import { ref } from 'vue'
9+
10+
const count = ref(0)
11+
</script>
12+
13+
<style>
14+
.hmr-tsx-inc {
15+
color: red;
16+
}
17+
</style>

playground/vue/Main.vue

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<div class="hmr-block">
66
<Hmr />
77
</div>
8+
<div class="hmr-tsx-block">
9+
<HmrTsx />
10+
</div>
811
<Syntax />
912
<PreProcessors />
1013
<CssModules />
@@ -27,6 +30,7 @@
2730

2831
<script setup lang="ts">
2932
import Hmr from './Hmr.vue'
33+
import HmrTsx from './HmrTsx.vue'
3034
import Syntax from './Syntax.vue'
3135
import PreProcessors from './PreProcessors.vue'
3236
import CssModules from './CssModules.vue'

playground/vue/__tests__/vue.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ describe('hmr', () => {
191191
editFile('Hmr.vue', (code) => code.replace(/<template>.+<\/template>/s, ''))
192192
await untilUpdated(() => page.innerHTML('.hmr-block'), '<!---->')
193193
})
194+
195+
test('should re-render when template and tsx script both changed', async () => {
196+
editFile('HmrTsx.vue', (code) => code.replace(/count/g, 'updatedCount'))
197+
await untilUpdated(
198+
() => page.innerHTML('.hmr-tsx-block .hmr-tsx-inc'),
199+
'updatedCount is 0'
200+
)
201+
})
194202
})
195203

196204
describe('src imports', () => {

0 commit comments

Comments
 (0)