Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add certain script snippets inside <head> and <body> tags when using HtmlWebackPlugin #1001

Closed
go2sujeet opened this issue Jul 11, 2018 · 2 comments

Comments

@go2sujeet
Copy link

Description

I would like to add custom script at different parts of <head> and <body> tags.

Use case: trying to add GTM tags to different pages.

Example:

How do I,

  1. Add <script> alert('in head tag') </script> inside the <head> tag as the first child
  2. Add <script> alert('in body tag') </script> inside the <body> tag as the first child

Here is the stackoverflow link: https://stackoverflow.com/questions/51285711/how-do-i-add-certain-script-tags-inside-head-and-body-tags-when-using-htmlwe

Here is the snippet in my Webpack config

Config

      new HtmlWebpackPlugin({
        hash: true,
        chunks: ["app"],
        filename: path.resolve(__dirname, "./public/pages/app.html"),
        title: "Title of webpage",
        template: path.resolve(__dirname, "./src/pages/app.page.html"),
        minify: {
            collapseWhitespace: true
        }
    })
@jantimon
Copy link
Owner

The next major will provide an easier way to inject html tags to the head or body.
For now you can already use the htmlWebpackPluginAlterAssetTags hook.

For now I wrote down a solution quickly without testing - so it might not work properly.
But hopefully you still get the idea and will be able to finalize it:

This could look like the following (in a new file or in your webpack config)

class GoogleTagManagerHtmlPlugin{
  apply(compiler) {
    // Hook into a webpack compilation
    compiler.hooks.compilation.tap('GoogleTagManagerHtmlPlugin', (compilation) => {
      // Hook into `htmlWebpackPluginAlterAssetTags`
      // !! Careful this will change in the upcoming html webpack plugin version !!
      compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('GoogleTagManagerHtmlPlugin',
        (pluginArgs, cb) => {
          // You can use either `head` or `body` and either `push` or `unshift`:
          pluginArgs.head.push({
            tagName: 'script',
            voidTag: false,
            attributes: {
            },
            innerHTML: 'alert("hello world")'
          });
          cb(null, pluginArgs)
      })
    })
  }
}

The plugin section of your config

[
   new HtmlWebpackPlugin({ /* your config  */}),
   new GoogleTagManagerHtmlPlugin()
}

@lock
Copy link

lock bot commented Aug 15, 2018

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Aug 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants