How to inject custom meta tags in html-webpack-plugin?
Asked Answered
T

4

9

I use Webpack along with the plugin html-webpack-plugin. Based on an environment variable, I want to inject a <meta></meta> tag into the final index.html file.

How do I do this?

Typhoon answered 25/3, 2017 at 16:35 Comment(0)
P
23

You can define your own template. It's briefly mentioned in Writing Your Own Templates that you can pass any options you'd like to it and use them in the template with htmlWebpackPlugin.options:

htmlWebpackPlugin.options: the options hash that was passed to the plugin. In addition to the options actually used by this plugin, you can use this hash to pass arbitrary data through to your template.

For example you could define the author with the environment variable AUTHOR and add an author option to the plugin:

new HtmlWebpackPlugin({
  template: 'template.ejs',
  author: process.env.AUTHOR
})

In your template.ejs you can create a <meta> tag with that information:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <% if (htmlWebpackPlugin.options.author) { %>
    <meta name="author" content="<%= htmlWebpackPlugin.options.author %>">
    <% } %>
  </head>
  <body>
  </body>
</html>

You could use a .html file instead and the plugin will fallback to ejs-loader, but if you have html-loader configured for .html files, it will use that instead of the fallback, so the embedding won't work.

When AUTHOR is set it will include the meta tag with the author, otherwise it's not included. Running:

AUTHOR='Foo Bar' webpack

will include the following meta tag:

<meta name="author" content="Foo Bar">
Pulpwood answered 25/3, 2017 at 18:49 Comment(1)
great, this is what i was after :)Typhoon
M
11
   new HtmlWebpackPlugin({
     template: 'index.html',
     meta: {
       author: process.env.AUTHOR
     }
   });

resulting in the inclusion of the following within your head tag.

<meta name="author" content="Foo Bar">

Masseur answered 13/8, 2018 at 17:44 Comment(0)
P
2

Try html-webpack-tags-plugin

new HtmlWebpackTagsPlugin({
    metas: [{
        path: 'img/logo.png',
        attributes: {
            property: 'og:image'
        }
    },{
        attributes: {
            property: 'og:image:type',
            content: "image/png"
        }
    },{
        attributes: {
            property: 'og:image:width',
            content: "200"
        }
    },{
        attributes: {
            property: 'og:image:height',
            content: "200"
        }
    },]
}),
Photomontage answered 28/7, 2020 at 5:34 Comment(1)
it's not loading my imageTaneshatang
A
2

Following on @Will, in addition to name/content meta tags the value can be an object. The key/values of the object will be assigned to the meta tag.

new HtmlWebpackPlugin({
    template: 'index.html',
    meta: {
        someName: {
            httpEquiv: 'Content-Language',
            content: 'en_US'
        },
        anotherName: { 
            key: 'description',
            content: 'description goes here'
        }
    }
})

<meta key="description" content="description goes here">

<meta httpEquiv="Content-Language" content="en_US">

Auscultation answered 23/5, 2021 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.