How Dex Works
How Dex Works
This page explains Dex's mental model: how the Native Go CLI drives the workspace, how pages become routes, how layouts compose, and how the dev and production pipelines work.
The Big Picture
┌──────────────┐ ┌──────────────┐ ┌──────────────────┐
│ web/pages/ │────▶│ @dex/router │────▶│ routes.ts / json │
│ web/layouts │ │ generator │ │ layouts.ts │
└──────────────┘ └──────────────┘ └──────────────────┘
│ │ │
▼ ▼ ▼
┌──────────────┐ ┌──────────────┐ ┌──────────────────┐
│ @dex/dev │────▶│ Bun ESM │────▶│ build/server │
│ dexBuild() │ │ + SSG + CSS │ │ (Bun executable) │
└──────────────┘ └──────────────┘ └──────────────────┘1. Native Go CLI (dex)
The dex CLI is a compiled Go binary (<8 MB, <5ms startup) that manages:
- Scaffolding & template sync:
dex scaffold,dex sync, anddex template - Multi-version management:
dex update,dex versions, anddex useunder~/.dex/ - Execution:
dex startrunscore/bootstrap/dev.ts,dex buildrunscore/bootstrap/build.ts, anddex start -pexecutes the compiledbuild/serverbinary - OpenAPI codegen:
dex pie generatebuildscore/api/generated.tsfrom JSON or YAML specs
2. Pages to Routes
Dex uses file-based routing. Your folder structure under web/pages/ defines your routes:
| File | Route |
|---|---|
web/pages/index.tsx | / |
web/pages/about.tsx | /about |
web/pages/blog/index.tsx | /blog |
web/pages/blog/[slug].tsx | /blog/:slug |
web/pages/[...catchall].tsx | /*catchall |
Rules
- Files or folders starting with
_are ignored index.tsxfiles map to their directory path- Dynamic segments use
[param]syntax - Catch-all routes use
[...param]syntax - Generated routes in
core/router/.generated/routes.tsare sorted by specificity (static before param before catch-all) and expose lazyimportPage: () => import(...)loaders
3. Layouts Compose
Layouts wrap your pages cleanly:
web/layouts/global.tsx— imported incore/bootstrap/web.tsxand passed asGlobalLayoutto<FileRouter />web/layouts/<name>.tsx— named layouts registered incore/router/.generated/layouts.tsand selected viaexport const layout = '<name>'
// web/layouts/global.tsx
export default function GlobalLayout({ children }: { children: React.ReactNode }) {
return (
<div className="app-shell">
<header>Dex App</header>
<main>{children}</main>
</div>
)
}// web/pages/about.tsx
export const layout = 'about'
export default function About() {
return <h1>About Us</h1>
}4. The Build Process
Development (dex start)
Runs core/bootstrap/dev.ts (dexDev()), which generates routes once and then spawns four concurrent tasks via @dex/dev:
routes:watch— watchesweb/pages/andweb/layouts/and regeneratescore/router/.generated/*styles:watch— runs Tailwind CSS v4 CLI to compileweb/styles/index.cssintoweb/public/assets/styles.cssclient:watch— runsbun build core/bootstrap/web.tsx --splitting --format esmintoweb/public/assets/server:dev— runsbun --watch core/runtime/app/index.ts(Elysia API routes +/assets/*+/__dev/reloadSSE + SPA fallback)
Production (dex build)
Runs core/bootstrap/build.ts (dexBuild()):
- Generate routes & layouts into
core/router/.generated/ - Prepare
build/— copiesweb/public/index.htmltobuild/index.html - SSG prerender — renders static routes whose strategy resolves to
'ssg'with<RouterContext.Provider>, injects<head>metadata (injectHeadMeta) and Google Fonts (injectGoogleFonts), and writes HTML tobuild/__ssg/andbuild/<route>/index.html - Compile CSS — minifies
web/styles/index.csstobuild/assets/styles.css - Bundle client (ESM code splitting) — compiles
core/bootstrap/web.tsxwith--splitting --format esm --minifytobuild/assets/client.jsandbuild/assets/chunks/[name]-[hash].js - Cache busting — hashes
client.jsandstyles.cssand updates HTML references with?v=<hash> - Compile server — compiles
core/runtime/server/prod.tsinto a Standalone Bun server executable (build/server)
Output structure:
build/
├─ server # Standalone Bun server executable
├─ index.html # Entry HTML (with ?v=<hash> asset URLs)
├─ __ssg/ # Prerendered SSG HTML lookup tree
├─ <route>/index.html # Route-level prerendered HTML (for SSG routes)
└─ assets/
├─ client.js # Client router entry bundle
├─ chunks/
│ └─ [name]-[hash].js # ESM code-split route & layout chunks
└─ styles.css # Compiled & minified Tailwind CSS5. Client-Side Navigation & Subdomains
@dex/router/client provides:
<Link>with intent/render prefetching, unsafe scheme filtering, and automaticnoopener noreferrerontarget="_blank"useNavigate(),useParams(),useLocation(),useQuery(),useIsNavigating(),usePrefetch(),useRouterState()Outlet&useOutletContext()ClientOnly&clientOnly()for SSG-safe client-only renderingRouterErrorBoundaryfor route & layout error recoverysubdomainsmounting (resolveSubdomainMount, canonical redirects, and clean external URLs)
6. Configuration
By default, Dex uses conventions:
web/pages/ -> routes
web/layouts/ -> layouts
web/public/ -> static index.html & dev assets
web/styles/ -> Tailwind CSS source
routes/api/ -> Elysia API routesCustomize paths, subdomains, Google fonts, rendering strategy, and @dex/pie codegen options in dex.config.ts.