M
M
may-cat2020-08-07 11:14:50
Frontend
may-cat, 2020-08-07 11:14:50

How to dynamically configure an application in webpack?

I have an abstract frontend that assembles html pages with webpack. When building, I forward parameters (for example, urls) to these html pages.
Something like:

new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src", "index.html"),
    title: 'Some prebuild constant here',
    some_url: variables.url
})


And in the html page they are used something like
Hello world. here be <a href="<%= htmlWebpackPlugin.options.some_url %>">link</a>


And let's say I have a huge application with a rich history and code base.

I really want all these parameters (`title`, `some_url`) to be passed to html not at the build stage, but by client-side javascript.

I see two options:
- bite the bullet and rewrite the application
- use some kind of magical webpack inline-plugin that will convert the html code of the page and replace the view constructs
<a href="<%= htmlWebpackPlugin.options.some_url %>">link</a>

to something like
<a href="#default_value" id="justarandomhash">link</a>

To then load the necessary values ​​​​with Ajax in the JS wrapper and forward them
document.getElementById('justarandomhash').href = variables.url;

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question