How to load image files with webpack file-loader
Asked Answered
S

7

178

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

const webpack = require('webpack');
const path = require('path');
const NpmInstallPlugin = require('npm-install-webpack-plugin');

const PATHS = {
    react: path.join(__dirname, 'node_modules/react/dist/react.min.js'),
    app: path.join(__dirname, 'src'),
    build: path.join(__dirname, './dist')
};

module.exports = {
    entry: {
        jsx: './app/index.jsx',
    },
    output: {
        path: PATHS.build,
        filename: 'app.bundle.js',
    },
    watch: true,
    devtool: 'eval-source-map',
    relativeUrls: true,
    resolve: {
        extensions: ['', '.js', '.jsx', '.css', '.less'],
        modulesDirectories: ['node_modules'],
        alias: {
            normalize_css: __dirname + '/node_modules/normalize.css/normalize.css',
        }
    },
    module: {
        preLoaders: [

            {
                test: /\.js$/,
                loader: "source-map-loader"
            },
        ],
        loaders: [

            {
                test: /\.html$/,
                loader: 'file?name=[name].[ext]',
            },
            {
                test: /\.jsx?$/,
                exclude: /node_modules/,
                loader: 'babel-loader?presets=es2015',
            },
            {test: /\.css$/, loader: 'style-loader!css-loader'},
            {test: /\.(jpe?g|png|gif|svg)$/i, loader: "file-loader?name=/public/icons/[name].[ext]"},
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loaders: ['babel-loader?presets=es2015']
            }
        ]
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
            },
            output: {
                comments: false,
            },
        }),
        new NpmInstallPlugin({
            save: true // --save
        }),
        new webpack.DefinePlugin({
            "process.env": {
                NODE_ENV: JSON.stringify("production")
            }
        }),
    ],
    devServer: {
        colors: true,
        contentBase: __dirname,
        historyApiFallback: true,
        hot: true,
        inline: true,
        port: 9091,
        progress: true,
        stats: {
            cached: false
        }
    }
}

I used this line to load image files and copy them to dist/public/icons directory and keep the same file name.

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

But I have two problems when using it. When I run webpack command, the image file was copied to dist/public/icons/ directory as expected. However it was also copied to dist directory with this file name "df55075baa16f3827a57549950901e90.png".

Below is my project structure: enter image description here

Another problem is that I used below code to import this image file but it is not showing on the browser. If I am using url 'public/icons/imageview_item_normal.png' on the img tag, it works fine. How to use the object imported from the image file?

import React, {Component} from 'react';
import {render} from 'react-dom';
import img from 'file!../../public/icons/imageview_item_normal.png'

export default class MainComponent extends Component {

  render() {
    return (
      <div style={styles.container}>
        download
        <img src={img}/>
      </div>
    )
  }

}

const styles = {
  container: {
    width: '100%',
    height: '100%',
  }
}
Stalagmite answered 7/6, 2016 at 5:40 Comment(2)
Have you tried removing the url-loader?Buckingham
I removed the url-loader from webpack config file and remove the dist directory and rerun webpack command, the problem is still there. The file was generated.Stalagmite
V
243

Regarding problem #1

Once you have the file-loader configured in the webpack.config, whenever you use import/require it tests the path against all loaders, and in case there is a match it passes the contents through that loader. In your case, it matched

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

// For newer versions of Webpack it should be
{
    test: /\.(jpe?g|png|gif|svg)$/i, 
    loader: 'file-loader',
    options: {
      name: '/public/icons/[name].[ext]'
    }
}

and therefore you see the image emitted to

dist/public/icons/imageview_item_normal.png

which is the wanted behavior.

The reason you are also getting the hash file name, is because you are adding an additional inline file-loader. You are importing the image as:

'file!../../public/icons/imageview_item_normal.png'.

Prefixing with file!, passes the file into the file-loader again, and this time it doesn't have the name configuration.

So your import should really just be:

import img from '../../public/icons/imageview_item_normal.png'

Update

As noted by @cgatian, if you actually want to use an inline file-loader, ignoring the webpack global configuration, you can prefix the import with two exclamation marks (!!):

import '!!file!../../public/icons/imageview_item_normal.png'.

Regarding problem #2

