Skip to content

Commit

Permalink
some small adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
pzerelles committed Mar 26, 2024
1 parent 67aeeee commit 34d91a4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/guide/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
let viteConfig: ResolvedConfig
let basePath: string

const processPath = process.cwd()
const cwd = process.cwd()

const generatedImages = new Map<string, Sharp | ProcessedCachableImageMetadata>()

Expand Down Expand Up @@ -114,7 +114,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): 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 {
Expand Down Expand Up @@ -203,7 +203,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): 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)
Expand Down Expand Up @@ -285,6 +285,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): 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) {
Expand Down

0 comments on commit 34d91a4

Please sign in to comment.