Skip to content

Commit

Permalink
fix(plugin-vue): global hmr for some scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Nov 6, 2021
1 parent d383c2a commit 48806de
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/playground/vue/Hmr.vue
Expand Up @@ -6,6 +6,7 @@

<script setup lang="ts">
import { ref } from 'vue'
import Node from './Node.vue'
let foo: number = 0
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/vue/Node.vue
@@ -0,0 +1,3 @@
<template>
<div class="node">this is node</div>
</template>
14 changes: 14 additions & 0 deletions packages/playground/vue/__tests__/vue.spec.ts
Expand Up @@ -157,10 +157,24 @@ describe('hmr', () => {
await untilUpdated(() => page.textContent('.hmr-inc'), 'count is 100')
})

test('global hmr for some scenarios', async () => {
editFile('Hmr.vue', (code) =>
code.replace('</template>', ' <Node/>\n' + '</template>')
)
await untilUpdated(() => page.innerHTML('.node'), 'this is node')
})

test('should re-render when template is emptied', async () => {
editFile('Hmr.vue', () => '')
await untilUpdated(() => page.innerHTML('.hmr-block'), '<!---->')
})

test('should global hmr when setup with lang=ts or lang=tsx', async () => {
editFile('Hmr.vue', (code) =>
code.replace('</template>', ' <Node/>\n' + '</template>')
)
await untilUpdated(() => page.innerHTML('.node'), 'this is node')
})
})

describe('src imports', () => {
Expand Down
23 changes: 21 additions & 2 deletions packages/plugin-vue/src/handleHotUpdate.ts
@@ -1,5 +1,5 @@
import _debug from 'debug'
import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import { SFCBlock, SFCDescriptor, resolveTemplateUsageCheckString } from '@vue/compiler-sfc'
import {
createDescriptor,
getDescriptor,
Expand Down Expand Up @@ -40,7 +40,8 @@ export async function handleHotUpdate(

if (
!isEqualBlock(descriptor.script, prevDescriptor.script) ||
!isEqualBlock(descriptor.scriptSetup, prevDescriptor.scriptSetup)
!isEqualBlock(descriptor.scriptSetup, prevDescriptor.scriptSetup) ||
needGlobHMR(descriptor, prevDescriptor)
) {
let scriptModule: ModuleNode | undefined
if (descriptor.script?.lang && !descriptor.script.src) {
Expand Down Expand Up @@ -169,13 +170,31 @@ export function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null): boolean {
return keysA.every((key) => a.attrs[key] === b.attrs[key])
}

// fix vue issue #3176
function needGlobHMR(
prev: SFCDescriptor | null,
next: SFCDescriptor | null
): boolean {
const isDynamicUpdate = resolveTemplateUsageCheckString(prev!) !== resolveTemplateUsageCheckString(next!)
const prevSetup = prev!.scriptSetup
const nextSetup = next!.scriptSetup
const isSetupWithTS = !!(
prevSetup?.setup &&
nextSetup?.setup &&
(prevSetup.lang === 'ts' || prevSetup.lang === 'tsx')
&& (nextSetup.lang === 'ts' || nextSetup.lang === 'tsx')
)
return isDynamicUpdate && isSetupWithTS
}

export function isOnlyTemplateChanged(
prev: SFCDescriptor,
next: SFCDescriptor
): boolean {
return (
isEqualBlock(prev.script, next.script) &&
isEqualBlock(prev.scriptSetup, next.scriptSetup) &&
!needGlobHMR(prev, next) &&
prev.styles.length === next.styles.length &&
prev.styles.every((s, i) => isEqualBlock(s, next.styles[i])) &&
prev.customBlocks.length === next.customBlocks.length &&
Expand Down

0 comments on commit 48806de

Please sign in to comment.