@@ -68,29 +68,33 @@ export function vanillaExtractPlugin({
68
68
virtualExt = `.vanilla.${ config . command === 'serve' ? 'js' : 'css' } ` ;
69
69
} ,
70
70
resolveId ( source ) {
71
- if ( ! source . endsWith ( virtualExt ) ) {
71
+ const [ validId , query ] = source . split ( '?' ) ;
72
+ if ( ! validId . endsWith ( virtualExt ) ) {
72
73
return ;
73
74
}
74
75
75
76
// Absolute paths seem to occur often in monorepos, where files are
76
77
// imported from outside the config root.
77
78
const absoluteId = source . startsWith ( config . root )
78
79
? source
79
- : getAbsoluteVirtualFileId ( source ) ;
80
+ : getAbsoluteVirtualFileId ( validId ) ;
80
81
81
82
// There should always be an entry in the `cssMap` here.
82
83
// The only valid scenario for a missing one is if someone had written
83
84
// a file in their app using the .vanilla.js/.vanilla.css extension
84
85
if ( cssMap . has ( absoluteId ) ) {
85
- return absoluteId ;
86
+ // Keep the original query string for HMR.
87
+ return absoluteId + ( query ? `?${ query } ` : '' ) ;
86
88
}
87
89
} ,
88
90
load ( id ) {
89
- if ( ! cssMap . has ( id ) ) {
91
+ const [ validId ] = id . split ( '?' ) ;
92
+
93
+ if ( ! cssMap . has ( validId ) ) {
90
94
return ;
91
95
}
92
96
93
- const css = cssMap . get ( id ) ;
97
+ const css = cssMap . get ( validId ) ;
94
98
95
99
if ( typeof css !== 'string' ) {
96
100
return ;
@@ -102,21 +106,23 @@ export function vanillaExtractPlugin({
102
106
103
107
return outdent `
104
108
import { injectStyles } from '@vanilla-extract/css/injectStyles';
105
-
109
+
106
110
const inject = (css) => injectStyles({
107
- fileScope: ${ JSON . stringify ( { filePath : id } ) } ,
111
+ fileScope: ${ JSON . stringify ( { filePath : validId } ) } ,
108
112
css
109
113
});
110
114
111
115
inject(${ JSON . stringify ( css ) } );
112
116
113
- import.meta.hot.on('${ styleUpdateEvent ( id ) } ', (css) => {
117
+ import.meta.hot.on('${ styleUpdateEvent ( validId ) } ', (css) => {
114
118
inject(css);
115
- });
119
+ });
116
120
` ;
117
121
} ,
118
122
async transform ( code , id , ssrParam ) {
119
- if ( ! cssFileFilter . test ( id ) ) {
123
+ const [ validId ] = id . split ( '?' ) ;
124
+
125
+ if ( ! cssFileFilter . test ( validId ) ) {
120
126
return null ;
121
127
}
122
128
@@ -128,10 +134,7 @@ export function vanillaExtractPlugin({
128
134
ssr = ssrParam ?. ssr ;
129
135
}
130
136
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 ) {
135
138
return addFileScope ( {
136
139
source : code ,
137
140
filePath : normalizePath ( validId ) ,
@@ -149,7 +152,7 @@ export function vanillaExtractPlugin({
149
152
for ( const file of watchFiles ) {
150
153
// In start mode, we need to prevent the file from rewatching itself.
151
154
// 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 ) {
153
156
this . addWatchFile ( file ) ;
154
157
}
155
158
}
@@ -183,10 +186,16 @@ export function vanillaExtractPlugin({
183
186
cssMap . get ( absoluteId ) !== source
184
187
) {
185
188
const { moduleGraph } = server ;
186
- const module = moduleGraph . getModuleById ( absoluteId ) ;
189
+ const [ module ] = Array . from (
190
+ moduleGraph . getModulesByFile ( absoluteId ) || [ ] ,
191
+ ) ;
187
192
188
193
if ( module ) {
189
194
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 ( ) ;
190
199
}
191
200
192
201
server . ws . send ( {
0 commit comments