Skip to content

Commit d832d2c

Browse files
committed
fix: ensure full compatibility with Laravel Blade templates
1 parent b67d1c7 commit d832d2c

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

packages/vite/src/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { basename, extname } from 'node:path'
1+
import { basename, extname, relative } from 'node:path'
22
import { statSync, mkdirSync, createReadStream } from 'node:fs'
33
import { writeFile, readFile, opendir, stat, rm } from 'node:fs/promises'
44
import type { Plugin, ResolvedConfig } from 'vite'
@@ -19,7 +19,7 @@ import {
1919
} from 'imagetools-core'
2020
import { createFilter, dataToEsm } from '@rollup/pluginutils'
2121
import sharp, { type Metadata, type Sharp } from 'sharp'
22-
import { createBasePath, generateImageID, hash } from './utils.js'
22+
import { createBasePath } from './utils.js'
2323
import type { VitePluginOptions } from './types.js'
2424

2525
export type {
@@ -132,11 +132,11 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
132132
error: (msg) => this.error(msg)
133133
}
134134

135-
const imageBuffer = await img.clone().toBuffer()
136-
137-
const imageHash = hash([imageBuffer])
138135
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+
140140
let image: Sharp | undefined
141141
let metadata: ImageMetadata
142142

@@ -170,6 +170,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
170170
} else {
171171
const fileHandle = this.emitFile({
172172
name: basename(pathname, extname(pathname)) + `.${metadata.format}`,
173+
originalFileName: id,
173174
source: image ? await image.toBuffer() : await readFile(`${cacheOptions.dir}/${id}`),
174175
type: 'asset'
175176
})

packages/vite/src/utils.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
import { createHash } from 'node:crypto'
2-
import type { ImageConfig } from 'imagetools-core'
3-
41
export const createBasePath = (base?: string) => {
52
return (base?.replace(/\/$/, '') || '') + '/@imagetools/'
63
}
7-
8-
export function generateImageID(config: ImageConfig, imageHash: string) {
9-
return hash([JSON.stringify(config), imageHash])
10-
}
11-
12-
export function hash(keyParts: Array<string | NodeJS.ArrayBufferView>) {
13-
let hash = createHash('sha1')
14-
for (const keyPart of keyParts) {
15-
hash = hash.update(keyPart)
16-
}
17-
return hash.digest('hex')
18-
}

0 commit comments

Comments
 (0)