I'm using webpack to make cms templates.
In some css file, I'm using some images like background-image: url('../images/space-bg.jpg');
All images are in the webpack folder, and I want webpack to push them to the public folder.
For now I'm using in my index.js file some hardcoded rules such as import '../images/space-bg.jpg';
But I'm wondering if there is a way to handle them automatically?
Here is my webpack.config.js:
var path = require('path');
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
mode: 'development',
entry: './src/js/index.js',
output: {
filename: 'js/theme-[contentHash].js',
path: path.resolve('./assets'),
publicPath: 'themes/mazdigital/assets'
},
plugins: [
new HtmlWebpackPlugin({
filename: path.resolve(__dirname, 'layouts/default.htm'),
template: './src/layouts/default.htm'
}),
new MiniCssExtractPlugin({
filename: 'css/theme-[contenthash].css',
chunkFilename: '[id]-[contenthash].css',
}),
new CleanWebpackPlugin()
],
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader?url=false',
'postcss-loader'
],
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader',
options: {
name: 'fonts/[name].[ext]'
}
},
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name: 'images/[name].[ext]',
}
},
],
},
};