sveltejs / kit

web development, streamlined
https://kit.svelte.dev
MIT License
18.44k stars 1.89k forks source link

Allow passing imagetools parameters when dynamically importing images for enhanced:img #12608

Open Extarys opened 3 weeks ago

Extarys commented 3 weeks ago

Describe the problem

Technically it is a bug, because it should work, but seeing the source code it was not implemented at all.

I'm looking for a way to set the quality of all images by default, and then crop and tint a dynamically some imported image. Obviously, this issue is geared toward having imagetools work for dynamic import first :).

To add to @flippbit list of things that doesn't work: (https://github.com/sveltejs/kit/issues/11535#issuecomment-1941956125)

import MyImage from './path/to/your/image.jpg?enhanced&quality=80&tint=#ffaa22';

Throw an error:

Cannot find module './path/to/your/image.jpg?enhanced&quality=80' or its corresponding type declarations.ts(2307)

Sidenote: Nuxt Img can have a global configuration to set default quality, etc: https://image.nuxt.com/get-started/configuration

*EDIT Thanks to a discord mod: It should be implemented and is a type error: https://github.com/JonasKruckenberg/imagetools/issues/160#issuecomment-965021959

sveltejs/enhanced-img type: https://github.com/sveltejs/kit/blob/6056ba30e29ac5747c356fbf1a42dd71f2c4aa1f/packages/enhanced-img/types/ambient.d.ts

Describe the proposed solution

Implement

import MyImage from './path/to/your/image.jpg?enhanced&quality=80&tint=#ffaa22';

Alternatives considered

Make enhanced:img accept a variable as src for loops.

Importance

i cannot use SvelteKit without it

Additional Information

Not having this is kind of a nightmare IMO when trying to optimize images as much as possible and when using components for almost everything (Hero banner, Section's background, Image loop, galleries, etc all must use dynamic import of the images if using enhanced:img.

I didn't find a way yet on how to use it with user uploaded content since it change the image at build time.

vvnsrzn commented 1 week ago

What you can do it's to:

import MyImage from '../assets/01.jpg?w=1280;640;400&enhanced';

and update your ambient.d.ts

declare module '*?enhanced' {
    import type { Picture } from 'vite-imagetools';

    const value: Picture;
    export default value;
}

declare module '*&enhanced' {
    import type { Picture } from 'vite-imagetools';

    const value: Picture;
    export default value;
}

It works for me.

However, I agree with you that we should be able to dynamically import images more easily.

This is how I achieved this :

    const imageModules = Object.entries(
        import.meta.glob(`$lib/assets/portfolio/*.jpg`, {
            eager: true,
            query: { enhanced: true, w: '1280;640;400' } // you can pass some options here!
        })
    ).filter(([k]) => data.portfolio && k.includes(data.portfolio.id));

Not sure if it helps, I'm a Svelte newbie, but feel free to forward a minimal reproduction example, I will get a look!

Extarys commented 1 week ago

Thanks for chiming in.

Yes, someone on discord pointed out that it should work, and it's only a TS issue, so I simply add //@ts-ignore every time I use that haha and now it works. Thanks for the ambient template, that is a big quality of life improvement... Where is that file? Directly in the node_module for enhanced image ?

I never used the meta.blob, I find it polute the code and reduce readability by a lot, but if need be I guess I'll have no choice. I'll keep that snippet somewhere :D

vvnsrzn commented 1 week ago

You're welcome!

Where is that file? Directly in the node_module for enhanced image ?

Yes! https://github.com/sveltejs/kit/blob/main/packages/enhanced-img/types/ambient.d.ts