Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-vue): execute global hmr when lang=ts #5561

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/playground/vue/Hmr.vue
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div class="node">this is node</div>
</template>
7 changes: 7 additions & 0 deletions packages/playground/vue/__tests__/vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ 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'), '<!---->')
Expand Down
23 changes: 21 additions & 2 deletions packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
@@ -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