Skip to content

Commit 8c4f71b

Browse files
authored
Support React Server Components in Vite plugin and fix HMR (#751)
1 parent 64083a3 commit 8c4f71b

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

.changeset/silly-yaks-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': minor
3+
---
4+
5+
Add [experimental support for React Server Components](https://github.com/facebook/react/pull/22952)

.changeset/soft-lobsters-promise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/vite-plugin': patch
3+
---
4+
5+
Fix HMR

packages/vite-plugin/src/index.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,33 @@ export function vanillaExtractPlugin({
6868
virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
6969
},
7070
resolveId(source) {
71-
if (!source.endsWith(virtualExt)) {
71+
const [validId, query] = source.split('?');
72+
if (!validId.endsWith(virtualExt)) {
7273
return;
7374
}
7475

7576
// Absolute paths seem to occur often in monorepos, where files are
7677
// imported from outside the config root.
7778
const absoluteId = source.startsWith(config.root)
7879
? source
79-
: getAbsoluteVirtualFileId(source);
80+
: getAbsoluteVirtualFileId(validId);
8081

8182
// There should always be an entry in the `cssMap` here.
8283
// The only valid scenario for a missing one is if someone had written
8384
// a file in their app using the .vanilla.js/.vanilla.css extension
8485
if (cssMap.has(absoluteId)) {
85-
return absoluteId;
86+
// Keep the original query string for HMR.
87+
return absoluteId + (query ? `?${query}` : '');
8688
}
8789
},
8890
load(id) {
89-
if (!cssMap.has(id)) {
91+
const [validId] = id.split('?');
92+
93+
if (!cssMap.has(validId)) {
9094
return;
9195
}
9296

93-
const css = cssMap.get(id);
97+
const css = cssMap.get(validId);
9498

9599
if (typeof css !== 'string') {
96100
return;
@@ -102,21 +106,23 @@ export function vanillaExtractPlugin({
102106

103107
return outdent`
104108
import { injectStyles } from '@vanilla-extract/css/injectStyles';
105-
109+
106110
const inject = (css) => injectStyles({
107-
fileScope: ${JSON.stringify({ filePath: id })},
111+
fileScope: ${JSON.stringify({ filePath: validId })},
108112
css
109113
});
110114
111115
inject(${JSON.stringify(css)});
112116
113-
import.meta.hot.on('${styleUpdateEvent(id)}', (css) => {
117+
import.meta.hot.on('${styleUpdateEvent(validId)}', (css) => {
114118
inject(css);
115-
});
119+
});
116120
`;
117121
},
118122
async transform(code, id, ssrParam) {
119-
if (!cssFileFilter.test(id)) {
123+
const [validId] = id.split('?');
124+
125+
if (!cssFileFilter.test(validId)) {
120126
return null;
121127
}
122128

@@ -128,10 +134,7 @@ export function vanillaExtractPlugin({
128134
ssr = ssrParam?.ssr;
129135
}
130136

131-
const index = id.indexOf('?');
132-
const validId = index === -1 ? id : id.substring(0, index);
133-
134-
if (ssr) {
137+
if (ssr && !process.env.VITE_RSC_BUILD) {
135138
return addFileScope({
136139
source: code,
137140
filePath: normalizePath(validId),
@@ -149,7 +152,7 @@ export function vanillaExtractPlugin({
149152
for (const file of watchFiles) {
150153
// In start mode, we need to prevent the file from rewatching itself.
151154
// If it's a `build --watch`, it needs to watch everything.
152-
if (config.command === 'build' || file !== id) {
155+
if (config.command === 'build' || file !== validId) {
153156
this.addWatchFile(file);
154157
}
155158
}
@@ -183,10 +186,16 @@ export function vanillaExtractPlugin({
183186
cssMap.get(absoluteId) !== source
184187
) {
185188
const { moduleGraph } = server;
186-
const module = moduleGraph.getModuleById(absoluteId);
189+
const [module] = Array.from(
190+
moduleGraph.getModulesByFile(absoluteId) || [],
191+
);
187192

188193
if (module) {
189194
moduleGraph.invalidateModule(module);
195+
196+
// Vite uses this timestamp to add `?t=` query string automatically for HMR.
197+
module.lastHMRTimestamp =
198+
(module as any).lastInvalidationTimestamp || Date.now();
190199
}
191200

192201
server.ws.send({

0 commit comments

Comments
 (0)