Vite Plugin

↓ Download .md

Vite Plugin

The veloPlugin() is the core of VeloJS. It's a single function that returns 6 Vite plugins working together to handle routing, SSR, code transforms, and more.

The 6 plugins

Plugin What it does
velo:config Sets up build config for client/server modes, Preact aliases, and environment defines
velo:transform Performs AST transforms on your code (the most important plugin)
velo:static-url Rewrites CSS url(/path) to use STATIC_BASE_URL in production builds
@preact/preset-vite Adds Preact JSX support
@hono/vite-dev-server Runs the SSR dev server
velo:ws-bridge Exposes Vite's HTTP server for WebSocket handlers during development

AST Transformations

The velo:transform plugin uses Babel to modify your code at build time. It applies 9 transformations to .ts/.tsx files inside your appDirectory (a file elsewhere, or a .js/.jsx one, is not transformed at all):

# Transform When What it does
1 Remove middlewares Client only Strips the middlewares property and any import only it referenced
2 Remove endpoint routes Client only Strips { method, handler } nodes from routes.tsx, and their imports
3 Inject metadata Server + Client Adds export const metadata = { moduleId, fullPath, path } to each module
4 Transform loader functions Server + Client Injects moduleId into useLoader() and Loader() calls so they know which data to read
5 Transform actions for client Client only Replaces action_* bodies with fetch() stubs (the RPC mechanism)
6 Transform streams for client Client only Replaces stream_* with a { __path } stub
7 Transform sockets for client Client only Replaces socket_* with a { __path } stub
8 Remove loaders Client only Strips export const loader entirely from the client bundle
9 Prune orphaned imports Client only Removes imports left unreferenced by 1–8, so server-only code doesn't reach the browser

These transforms are why VeloJS can provide such a seamless developer experience — you write one file, and the framework generates the right code for server and client automatically.

Build process

When you run velojs build, two Vite builds happen in sequence:

velojs build
# 1. vite build              → dist/client/ (client.[hash].js, client.[hash].css + .vite/manifest.json)
# 2. vite build --mode server → dist/server.js

The first build creates the client bundle (JavaScript, CSS, and assets). The second build creates the server entry point that handles SSR and API routes.

Virtual modules

VeloJS uses Vite virtual modules to generate the server and client entry points automatically:

Module Purpose
virtual:velo/server-entry Imports your server.tsx + routes, calls startServer()
virtual:velo/client-entry Imports your client.tsx + routes, calls startClient()
/__velo_client.js Alias for the client entry (used in dev mode)

You never import these directly — they're generated by the plugin.

Hot reload

When you edit routes.tsx, the plugin detects the change, rebuilds the path map, and triggers a full page reload. Other file changes use Vite's standard HMR (hot module replacement).

Request isolation

VeloJS uses Node's AsyncLocalStorage to isolate data per request. Each SSR render runs in its own storage context, so concurrent requests never leak data between each other.

This is why hooks like useParams(), useQuery(), and Loader() work correctly on the server — they read from the current request's storage, not from a global variable.