Skip to content

Commit

Permalink
rename "checksum" to "dataHash" and "checksumFile" to "hashFile"
Browse files Browse the repository at this point in the history
  • Loading branch information
pzerelles committed Mar 26, 2024
1 parent 611987d commit 67aeeee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from 'imagetools-core'
import { createFilter, dataToEsm } from '@rollup/pluginutils'
import sharp, { type Metadata, type Sharp } from 'sharp'
import { checksumFile, createBasePath, hash, generateImageID } from './utils.js'
import { hashFile, createBasePath, hash, generateImageID } from './utils.js'
import type { VitePluginOptions } from './types.js'

export type {
Expand Down Expand Up @@ -118,12 +118,12 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
const cacheID = cacheOptions.enabled ? hash([relativeID]) : undefined
if (cacheID && cacheOptions.dir && existsSync(`${cacheOptions.dir}/${cacheID}/index.json`)) {
try {
const srcChecksum = await checksumFile('sha1', pathname)
const { checksum, metadatas } = JSON.parse(
const srcHash = await hashFile(pathname)
const { dataHash, metadatas } = JSON.parse(
await readFile(`${cacheOptions.dir}/${cacheID}/index.json`, { encoding: 'utf8' })
)

if (srcChecksum === checksum) {
if (srcHash === dataHash) {
const date = new Date()
utimes(`${cacheOptions.dir}/${cacheID}/index.json`, date, date)

Expand Down Expand Up @@ -206,7 +206,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
const relativeID = id.startsWith(processPath) ? id.slice(processPath.length + 1) : id
const cacheID = hash([relativeID])
try {
const checksum = await checksumFile('sha1', pathname)
const dataHash = await hashFile(pathname)
await mkdir(`${cacheOptions.dir}/${cacheID}`, { recursive: true })
await Promise.all(
outputMetadatas.map(async (metadata) => {
Expand All @@ -222,7 +222,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
await writeFile(
`${cacheOptions.dir}/${cacheID}/index.json`,
JSON.stringify({
checksum,
dataHash,
created: Date.now(),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
metadatas: outputMetadatas.map(({ src, image, ...metadata }) => metadata)
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function hash(keyParts: Array<string | NodeJS.ArrayBufferView>) {
return hash.digest('hex')
}

export const checksumFile = (algorithm: string, path: string) => {
export const hashFile = (path: string) => {
return new Promise<string>(function (resolve, reject) {
const hash = createHash(algorithm).setEncoding('hex')
const hash = createHash('sha1').setEncoding('hex')
createReadStream(path)
.pipe(hash)
.on('error', reject)
Expand Down

0 comments on commit 67aeeee

Please sign in to comment.