imagetools icon indicating copy to clipboard operation
imagetools copied to clipboard

Question: Is it possible to make sure the image output is always webp?

Open wentallout opened this issue 2 years ago • 1 comments

I want to make all of the images on my website webp. I don't want to constantly type ?webp' behind the imports.

wentallout avatar Sep 17 '22 14:09 wentallout

defaultDirectives is exactly what you are looking for 👍

JonasKruckenberg avatar Sep 17 '22 20:09 JonasKruckenberg

defaultDirectives is exactly what you are looking for 👍

the doc doesn't look useful to me. where exactly do I put webp ? You need make the example more detailed

wentallout avatar Sep 28 '22 09:09 wentallout

@wentallout i solve this in my sveltekit project like this:

// vite.config.ts

plugins: [
   imagetools({
      defaultDirectives: () => {
         return new URLSearchParams({
            format: 'webp'
         })
      }
   })
]

and then i imported like this:

import image from '$lib/assets/image.png?&imagetools'

I don't know why i need to add ?&imagetools but otherwise it does not work, i hope it helps you.

niklasgrewe avatar Sep 28 '22 13:09 niklasgrewe

I don't know why i need to add ?&imagetools but otherwise it does not work, i hope it helps you.

Yeah, that seems like it's potentially a bug. It's coming from this line:

https://github.com/JonasKruckenberg/imagetools/blob/a5d1fb92b915e6b38925b363203aeed324d709cb/packages/vite/src/index.ts#L21

benmccann avatar Sep 28 '22 18:09 benmccann