1
- import fs from 'fs'
2
1
import _debug from 'debug'
3
2
import { SFCBlock , SFCDescriptor } from '@vue/compiler-sfc'
4
3
import {
@@ -17,25 +16,18 @@ const debug = _debug('vite:hmr')
17
16
export async function handleHotUpdate (
18
17
file : string ,
19
18
modules : ModuleNode [ ] ,
19
+ read : ( ) => string | Promise < string > ,
20
20
server : ViteDevServer
21
21
) : Promise < ModuleNode [ ] | void > {
22
- if ( ! file . endsWith ( '.vue' ) ) {
23
- return
24
- }
25
-
26
22
const prevDescriptor = getDescriptor ( file )
27
23
if ( ! prevDescriptor ) {
28
24
// file hasn't been requested yet (e.g. async component)
29
25
return
30
26
}
31
27
32
- let content = fs . readFileSync ( file , 'utf-8' )
33
- if ( ! content ) {
34
- await untilModified ( file )
35
- content = fs . readFileSync ( file , 'utf-8' )
36
- }
37
-
38
28
setPrevDescriptor ( file , prevDescriptor )
29
+
30
+ const content = await read ( )
39
31
const { descriptor } = createDescriptor (
40
32
file ,
41
33
content ,
@@ -147,26 +139,6 @@ export async function handleHotUpdate(
147
139
return [ ...affectedModules ] . filter ( Boolean ) as ModuleNode [ ]
148
140
}
149
141
150
- // vitejs/vite#610 when hot-reloading Vue files, we read immediately on file
151
- // change event and sometimes this can be too early and get an empty buffer.
152
- // Poll until the file's modified time has changed before reading again.
153
- async function untilModified ( file : string ) {
154
- const mtime = fs . statSync ( file ) . mtimeMs
155
- return new Promise ( ( r ) => {
156
- let n = 0
157
- const poll = async ( ) => {
158
- n ++
159
- const newMtime = fs . statSync ( file ) . mtimeMs
160
- if ( newMtime !== mtime || n > 10 ) {
161
- r ( 0 )
162
- } else {
163
- setTimeout ( poll , 10 )
164
- }
165
- }
166
- setTimeout ( poll , 10 )
167
- } )
168
- }
169
-
170
142
function isEqualBlock ( a : SFCBlock | null , b : SFCBlock | null ) {
171
143
if ( ! a && ! b ) return true
172
144
if ( ! a || ! b ) return false
0 commit comments