Create-react-app: Support for image compression

Created on 5 Dec 2016  路  5Comments  路  Source: facebook/create-react-app

In response to the following tweet
https://twitter.com/dan_abramov/status/805036037940592641
I would love to make a pull request for adding image compression. But i do have a concern and would like to have a discussion, before proceeding with the pull request.

The webpack loader for image compression i used is https://github.com/tcoopman/image-webpack-loader
I used the following config

{
      test: /\.(jpe?g|png|gif|svg)$/i,
       loaders: [
        'file?hash=sha512&digest=hex&name=static/media/[name].[hash:8].[ext]',
        'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
    ]
}

i deliberately omitted pngquant support as it has been reported buggy by several user using this library (though i haven't found any issues yet). https://github.com/tcoopman/image-webpack-loader/issues/45 https://github.com/tcoopman/image-webpack-loader/issues/42 . One other library to consider is https://github.com/thetalecrafter/img-loader. It is relatively less popular but haven't had issues and also it fixes issues for people who had trouble with image-webpack-loader.

Any opinions ?

proposal

Most helpful comment

@gaearon How about adding a precommit hook that runs imagemin only when there is an addition or an edit of an image?

All 5 comments

One downside of using a loader to compress images is that it has to run for every build. I compress my images with imagemin before adding em to the repo. It has a CLI and desktop app as well. Maybe suggesting that in the docs is another option?

I don't know if it's doable or not, but similar to how the build size change is reported, could the loader only re-compress the file if it isn't already in the build output from before? Somehow hash the original and just check if it's changed before trying to compress it.

@cesarandreu image-webpack-loader and image-loader both under the hood use imagemin itself.
Though you are correct that which each build these will run. This can be avoided for development, by simply using file loader for them.

In theory I鈥檇 like to include this but I think it鈥檚 impractical without a cross-machine caching mechanism. Having super slow builds with lots of images would be very frustrating given that JS builds aren鈥檛 fast anyway. So bailing on this unless somebody has a bright idea on how to make it happen without making (first) builds slow.

@gaearon How about adding a precommit hook that runs imagemin only when there is an addition or an edit of an image?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JimmyLv picture JimmyLv  路  3Comments

jnachtigall picture jnachtigall  路  3Comments

ap13p picture ap13p  路  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  路  3Comments

adrice727 picture adrice727  路  3Comments