Skip to content

Commit

Permalink
fix: refresh images in dev mode when they change
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Nov 13, 2023
1 parent 11a406b commit 3365fe7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-insects-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'imagetools-core': major
---

breaking: remove utils
5 changes: 5 additions & 0 deletions .changeset/silent-cougars-wave.md
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
1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export * from './transforms/tint.js'
export * from './types.js'
export * from './builtins.js'
export * from './output-formats.js'
export * from './util.js'
export { parseURL, extractEntries } from './lib/parse-url.js'
export { resolveConfigs } from './lib/resolve-configs.js'
export { generateTransforms } from './lib/generate-transforms.js'
Expand Down
18 changes: 0 additions & 18 deletions packages/core/src/util.ts

This file was deleted.

12 changes: 5 additions & 7 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import {
builtins,
builtinOutputFormats,
extractEntries,
generateImageID,
generateTransforms,
getMetadata,
loadImage,
parseURL,
urlFormat,
resolveConfigs,
Expand All @@ -17,8 +15,8 @@ import {
type ProcessedImageMetadata
} from 'imagetools-core'
import { createFilter, dataToEsm } from '@rollup/pluginutils'
import type { Metadata, Sharp } from 'sharp'
import { createBasePath } from './utils.js'
import sharp, { type Metadata, type Sharp } from 'sharp'
import { createBasePath, generateImageID } from './utils.js'
import type { VitePluginOptions } from './types.js'

export type {
Expand Down Expand Up @@ -53,7 +51,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
let viteConfig: ResolvedConfig
let basePath: string

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

return {
name: 'imagetools',
Expand All @@ -72,7 +70,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
let lazyImg: Sharp
const lazyLoadImage = () => {
if (lazyImg) return lazyImg
return (lazyImg = loadImage(decodeURIComponent(srcURL.pathname)))
return (lazyImg = sharp(decodeURIComponent(srcURL.pathname)))
}

let lazyMetadata: Metadata
Expand Down Expand Up @@ -128,7 +126,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
const { image, metadata } = await applyTransforms(transforms, img.clone(), pluginOptions.removeMetadata)

if (viteConfig.command === 'serve') {
const id = generateImageID(srcURL, config)
const id = await generateImageID(srcURL, config, img)

Check warning on line 129 in packages/vite/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/src/index.ts#L129

Added line #L129 was not covered by tests
generatedImages.set(id, image)
metadata.src = basePath + id
} else {
Expand Down
28 changes: 28 additions & 0 deletions packages/vite/src/utils.ts
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()])
}

Check warning on line 23 in packages/vite/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/src/utils.ts#L11-L23

Added lines #L11 - L23 were not covered by tests

function hash(keyParts: Array<string | NodeJS.ArrayBufferView>) {
let hash = createHash('sha1');
for (const keyPart of keyParts) {
hash = hash.update(keyPart)
}
return hash.digest('hex')
}

Check warning on line 31 in packages/vite/src/utils.ts

View check run for this annotation

Codecov / codecov/patch

packages/vite/src/utils.ts#L25-L31

Added lines #L25 - L31 were not covered by tests

0 comments on commit 3365fe7

Please sign in to comment.