1
- import { basename , extname } from 'node:path'
1
+ import { basename , extname , relative } from 'node:path'
2
2
import { statSync , mkdirSync , createReadStream } from 'node:fs'
3
3
import { writeFile , readFile , opendir , stat , rm } from 'node:fs/promises'
4
4
import type { Plugin , ResolvedConfig } from 'vite'
@@ -19,7 +19,7 @@ import {
19
19
} from 'imagetools-core'
20
20
import { createFilter , dataToEsm } from '@rollup/pluginutils'
21
21
import sharp , { type Metadata , type Sharp } from 'sharp'
22
- import { createBasePath , generateImageID , hash } from './utils.js'
22
+ import { createBasePath } from './utils.js'
23
23
import type { VitePluginOptions } from './types.js'
24
24
25
25
export type {
@@ -132,11 +132,11 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
132
132
error : ( msg ) => this . error ( msg )
133
133
}
134
134
135
- const imageBuffer = await img . clone ( ) . toBuffer ( )
136
-
137
- const imageHash = hash ( [ imageBuffer ] )
138
135
for ( const config of imageConfigs ) {
139
- const id = generateImageID ( config , imageHash )
136
+ const relativePath = relative ( viteConfig . root , srcURL . pathname )
137
+ const tokens = Object . keys ( config ) . map ( key => `${ key } =${ config [ key ] } ` )
138
+ const id = ( tokens . length > 0 ) ? `${ relativePath } ?${ tokens . join ( '&' ) } ` : ''
139
+
140
140
let image : Sharp | undefined
141
141
let metadata : ImageMetadata
142
142
@@ -170,6 +170,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
170
170
} else {
171
171
const fileHandle = this . emitFile ( {
172
172
name : basename ( pathname , extname ( pathname ) ) + `.${ metadata . format } ` ,
173
+ originalFileName : id ,
173
174
source : image ? await image . toBuffer ( ) : await readFile ( `${ cacheOptions . dir } /${ id } ` ) ,
174
175
type : 'asset'
175
176
} )
0 commit comments