Files
agent/.agent/skills/tech-stack/elysiajs/references/bun-fullstack-dev-server.md

2.3 KiB

Fullstack Dev Server

What It Is

Bun 1.3 Fullstack Dev Server with HMR. React without bundler (no Vite/Webpack).

Example: elysia-fullstack-example

Setup

  1. Install + use Elysia Static:
import { Elysia } from 'elysia'
import { staticPlugin } from '@elysiajs/static'

new Elysia()
  .use(await staticPlugin())  // await required for HMR hooks
  .listen(3000)
  1. Create public/index.html + public/index.tsx:
<!-- public/index.html -->
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Elysia React App</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="./index.tsx"></script>
  </body>
</html>
// public/index.tsx
import { useState } from 'react'
import { createRoot } from 'react-dom/client'

function App() {
  const [count, setCount] = useState(0)
  const increase = () => setCount((c) => c + 1)
  
  return (
    <main>
      <h2>{count}</h2>
      <button onClick={increase}>Increase</button>
    </main>
  )
}

const root = createRoot(document.getElementById('root')!)
root.render(<App />)
  1. Enable JSX in tsconfig.json:
{
  "compilerOptions": {
    "jsx": "react-jsx"
  }
}
  1. Navigate to http://localhost:3000/public.

Frontend + backend in single project. No bundler. Works with HMR, Tailwind, Tanstack Query, Eden Treaty, path alias.

Custom Prefix

.use(await staticPlugin({ prefix: '/' }))

Serves at / instead of /public.

Tailwind CSS

  1. Install:
bun add tailwindcss@4
bun add -d bun-plugin-tailwind
  1. Create bunfig.toml:
[serve.static]
plugins = ["bun-plugin-tailwind"]
  1. Create public/global.css:
@tailwind base;
  1. Add to HTML or TS:
<link rel="stylesheet" href="tailwindcss">

Or:

import './global.css'

Path Alias

  1. Add to tsconfig.json:
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@public/*": ["public/*"]
    }
  }
}
  1. Use:
import '@public/global.css'

Works out of box.

Production Build

bun build --compile --target bun --outfile server src/index.ts

Creates single executable server. Include public folder when running.