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

谁说我不能喝 提交于 2019-12-02 02:20:35

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.

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