Last active
May 20, 2025 18:17
-
-
Save maca134/cd9fd5fdddddcaca13b05fa08681f196 to your computer and use it in GitHub Desktop.
Bun + Hono Dynamic HTML Bundler Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Hono } from "hono"; | |
export async function bundler( | |
entrypoint: Bun.HTMLBundle, | |
config?: Bun.BuildConfig, | |
) { | |
let router = new Hono(); | |
const build = await Bun.build({ | |
...config, | |
entrypoints: [entrypoint.index], | |
outdir: undefined, | |
}); | |
if (!build.success) { | |
build.logs.forEach((log) => console.log(log)); | |
throw new Error("Build failed"); | |
} | |
for (const file of build.outputs) { | |
const path = file.path.slice(2); | |
const handler = () => | |
new Response(file.stream(), { | |
headers: { | |
"Content-Type": file.type, | |
"Cache-Control": "no-store", | |
}, | |
status: 200, | |
}); | |
if (path === "index.html" || path === "index.htm") { | |
router.get("/", handler); | |
} | |
router.get(path, handler); | |
} | |
return router; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bun + Hono Dynamic HTML Bundler Example
This code sets up a minimal web server using Hono and Bun to serve a web application with dynamic, in-memory asset bundling.
How it works
bundler
function to bundle the HTML entrypoint and its dependencies at runtime./
through the dynamically bundled application.Example (
index.ts
)Features
Note: This setup is best for development or prototyping. For production, pre-bundle your assets for better performance.