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

Add image optimization #11279

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sumanthratna
Copy link

the first step towards implementing #10467

This PR implements image optimization via Webpack. All image optimizations are lossless; if a user wants to customize optimization then they should either eject or optimize images themselves without ejecting.

I have added a unit test (which passes)—the unoptimized image is ~66.5 KB while the optimized image is ~32.1 KB

@stale
Copy link

stale bot commented Jan 9, 2022

This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Jan 9, 2022
@stale stale bot removed the stale label Jan 9, 2022
`npm install image-minimizer-webpack-plugin imagemin imagemin-gifsicle
imagemin-jpegtran imagemin-optipng imagemin-svgo`
@sumanthratna
Copy link
Author

Hi @iansu, sorry for the ping, but just wanted to see if you're willing to merge this feature.

@iansu iansu added this to the 5.1 milestone Jan 26, 2022
@iansu
Copy link
Contributor

iansu commented Jan 26, 2022

Hi, thanks for the PR. I do think this seems like a good feature to include in our 5.1 release. There are a couple of things I'd like to do first:

  1. Confirm that all these image optimizations are in fact lossless and also don't ever result in larger file size
  2. Add a flag to opt out of image optimization (see https://create-react-app.dev/docs/advanced-configuration)

@sumanthratna
Copy link
Author

Thanks for the comments!

Confirm that all these image optimizations are in fact lossless and also don't ever result in larger file size

good catch, imagemin does not guarantee that the returned image is indeed smaller than the input: https://github.com/imagemin/imagemin/blob/6832717d644ac69afc402939f5bca0d5f297697f/test.js#L65-L85

also, imagemin is unmaintained (imagemin/imagemin#385) so I think we should use squoosh instead.

The only downside I see to using squoosh is that JPEG images are not guaranteed to be lossless (GoogleChromeLabs/squoosh#85). (I don't know how to generate input cases where the optimization would be lossy). What do you think? Can we proceed with squoosh?

@sumanthratna
Copy link
Author

@iansu friendly bump -- are you okay with using sqoosh for optimization per my previous message in this thread?

@YPCrumble
Copy link

@sumanthratna as a huge CRA fan I'm excited about this PR! And @iansu thank you for all your contributions here.

I just took a look at the NextJS ecosystem that has been doing image compression for a while and it seems they're moving to sharp over squoosh - perhaps we learn from their issues and use sharp?

I think it's hard to guarantee that image compression is lossless but to me that caveat can be documented with the flag to disable image compression, and any issues should be directed to the maintainers of sharp or whichever compression utility we're implementing.

Thanks for this PR @sumanthratna I'm looking forward to using it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants