From 1153565bdb728c2492294b0f92b61255e2bd9063 Mon Sep 17 00:00:00 2001 From: CHOYSEN Date: Wed, 24 Feb 2021 23:04:51 +0800 Subject: [PATCH] fix(plugin-vue): fix hmr when emptying sfc file (#2142) fix #2128 --- packages/plugin-vue/src/handleHotUpdate.ts | 2 +- packages/plugin-vue/src/main.ts | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/plugin-vue/src/handleHotUpdate.ts b/packages/plugin-vue/src/handleHotUpdate.ts index 734c6d4a..3b3ec974 100644 --- a/packages/plugin-vue/src/handleHotUpdate.ts +++ b/packages/plugin-vue/src/handleHotUpdate.ts @@ -144,7 +144,7 @@ export async function handleHotUpdate({ return [...affectedModules].filter(Boolean) as ModuleNode[] } -function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) { +export function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) { if (!a && !b) return true if (!a || !b) return false // src imports will trigger their own updates diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index d1996b08..645a5032 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -10,7 +10,7 @@ import { import { PluginContext, TransformPluginContext } from 'rollup' import { resolveScript } from './script' import { transformTemplateInMain } from './template' -import { isOnlyTemplateChanged } from './handleHotUpdate' +import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate' import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map' import { createRollupError } from './utils/error' @@ -71,11 +71,23 @@ export async function transformMain( )) } - const renderReplace = hasTemplateImport - ? ssr + let renderReplace = '' + if (hasTemplateImport) { + renderReplace = ssr ? `_sfc_main.ssrRender = _sfc_ssrRender` : `_sfc_main.render = _sfc_render` - : '' + } else { + // #2128 + // User may empty the template but we didn't provide rerender function before + if ( + prevDescriptor && + !isEqualBlock(descriptor.template, prevDescriptor.template) + ) { + renderReplace = ssr + ? `_sfc_main.ssrRender = () => {}` + : `_sfc_main.render = () => {}` + } + } // styles const stylesCode = await genStyleCode(descriptor, pluginContext)