I began to use webpack in my React application for bundling images but they don't show up in the browser (there is an <img/>
tag, but no image). If I run "webpack" images are present in the public folder with hashed names and if I run "webpack-dev-server" it also shows that images are bundled, but still nothing in the browser.
This is my webpack configuration file:
module.exports = (env) => {
const isProduction = env === 'production';
const CSSExtract = new ExtractTextPlugin('styles.css');
return {
entry: ['babel-polyfill', './src/app.js'],
output: {
path: path.resolve(__dirname, './public/scripts'),
filename: 'bundle.js'
},
module: {
rules: [{
loader: 'babel-loader',
test: /\.js$/,
exclude: /node_modules/
},
{
test: /\.(png|jp(e*)g|svg)$/,
use: [{
loader: 'url-loader',
options: {
limit: 8000, // Convert images < 8kb to base64 strings
name: 'images/[hash]-[name].[ext]'
}
}]
},
{
test: /\.s?css$/,
use: CSSExtract.extract({
use : [
{
loader : 'css-loader',
options : {
sourceMap : true // source map for css in development
}
},
{
loader : 'sass-loader',
options : {
sourceMap : true
}
},
{
loader: "jshint-loader"
}
]
})
},
]
},
plugins : [
CSSExtract,
],
devtool: isProduction ? 'source-map' : 'cheap-eval-source-map', // source map for locating a bug in source files, not in the bundle
devServer: {
contentBase: path.join(__dirname, "public"),
publicPath: "/scripts/",
historyApiFallback: true // this line is for client-side routing
},
}
}
and this is how I add image to my application:
import img1 from '../../assets/img/ticket.jpg';
and this is how I use it in render()
method.
<img src={img1}/>
and I see this in the browser devtools but image itself does not show up:
<img src="images/9011429377e91d003e5b64251ac3b9a8-ticket.jpg">
How can I fix it and make images show up?