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

Optimize images only when required #189

Closed
arso1er opened this issue Aug 11, 2020 · 3 comments
Closed

Optimize images only when required #189

arso1er opened this issue Aug 11, 2020 · 3 comments

Comments

@arso1er
Copy link

arso1er commented Aug 11, 2020

Hi,

I just installed this package to optimize images on a page of my project. I used require('path/to/image?webp');. I expected images to be optimized only when I use the require statement and not when I reference the path directly, but all my images are being optimized. Is there a way to solve this ?

Thanks.

@arso1er
Copy link
Author

arso1er commented Aug 11, 2020

After some search, it turns out it's a webpack thing. Webpack will process all images in the folder when dynamic require is used with a variable.
@cyrilwanner Can you confirm there's no way to bypass this?

@cyrilwanner
Copy link
Owner

This is indeed a webpack limitation. The only way would be to move all images you want to optimize into a separate folder and use that folder as a prefix in your require statement (require('./images/webp/${img}')). Webpack will then only optimize the ones in ./images/webp/.

@arso1er
Copy link
Author

arso1er commented Aug 12, 2020

OK. Guess I'll roll my own solution server side with sharp.

Thanks.

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

2 participants