Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack - How to convert jpg/png to webp via image-webpack-loader

I want to generate webp files from jpg/png from webpack. To do that i using image-webpack-plugin (https://github.com/tcoopman/image-webpack-loader)

In the plugin documentation it's written that the webp additional optimizer "Compress JPG & PNG images into WEBP" (https://github.com/tcoopman/image-webpack-loader#usage) but after followed the documentation steps the conversion not work.

The files are exported in jpg but nothing is converted.

I've already followed these posts but i've don't understand how to translate in a "non-react" environment :

  1. Webpack imagemin plugin to compress jpg, png and create webp?

  2. Webpack (Encore): convert images to webp using image-webpack-loader

webpack.config.js

 { 
   test:/\.(gif|png|jpe?g|svg)$/i,
   use:[ 
      { 
         loader:'file-loader',
         options:{ 
            outputPath:'images',
            name:'[name].[ext]'
         }
      },
      { 
         loader:'image-webpack-loader',
         options:{ 
            mozjpeg:{ 
               progressive:true,
               quality:65
            },
            optipng:{ enabled: false },
            pngquant:{ quality: [ 0.65, 0.90 ], speed:4 },
            gifsicle:{ interlaced: false },
            webp:{ quality: 75 }
         }
      }
   ]
}

Is there a reliable and clean way to turn jpg / png files into webp via webpack ?

like image 488
PedroZorus Avatar asked Nov 12 '19 22:11

PedroZorus


People also ask

How do I save a PNG as a transparent WebP?

Right-click on the WEBP image and select the new option Save Image As PNG. That's it, now you can save the image in PNG format.

How do you save an image as a WebP?

To export an image to WebP, select a resource on the canvas, open the Export panel on the right hand side, and choose “WEBP” in the format dropdown. After you make your selection, click the Export Bitmap… button. The resulting dialog will predictably ask where you want the image to be exported to.

Can I convert a WebP image to JPEG or PNG?

Don’t worry, you can convert your WebP image to a more widely supported format like JPEG and PNG on any of your devices. This guide shows how you convert WebP images using Windows, macOS, iOS, and Android devices. What Is WebP? WebP is an image file format mainly used to serve images on the web.

Is it possible to generate WebP images with Webpack?

Compared to other build systems, webpack is an incredibly rich (and at times complex) tool. This example is not meant to be the authoritative approach to generating WebP images within webpack, but rather to show that it’s possible. Did You Like This Excerpt?

How do I convert a WebP image to MS Paint?

Right-click your WebP image and choose Open with followed by Paint. Your image should open in MS Paint. Click the File menu at the top and hover your mouse over Save as. You’ll see several image formats on the right of Save as. Click the format you want to convert your WebP image to.

What is WebP image format?

WebP is an image file format mainly used to serve images on the web. This format supports both lossless as well as lossy compressions. When you use WebP format for your images, the size tends to be much smaller than their JPEG or PNG counterparts. Because of these features, many websites use WebP as the default format for all their website images.


Video Answer


1 Answers

Finally, i've found a proper solution. For future people who will come here :

I no longer use image-webpack-loader but imagemin & imagemin-webp


Setting up :

  1. Verify you have imagemin & imagemin-webp installed before do anything.

  2. Create a webpack.config.prod.js file to create a specific image conversion script before the webpack build script.

Into the array ['src/images/*.{jpg,png}'] is the input, 'destination' the output. The output is via src to avoid to load unused stuff in the dist directory and it permit to a potential ejs plugin to require directly .webp files by a 'require' command.

const imagemin = require( "imagemin" )
const webp = require( "imagemin-webp" )

imagemin( ['src/images/*.{jpg,png}'], {
    destination: 'src/images',
    plugins: [
        webp( { quality: 60 } )
    ]
} )

  1. Create a "prescript" in package.json dedicated to the production
"scripts": {
    "preprod": "node webpack.config.prod.js",
    "prod": "npm webpack"
}

Sources :

Pre & Post Hooks

Imagemin Plugin

Imagemin Webp

like image 88
PedroZorus Avatar answered Sep 23 '22 18:09

PedroZorus