imagetools icon indicating copy to clipboard operation
imagetools copied to clipboard

Invoking on html

Open bronze opened this issue 2 years ago • 22 comments

Hi

Anyone knows if there is a way to call imagetools transformation directly from the html? Like <img src="./image.jpg?width=300;500;700&format=webp;avif;png" />

cheers

bronze avatar Sep 24 '21 19:09 bronze

@bronze if it's JSX, then try the following syntax :

<img src={(await import("./image.jpg?width=300;500;700&format=webp;avif;png")).default} />

RafidMuhymin avatar Nov 27 '21 09:11 RafidMuhymin

I am having the same problem when building a static website. Is there any other way to apply?

victoralbino avatar Dec 08 '21 19:12 victoralbino

Can one of you provide a quick repro that I can check out? This definitely doesn't seem right, so I'd like to investigate.

JonasKruckenberg avatar Dec 10 '21 09:12 JonasKruckenberg

I created demo repo for Vue here https://github.com/JonasKruckenberg/imagetools/discussions/214

ivanq3w avatar Dec 10 '21 09:12 ivanq3w

I created demo repo for Vue here

https://github.com/JonasKruckenberg/imagetools/discussions/214

As far as I can see that has nothing to do with importing from html though.

JonasKruckenberg avatar Dec 10 '21 09:12 JonasKruckenberg

Any update on this one? I would like to call the plugin directly on the img src as well

pierremouchan avatar Jan 30 '22 10:01 pierremouchan

It doesn't work for me either. All I did was create an index.html like below:

None of them seem to get processed by imagetools, except maybe the one with @imagetools - however it doesn't transform it in anyway. Did a previous version support this correcly? https://www.npmjs.com/package/vite-plugin-imageset @JonasKruckenberg

<html lang="en">
<head>
  <meta charset="UTF-8" />
</head>
<body>
<img class="videoCardPoster" src="@imagetools/../assets/img/placeholder-01.jpg?size=100x100" alt="placeholder" />
<img class="videoCardPoster" src="/assets/img/placeholder-01.jpg?size=100x100" alt="placeholder" />
<img class="videoCardPoster" src="assets/img/placeholder-01.jpg?size=100x100" alt="placeholder" />
<script type="module" src="/src/main.ts"></script>
</body>
</html>

supermoos avatar Feb 03 '22 13:02 supermoos

None of them seem to get processed by imagetools,

So again. The problem with all vite related file processing plugins is, that they are entirely dependent on vite picking up that specific import. If vite doesn't tell imagetools about an import there is not much I can do.

If you could create a minimal repro using the html you showed I will have a look if there is something I can do 👍🏻

Did a previous version support this correcly? https://www.npmjs.com/package/vite-plugin-imageset

Not at all, please don't use that version, it hasn't been updated in a year and is deprecated for a reason.

JonasKruckenberg avatar Feb 04 '22 08:02 JonasKruckenberg

This plugin actually works with html defined img src's, I wonder if it's approach can be used to add this functionality to this plugin, as your plugin all in all seems better: https://www.npmjs.com/package/vite-plugin-html-images

supermoos avatar Feb 04 '22 08:02 supermoos

This plugin actually works with html defined img src's, I wonder if it's approach can be used to add this functionality to this plugin, as your plugin all in all seems better: https://www.npmjs.com/package/vite-plugin-html-images

I know this approach is a possibility, but it's a workaround that requires this plugin to parse the html - which seems kinda awkward - so I have been avoiding this for a long time.

Maybe this is something to fix upstream in vite, but if nothing helps and enough people would benefit, this could be a solution.

I would still appreciate you creating a quick repro so I can test against it 👍🏻

JonasKruckenberg avatar Feb 04 '22 08:02 JonasKruckenberg

Sure I can do that. It would just be a blank project with your plugin installed, and 1 <img src="..."> in the index.html file. Is that okay?

supermoos avatar Feb 04 '22 08:02 supermoos

Yes that is perfect 👍🏻

JonasKruckenberg avatar Feb 04 '22 08:02 JonasKruckenberg

@JonasKruckenberg Here you go: https://github.com/supermoos/imagetools-issue

supermoos avatar Feb 04 '22 09:02 supermoos

I love the idea of using this great plugin for static HTML websites or even with handlebars partials. Would save me from lots of image resizing in Photoshop 😁

xIRaguit avatar Feb 06 '22 00:02 xIRaguit

FWIW: I too would like this to be possible. Currently not working here. Using Vite, Elm, Tailwind CSS: https://github.com/gacallea/anerandros.info/blob/main/src/elm/Main.elm#L508 -- not sure where the issue lies, posting just my 2c :)

gacallea avatar May 12 '22 08:05 gacallea

I've found myself wanting this for a basic Vite powered static site too.

lostPixels avatar Sep 01 '22 17:09 lostPixels

@JonasKruckenberg Anything we can do to help track this bug along?

supermoos avatar Sep 08 '22 13:09 supermoos

For Svelte projects you can use https://github.com/bluwy/svelte-preprocess-import-assets

benmccann avatar Sep 28 '22 18:09 benmccann

I would really appreciate this feature!

housejumper avatar Nov 24 '22 23:11 housejumper

So again. The problem with all vite related file processing plugins is, that they are entirely dependent on vite picking up that specific import. If vite doesn't tell imagetools about an import there is not much I can do.

Do you (or anyone else) happen to know if there is any upstream work to support this use case? Otherwise it might be a a good idea to raise this issue upstream.

This is potentially useful in many different circumstances: run sharp on raster graphics, run svgo on SVGs, generate different favicon resolutions, video/audio processing, select PDFs subset for etc.

septatrix avatar Nov 26 '22 23:11 septatrix

Just as a reference, I created "Invoke plugins for assets in html (e.g. img src)" upstream

septatrix avatar Nov 28 '22 14:11 septatrix

Are there any updates on this? This is a really necessary feature, as many build sites without a framework. Sometimes, for the simplest websites, they are not needed, but the ability to optimize images is very necessary, for which this plugin is ideal.

ghost avatar Jan 24 '23 13:01 ghost

There's an open pull request upstream (vitejs/vite#11138) but it's being held off and is planned on being implemented in Vite 5.

Maytha8 avatar Feb 25 '23 05:02 Maytha8