Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load images through webpack when using HTMLWebpackPlugin?

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?

like image 515
Anonymous Avatar asked Nov 05 '17 20:11

Anonymous


People also ask

How do you use an image in HTML webpack?

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 .

Where do webpack images go?

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/.

What is the use of HTML webpack plugin?

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.

What is HTML-loader?

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.

What is the htmlwebpackplugin?

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.

How do I add images to a Webpack project?

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.

How do I generate an HTML5 File from a Webpack bundle?

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.

How does the File Loader work in Webpack?

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.


Video Answer


3 Answers

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.

like image 116
Eric Guan Avatar answered Oct 21 '22 01:10

Eric Guan


Using html-loader with HTML webpack plugin worked for me.

module: {
    rules: [
      {
         test: /\.(html)$/,
         use: ['html-loader']
      }
    ]
},
plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html'
    })
  ]
like image 24
Norma Mendonca Avatar answered Oct 21 '22 02:10

Norma Mendonca


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'.

like image 11
Voro Avatar answered Oct 21 '22 02:10

Voro