Skip to content

Commit

Permalink
fix(plugin-vue): fix hmr when emptying sfc file (#2142)
Browse files Browse the repository at this point in the history
fix #2128
  • Loading branch information
CHOYSEN authored Feb 24, 2021
1 parent 3a60f84 commit 1153565
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions packages/plugin-vue/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1153565

Please sign in to comment.