From 49dac87060404aeae67e20923ed330500cbf368f Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 28 Dec 2020 16:33:58 -0500 Subject: [PATCH] wip: tweaks for vitepress --- packages/plugin-vue/src/handleHotUpdate.ts | 34 ++-------------------- packages/plugin-vue/src/index.ts | 7 ++++- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/packages/plugin-vue/src/handleHotUpdate.ts b/packages/plugin-vue/src/handleHotUpdate.ts index f2b25cf2..6071115b 100644 --- a/packages/plugin-vue/src/handleHotUpdate.ts +++ b/packages/plugin-vue/src/handleHotUpdate.ts @@ -1,4 +1,3 @@ -import fs from 'fs' import _debug from 'debug' import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc' import { @@ -17,25 +16,18 @@ const debug = _debug('vite:hmr') export async function handleHotUpdate( file: string, modules: ModuleNode[], + read: () => string | Promise, server: ViteDevServer ): Promise { - if (!file.endsWith('.vue')) { - return - } - const prevDescriptor = getDescriptor(file) if (!prevDescriptor) { // file hasn't been requested yet (e.g. async component) return } - let content = fs.readFileSync(file, 'utf-8') - if (!content) { - await untilModified(file) - content = fs.readFileSync(file, 'utf-8') - } - setPrevDescriptor(file, prevDescriptor) + + const content = await read() const { descriptor } = createDescriptor( file, content, @@ -147,26 +139,6 @@ export async function handleHotUpdate( return [...affectedModules].filter(Boolean) as ModuleNode[] } -// vitejs/vite#610 when hot-reloading Vue files, we read immediately on file -// change event and sometimes this can be too early and get an empty buffer. -// Poll until the file's modified time has changed before reading again. -async function untilModified(file: string) { - const mtime = fs.statSync(file).mtimeMs - return new Promise((r) => { - let n = 0 - const poll = async () => { - n++ - const newMtime = fs.statSync(file).mtimeMs - if (newMtime !== mtime || n > 10) { - r(0) - } else { - setTimeout(poll, 10) - } - } - setTimeout(poll, 10) - }) -} - function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) { if (!a && !b) return true if (!a || !b) return false diff --git a/packages/plugin-vue/src/index.ts b/packages/plugin-vue/src/index.ts index 220a6356..b693af55 100644 --- a/packages/plugin-vue/src/index.ts +++ b/packages/plugin-vue/src/index.ts @@ -65,7 +65,12 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { return { name: 'vite:vue', - handleHotUpdate, + handleHotUpdate(file, mods, read, server) { + if (!filter(file)) { + return + } + return handleHotUpdate(file, mods, read, server) + }, config(config) { // provide default values for vue runtime esm defines