i have a directory named src with 3 files:
- index.html
- index.js
- index.css
this is how my html file looks like what esbuild recommends
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<script src="./index.js"></script>
</body>
</html>
i would like to bundle all of them into a single html file, such as
<!doctype html>
<html>
<head>
<style>
/* the content of index.css */
</style>
</head>
<body>
<script>
// the content of index.js
</script>
</body>
</html>
but i failed to do it with esbuild even after digging heavily into esbuild loaders. would appreciate your help.
I also saw no direct way of doing this with esbuild, but this is how I achieved the same goal (simplified for brevity);
And the output looks like so;
The dependencies I used were
[email protected]
and[email protected]
.You could modify the above to read/write the scripts to and from disk using the
node:fs
module or to an S3 bucket or whatever. I removed that logic from the example above to show the core idea.You could also swap out
mustache
with whatever is trending nowadays if you want.Having said that, this approach has some disadvantages like lack of bundling and plugin support. Maybe one could use temporary files and a templating engine if you want these features.
Thanks.