11import { fileURLToPath } from "node:url" ;
2- import { createFilter , type FilterPattern , normalizePath , type Plugin } from "vite" ;
2+ import {
3+ createFilter ,
4+ EnvironmentModuleGraph ,
5+ type FilterPattern ,
6+ normalizePath ,
7+ type Plugin ,
8+ ViteDevServer ,
9+ } from "vite" ;
310import { compile , type CompileOptions } from "./compile.ts" ;
411
512export interface ServerFunctionsFilter {
@@ -108,6 +115,40 @@ class Debouncer<T> {
108115 }
109116}
110117
118+ function mergeManifestRecord (
119+ source : Set < string > ,
120+ target : Set < string > ,
121+ ) : { invalidPreload : boolean ; invalidated : string [ ] } {
122+ const current = source . size ;
123+ for ( const entry of target ) {
124+ source . add ( entry ) ;
125+ }
126+ return {
127+ invalidPreload : current !== source . size ,
128+ invalidated : [ ...source ] ,
129+ } ;
130+ }
131+
132+ function invalidateModule ( moduleGraph : EnvironmentModuleGraph , path : string ) {
133+ const target = moduleGraph . getModuleById ( path ) ;
134+ if ( target ) {
135+ moduleGraph . invalidateModule ( target ) ;
136+ }
137+ }
138+
139+ function invalidateModules (
140+ server : ViteDevServer | undefined ,
141+ result : ReturnType < typeof mergeManifestRecord > ,
142+ manifest : string ,
143+ ) : void {
144+ if ( server ) {
145+ if ( result . invalidPreload ) {
146+ invalidateModule ( server . environments . client . moduleGraph , manifest ) ;
147+ invalidateModule ( server . environments . ssr . moduleGraph , manifest ) ;
148+ }
149+ }
150+ }
151+
111152export function serverFunctionsPlugin ( options : ServerFunctionsOptions ) : Plugin [ ] {
112153 const filter = createFilter (
113154 options . filter ?. include || DEFAULT_INCLUDE ,
@@ -122,6 +163,7 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[]
122163 server : undefined ,
123164 client : undefined ,
124165 } ;
166+ let currentServer : ViteDevServer ;
125167
126168 return [
127169 {
@@ -130,6 +172,9 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[]
130172 configResolved ( config ) {
131173 env = config . mode !== "production" ? "development" : "production" ;
132174 } ,
175+ configureServer ( server ) {
176+ currentServer = server ;
177+ } ,
133178 } ,
134179 {
135180 name : "solid-start:server-functions/preload" ,
@@ -161,19 +206,23 @@ export function serverFunctionsPlugin(options: ServerFunctionsOptions): Plugin[]
161206 if ( ! filter ( id ) ) {
162207 return null ;
163208 }
164- const preloader = preload [ mode ] ;
165- if ( preloader ) {
166- preloader . defer ( ) ;
167- }
168209
169210 const result = await compile ( id ! , code , {
170- ...( mode === ' server' ? SERVER_OPTIONS : CLIENT_OPTIONS ) ,
211+ ...( mode === " server" ? SERVER_OPTIONS : CLIENT_OPTIONS ) ,
171212 mode,
172213 env,
173214 } ) ;
174215
175216 if ( result . valid ) {
176- manifest [ mode ] . add ( id ! ) ;
217+ const preloader = preload [ mode ] ;
218+ if ( preloader ) {
219+ preloader . defer ( ) ;
220+ }
221+ invalidateModules (
222+ currentServer ,
223+ mergeManifestRecord ( manifest . server , new Set ( [ id ! ] ) ) ,
224+ options . manifest ,
225+ ) ;
177226
178227 return {
179228 code : result . code || "" ,
0 commit comments