-
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refresh images in dev mode when they change
- Loading branch information
Showing
6 changed files
with
43 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'imagetools-core': major | ||
--- | ||
|
||
breaking: remove utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'vite-imagetools': patch | ||
--- | ||
|
||
fix: refresh images in dev mode when they change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,31 @@ | ||
import { createHash } from 'node:crypto' | ||
import path from 'node:path' | ||
import { stat } from 'node:fs/promises' | ||
import { ImageConfig } from 'imagetools-core' | ||
import { Sharp } from 'sharp' | ||
|
||
export const createBasePath = (base?: string) => { | ||
return (base?.replace(/\/$/, '') || '') + '/@imagetools/' | ||
} | ||
|
||
export async function generateImageID(url: URL, config: ImageConfig, originalImage: Sharp) { | ||
if (url.host) { | ||
const baseURL = new URL(url.origin + url.pathname) | ||
const buffer = await originalImage.toBuffer() | ||
return hash([baseURL.href, JSON.stringify(config), buffer]) | ||
} | ||
|
||
// 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)) | ||
const { mtime } = (await stat(path.resolve(process.cwd(), url.pathname))) | ||
return hash([baseURL.href, JSON.stringify(config), mtime.getTime().toString()]) | ||
} | ||
|
||
function hash(keyParts: Array<string | NodeJS.ArrayBufferView>) { | ||
let hash = createHash('sha1'); | ||
for (const keyPart of keyParts) { | ||
hash = hash.update(keyPart) | ||
} | ||
return hash.digest('hex') | ||
} | ||