Skip to content

fix(plugin-vue): ensure HMR updates styles when SFC is treated as a type dependency #541

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

Merged
merged 2 commits into from
Mar 10, 2025
Merged
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
5 changes: 4 additions & 1 deletion packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function handleHotUpdate(
{ file, modules, read }: HmrContext,
options: ResolvedOptions,
customElement: boolean,
typeDepModules?: ModuleNode[],
): Promise<ModuleNode[] | void> {
const prevDescriptor = getDescriptor(file, options, false, true)
if (!prevDescriptor) {
Expand Down Expand Up @@ -172,7 +173,9 @@ export async function handleHotUpdate(
}
debug(`[vue:update(${updateType.join('&')})] ${file}`)
}
return [...affectedModules].filter(Boolean) as ModuleNode[]
return [...affectedModules, ...(typeDepModules || [])].filter(
Boolean,
) as ModuleNode[]
}

export function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null): boolean {
Expand Down
14 changes: 11 additions & 3 deletions packages/plugin-vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs'
import type { Plugin, ViteDevServer } from 'vite'
import type { ModuleNode, Plugin, ViteDevServer } from 'vite'
import { createFilter, normalizePath } from 'vite'
import type {
SFCBlock,
Expand Down Expand Up @@ -224,14 +224,22 @@ export default function vuePlugin(rawOptions: Options = {}): Plugin<Api> {
if (options.value.compiler.invalidateTypeCache) {
options.value.compiler.invalidateTypeCache(ctx.file)
}

let typeDepModules: ModuleNode[] | undefined
const matchesFilter = filter.value(ctx.file)
if (typeDepToSFCMap.has(ctx.file)) {
return handleTypeDepChange(typeDepToSFCMap.get(ctx.file)!, ctx)
typeDepModules = handleTypeDepChange(
typeDepToSFCMap.get(ctx.file)!,
ctx,
)
if (!matchesFilter) return typeDepModules
}
if (filter.value(ctx.file)) {
if (matchesFilter) {
return handleHotUpdate(
ctx,
options.value,
customElementFilter.value(ctx.file),
typeDepModules,
)
}
},
Expand Down
19 changes: 19 additions & 0 deletions playground/vue/ExportTypeProps1.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<div class="export-type-props1">{{ props }}</div>
</template>

<script lang="ts">
export interface FooProps {
msg: string
}
</script>

<script setup lang="ts">
const props = defineProps<FooProps>()
</script>

<style>
.export-type-props1 {
color: red;
}
</style>
8 changes: 8 additions & 0 deletions playground/vue/ExportTypeProps2.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<div class="export-type-props2">{{ props }}</div>
</template>

<script setup lang="ts">
import type { FooProps } from './ExportTypeProps1.vue'
const props = defineProps<FooProps>()
</script>
4 changes: 4 additions & 0 deletions playground/vue/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<PreCompiledExternalScoped />
<PreCompiledExternalCssModules />
<ParserOptions />
<ExportTypeProps1 msg="msg" />
<ExportTypeProps2 msg="msg" />
</template>

<script setup lang="ts">
Expand Down Expand Up @@ -66,6 +68,8 @@ import PreCompiledExternalScoped from './pre-compiled/external-scoped.vue'
import PreCompiledExternalCssModules from './pre-compiled/external-cssmodules.vue'
import ParserOptions from './ParserOptions.vue'
import HmrCircularReference from './HmrCircularReference.vue'
import ExportTypeProps1 from './ExportTypeProps1.vue'
import ExportTypeProps2 from './ExportTypeProps2.vue'

const TsGeneric = defineAsyncComponent(() => import('./TsGeneric.vue'))

Expand Down
14 changes: 14 additions & 0 deletions playground/vue/__tests__/vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ describe('macro imported types', () => {
),
)
})

test('should hmr when SFC is treated as a type dependency', async () => {
const cls1 = '.export-type-props1'
expect(await getColor(cls1)).toBe('red')
editFile('ExportTypeProps1.vue', (code) => code.replace('red', 'blue'))
await untilUpdated(() => getColor(cls1), 'blue')

const cls2 = '.export-type-props2'
editFile('ExportTypeProps1.vue', (code) => code.replace('msg: string', ''))
await untilUpdated(
() => page.textContent(cls2),
JSON.stringify({}, null, 2),
)
})
})

test('TS with generics', async () => {
Expand Down