Skip to content

Commit c1383f8

Browse files
authored
fix(rsc): avoid unnecessary server hmr due to tailwind module deps (#658)
1 parent c602225 commit c1383f8

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

packages/plugin-rsc/e2e/basic.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,24 @@ function defineTest(f: Fixture) {
637637
'rgb(255, 0, 0)',
638638
)
639639
})
640+
641+
test('tailwind no redundant server hmr', async ({ page }) => {
642+
await page.goto(f.url())
643+
await waitForHydration(page)
644+
const logs: string[] = []
645+
page.on('console', (msg) => {
646+
if (msg.type() === 'log') {
647+
logs.push(msg.text())
648+
}
649+
})
650+
f.createEditor('src/routes/tailwind/unused.tsx').resave()
651+
await page.waitForTimeout(200)
652+
f.createEditor('src/routes/tailwind/server.tsx').resave()
653+
await page.waitForTimeout(200)
654+
expect(logs).toEqual([
655+
expect.stringMatching(/\[vite-rsc:update\].*\/tailwind\/server.tsx/),
656+
])
657+
})
640658
})
641659

642660
test('temporary references @js', async ({ page }) => {

packages/plugin-rsc/e2e/fixture.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export function useFixture(options: {
142142
reset(): void {
143143
fs.writeFileSync(filepath, originalFiles[filepath]!)
144144
},
145+
resave(): void {
146+
fs.writeFileSync(filepath, current)
147+
},
145148
}
146149
}
147150

packages/plugin-rsc/examples/basic/src/framework/entry.browser.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ async function main() {
7070

7171
// implement server HMR by trigering re-fetch/render of RSC upon server code change
7272
if (import.meta.hot) {
73-
import.meta.hot.on('rsc:update', () => {
73+
import.meta.hot.on('rsc:update', (e) => {
74+
console.log('[vite-rsc:update]', e.file)
7475
fetchRscPayload()
7576
})
7677
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(<div className="unused">unused</div>)

packages/plugin-rsc/examples/basic/src/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import 'tailwindcss' source('./');
1+
@import 'tailwindcss';
22

33
button {
44
@apply bg-gray-100 mx-1 px-2 border hover:bg-gray-200 active:bg-gray-300;

packages/plugin-rsc/src/plugin.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,18 @@ export default function vitePluginRsc(
398398

399399
if (!isInsideClientBoundary(ctx.modules)) {
400400
if (this.environment.name === 'rsc') {
401+
// detect if this module is only created as css deps (e.g. tailwind)
402+
// (NOTE: this is not necessary since Vite 7.1.0-beta.0 https://github.com/vitejs/vite/pull/20391 )
403+
if (ctx.modules.length === 1) {
404+
const importers = [...ctx.modules[0]!.importers]
405+
if (
406+
importers.length > 0 &&
407+
importers.every((m) => m.id && isCSSRequest(m.id))
408+
) {
409+
return []
410+
}
411+
}
412+
401413
// transform js to surface syntax errors
402414
for (const mod of ctx.modules) {
403415
if (mod.type === 'js') {
@@ -426,6 +438,7 @@ export default function vitePluginRsc(
426438
// Server files can be included in client module graph, for example,
427439
// when `addWatchFile` is used to track js files as style dependency (e.g. tailwind)
428440
// In this case, reload all importers (for css hmr), and return empty modules to avoid full-reload.
441+
// (NOTE: this is not necessary since Vite 7.1.0-beta.0 https://github.com/vitejs/vite/pull/20391 )
429442
const env = ctx.server.environments.rsc!
430443
const mod = env.moduleGraph.getModuleById(ctx.file)
431444
if (mod) {

0 commit comments

Comments
 (0)