Skip to content

Commit

Permalink
verify cached image has > 0 bytes before use (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMerrifield authored Mar 28, 2024
1 parent 5b6c588 commit aa52dae
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { basename, extname } from 'node:path'
import { join } from 'node:path/posix'
import { existsSync, mkdirSync, createReadStream } from 'node:fs'
import { statSync, mkdirSync, createReadStream } from 'node:fs'
import { utimes, writeFile, readFile, opendir, stat, rm } from 'node:fs/promises'
import type { Plugin, ResolvedConfig } from 'vite'
import {
Expand Down Expand Up @@ -140,7 +140,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
let image: Sharp | undefined
let metadata: ImageMetadata

if (cacheOptions.enabled && existsSync(`${cacheOptions.dir}/${id}`)) {
if (cacheOptions.enabled && (statSync(`${cacheOptions.dir}/${id}`, { throwIfNoEntry: false })?.size ?? 0) > 0) {
const imagePath = `${cacheOptions.dir}/${id}`
metadata = (await sharp(imagePath).metadata()) as ImageMetadata
metadata.imagePath = imagePath
Expand Down

0 comments on commit aa52dae

Please sign in to comment.