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

ぃ、小莉子 提交于 2019-12-20 03:14:49

问题


I am trying to convert my jpeg images to the webpformat using the image-webpack-loader plugin in Webpack Encore. The following config successfully minifies my files but does not convert them to webp images.

webpack.config.js

test: /\.(gif|png|jpe?g|svg)$/i,
loader: 'image-webpack-loader',
options: {
   disable: true, //bypassOnDebug
   convertPathData: false,
   mozjpeg: { //works
      progressive: true,
      quality: '80-90'
    },
   webp: { //doesn't convert my images to webp
      quality: 75,
      enabled: true
   }
 }

How can I achieve what I want using the plugin image-webpack-loader? Or is there another plugin I should use alongside this one?


回答1:


Too late to be true, but just for future googlers:

It looks like info in image-webpack-loader is kind of misleading. What it can do is optimize already existing webp file to reduce its size.

While previous statement might be true or not, I haven't figured out how to convert images to webp with this loader.

Instead I used imagemin-webp to generate webps & then just imported it into a file passing it through image-webpack-loader & file-loader.

So, the final result was like:

import React from "react";
import waveImgJpg from "Images/common/wave.jpg";
import waveImgWebP from "Images/webp/wave.webp";
import styles from "IndexStyles/mainContent.scss";

export const MainContent = () => {
    return (
        <picture>
            <source className={styles.waveImg} srcSet={waveImgWebP} type="image/webp" />
            <img className={styles.waveImg} src={waveImgJpg} alt="beatiful wave" title="beatiful wave" />
        </picture>
    );
};

This code is a part of the set-up that I use to start new projects with config already in place.

So, if you're interested, you may find complete webpack.js as well as all other files here.



来源:https://stackoverflow.com/questions/53483962/webpack-encore-convert-images-to-webp-using-image-webpack-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!