1
1
import { createFilter , dataToEsm } from '@rollup/pluginutils'
2
2
import { Plugin , ResolvedConfig } from 'vite'
3
- import { put as cachePut , tmp } from 'cacache'
3
+ import { put as cachePut , tmp , ls as listCache , rm as removeFromCache } from 'cacache'
4
4
import fs from 'fs/promises'
5
5
import { join , relative } from 'path'
6
6
import findCacheDir from 'find-cache-dir'
@@ -36,6 +36,9 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
36
36
// wether to actually transform the images, disabled in devmode by default
37
37
let transformImages : boolean
38
38
39
+ // this set keeps track of all cacheId we have used during the build
40
+ let totalImages = new Set ( )
41
+
39
42
const filter = createFilter ( pluginOptions . include , pluginOptions . exclude )
40
43
41
44
const directives = [ ...pluginOptions . customDirectives , ...Object . values ( builtinDiretcives ) ]
@@ -51,7 +54,7 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
51
54
} ,
52
55
async load ( id ) {
53
56
const src = new URL ( id , 'file://' )
54
-
57
+
55
58
if ( ! filter ( src . href ) ) return null
56
59
57
60
// get all parameters from the url query string
@@ -62,7 +65,9 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
62
65
63
66
64
67
const outputMetadatas = await Promise . all ( pipelineConfigs . map ( async config => {
65
- const cacheId = JSON . stringify ( config ) // each image is addressed by its configuration
68
+ const cacheId = JSON . stringify ( config ) + src . href // each image is addressed by its configuration
69
+
70
+ totalImages . add ( cacheId )
66
71
67
72
let data : Uint8Array | undefined
68
73
let metadata : Record < string , any > = { }
@@ -121,9 +126,9 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
121
126
const dir = await tmp . mkdir ( pluginOptions . cache )
122
127
const tmpFile = join ( dir , `${ fileName } .${ metadata . format } ` )
123
128
124
- await fs . writeFile ( tmpFile , data || '' )
129
+ await fs . writeFile ( tmpFile , data || '' )
125
130
126
- metadata . src = relative ( viteConfig . root , tmpFile )
131
+ metadata . src = relative ( viteConfig . root , tmpFile )
127
132
}
128
133
129
134
return metadata
@@ -169,5 +174,20 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
169
174
return null
170
175
}
171
176
} ,
177
+ // cleanup the cache on build end
178
+ async buildEnd ( ) {
179
+ if ( pluginOptions . cache ) {
180
+ const cachedImages = await listCache ( pluginOptions . cache )
181
+
182
+ // we will delete all images that are in the cache that have not been referenced during the current build.
183
+ // This means that the image is likely not needed anymore
184
+ for ( const key in cachedImages ) {
185
+ if ( ! totalImages . has ( key ) ) {
186
+ await removeFromCache ( pluginOptions . cache , key )
187
+ console . log ( `deleted image ${ key } from cache` ) ;
188
+ }
189
+ }
190
+ }
191
+ }
172
192
}
173
193
}
0 commit comments