13

I now have webpack generating my bunde file with a hash in the name:

bundle.649c4c4e6c8076189257.cache.js

I also have figured out how to have webpack generate my main html page from an html template using HtmlWebpackPlugin.

Here is my problem, in my html template I have the bundle file name hardcoded:

<script src="bundle.649c4c4e6c8076189257.cache.js"></script>

I would rather have some kind of variable, like this:

<script src="${bundleFileName}"></script>

How do I do this with webpack?

1 Answer 1

3

The html-webpack-plugin lets you configure your own templating if you need to as described here: https://github.com/ampedandwired/html-webpack-plugin#writing-your-own-templates.

But your use case looks pretty straightforward so that you should not need that. The html-webpack-plugin already handles putting in script tags for you using the correct naming pattern. So you can just omit the script tag from your source index html file and let the plugin inserting them.

For details see the html-webpack-plugin docs at https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates how to configure the plugin to use your html file. Usually it should be enough to reference the template to achieve your desired result.

Sign up to request clarification or add additional context in comments.

3 Comments

two problems: first path in script tag, it's putting ../../../public/compiled etc into tag, /compiled/.. would have been correct. 2nd: it generates whole html boilerplate, I only want script tag which i can include somewhere in actual template that has lot more going on
I'm not using default html generated by react, react is small part of the whole website, so is there a way that scans all views and update js, css, image filenames in templates (pug, handlebars)
If I have two html files and I need to reference to one another but they are both being hashed by webpack? I tried referencing them like this ./index.[contenthash].htmlbut of course it dit not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.