BjornTheProgrammer / bun-plugin-html

A plugin for bun build which allows html entrypoints
MIT License
34 stars 2 forks source link

add support for inline html #13

Closed damonsmith closed 4 months ago

damonsmith commented 5 months ago

I'm not sure if you'll want this one but I found it useful for doing small projects with webcomponents where I need a simple way to get template content inlined into the generated html.

I'm aware that <link rel="import" ...> is no longer supported by browsers and is probably destined to be forgotten but it seemed simple and harmless enough to add support for inlining them to this plugin.

Please let me know if you're not interested and I'll publish it under a namespace.

Thanks!

BjornTheProgrammer commented 5 months ago

Really great addition! The only issue I have with the current implementation, is that this doesn't work for non-inlined scripts. Maybe we can add a polyfill if you decide not to inline html. Could also make it so that inlining is the default in this case also. What do you think?

An example of a ployfill can be seen here html-imports.

BjornTheProgrammer commented 5 months ago

Now that I think about it, could just replace the <link rel="import" href="item.html"> when detected with a script tag with some javascript like this... Probably easier, plus this presumably has nearly zero impact on anyone using this library anyway.

<script>
(() => {
    fetch('item.html').then(r => r.text()).then(html => {
        includeHtmlHereSomehowRightNow(html)
        deleteThisScriptTag()
    })
})()
</script>
damonsmith commented 5 months ago

Yeah I see what you mean, If rel="import" is not going to be supported by browsers it doesn't really make sense without an automatic polyfill of some sort. I'll change it to just do the inline html by default and then raise another PR when I've had a think about polyfilling it.

I wouldn't use the polyfill myself because I'd need some way to know that the document had loaded and also all the templates had been retrieved and loaded but some folks might find it useful.

damonsmith commented 4 months ago

Actually looking at the template polyfill implementations out there and the various limitations around them I don't think I have the time or interest to implement a polyfill here sorry. I'll just migrate this code into a separate bun plugin for inline html templates and use it alongside this one. Thanks for your work.