Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to import dynamic paths with Vite's new URL(url, import.meta.url)? #327

Closed
imShara opened this issue Apr 27, 2022 · 10 comments
Closed

Comments

@imShara
Copy link

imShara commented Apr 27, 2022

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 #321

@RodrigoSosa96
Copy link

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?

@MoogyG

This comment was marked as off-topic.

@benmccann benmccann changed the title How to use with "new URL(url, import.meta.url)" in vite? How to import dynamic paths with Vite's new URL(url, import.meta.url)? Jan 15, 2023
@holtergram

This comment was marked as off-topic.

@chojnicki
Copy link

chojnicki commented Mar 24, 2023

@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

@benmccann
Copy link
Collaborator

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
Copy link
Collaborator

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

@benmccann
Copy link
Collaborator

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
Copy link
Collaborator

The fix for this will be in the next version of Vite: vitejs/vite#13034

@chojnicki
Copy link

@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.

@benmccann
Copy link
Collaborator

Closing as fixed in Vite 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants