@@ -77,16 +77,13 @@ Create a `vite.config.ts` file:
7777import { defineConfig } from ' vite'
7878import srvx from ' vite-plugin-srvx'
7979
80- export default defineConfig (({ mode }) => ({
81- build: {
82- outDir: mode === ' server' ? ' dist' : ' dist/public' ,
83- },
80+ export default defineConfig ({
8481 plugins: [
8582 ... srvx ({
8683 entry: ' ./src/server.ts' ,
8784 }),
8885 ],
89- }))
86+ })
9087```
9188
9289### 4. Run the development server
@@ -110,10 +107,8 @@ Add these scripts to your `package.json`:
110107{
111108 "scripts" : {
112109 "dev" : " vite" ,
113- "build" : " npm run build:client && npm run build:server" ,
114- "build:client" : " vite build" ,
115- "build:server" : " vite build --mode server" ,
116- "preview" : " srvx dist/server.js"
110+ "build" : " vite build && vite build --mode server" ,
111+ "start" : " srvx dist/server.js"
117112 }
118113}
119114```
@@ -125,13 +120,13 @@ npm run build
125120```
126121
127122This will:
128- 1 . Build your frontend (HTML, CSS, JS) to ` dist/public `
129- 2 . Build your srvx server to ` dist/server.js `
123+ 1 . Build your frontend (HTML, CSS, JS) to ` dist/public ` (first ` vite build ` )
124+ 2 . Build your srvx server to ` dist/server.js ` (second ` vite build --mode server ` )
130125
131126Run your production build:
132127
133128``` bash
134- npm run preview
129+ npm run start
135130# or directly: srvx dist/server.js
136131```
137132
@@ -152,6 +147,11 @@ interface SrvxOptions {
152147 // Server output filename (default: 'server.js')
153148 serverOutFile? : string
154149
150+ // Target framework for deployment (e.g., 'vercel')
151+ // When set to 'vercel' OR when VERCEL=1 env var is set (auto-detected),
152+ // outputs to dist/api/index.js for Vercel Edge Functions
153+ framework? : ' vercel'
154+
155155 // Development server options
156156 // Patterns to exclude from the srvx handler (will be handled by Vite instead)
157157 exclude? : (string | RegExp )[]
@@ -164,7 +164,7 @@ interface SrvxOptions {
164164}
165165```
166166
167- > ** Note:** The plugin returns an array of two plugins (dev server + build), so use the spread operator: ` ...srvx({}) `
167+ > ** Note:** The plugin returns an array of three plugins (dev server + client build + server build), so use the spread operator: ` ...srvx({}) `
168168
169169### Example with custom options
170170
@@ -173,9 +173,6 @@ import { defineConfig } from 'vite'
173173import srvx from ' vite-plugin-srvx'
174174
175175export default defineConfig (({ mode }) => ({
176- build: {
177- outDir: mode === ' server' ? ' build' : ' build/public' ,
178- },
179176 plugins: [
180177 ... srvx ({
181178 entry: ' ./src/server.ts' ,
@@ -194,27 +191,27 @@ export default defineConfig(({ mode }) => ({
194191
195192Then build with:
196193``` bash
197- npm run build:client # builds to build/public
198- npm run build:server # builds to build/app.js
194+ npm run build
195+ # This runs: vite build && vite build --mode server
196+ # - Client build outputs to build/public
197+ # - Server build outputs to build/app.js
199198```
200199
201- And run: ` srvx build/app.js ` (it will serve static files from ` build/public ` )
200+ And run: ` srvx build/app.js ` (it will automatically serve static files from ` build/public ` )
202201
203202### Using Individual Plugins (Advanced)
204203
205204If you need more control, you can import the plugins separately:
206205
207206``` typescript
208207import { defineConfig } from ' vite'
209- import { devServer , srvxBuild } from ' vite-plugin-srvx'
208+ import { devServer , clientBuild , srvxBuild } from ' vite-plugin-srvx'
210209
211210export default defineConfig (({ mode }) => ({
212- build: {
213- outDir: mode === ' server' ? ' dist' : ' dist/public' ,
214- },
215211 plugins: [
216212 devServer ({ entry: ' ./src/server.ts' }),
217- srvxBuild ({ entry: ' ./src/server.ts' }),
213+ clientBuild ({ outDir: ' dist' }),
214+ srvxBuild ({ entry: ' ./src/server.ts' , outDir: ' dist' }),
218215 ],
219216}))
220217```
@@ -235,16 +232,18 @@ The `devServer` plugin creates a Vite middleware that:
235232
236233### Production Build
237234
238- The ` srvxBuild ` plugin uses Vite's mode system:
235+ The plugin uses Vite's mode system with three separate plugins :
239236
2402371 . ** Client build** (` vite build ` ):
238+ - ` clientBuild ` plugin is active (mode !== 'server')
241239 - Builds frontend to ` dist/public `
242- - Plugin is inactive (mode !== 'server')
240+ - ` srvxBuild ` plugin is inactive
243241
2442422 . ** Server build** (` vite build --mode server ` ):
245- - Plugin activates (mode === 'server')
243+ - ` srvxBuild ` plugin is active (mode === 'server')
246244 - Sets ` ssr: true ` via the ` config ` hook
247245 - Builds server to ` dist/server.js `
246+ - ` clientBuild ` plugin is inactive
248247
2492483 . ** Run with srvx** :
250249 - ` srvx dist/server.js `
@@ -254,16 +253,18 @@ This approach follows the same pattern as [@hono/vite-build](https://github.com/
254253
255254This gives you the best of both worlds: srvx's universal server API and Vite's lightning-fast development experience!
256255
257- ## Example
256+ ## Examples
258257
259- Check out the [ example] ( ./example ) directory for a full working example.
258+ Check out the [ examples] ( ./examples ) directory for full working examples:
259+ - [ examples/basic] ( ./examples/basic ) - Basic srvx + Vite setup
260+ - [ examples/vercel] ( ./examples/vercel ) - Vercel Edge Functions deployment
260261
261- To run the example:
262+ To run an example:
262263
263264``` bash
264265pnpm install
265266pnpm build
266- cd example
267+ cd examples/basic # or examples/vercel
267268pnpm dev
268269```
269270
0 commit comments