File tree 5 files changed +36
-2
lines changed
5 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 1
1
import fs from 'node:fs'
2
2
import type { Plugin , ViteDevServer } from 'vite'
3
- import { createFilter } from 'vite'
3
+ import { createFilter , normalizePath } from 'vite'
4
4
/* eslint-disable import/no-duplicates */
5
5
import type {
6
6
SFCBlock ,
@@ -200,6 +200,12 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin<Api> {
200
200
} ,
201
201
202
202
handleHotUpdate ( ctx ) {
203
+ ctx . server . ws . send ( {
204
+ type : 'custom' ,
205
+ event : 'file-changed' ,
206
+ data : { file : normalizePath ( ctx . file ) } ,
207
+ } )
208
+
203
209
if ( options . value . compiler . invalidateTypeCache ) {
204
210
options . value . compiler . invalidateTypeCache ( ctx . file )
205
211
}
Original file line number Diff line number Diff line change @@ -148,9 +148,20 @@ export async function transformMain(
148
148
`typeof __VUE_HMR_RUNTIME__ !== 'undefined' && ` +
149
149
`__VUE_HMR_RUNTIME__.createRecord(_sfc_main.__hmrId, _sfc_main)` ,
150
150
)
151
+ output . push (
152
+ `import.meta.hot.on('file-changed', ({ file }) => {` ,
153
+ ` __VUE_HMR_RUNTIME__.CHANGED_FILE = file` ,
154
+ `})` ,
155
+ )
151
156
// check if the template is the only thing that changed
152
157
if ( prevDescriptor && isOnlyTemplateChanged ( prevDescriptor , descriptor ) ) {
153
- output . push ( `export const _rerender_only = true` )
158
+ // #7 only consider re-render if the HMR is triggered by the current component,
159
+ // otherwise reload. Due to vite will cache the transform result. If the HMR
160
+ // is triggered by other files that the current component relies on, a reload
161
+ // is required.
162
+ output . push (
163
+ `export const _rerender_only = __VUE_HMR_RUNTIME__.CHANGED_FILE === ${ JSON . stringify ( normalizePath ( filename ) ) } ` ,
164
+ )
154
165
}
155
166
output . push (
156
167
`import.meta.hot.accept(mod => {` ,
Original file line number Diff line number Diff line change 2
2
<h2 class =" hmr" >HMR</h2 >
3
3
<p >Click the button then edit this message. The count should be preserved.</p >
4
4
<button class =" hmr-inc" @click =" count++" >count is {{ count }}</button >
5
+ <span class =" hmr-number" >{{ number }}</span >
5
6
</template >
6
7
7
8
<script setup lang="ts">
8
9
import { ref } from ' vue'
9
10
import Node from ' ./Node.vue'
11
+ import { test } from ' ./lib.js'
12
+ const number = test ()
10
13
11
14
let foo: number = 0
12
15
Original file line number Diff line number Diff line change @@ -206,6 +206,17 @@ describe('hmr', () => {
206
206
await untilUpdated ( ( ) => page . textContent ( '.hmr-inc' ) , 'count is 100' )
207
207
} )
208
208
209
+ test ( 'should reload when relies file changed' , async ( ) => {
210
+ // rerender
211
+ editFile ( 'Hmr.vue' , ( code ) => code . replace ( 'HMR' , 'HMR updated' ) )
212
+ await untilUpdated ( ( ) => page . textContent ( 'h2.hmr' ) , 'HMR updated' )
213
+ await untilUpdated ( ( ) => page . textContent ( '.hmr-number' ) , '100' )
214
+
215
+ // reload
216
+ editFile ( 'lib.js' , ( code ) => code . replace ( '100' , '200' ) )
217
+ await untilUpdated ( ( ) => page . textContent ( '.hmr-number' ) , '200' )
218
+ } )
219
+
209
220
test ( 'global hmr for some scenarios' , async ( ) => {
210
221
editFile ( 'Hmr.vue' , ( code ) =>
211
222
code . replace ( '</template>' , ' <Node/>\n' + '</template>' ) ,
Original file line number Diff line number Diff line change
1
+ export function test ( ) {
2
+ return 100
3
+ }
You can’t perform that action at this time.
0 commit comments