I want to add a content hash to the images generated in my webpack config. The files are output with a hash but I'm unsure how to get these inserted into my html. Html with is generated with pug and HtmlWebpackPlugin.
Relevant bits of webpack.config.js:
{
test: /\.(jpeg|jpg|png|gif)$/,
use: [
'file-loader?name=images/[name].[contenthash:4].[ext]',
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
progressive: true,
quality: 65
}
}
}
]
},
new HtmlWebpackPlugin({
template: './src/pug/index.pug',
filename: 'index.htm',
inject: true
})
Image in pug where i want the correct filename output:
img(src='/images/logo.png') <-- this needs to be img(src='/images/logo.64fd.png')
I had the same question; below are code samples from the solution I developed.
The main thing that helped me was the pug-loader README.md "Embedded resources" section: "Try to use require for all your embedded resources, to process them with webpack."
As long as you require your files properly, Webpack will add them to its dependency graph and process them according to the configuration rules that you define for each file extension type. This processing includes adding the
contenthashto your image filenames in the HTML output in accordance with the method you specify in the file-loaderoptions.nameconfig object.Webpack config:
Pug template:
I've also listed my dependency versions below; there may be differences in configuration due to package versioning but the same general principles should apply. Good luck!