oblador / esbuild-server

Fast, lightweight and powerful development server for esbuild
MIT License
37 stars 5 forks source link

sendHtml callback #10

Open klasjersevi opened 12 months ago

klasjersevi commented 12 months ago

It would be great if the was a possibility to provide a callback function that gets called right after the injectLiveReload in sendHtml(). That way it would be possible to modify static files dynamically.

// serverOptions example
{
  onSendHtml: (html) => {
    return html.replace("x", "y")
  }
}

Maybe it would be useful to also get the res and status in the callback function?

oblador commented 12 months ago

Sounds like a fairly simple thing to implement, but I'm curious of your use case – why do you need this?

klasjersevi commented 12 months ago

In this case I want to be able to inject som variables from process.env into static .html files. For example the url of a 3rd-party data server, which may be different for each developer. Of course there are many other ways to achieve that, but they are not optimal.

oblador commented 12 months ago

I would suggest to have a serverOptions called indexHtml where you can pass any string you want over a callback. That would work with your use case too right?

klasjersevi commented 12 months ago

Yes, the naming etc are not important to me. I just want to be able to perform something similar to what the injectLiveReload does, but via options.

klasjersevi commented 10 months ago

Any update on this? Do you want me to submit a PR?

klasjersevi commented 9 months ago

I would suggest to have a serverOptions called indexHtml where you can pass any string you want over a callback. That would work with your use case too right?

After thinking some time on this I'm not quite sure I understand exactly what you were after? Do you want it work kind of the same as my suggestion but with another name or do you want a different kind of functionality?

If you want a different kind of functionality, what would be your case or goal? It would also clarify if you could provide a simple code example so I can compare to my suggestion?

To clarify my case: I need to replace different "{SOME_ENV_VAR}" with an env variable in various places in the html file, mostly in html attributes on web components.

<body>
    ...
    <my-component setting="{SOME_ENV_VAR}"></my-component>
</body>

Side note: I have also been using the proposed PR in my project for a while now and it works great for my case.