After importing the png, the img variable only holds the path the file-loader "knows about", which is public/icons/[name].[ext] (aka "file-loader? name=/public/icons/[name].[ext]"). Your output dir "dist" is unknown. You could solve this in two ways:

  1. Run all your code under the "dist" folder
  2. Add publicPath property to your output config, that points to your output directory (in your case ./dist).

Example:

output: {
  path: PATHS.build,
  filename: 'app.bundle.js',
  publicPath: PATHS.build
},
Vichy answered 7/6, 2016 at 7:32 Comment(6)
THIS IS THE ONLY DOCUMENT ON THE ENTIRE INTERNET that says "publicPath" will change the url used by the file loader.Pneumogastric
if i use it like this &name=/fonts/[name].[ext] it ignores the publicPath and if i use it like this &name=fonts/[name].[ext] it tries to search for it inside the relative path, for example if a package uses this font resource and is inside a vender folder it will search in [publicPath]/vendor/file but the file is saved 1 level above the vendor folder. (i'm using url-loader by the way but giving it a low limit so it falls back to file-loader)Interpose
Just to add to the answer. (please edit as I think this is useful) You if you find yourself in a situation where you want to bypass the webpack configuration on a require statement you can add two additional bangs before the import require('!!file-loader!wenis.png');Lenzi
Documentation has been greatly improved since above claims :) - See: github.com/webpack-contrib/file-loader#options and more info on publicPath: github.com/webpack-contrib/file-loader#publicpathOversell
Error: Compiling RuleSet failed: Query arguments on 'loader' has been removed in favor of the 'options' property (at ruleSet[1].rules[1].loader: file-loader?name=/img/[name].[ext])Sperrylite
@JoãoPimentelFerreira Updated configuration for newer versions of webpack, thanksVichy
M
24

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'
<div className="large-8 columns">

      <img  style={{ width: 300, height: 150 }} src={img} />
</div>
Musca answered 1/8, 2017 at 2:46 Comment(1)
THANK YOU! Tried so much and read your comment to use url-loader. Many posts say to use file-loader, which kept giving me a 404 while using webpack and react. I used {test: /\.(jpe?g|png|gif|svg)$/i, loader: "url-loader"} in the webpack config.Scopoline
S
12

Install file loader first:

$ npm install file-loader --save-dev

And add this rule in webpack.config.js

           {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'file-loader',
                    options: {}
                }]
            }
Strobilaceous answered 29/7, 2018 at 19:59 Comment(0)
C
9

Alternatively you can write the same like

{
    test: /\.(svg|png|jpg|jpeg|gif)$/,
    include: 'path of input image directory',
    use: {
        loader: 'file-loader',
        options: {
            name: '[path][name].[ext]',
            outputPath: 'path of output image directory'
        }
    }
}

and then use simple import

import varName from 'relative path';

and in jsx write like <img src={varName} ..../>

.... are for other image attributes

Crawford answered 27/6, 2018 at 12:12 Comment(2)
This end part is very important for images inserted by jsx thanks @gaurav-paliwal. using import imgPath helped me resolve it and the images were copied to the dist/build folderAldus
That outputPath key helped me figuring out where to place files with subfolders and all that. The other answers here didn't work. Thanks a lot.Bennie
H
6

With the webpack5 you can use Asset Modules instead of file loader.

Asset Modules is a type of module that allows one to use asset files (fonts, icons, etc) without configuring additional loaders.Asset Modules type replaces by adding asset/resource emits a separate file and exports the URL. Previously achievable by using file-loader.

 module: {
   rules: [
     {
       test: /\.png/,
       type: 'asset/resource'
     }
   ]
 },

For more information please visit the documentation.

Hermosillo answered 19/8, 2022 at 20:41 Comment(0)
M
1

webpack.config.js

{
    test: /\.(png|jpe?g|gif)$/i,
    loader: 'file-loader',
    options: {
        name: '[name].[ext]',
    },
}

anyfile.html

<img src={image_name.jpg} />
Merrie answered 25/9, 2019 at 10:34 Comment(0)
I
0

This is my working example of our simple Vue component.

<template functional>
    <div v-html="require('!!html-loader!./../svg/logo.svg')"></div>
</template>
Iliac answered 21/1, 2020 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.