I'm using HtmlWebpackPlugin to generate HTML files with javascript.
Now I would like to add custom script at different parts of <head>
and <body>
tags
Example:
How do I,
- Add
<script> alert('in head tag') </script>
inside the<head>
tag as the first child - Add
<script> alert('in body tag') </script>
inside the<body>
tag as the first child
Here is the snippet in my Webpack 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
}
})
Your question is a bit confusing. It implies you want to add static script tags to your template. If that's the case you just need to go into your
src/pages/app.page.html
file and add those two script tags in thehead
andbody
.What I'm guessing that you're asking is "How do I insert generated bundles in two different areas of my template?". If that's the case there's a section in the docs that mentions what data is passed to the template file:
So if your
entry
looked likeand your plugin was set to
then
app.page.ejs
should be able to access the data from the plugin and you can place those entries where ever you'd like. There's a large ejs example file in their repo. A simpler example, and one more specific to your use case would be:Note that I'm not using
files.js
but ratherfiles.chunks
since you can access single files by entry name instead.Multi-Page Set-Up
For a multi-page set-up your WP config could look like
The template would look something like
You can use template parameters like shown in the official example
I apologize for necro-ing your question, but I had the same problem and was brought here.
So...I made a plugin. And it seems to work.
This(as of 2019-11-20) might require you to uninstall html-webpack-plugin(current stable), then install html-webpack-plugin@next.
TL;DR:
npm install -D html-webpack-inject-string-plugin
I made a plugin that replaces or inserts text in the
htmlWebpackPlugin
output. That means any text, anywhere, as long as what you're searching for is unique on the page(like a</body>
tag).Here's how
html-webpack-plugin gives hooks for it's compilation process. I made a plugin that searches the output string(after it's been compiled) and adds a custom string either before, after, or replacing the one that was searched.
Here's why
My problem was creating a minimal-pain Wordpress theme framework with Webpack that automates the more tedious parts. (I know it's a mouthful)
I needed to inject an async script tag for browser-sync to connect with the page. But(like you) I couldn't find a way to universally attach a script to the page without a bit of boilerplate.
So I made a plugin that put the string I needed into each file that contained the string
</body>
, because that would mean it was a full-page template and I wanted each full page template to automatically refresh when I updated the source file.It works!
The only issue I've found is having to escape an already escaped backslash, because the output is run through a compiler before actually being html in the browser.
So just be warned you might have to fudge it a bit, and there are no doubt more problems like that someone will run into.
But I'll go out on a limb and say this looks like the solution to what you were originally asking.
Again, the plugin:
https://github.com/smackjax/html-webpack-inject-string-plugin
If it's not what you're looking for or you have problems, let me know!
I came across the same problem that's why I created a plugin.
HtmlWebpackInjector - A
HtmlWebpackPlugin
helper to inject some chunks to headIt works with
HtmlWebpackPlugin
and by just adding_head
in the name of chunk, it automaticlly injects the chunk in the head.This automatically injects
index
chunk to the body andindex_head
to the head of the html document. Final html looks like:use this settings.
template: root to your html file