From da8af107e94619ffab4f3abd71cc820f772bd3f3 Mon Sep 17 00:00:00 2001 From: brenner8023 Date: Fri, 1 Dec 2023 00:03:38 +0800 Subject: [PATCH] fix: hmr got error by transformSFC to inject code --- packages/plugin-vue/src/handleHotUpdate.ts | 2 +- packages/plugin-vue/src/index.ts | 12 ++++++--- packages/plugin-vue/src/main.ts | 2 +- .../plugin-vue/src/utils/descriptorCache.ts | 26 ++++++++++++++++--- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/packages/plugin-vue/src/handleHotUpdate.ts b/packages/plugin-vue/src/handleHotUpdate.ts index 233c8d89..cd537614 100644 --- a/packages/plugin-vue/src/handleHotUpdate.ts +++ b/packages/plugin-vue/src/handleHotUpdate.ts @@ -31,7 +31,7 @@ export async function handleHotUpdate( { file, modules, read }: HmrContext, options: ResolvedOptions, ): Promise { - const prevDescriptor = getDescriptor(file, options, false, true) + const prevDescriptor = await getDescriptor(file, options, false, true) if (!prevDescriptor) { // file hasn't been requested yet (e.g. async component) return diff --git a/packages/plugin-vue/src/index.ts b/packages/plugin-vue/src/index.ts index bb08d855..b61b713a 100644 --- a/packages/plugin-vue/src/index.ts +++ b/packages/plugin-vue/src/index.ts @@ -16,6 +16,7 @@ import { resolveCompiler } from './compiler' import { parseVueRequest } from './utils/query' import { getDescriptor, + getReadCode, getSrcDescriptor, getTempSrcDescriptor, } from './utils/descriptorCache' @@ -81,6 +82,7 @@ export interface ResolvedOptions extends Options { cssDevSourcemap: boolean devServer?: ViteDevServer devToolsEnabled?: boolean + readCode: ReturnType } export interface Api { @@ -100,6 +102,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { sourceMap: true, cssDevSourcemap: false, devToolsEnabled: process.env.NODE_ENV !== 'production', + readCode: getReadCode([]), }) const filter = computed(() => @@ -157,6 +160,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { configResolved(config) { options.value = { ...options.value, + readCode: getReadCode(config.plugins), root: config.root, sourceMap: config.command === 'build' ? !!config.build.sourcemap : true, cssDevSourcemap: config.css?.devSourcemap ?? false, @@ -191,7 +195,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { } }, - load(id, opt) { + async load(id, opt) { const ssr = opt?.ssr === true if (id === EXPORT_HELPER_ID) { return helperCode @@ -204,7 +208,7 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin { if (query.src) { return fs.readFileSync(filename, 'utf-8') } - const descriptor = getDescriptor(filename, options.value)! + const descriptor = (await getDescriptor(filename, options.value))! let block: SFCBlock | null | undefined if (query.type === 'script') { // handle