2

I am trying to use WebP images in my react project which is:

1- Ejected (Yes it is very stupid of me-but i was very unexperienced)

2- Using webpack

so I store images in assets/images where I have .png, .svg, .jpg, .jpeg and .webp extensions. This is how I pass both images in a component like this:

import FemaleDoctorWebp from "../../../../../assets/images/women.webp";
import FemaleDoctor from "../../../../../assets/images/women.png";

 <picture>
   <source srcSet={FemaleDoctorWebp} type="image/webp" />
   <source srcSet={FemaleDoctor} />
   <img alt="Female doctor image" src={FemaleDoctor} />
 </picture>

And this is my webpack.api.config.js:

const path = require("path");
const getEnvironmentConstants = require("./getEnvironmentConstants");

module.exports = {
  mode: "development",
  devtool: "cheap-module-source-map",
  entry: ["./src/index.js"],
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name]-bundle.js",
    publicPath: "/dist/"
  },
  devServer: {
    hot: true,
    historyApiFallback: {
      disableDotRule: true
    }
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        include: /node_modules/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              importLoaders: true
            }
          }
        ]
      },
      {
        test: /\.(s*)css$/,
        exclude: /node_modules/,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              modules: {
                localIdentName: "[name]__[local]__[hash:base64:5]"
              },
              importLoaders: true,
              sourceMap: true
            }
          },
          {
            loader: "postcss-loader",
            options: {
              plugins: () => [require("autoprefixer")()]
            }
          }
        ]
      },
      // images
      {
        test: /\.(png|jp(e*)g|svg|webp|gif)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 8000, // Convert images < 8kb to base64 strings
              name: "images/[hash]-[name].[ext]"
            }
          }
        ]
      },
      //File loader used to load fonts
      {
        test: /\.(gif|png|jpe?g|svg|webp)$/i,
        use: [
          'file-loader',
          {
            loader: 'image-webpack-loader',
            options: {
              webp: {
                quality: 80
              }
            }
          }
        ]
      }
    ]
  },
  resolve: {
    modules: [path.resolve(__dirname, "src"), "node_modules"],
    alias: {
      assets: path.resolve(__dirname, "src/assets/")
    }
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": getEnvironmentConstants()
    })
  ]
};

This is what I get in HTML DOM:

<picture>
  <source srcset="data:image/webp;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICJlNzgwZTk0OTBiNzU1OGZlNDY2NjNiNTllNDg5Mjg4MS53ZWJwIjs=" type="image/webp">
  <source srcset="data:image/png;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICI0MWNmYjk2NGVkMTJmNTc3MWFhYWJkZWU0YmY1MTIyZS5wbmciOw==">
  <img alt="Female doctor image" src="data:image/png;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICI0MWNmYjk2NGVkMTJmNTc3MWFhYWJkZWU0YmY1MTIyZS5wbmciOw==">
</picture>

But it looks like ReactJS doesn't load the image properly. I tried to use other npm packages but no chance. Is my webpack config not right for this? Or I need to an extra step to use in HTML?

Any help or direction?

0 Answers0