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

后端 未结 1 743
天命终不由人
天命终不由人 2020-12-21 14:28

I am trying to convert my jpeg images to the webpformat using the image-webpack-loader plugin in Webpack Encore. The following config successfully

相关标签:
1条回答
  • 2020-12-21 15:13

    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.

    0 讨论(0)
提交回复
热议问题