From 34d91a4b9d430021488d581d117a851f427765bd Mon Sep 17 00:00:00 2001 From: Philipp Zerelles Date: Tue, 26 Mar 2024 23:01:06 +0100 Subject: [PATCH] some small adjustments --- docs/guide/caching.md | 2 +- packages/vite/src/__tests__/main.test.ts | 6 +++--- packages/vite/src/index.ts | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/guide/caching.md b/docs/guide/caching.md index a948fbbb..a945741e 100644 --- a/docs/guide/caching.md +++ b/docs/guide/caching.md @@ -27,7 +27,7 @@ You can disable caching or change the directory with options. When an image is no longer there or the transformation parameters change, the previously cached images will be removed after a configurable retention period. -The default retention is 86400 seconds. A value of 0 will disable this mechanism. +The default retention is 1 day (86400 seconds). A value of 0 will disable this mechanism. ``` // vite.config.js, etc diff --git a/packages/vite/src/__tests__/main.test.ts b/packages/vite/src/__tests__/main.test.ts index 3a4affcf..36b8cdc0 100644 --- a/packages/vite/src/__tests__/main.test.ts +++ b/packages/vite/src/__tests__/main.test.ts @@ -10,7 +10,7 @@ import { afterEach, describe, test, expect, it, vi } from 'vitest' import { createBasePath, hash } from '../utils' import { readFile, rm, utimes } from 'fs/promises' -const processPath = process.cwd() +const cwd = process.cwd() const extractCreated = (path: string) => readFile(path, { encoding: 'utf8' }) @@ -485,7 +485,7 @@ describe('vite-imagetools', () => { }) await build(config(300)) - const relativeRoot = root.startsWith(processPath) ? root.slice(processPath.length + 1) : root + const relativeRoot = root.startsWith(cwd) ? root.slice(cwd.length + 1) : root const cacheID = hash([`${relativeRoot}/pexels-allec-gomes-5195763.png?w=300`]) const indexPath = `${dir}/${cacheID}/index.json` const created = await extractCreated(indexPath) @@ -518,7 +518,7 @@ describe('vite-imagetools', () => { ] }) - const relativeRoot = root.startsWith(processPath) ? root.slice(processPath.length + 1) : root + const relativeRoot = root.startsWith(cwd) ? root.slice(cwd.length + 1) : root const cacheID = hash([`${relativeRoot}/pexels-allec-gomes-5195763.png?w=300`]) const indexPath = `${dir}/${cacheID}/index.json` const created = await extractCreated(indexPath) diff --git a/packages/vite/src/index.ts b/packages/vite/src/index.ts index 6e3f1d61..8042d648 100644 --- a/packages/vite/src/index.ts +++ b/packages/vite/src/index.ts @@ -62,7 +62,7 @@ export function imagetools(userOptions: Partial = {}): Plugin let viteConfig: ResolvedConfig let basePath: string - const processPath = process.cwd() + const cwd = process.cwd() const generatedImages = new Map() @@ -114,7 +114,7 @@ export function imagetools(userOptions: Partial = {}): Plugin error: (msg) => this.error(msg) } - const relativeID = id.startsWith(processPath) ? id.slice(processPath.length + 1) : id + const relativeID = id.startsWith(cwd) ? id.slice(cwd.length + 1) : id const cacheID = cacheOptions.enabled ? hash([relativeID]) : undefined if (cacheID && cacheOptions.dir && existsSync(`${cacheOptions.dir}/${cacheID}/index.json`)) { try { @@ -203,7 +203,7 @@ export function imagetools(userOptions: Partial = {}): Plugin } if (cacheOptions.enabled) { - const relativeID = id.startsWith(processPath) ? id.slice(processPath.length + 1) : id + const relativeID = id.startsWith(cwd) ? id.slice(cwd.length + 1) : id const cacheID = hash([relativeID]) try { const dataHash = await hashFile(pathname) @@ -285,6 +285,7 @@ export function imagetools(userOptions: Partial = {}): Plugin }, async buildEnd(error) { + // clear expired cache entries if (!error && cacheOptions.enabled && cacheOptions.retention && viteConfig.command !== 'serve') { const dir = await opendir(cacheOptions.dir) for await (const dirent of dir) {