Dynamic import
See original GitHub issueWill 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:
const imageLoader = (sizes: number[]) => {
return (src: string) => {
const res = {};
sizes.forEach((size: number) =>
res[size] = import(`${src}?width=${size}`)
);
return res;
};
};
I then use it to create image loader with predefined widths like this:
const img = imageLoader([100, 200, 300]);
Then when I need to import images anywhere in my app I do it like this:
{
main: img("./main.jpg"),
other: [
img("./01.jpg"),
img("./02.jpg"),
img("./03.jpg")
]
}
The value of the main
prop would be something along the lines of this:
{
aspectRatio: 0.6,
original: "./main.jpg",
variants: {
jpg: {
100: "cache/main.jpg?width=100",
200: "cache/main.jpg?width=200",
300: "cache/main.jpg?width=300"
}
}
}
I could then use it in JSX like this:
return (
<div>
<div style={`height:0;padding-bottom:${image.main.aspectRatio*100}%;`} />
<img src={image.main.variants.jpg[parentWidth]} />
</div>
);
Thanks to tree-shaking the resulting code would be minimal.
All of this is not possible using normal imports like:
import main from "/image.jpg?width=100";
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Dynamic import() - V8 JavaScript engine
Dynamic import () introduces a new function-like form of import that unlocks new capabilities compared to static import .
Read more >Dynamic imports - The Modern JavaScript Tutorial
Export and import statements that we covered in previous chapters are called “static”. The syntax is very simple and strict.
Read more >import - JavaScript - MDN Web Docs - Mozilla
The static import declaration is used to import read-only live bindings ... To load modules in non-module contexts, use the dynamic import ......
Read more >Advanced Features: Dynamic Import - Next.js
Dynamically import JavaScript modules and React Components and split your code into manageable chunks.
Read more >Dynamic Import - Patterns.dev
An easy way to dynamically import components in React is by using React Suspense. The React.Suspense component receives the component that should be...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#imports-must-end-with-a-file-extension
As soon as I remove the query string from the dynamic import, the build succeeds. But without any transformations obviously…
The
next
branch is now publishing on npm under the tag ‘next’. You can check it out by runningnpm install --save-dev vite-imagetools@next
This issue is addressed in that release.