How to load image files with webpack file-loader

后端 未结 6 1948
[愿得一人]
[愿得一人] 2020-11-27 09:13

I am using webpack to manage a reactjs project. I want to load images in javascript by webpack file-loader. Below is the

6条回答
  •  醉酒成梦
    2020-11-27 09:45

    I had an issue uploading images to my React JS project. I was trying to use the file-loader to load the images; I was also using Babel-loader in my react.

    I used the following settings in the webpack:

    {test: /\.(jpe?g|png|gif|svg)$/i, loader: "file-loader?name=app/images/[name].[ext]"},
    

    This helped load my images, but the images loaded were kind of corrupted. Then after some research I came to know that file-loader has a bug of corrupting the images when babel-loader is installed.

    Hence, to work around the issue I tried to use URL-loader which worked perfectly for me.

    I updated my webpack with the following settings

    {test: /\.(jpe?g|png|gif|svg)$/i, loader: "url-loader?name=app/images/[name].[ext]"},
    

    I then used the following command to import the images

    import img from 'app/images/GM_logo_2.jpg'
    

提交回复
热议问题