Skip to content

Commit 2289d04

Browse files
sibbngpatak-dev
andauthored
fix(plugin-vue): trigger css hmr on custom template languages (#6987)
Co-authored-by: patak <[email protected]>
1 parent 638b168 commit 2289d04

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

packages/plugin-vue/src/handleHotUpdate.ts

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ export async function handleHotUpdate(
146146
// template is inlined into main, add main module instead
147147
if (!templateModule) {
148148
affectedModules.add(mainModule)
149+
} else if (mainModule && !affectedModules.has(mainModule)) {
150+
const styleImporters = [...mainModule.importers].filter((m) =>
151+
/\.css($|\?)/.test(m.url)
152+
)
153+
styleImporters.forEach((m) => affectedModules.add(m))
149154
}
150155
}
151156
if (didUpdateStyle) {

playground/tailwind/__test__/tailwind.spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
editFile,
44
untilUpdated,
55
getColor,
6+
getBgColor,
67
browserLogs,
78
page
89
} from '~utils'
@@ -64,4 +65,24 @@ if (!isBuild) {
6465

6566
browserLogs.length = 0
6667
})
68+
69+
test('regenerate CSS and HMR (pug template)', async () => {
70+
browserLogs.length = 0
71+
const el = await page.$('.pug')
72+
73+
expect(await getBgColor(el)).toBe('rgb(248, 113, 113)')
74+
75+
editFile('src/components/PugTemplate.vue', (code) =>
76+
code.replace('bg-red-400', 'bg-red-600')
77+
)
78+
79+
await untilUpdated(() => getBgColor(el), 'rgb(220, 38, 38)')
80+
81+
expect(browserLogs).toMatchObject([
82+
'[vite] css hot updated: /index.css',
83+
'[vite] hot updated: /src/components/PugTemplate.vue?vue&type=template&lang.js'
84+
])
85+
86+
browserLogs.length = 0
87+
})
6788
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template lang="pug">
2+
.bg-red-400.pug Pug template
3+
</template>

playground/tailwind/src/views/Page.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
Tailwind style
99
</div>
1010
<HelloWorld />
11+
<PugTemplate />
1112
</div>
1213
</template>
1314

1415
<script lang="ts">
1516
import { defineComponent, ref } from 'vue'
1617
import HelloWorld from '../components/HelloWorld.vue'
18+
import PugTemplate from '../components/PugTemplate.vue'
1719
1820
export default defineComponent({
19-
components: { HelloWorld },
21+
components: { HelloWorld, PugTemplate },
2022
setup() {
2123
const val = ref(0)
2224

0 commit comments

Comments
 (0)