Skip to content

Commit 7cf641c

Browse files
feat: add cache cleanup functionality
We go through the cache each time the build ends and check if each image has been referenced during the current build. If it was we keep it, if not this likely means the image is not needed anymore and we evict it
1 parent bedcca8 commit 7cf641c

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/index.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createFilter, dataToEsm } from '@rollup/pluginutils'
22
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'
44
import fs from 'fs/promises'
55
import { join, relative } from 'path'
66
import findCacheDir from 'find-cache-dir'
@@ -36,6 +36,9 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
3636
// wether to actually transform the images, disabled in devmode by default
3737
let transformImages: boolean
3838

39+
// this set keeps track of all cacheId we have used during the build
40+
let totalImages = new Set()
41+
3942
const filter = createFilter(pluginOptions.include, pluginOptions.exclude)
4043

4144
const directives = [...pluginOptions.customDirectives, ...Object.values(builtinDiretcives)]
@@ -51,7 +54,7 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
5154
},
5255
async load(id) {
5356
const src = new URL(id, 'file://')
54-
57+
5558
if (!filter(src.href)) return null
5659

5760
// get all parameters from the url query string
@@ -62,7 +65,9 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
6265

6366

6467
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)
6671

6772
let data: Uint8Array | undefined
6873
let metadata: Record<string, any> = {}
@@ -121,9 +126,9 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
121126
const dir = await tmp.mkdir(pluginOptions.cache)
122127
const tmpFile = join(dir, `${fileName}.${metadata.format}`)
123128

124-
await fs.writeFile(tmpFile, data || '')
129+
await fs.writeFile(tmpFile, data || '')
125130

126-
metadata.src = relative(viteConfig.root,tmpFile)
131+
metadata.src = relative(viteConfig.root, tmpFile)
127132
}
128133

129134
return metadata
@@ -169,5 +174,20 @@ export function imagetools(userOptions: Partial<PluginOptions> = {}): Plugin {
169174
return null
170175
}
171176
},
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+
}
172192
}
173193
}

0 commit comments

Comments
 (0)