I have a very simple Webpack 5 config, and I am trying to import images for use in my React app. I am able to import the image and use it without Webpack complaining:
import SomeImage from 'assets/some-image-jpg'
...
<img src={SomeImage} />
However, the image is not found in the browser. I am using webpack-manifest-plugin and the devServer option writeToDisk to serve the assets via Django. The JS and CSS work as expected.
const path = require('path');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
entry: './front/index.js',
output: {
filename: 'index-bundle.js',
path: path.resolve(__dirname, './local-static'),
publicPath: '/',
assetModuleFilename: 'img/[hash][ext][query]',
},
devServer: {
writeToDisk: true,
disableHostCheck: true,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: "babel-loader",
},
{
test: /\.(scss|css)$/,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
]
},
resolve: {
modules: [
path.join(__dirname, "front"),
"node_modules"
]
},
plugins: [
new MiniCssExtractPlugin({ filename: 'css/[name].css' }),
new WebpackManifestPlugin({ publicPath: './' }),
],
};
EDIT: Sample output of mainfest.json file:
{
"main.css": "./css/main.css",
"main.js": "./index-bundle.js",
"main.jpg": "./img/fbf8476a272a27c10e80.jpg",
"front/assets/no-image.jpg": "./img/fbf8476a272a27c10e80.jpg"
}
"front" is the name of the folder where the source files are stored.
EDIT 2: Contents of output folder:
├── css
│ └── main.css
├── img
│ └── fbf8476a272a27c10e80.jpg
├── index-bundle.js
└── manifest.json