Skip to content

Instantly share code, notes, and snippets.

@hUwUtao
Created December 7, 2025 07:26
Show Gist options
  • Select an option

  • Save hUwUtao/96ea1da1921632fd961332e53e1ed500 to your computer and use it in GitHub Desktop.

Select an option

Save hUwUtao/96ea1da1921632fd961332e53e1ed500 to your computer and use it in GitHub Desktop.
Bun bundler dev & single-bundle build serve
/*
This is suppose to bridge the gap between bun production bundler and elysia, while managing to make it work without building the server bundle.
Suppose to build a single executable server with SPA.
https://bun.com/reference/bun/HTMLBundle
*/
import Elysia from "elysia";
import indexHtml from "../public/index.html";
const built = !!indexHtml.files
function remap(path: string): string {
let p = path
if (p.endsWith("index.html") || p.endsWith(indexHtml.index)) {
p = ""
}
else if (p.startsWith("/$bunfs/root/")) {
p = p.slice("/$bunfs/root/".length)
}
return p
}
const frontend = built ? indexHtml.files?.reduce((ely, file) => ely.get(remap(file.path), () => new Response(Bun.file(file.path), {
headers: {
...file.headers
}
})), new Elysia()) : await (async () => {
let build = await Bun.build({
entrypoints: ["./public/index.html"],
minify: false,
sourcemap: "linked"
})
return build.outputs.reduce((ely, file) => ely.get(remap(file.path.slice(2)), () => new Response(file, {
headers: {
"Content-Type": file.type
}
})), new Elysia())
})()
export default frontend
@eastgold15
Copy link

Is this about packaging the entire service (including HTML, JS, CSS, and Elysia logic) into a single binary file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment