I am using HTMLWebpackPlugin and in my template I have an img tag:
<img src="./images/logo/png">
If you notice, here I am using a relative path thinking webpack will trigger the file loader that's configured in my webpack.config.js file but after compilation I get the exact same src attribute in my html:
<img src="./images/logo/png">
How can I trigger webpack to dynamically replace these relative paths with, well whatever I've configured in my webpack configuration?
You can use file-loader to extract images. Then using html-loader you can specify which tag-attribute combination should be processed by this loader via the query parameter attrs .
Essentially, there is not much in Webpack to include your desired images for your web application. First, put your image files into one folder of your projects application. For instance, your src/ folder may have a folder assets/ which has a folder images/.
The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.
Disclaimer: html-loader is a third-party package maintained by community members, it potentially does not have the same support, security policy or license as webpack, and it is not maintained by webpack. Exports HTML as string. HTML is minimized when the compiler demands.
The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation. You can either let the plugin generate an HTML file for you, supply your own template using lodash templates, or use your own loader.
For instance, your src/ folder may have a folder assets/ which has a folder images/. Second, install a commonly used Webpack loader to include the images into your bundling process: And third, include the new loader in your Webpack configuration: ... ... ... It's quite similar to setting up fonts with Webpack.
You can either let the plugin generate an HTML file for you, supply your own template using lodash templates, or use your own loader. The plugin will generate an HTML5 file for you that includes all your webpack bundles in the body using script tags.
By default (if you don't specify any loader in any way) a fallback lodash loader kicks in. Under the hood it is using a webpack child compilation which inherits all loaders from your main configuration. Calling require on your img path will then call the file loader.
I'm not a webpack expert, but i got it to work by doing this:
<img src="<%=require('./src/assets/logo.png')%>">
Plugin config
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html'
}),
According to the docs: https://github.com/jantimon/html-webpack-plugin/blob/master/docs/template-option.md
By default (if you don't specify any loader in any way) a fallback lodash loader kicks in.
The <%= %>
signifies a lodash template
Under the hood it is using a webpack child compilation which inherits all loaders from your main configuration.
Calling require
on your img path will then call the file loader.
You may run into some path issues, but it should work.
Using html-loader with HTML webpack plugin worked for me.
module: {
rules: [
{
test: /\.(html)$/,
use: ['html-loader']
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html'
})
]
You should use the CopyWebpackPlugin
.
const CopyWebpackPlugin = require('copy-webpack-plugin');
plugins:[
....
new CopyWebpackPlugin({'patterns': [
{from:'./src/assets/images', to:'images'}
]}),
....
]
This is copy the src/assets/images
to your `distfolder/images'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With