(problem solved!)
I am trying to use html-loader
and file-loader
to pack my image source which is mentioned in index.html
img
tag src
attribute.
But it's not working because the img
tag src
is not refering to the correct path.
Just want to know is there anything wrong within my config?
my webpack
config:
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
entry: {
main: ['./src/index.js', './src/scss/main.scss'],
},
output: {
filename: '[name].js',
chunkFilename: '[name].min.js',
path: resolve(__dirname, 'build'),
},
mode: 'development',
module: {
rules: [
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
}
],
},
{
test: /\.scss$/,
use: [
'style-loader',
'css-loader',
'sass-loader'
]
},
{
test: /\.(jpg|png|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: '/img/',
pubicPath: '/img/'
}
}
],
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './index.html'
})
]
}
./build folder after run webpack
my index.html
(I also tried to change the img src attribute to "./img/women.jpg" or "/img/women.jpg" or "img/women.jpg", but nothing work but just make webpack compiling error..)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="./src/img/women.jpg" alt="">
</body>
</html>
built index.html
in ./build
folder
(so what is this 620b11833eb3b1be1f33.jpg
?)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script defer src="main.js"></script></head>
<body>
<img src="620b11833eb3b1be1f33.jpg" alt="">
</body>
</html>