Skip to content

Commit 49dac87

Browse files
committed
wip: tweaks for vitepress
1 parent 2d93dff commit 49dac87

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

packages/plugin-vue/src/handleHotUpdate.ts

+3-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs'
21
import _debug from 'debug'
32
import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
43
import {
@@ -17,25 +16,18 @@ const debug = _debug('vite:hmr')
1716
export async function handleHotUpdate(
1817
file: string,
1918
modules: ModuleNode[],
19+
read: () => string | Promise<string>,
2020
server: ViteDevServer
2121
): Promise<ModuleNode[] | void> {
22-
if (!file.endsWith('.vue')) {
23-
return
24-
}
25-
2622
const prevDescriptor = getDescriptor(file)
2723
if (!prevDescriptor) {
2824
// file hasn't been requested yet (e.g. async component)
2925
return
3026
}
3127

32-
let content = fs.readFileSync(file, 'utf-8')
33-
if (!content) {
34-
await untilModified(file)
35-
content = fs.readFileSync(file, 'utf-8')
36-
}
37-
3828
setPrevDescriptor(file, prevDescriptor)
29+
30+
const content = await read()
3931
const { descriptor } = createDescriptor(
4032
file,
4133
content,
@@ -147,26 +139,6 @@ export async function handleHotUpdate(
147139
return [...affectedModules].filter(Boolean) as ModuleNode[]
148140
}
149141

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-
170142
function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) {
171143
if (!a && !b) return true
172144
if (!a || !b) return false

packages/plugin-vue/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin {
6565
return {
6666
name: 'vite:vue',
6767

68-
handleHotUpdate,
68+
handleHotUpdate(file, mods, read, server) {
69+
if (!filter(file)) {
70+
return
71+
}
72+
return handleHotUpdate(file, mods, read, server)
73+
},
6974

7075
config(config) {
7176
// provide default values for vue runtime esm defines

0 commit comments

Comments
 (0)