Skip to content

Commit

Permalink
remove url from hash
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianGonz97 committed Apr 30, 2024
1 parent edda6c7 commit e43d13f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ describe('vite-imagetools', () => {
test('is consistent', async () => {
const image = (await readdir(dir))[0]

expect(image).toBe('7a7bca50a264376853d5c77aba0bddc0163c02f9')
expect(image).toBe('325b80fade286c672ea884b87e65f7a3278a9f8a')
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin

const imageHash = hash([imageBuffer])
for (const config of imageConfigs) {
const id = generateImageID(srcURL, config, imageHash)
const id = generateImageID(config, imageHash)
let image: Sharp | undefined
let metadata: ImageMetadata

Expand Down
13 changes: 2 additions & 11 deletions packages/vite/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
import { createHash } from 'node:crypto'
import path from 'node:path'
import type { ImageConfig } from 'imagetools-core'

export const createBasePath = (base?: string) => {
return (base?.replace(/\/$/, '') || '') + '/@imagetools/'
}

export function generateImageID(url: URL, config: ImageConfig, imageHash: string) {
if (url.host) {
const baseURL = new URL(url.origin + url.pathname)
return hash([baseURL.href, JSON.stringify(config), imageHash])
}

// baseURL isn't a valid URL, but just a string used for an identifier
// use a relative path in the local case so that it's consistent across machines
const baseURL = new URL(url.protocol + path.relative(process.cwd(), url.pathname))
return hash([baseURL.href, JSON.stringify(config), imageHash])
export function generateImageID(config: ImageConfig, imageHash: string) {
return hash([JSON.stringify(config), imageHash])
}

export function hash(keyParts: Array<string | NodeJS.ArrayBufferView>) {
Expand Down

0 comments on commit e43d13f

Please sign in to comment.