I’m building an app with HTMX, but I still want to use JSX, write some JS, custom HTML elements, and use TypeScript.
I didn’t want to add a build step for the browser or split code into separate files. I like keeping CSS and scripts together. So I inline the CSS and small client scripts from the server. Should you do it? Not for everything. But it has some nice wins, worth a look if you’re curious.
Then I found out I can write a normal client function and use Function.prototype.toString() to get its source code. That means I can just pass the function directly into a <script> tag. Same idea for CSS.
It feels like writing client-side code in the browser, but you do it on the server, taking advantage of the built-in compiler and then serializing it. TSX for free.
Of course, this pattern won’t scale for every app or every part of the codebase, but it’s another tool we can use when it feels right.
As for minification, we can do it when the server starts or let our CDN (like Cloudflare) handle it. In my case, I don’t need minification at all, HTMX and Web APIs handle most of the work, so the custom JS I write stays minimal.
When it comes to third-party libraries, I’d probably use import maps in the browser. I still need to explore the best way to handle that.
This pattern works best for custom HTML elements or small, self-contained functions, situations where you don’t need many external dependencies.
keep forward and shine yourself ...