@toapi/vite-plugin
@toapi/vite-plugin bundles a Toapi REST API alongside
your Vite frontend in a single project. In dev and preview modes the API is
served as middleware on the same port as the frontend — no CORS configuration
needed. For production, vite build emits a deployable server bundle.
Installation
Section titled “Installation”pnpm add -D @toapi/vite-pluginpnpm add @toapi/server srvxPeer dependencies: vite ^8 and @toapi/server.
Quick start
Section titled “Quick start”Add the plugin to your Vite config:
import { defineConfig } from "vite";import toapi from "@toapi/vite-plugin";
export default defineConfig({ plugins: [toapi()],});The plugin expects src/api.ts to export api (an ApiDefinition):
import { defineApi, defineHandler, TResponse } from "@toapi/server";
export const api = defineApi().route("/hello", { GET: defineHandler({ authorize: () => true }, async () => { return TResponse.json({ message: "hello" }); }),});In dev (vite) and preview (vite preview) modes, the plugin attaches a
middleware to Vite’s server that handles requests at the configured basePath
(default /api). The same Vite server serves both the frontend and the API on a
single port.
Features
Section titled “Features”- Single-port dev — frontend and API are served on the same port in dev and preview, so there is no CORS to configure.
- Isolated build output —
vite buildwritesdist/client/(static assets) anddist/server.js(server bundle) separately, so server secrets never leak to the static host. - Env file support —
.envfiles are loaded and mirrored intoprocess.envduring development, covering server-side libraries that readprocess.envdirectly. - Service-worker ready — composes cleanly with
vite-plugin-pwato add offline support and tag-based cache revalidation.
Guides
Section titled “Guides”- Getting Started — set up the plugin and configure your API entry point.
- Service Worker — add offline support
with
vite-plugin-pwa. - Deployment — serve the production bundle
with
srvx. - Reference — plugin options and build output.