@@ -2,6 +2,8 @@ import fsp from 'node:fs/promises'
2
2
import fg from 'fast-glob'
3
3
import { join , resolve } from 'pathe'
4
4
import { imageMeta } from 'image-meta'
5
+ import { BirpcGroupReturn } from 'birpc'
6
+ import { debounce } from 'perfect-debounce'
5
7
import type { AssetInfo , AssetType , ImageMeta , ViteRPCFunctions } from './types'
6
8
7
9
const defaultAllowedExtensions = [
@@ -52,9 +54,14 @@ function guessType(path: string): AssetType {
52
54
interface SetupAssetsOptions {
53
55
root : string
54
56
base : string
57
+ server : any
58
+ getRpcServer : ( ) => BirpcGroupReturn < ViteRPCFunctions >
55
59
}
56
60
57
61
export function setupAssetsRPC ( config : SetupAssetsOptions ) {
62
+ const server = config . server
63
+ const getRpcServer = config . getRpcServer
64
+
58
65
const _imageMetaCache = new Map < string , ImageMeta | undefined > ( )
59
66
let cache : AssetInfo [ ] | null = null
60
67
@@ -103,6 +110,19 @@ export function setupAssetsRPC(config: SetupAssetsOptions) {
103
110
return cache
104
111
}
105
112
113
+ function watchAssets ( ) {
114
+ const debouncedAssetsUpdated = debounce ( ( ) => {
115
+ getRpcServer ( ) . assetsUpdated ( )
116
+ } , 100 )
117
+
118
+ server . watcher . on ( 'all' , ( event ) => {
119
+ if ( event !== 'change' )
120
+ debouncedAssetsUpdated ( )
121
+ } )
122
+ }
123
+
124
+ watchAssets ( )
125
+
106
126
return {
107
127
async getStaticAssets ( ) {
108
128
return await scan ( )
@@ -131,5 +151,6 @@ export function setupAssetsRPC(config: SetupAssetsOptions) {
131
151
return undefined
132
152
}
133
153
} ,
154
+ assetsUpdated ( ) { } ,
134
155
} satisfies Partial < ViteRPCFunctions >
135
156
}
0 commit comments