-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Dynamic import #5
Comments
The answer is the famous "Yes! but..." 😄
But all in all it's totally possible! |
I see I've read a few things about vite and I stumbled upon glob import. Is |
The comma separated syntax is new with v3 it does not work with the current version. |
Huuu that's really cool! Thank you |
The This issue is addressed in that release. |
So it looks my This is also not possible: const query = "format=webp&width=1920&metadata";
const imageDir = "../../../images";
const imageNames = [
"01.jpg", "02.jpg", "03.jpg", "04.jpg", "05.jpg"
];
const images = imageNames
.map(fileName => import(`${imageDir}/${fileName}?${query}`)); Not even this: const images = {
"01.jpg": import(`${imageDir}/01.jpg?${query}`),
"02.jpg": import(`${imageDir}/02.jpg?${query}`),
"03.jpg": import(`${imageDir}/03.jpg?${query}`),
"04.jpg": import(`${imageDir}/04.jpg?${query}`),
"05.jpg": import(`${imageDir}/05.jpg?${query}`)
}; Only this works: const images = {
"01.jpg": import(`../../../images/01.jpg?${query}`),
"02.jpg": import(`../../../images/02.jpg?${query}`),
"03.jpg": import(`../../../images/03.jpg?${query}`),
"04.jpg": import(`../../../images/04.jpg?${query}`),
"05.jpg": import(`../../../images/05.jpg?${query}`)
}; |
Does it work when you create an alias for const images = {
"01.jpg": import(`$images/01.jpg?${query}`),
"02.jpg": import(`$images/02.jpg?${query}`),
"03.jpg": import(`$images/03.jpg?${query}`),
"04.jpg": import(`$images/04.jpg?${query}`),
"05.jpg": import(`$images/05.jpg?${query}`)
}; |
I am using this plugin and it works fine in development mode. But whenever I try to build via adapter-static the dynamic import fails. I think it has something to do with this limitation: As soon as I remove the query string from the dynamic import, the build succeeds. But without any transformations obviously… |
Currently fighting with this, did you find a workaround? |
Perhaps some jank, but here's my workaround: const images = import.meta.glob('../example/path/*.png', {
query: { width: '100,200,300' },
eager: true,
});
return {
image: images[`../example/path/${myVar}.png`]?.default || null,
}; |
Will it be possible in the v3 to use
import("./image.jpg?width=100")
?Why would this be useful? Let's say I create imageLoader function like this:
I then use it to create image loader with predefined widths like this:
Then when I need to import images anywhere in my app I do it like this:
The value of the
main
prop would be something along the lines of this:I could then use it in JSX like this:
Thanks to tree-shaking the resulting code would be minimal.
All of this is not possible using normal imports like:
The text was updated successfully, but these errors were encountered: