Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack html-webpack-plugin load favicons in template

I'm using Webpack with html-webpack-plugin and their provided template. I want to add a list of favicons in the header:

<link rel="apple-touch-icon" sizes="57x57" href="<%= htmlWebpackPlugin.extraFiles.apple-touch-icon-57x57 %>">
<link rel="apple-touch-icon" sizes="60x60" href="<%= htmlWebpackPlugin.extraFiles.favicons.fav60%>">
<link rel="apple-touch-icon" sizes="72x72" href="<%= htmlWebpackPlugin.extraFiles.favicons.fav72%>">
<link rel="apple-touch-icon" sizes="76x76" href="favicons/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="favicons/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="favicons/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="favicons/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="favicons/apple-touch-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="favicons/apple-touch-icon-180x180.png">
<link rel="icon" type="image/png" href="favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="favicons/android-chrome-192x192.png" sizes="192x192">
<link rel="icon" type="image/png" href="favicons/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="favicons/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="favicons/manifest.json">
<link rel="mask-icon" href="favicons/safari-pinned-tab.svg" color="#e53935">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="msapplication-TileImage" content="favicon/mstile-144x144.png">
<meta name="theme-color" content="#e53935">

How can I include all the favicons in my webpack build, with or without html-webpack-plugin?

I tried adding them as extraFiles like the docs say, but they don't end up in my build folder.

Note: The first 3 was me trying to something that didn't work.

like image 480
denislexic Avatar asked Feb 06 '16 10:02

denislexic


People also ask

What is HtmlWebpackPlugin for?

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.

Does webpack bundle HTML?

js in the console! This means Webpack has successfully bundled both our logic from src/index. js and HTML from src/index. html into the dist directory, even automatically linking them together for us.

What is HTML loader?

The html-loader will parse the URLs, require the images and everything you expect. The extract loader will parse the javascript back into a proper html file, ensuring images are required and point to proper path, and the asset modules will write the .html file for you. Example: webpack.config.js module.


2 Answers

You need to make sure, that the Images are processed by WebPack and thus that a matching loader exists for them (such as the file-loader).

For that to work, you have to explicitly require the files in the corresponding attributes. To be able to explicitly require files in the index.html you have to use a loader in turn for index.html itself, that allows for processing JS inline.

This one really depends on your setup (i.e. whether you have setup html-webpack-loader); have a look at the FAQ, explaining the basics.

So assuming, you have somewhat along this:

//somewhere in your webpack config.js

plugins: [

  new HtmlWebpackPlugin({
    template: 'index.html',
    inject: 'head',
  }) //..
]

You can require in your index.html images like that:

<link rel="apple-touch-icon" sizes="120x120" href="${require('./favicons/apple-touch-icon-120x120.png')}">

This will try to load apple-touch-icon-120x120.png via WebPack, so you must make sure that there is a loader for it and the html-loader needs to be configured as well:

//somewhere in your webpack.config.js
module: {
  loaders: [
    {
      test: /\.png$/,
      loader: 'file?name=assets/icons/[name].[hash].[ext]'
    },

    {
      test: /\.html$/,
      loader: 'html',
      query: {
        interpolate: 'require'
      }
    } //..

   ] //..
}

You only have to use require for images that are not inside <img> - tags, those will get picked up automagically by html-webpack-loader.

Future versions of html-loader might change this behaviour -> https://github.com/webpack/html-loader/issues/17

like image 121
Christian Ulbrich Avatar answered Sep 17 '22 14:09

Christian Ulbrich


With Webpack v4.17.2, and html-webpack-plugin v3.2.0, I only had to do:

new HtmlWebPackPlugin({
  favicon: "./path/to/favicon",
  filename: "index.html",
  template: "./path/to/index.html",
}),

in the plugins section of the webpack config.

like image 26
MattG Avatar answered Sep 16 '22 14:09

MattG