File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
packages/next-yak/loaders Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " next-yak " : patch
3+ ---
4+
5+ fix HMR for CSS in Vite
Original file line number Diff line number Diff line change @@ -279,6 +279,25 @@ export async function viteYak(
279279 }
280280 } ,
281281 } ,
282+
283+ // Vite's default HMR only updates the JS module when a source file changes.
284+ // The extracted CSS lives in a separate virtual module (virtual:yak-css:...)
285+ // which Vite doesn't know is derived from the source file. Without explicit
286+ // invalidation here, the browser keeps stale CSS after edits.
287+ hotUpdate ( { modules, file, type } ) {
288+ if ( type !== "update" && type !== "create" ) return ;
289+ if ( ! sourceFileRegex . test ( file ) ) return ;
290+
291+ // The SWC plugin generates virtual module paths relative to root
292+ // (via {{__MODULE_PATH__}}), so we must match that format.
293+ const relativePath = relative ( root , file ) ;
294+ const virtualId = "\0virtual:yak-css:" + relativePath + ".css" ;
295+ const mod = this . environment . moduleGraph . getModuleById ( virtualId ) ;
296+ if ( mod ) {
297+ this . environment . moduleGraph . invalidateModule ( mod ) ;
298+ return [ ...modules , mod ] ;
299+ }
300+ } ,
282301 } ;
283302}
284303
You can’t perform that action at this time.
0 commit comments