imagetools icon indicating copy to clipboard operation
imagetools copied to clipboard

How to use with "new URL(url, import.meta.url)" in vite?

Open imShara opened this issue 2 years ago • 2 comments

Hello. I'm trying to use it with latest Vite with this construction:

const image = new URL(`/data/articles/${id}/picture.jpg?width=100`, import.meta.url).href
...
<img :src="image">

More about this construction

image

But image variable is undefined. What I miss? Should it works? Possible simmilar to https://github.com/JonasKruckenberg/imagetools/issues/321

imShara avatar Apr 27 '22 01:04 imShara

I am having the same problem, I have my imgs in a json to import them dynamically.

if there is no solution for this, there is no way to import a folder of pictures and transform them?

RodrigoSosa96 avatar Jul 11 '22 21:07 RodrigoSosa96

same here

MoogyG avatar Jul 23 '22 16:07 MoogyG

Same 😔 Does anyone have a workaround for this?

holtergram avatar Mar 17 '23 12:03 holtergram

@JonasKruckenberg could you maybe look at this? Would be great, because it's pretty important feature in Vite right now.

This works, and is also transformed by imagetools:

const demo = (x) => new URL(`/src/assets/images/demo/${x}.jpg`, import.meta.url).href

This does not:

const demo = (x) => new URL(`/src/assets/images/demo/${x}.jpg?w=100`, import.meta.url).href

chojnicki avatar Mar 24 '23 10:03 chojnicki

I am having the same problem, I have my imgs in a json to import them dynamically.

Please note from the Vite docs that "the URL string must be static so it can be analyzed"

if there is no solution for this, there is no way to import a folder of pictures and transform them?

You can import a folder of pictures and transform them with import.meta.glob. I just added it to the docs and example

benmccann avatar Apr 26 '23 19:04 benmccann

@chojnicki this is a bug in Vite. I can reproduce this without imagetools being involved. I filed an issue here: https://github.com/vitejs/vite/issues/13017

benmccann avatar Apr 26 '23 21:04 benmccann

I wanted to mention that I haven't found much of a use case for this feature yet.

To use an example, let's assume you've got something like in the issue description:

const image = new URL(`/data/articles/${id}/picture.jpg?width=100`, import.meta.url).href

You could also accomplish that by simply doing a relative import from the article page. You can also use import.meta.glob to build a list of all the matching images which you dynamically select from. I think if you really really needed to dynamically select just a single image and could afford to build a list of all the images you could probably do something like import.meta.glob in an API endpoint (+server.js for SvelteKit users) so that you only hold the list on the server.

If the article and image are coming from a dynamic source like a CMS then you're probably not going to be using new URL(url, import.meta.url) anyway.

I'd love to know if there's some use case that I'm overlooking.

benmccann avatar Apr 27 '23 03:04 benmccann

The fix for this will be in the next version of Vite: https://github.com/vitejs/vite/pull/13034

benmccann avatar May 04 '23 19:05 benmccann

@benmccann Thanks for handling vite issue so well. Will check that when new vite stable version will be released.

About use case - it's very simple. I was using import.meta.glob for a long time, but is has one important "issue". It will not give you file names, but full paths to files as keys. Because of this, it harder to use such file in for example loops or any other scenario where file name comes from variable. Let's say I have photos of all flags (they should be svg's, but whatever), and want to display flag corresponding to user country. :src="flags[user.country]" - should be simple, but it's not. To do that you need to map that object first, and make keys as flag names. Easy, few lines of code, but it's boilerplate you need to use in every place you need that, which is pain in the ass. You cannot easily extract that import.meta.glob logic elsewhere, because glob path of course cannot be variable.

chojnicki avatar May 05 '23 10:05 chojnicki

Closing as fixed in Vite 5

benmccann avatar Oct 18 '23 16:10 benmccann