From 7ea0839e2e69eb652d2cee01c3f1d8148f7290f1 Mon Sep 17 00:00:00 2001 From: JonasKruckenberg Date: Fri, 19 Mar 2021 18:24:34 +0100 Subject: [PATCH] fix: image id generation This resolves #34, by including the images path when generating the internal image ID. --- packages/vite/src/index.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/index.ts b/packages/vite/src/index.ts index 6b3ce292..14264b54 100644 --- a/packages/vite/src/index.ts +++ b/packages/vite/src/index.ts @@ -1,16 +1,15 @@ import { Plugin, ResolvedConfig } from "vite"; -import { parseURL, loadImageFromDisk, builtins, resolveConfigs, applyTransforms, generateTransforms, getMetadata } from 'imagetools-core' +import { parseURL, loadImageFromDisk, builtins, resolveConfigs, applyTransforms, generateTransforms, getMetadata, generateImageID } from 'imagetools-core' import { basename, extname, join } from 'path' import { createFilter, dataToEsm } from "@rollup/pluginutils"; import { builtinOutputFormats, urlFormat } from './output-formats' import MagicString from 'magic-string' import { OutputFormat, PluginOptions } from "./types"; -import findCacheDir from "find-cache-dir"; +import { createHash } from 'crypto' const defaultOptions: PluginOptions = { include: '**\/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}?*', exclude: 'public\/**\/*', - cache: findCacheDir({ name: 'imagetools' }) || false, silent: false } @@ -19,12 +18,12 @@ export default function imagetools(userOptions: Partial = {}): Pl const filter = createFilter(pluginOptions.include, pluginOptions.exclude) - const directives = pluginOptions.extendDirectives - ? pluginOptions.extendDirectives(builtins) + const directives = pluginOptions.extendDirectives + ? pluginOptions.extendDirectives(builtins) : builtins - const outputFormats = pluginOptions.extendOutputFormats - ? pluginOptions.extendOutputFormats(builtinOutputFormats) + const outputFormats = pluginOptions.extendOutputFormats + ? pluginOptions.extendOutputFormats(builtinOutputFormats) : builtinOutputFormats let viteConfig: ResolvedConfig @@ -49,7 +48,7 @@ export default function imagetools(userOptions: Partial = {}): Pl const outputMetadatas = [] for (const config of imageConfigs) { - const id = Buffer.from(JSON.stringify(config)).toString('base64') + const id = generateImageID({ ...config, src: src.pathname }) const { transforms } = generateTransforms(config, directives) const { image, metadata } = await applyTransforms(transforms, img) @@ -73,6 +72,8 @@ export default function imagetools(userOptions: Partial = {}): Pl outputMetadatas.push(metadata) } + // console.log(outputMetadatas); + let outputFormat: OutputFormat = urlFormat for (const [key, format] of Object.entries(outputFormats)) {