Reputation: 25
This is my config file, I have tried with html-loader it works fine for .html files but i’m now trying to learn and use handlebarsjs its not working the same. I've also tried using the preprocessor option for html-loader whereby i created a function to read .hbs files but I keep getting errors.
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: './src/index.js',
plugins: [
new HtmlWebpackPlugin({
title: 'Output Management',
template: './src/index.hbs'
}),
],
devtool: 'inline-source-map',
devServer: {
static: './dist',
hot: true,
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath:'/dist/',
clean:true,
},
mode: 'development',
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
generator: {
filename: 'assets/img/[hash][ext][query]'
}
},
{
test: /\.hbs$/i,
use: ['handlebars-loader'],
},
],
},
};
Upvotes: 2
Views: 878
Reputation: 8491
The following solution should work in Webpack 5:
webpack.config.js
, use inlineRequires
option for handlebars-loader
:{
test: /\.hbs$/i,
loader: 'handlebars-loader',
options: {
inlineRequires: '\/assets\/img\/'
}
},
<img src="./assets/img/image.png" />
From handlebars-loader
documentation, the inlineRequires
option:
Defines a regex that identifies strings within helper/partial parameters that should be replaced by inline require statements.
If you have multiple subfolders in assets (eg: /img
, /audio
, /video
), use the following regex:
inlineRequires: \/assets\/(:?img|audio|video)\/,
Upvotes: 0