Skip to content

Commit

Permalink
fix: image id generation
Browse files Browse the repository at this point in the history
This resolves #34, by including the images path when generating the internal image ID.
  • Loading branch information
JonasKruckenberg committed Mar 19, 2021
1 parent 9e0673d commit 7ea0839
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -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
}

Expand All @@ -19,12 +18,12 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): 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
Expand All @@ -49,7 +48,7 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): 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)
Expand All @@ -73,6 +72,8 @@ export default function imagetools(userOptions: Partial<PluginOptions> = {}): Pl
outputMetadatas.push(metadata)
}

// console.log(outputMetadatas);

let outputFormat: OutputFormat = urlFormat

for (const [key, format] of Object.entries(outputFormats)) {
Expand Down

0 comments on commit 7ea0839

Please sign in to comment.