Comments (10)
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…
from imagetools.
The next
branch is now publishing on npm under the tag 'next'.
You can check it out by running npm install --save-dev vite-imagetools@next
This issue is addressed in that release.
from imagetools.
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,
};
from imagetools.
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?
from imagetools.
The answer is the famous "Yes! but..." 😄
Yes it works the way you proposed right out of the box, but there are a few caveats:
-
Glob import is very picky.
Lets say you have an import like this:import(`assets/${id}.jpg`)
.
Vite will try it's best to resolve the glob pattern and infer all possible values forid
. It it is able to do so, then no problem, your import statement works like a charm, but in my experience this is not always the case.
Vite seems to have some issues tracking the values through a lot of function calls and the like, so be careful.
(See https://vitejs.dev/guide/features.html#glob-import for more info) -
Avoid combinatory hell
Let's say you use your function like you said:const img = imageLoader([100, 200, 300])
and you then call it with 4 images,
a.jpg
,b.jpg
,c.jpg
andd.jpg
. Vite-imagetools now has to calculate 4 * 3 = 12 different images and this increases linearly with the number of images you import, soimg
is now a very expensive function to run.
This may be irrelevant to you, because you know full well what you're importing, but just keep an eye out.
(This turns extra crazy once you try to generate different output formats as well. For example the query${id}?width=100,200,300&format=webp,avif
will generate 6 images per input image. So 4 * 6 = 24 in the case above)
But all in all it's totally possible!
from imagetools.
I see I've read a few things about vite and I stumbled upon glob import.
Is width=100,200,300&format=webp,avif
just a pseudocode or can I import images like this? If yes it makes my image loader even simpler.
from imagetools.
The comma separated syntax is new with v3 it does not work with the current version.
But you can check out the next branch if you're curious.
It does not have real docs yet and it may be broken, so beware of the bugs 😄
from imagetools.
Huuu that's really cool! Thank you
from imagetools.
So it looks my imageLoader
falls exactly into the dynamic imports limitations https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations
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}`)
};
from imagetools.
Does it work when you create an alias for $images
which maps to your images
directory?
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}`)
};
from imagetools.
Related Issues (20)
- Upgrading Vite-imagetools 5.0.8 throws an error from 4.x version HOT 2
- generateImageID causes tests to fail on CI HOT 3
- Vite example has a typo HOT 1
- Renaming picture fallback to img is not reflected in the document HOT 1
- Waste less effort transforming images
- Dependency on sharp not declared correctly for yarn’s PnP loader HOT 9
- Vite plugin crash HOT 2
- Loading attribute HOT 1
- v6 broken due to missing sharp 0.33 binariers HOT 3
- Some images get rotated for some reason HOT 14
- crop from / "gravity" HOT 1
- Error: Unable to parse color from string: ##ffaa22
- Pre-transform error: Failed to parse source for import analysis because the content contains invalid JS syntax
- How to receive access to original image in `extendOutputFormats` after v7.0.0? HOT 2
- Resize image to fit in box
- Querying for GIF's metadata removes animation
- How to use with SSR
- Use the <Image> component HOT 2
- Plugin output is not reproducible HOT 1
- Wrong output path when building in Laravel project HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imagetools.