Skip to content

Commit 1054fe6

Browse files
authored
chore: update the docs (#2)
### TL;DR Simplified the Vite configuration for srvx and added Vercel Edge Functions deployment support. ### What changed? - Simplified the `vite.config.ts` example by removing the conditional `outDir` configuration - Streamlined build scripts in `package.json` to use a single `build` command - Renamed `preview` script to `start` for clarity - Added support for Vercel Edge Functions deployment via a new `framework` option - Updated the plugin architecture to use three separate plugins (dev server, client build, server build) - Added documentation for the new Vercel deployment option - Added a new example for Vercel Edge Functions deployment - Improved code formatting and organization ### How to test? 1. Use the simplified configuration: ```js export default defineConfig({ plugins: [ ...srvx({ entry: './src/server.ts', }), ], }) ``` 2. Run the build with the updated scripts: ```bash npm run build # Runs both client and server builds npm run start # Starts the production server ``` 3. For Vercel deployment, add the framework option: ```js ...srvx({ entry: './src/server.ts', framework: 'vercel' }) ``` ### Why make this change? The changes simplify the configuration and build process, making it more intuitive for users. The addition of Vercel Edge Functions support expands deployment options, allowing developers to easily deploy srvx applications to Vercel's edge network. The restructured plugin architecture provides better separation of concerns and more flexibility for advanced use cases.
1 parent ab12a8c commit 1054fe6

File tree

2 files changed

+219
-216
lines changed

2 files changed

+219
-216
lines changed

README.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,13 @@ Create a `vite.config.ts` file:
7777
import { defineConfig } from 'vite'
7878
import 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

127122
This 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

131126
Run 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'
173173
import srvx from 'vite-plugin-srvx'
174174

175175
export 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

195192
Then 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

205204
If you need more control, you can import the plugins separately:
206205

207206
```typescript
208207
import { defineConfig } from 'vite'
209-
import { devServer, srvxBuild } from 'vite-plugin-srvx'
208+
import { devServer, clientBuild, srvxBuild } from 'vite-plugin-srvx'
210209

211210
export 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

240237
1. **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

244242
2. **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

249248
3. **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

255254
This 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
264265
pnpm install
265266
pnpm build
266-
cd example
267+
cd examples/basic # or examples/vercel
267268
pnpm dev
268269
```
269270

0 commit comments

Comments
 (0)