I have a project that uses webpack and react. I use the file-loader / url-loader to use images with the webpack/react setup. In my react project I do import Img from '/image/source/image.jpg' but if then try <img src={Img}> the src is a base64 code that is saying module.exports = __webpack_public_path__ + "images/code.61e3a3939c2f93f30ac21419625c9a4f.jpg"; and no image is showing. How can I fix this?
webpack.config.js
var webpack = require( 'webpack' )
var path = require( 'path' )
var BUILD_DIR = path.resolve( __dirname, 'src/client/public' );
var APP_DIR = path.resolve( __dirname, 'src/client/App' );
var config = {
mode : 'development',
entry : APP_DIR + '/index.jsx',
output : {
path : BUILD_DIR,
filename : 'bundle.js'
},
devServer : {
publicPath : '/',
contentBase : './src/client'
},
module : {
rules : [
{
test : /\.jsx|js?/,
include : APP_DIR,
exclude : /(node_modules)/,
loader : 'babel-loader',
query : {
presets : [ 'env', 'react' ]
}
},
{
test : /\.scss|.css$/,
loaders : [ 'style-loader', 'css-loader', 'sass-loader' ]
},
{
test : /\.(png|jp(e*)g|svg)$/,
use : [{
loader : 'url-loader',
options : {
limit : 8000,
name : 'images/[hash]-[name].[ext]'
}
}]
},
{
test : /\.(png|jp(e*)g|svg)$/,
use : {
loader: "file-loader",
options: {
name: "images/[name].[hash].[ext]"
}
}
}
]
}
};
module.exports = config;
public-pathandfile-loaderexportnamein Webpack config. You should show us the Webpack configuration you are using now. - Up209ddev-serverhavecontentBaseofsrc/client, it is supposed to serve assets from yourBUILD_DIRsince all of your assets processed by Webpackfile loaderswill be delivered there. - Up209d