vite-elysia-forge 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -74
- package/dist/cli.d.ts +21 -22
- package/dist/cli.js +13 -11
- package/package.json +1 -5
- package/dist/production.d.ts +0 -43
- package/dist/production.js +0 -394
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ bun install vite-elysia-forge
|
|
|
18
18
|
|
|
19
19
|
### 2.1 Create Your API Handler
|
|
20
20
|
|
|
21
|
-
Place your Elysia handler at `
|
|
21
|
+
Place your Elysia handler at `server/api.ts` (default path):
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
import { Elysia } from "elysia";
|
|
@@ -43,7 +43,7 @@ import elysiaPlugin from "vite-elysia-forge";
|
|
|
43
43
|
export default defineConfig({
|
|
44
44
|
plugins: [
|
|
45
45
|
elysiaPlugin({
|
|
46
|
-
serverFile: "./
|
|
46
|
+
serverFile: "./server/api.ts",
|
|
47
47
|
}),
|
|
48
48
|
],
|
|
49
49
|
});
|
|
@@ -126,7 +126,7 @@ To enable WebSocket support, set `ws: true`:
|
|
|
126
126
|
|
|
127
127
|
```ts
|
|
128
128
|
elysiaPlugin({
|
|
129
|
-
serverFile: "./
|
|
129
|
+
serverFile: "./server/api.ts",
|
|
130
130
|
ws: true, // Enable WebSocket support
|
|
131
131
|
backendPort: 3001, // API runs on this port (default: 3001)
|
|
132
132
|
});
|
|
@@ -144,7 +144,7 @@ This ensures full Bun runtime support for WebSockets, even if Vite itself runs u
|
|
|
144
144
|
|
|
145
145
|
### 5.2 Production
|
|
146
146
|
|
|
147
|
-
In production, the built server (`
|
|
147
|
+
In production, the built server (`build/server.js` or the compiled binary) runs your Elysia app directly with full WebSocket support—no proxy needed.
|
|
148
148
|
|
|
149
149
|
## 6. Integration with @elysiajs/openapi
|
|
150
150
|
|
|
@@ -176,139 +176,144 @@ const app = new Elysia().use(
|
|
|
176
176
|
|
|
177
177
|
## 7. Production Deployment
|
|
178
178
|
|
|
179
|
-
The CLI provides
|
|
179
|
+
The CLI provides a `build` command for building your Elysia API server. Frontend assets should be built separately using Vite's standard build process (`vite build`).
|
|
180
180
|
|
|
181
|
-
By default, the CLI looks for your API at `
|
|
181
|
+
By default, the CLI looks for your API at `server/api.ts`. You can specify a custom path with the `--entry` flag.
|
|
182
|
+
|
|
183
|
+
### 7.1 Frontend Build
|
|
184
|
+
|
|
185
|
+
Build your frontend using Vite directly:
|
|
182
186
|
|
|
183
187
|
```bash
|
|
184
|
-
vite
|
|
188
|
+
vite build
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**Output:**
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
dist/
|
|
195
|
+
├── index.html
|
|
196
|
+
└── assets/
|
|
185
197
|
```
|
|
186
198
|
|
|
187
|
-
|
|
199
|
+
Deploy to any static host (Cloudflare Pages, Netlify, Vercel, etc.)
|
|
188
200
|
|
|
189
|
-
|
|
201
|
+
### 7.2 Server Build (`--outDir`)
|
|
202
|
+
|
|
203
|
+
Build the Elysia API server into a standalone JavaScript bundle.
|
|
190
204
|
|
|
191
205
|
```bash
|
|
192
|
-
vite-elysia-forge build
|
|
206
|
+
vite-elysia-forge build --outDir dist
|
|
193
207
|
```
|
|
194
208
|
|
|
195
209
|
**What it does:**
|
|
196
210
|
|
|
197
|
-
1.
|
|
198
|
-
2.
|
|
199
|
-
3. Bundles the server into `dist/server.js`
|
|
211
|
+
1. Generates a production entry file that imports your API
|
|
212
|
+
2. Bundles the server into `dist/server.js`
|
|
200
213
|
|
|
201
214
|
**package.json:**
|
|
202
215
|
|
|
203
216
|
```json
|
|
204
217
|
{
|
|
205
218
|
"scripts": {
|
|
206
|
-
"build": "vite-elysia-forge build",
|
|
219
|
+
"build:server": "vite-elysia-forge build --outDir dist",
|
|
207
220
|
"start": "bun dist/server.js"
|
|
208
221
|
}
|
|
209
222
|
}
|
|
210
223
|
```
|
|
211
224
|
|
|
212
|
-
|
|
225
|
+
**With custom entry and target:**
|
|
213
226
|
|
|
214
|
-
|
|
227
|
+
```bash
|
|
228
|
+
vite-elysia-forge build --entry src/my-api.ts --outDir .output --target node
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### 7.3 Compiled Binary (`--outFile`)
|
|
232
|
+
|
|
233
|
+
Build and compile the server into a **standalone executable** (no Bun runtime required).
|
|
215
234
|
|
|
216
235
|
```bash
|
|
217
|
-
vite-elysia-forge build
|
|
236
|
+
vite-elysia-forge build --outFile server
|
|
218
237
|
```
|
|
219
238
|
|
|
220
239
|
**What it does:**
|
|
221
240
|
|
|
222
|
-
1.
|
|
223
|
-
2. Compiles
|
|
241
|
+
1. Bundles the server code
|
|
242
|
+
2. Compiles into a native binary at the specified path
|
|
224
243
|
|
|
225
244
|
**package.json:**
|
|
226
245
|
|
|
227
246
|
```json
|
|
228
247
|
{
|
|
229
248
|
"scripts": {
|
|
230
|
-
"build": "vite-elysia-forge build
|
|
249
|
+
"build:server": "vite-elysia-forge build --outFile dist/server",
|
|
231
250
|
"start": "./dist/server"
|
|
232
251
|
}
|
|
233
252
|
}
|
|
234
253
|
```
|
|
235
254
|
|
|
236
|
-
### 7.
|
|
255
|
+
### 7.4 CLI Reference
|
|
237
256
|
|
|
238
|
-
|
|
257
|
+
**Command:**
|
|
239
258
|
|
|
240
259
|
```bash
|
|
241
|
-
vite-elysia-forge build
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
**Output structure:**
|
|
245
|
-
|
|
246
|
-
```
|
|
247
|
-
project/
|
|
248
|
-
├── dist/ # Static assets (deploy to CDN)
|
|
249
|
-
│ ├── index.html
|
|
250
|
-
│ └── assets/
|
|
251
|
-
└── .output/ # Server bundle (deploy to server)
|
|
252
|
-
└── server.js
|
|
260
|
+
vite-elysia-forge build [options]
|
|
253
261
|
```
|
|
254
262
|
|
|
255
|
-
**
|
|
263
|
+
**Options:**
|
|
256
264
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
265
|
+
| Option | Short | Default | Description |
|
|
266
|
+
| :------------------ | :---: | :-------------- | :----------------------------------------- |
|
|
267
|
+
| `--entry <path>` | `-e` | `server/api.ts` | Path to API entry file |
|
|
268
|
+
| `--outDir <dir>` | `-d` | `dist` | Output directory for bundled `server.js` |
|
|
269
|
+
| `--outFile <file>` | `-o` | — | Output path for compiled standalone binary |
|
|
270
|
+
| `--target <target>` | `-t` | `bun` | Build target: `bun`, `node`, or `browser` |
|
|
271
|
+
| `--no-minify` | — | — | Disable minification |
|
|
260
272
|
|
|
261
|
-
**
|
|
262
|
-
|
|
263
|
-
```json
|
|
264
|
-
{
|
|
265
|
-
"scripts": {
|
|
266
|
-
"build": "vite-elysia-forge build --static dist --server .output",
|
|
267
|
-
"start": "bun .output/server.js"
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
```
|
|
273
|
+
> **Note:** `--outDir` and `--outFile` are mutually exclusive. Use `--outDir` for a bundled JS file, or `--outFile` for a compiled binary.
|
|
271
274
|
|
|
272
|
-
|
|
275
|
+
**Examples:**
|
|
273
276
|
|
|
274
277
|
```bash
|
|
275
|
-
|
|
276
|
-
|
|
278
|
+
# Bundle server to dist/server.js
|
|
279
|
+
vite-elysia-forge build --outDir dist
|
|
277
280
|
|
|
278
|
-
|
|
281
|
+
# Bundle with node target
|
|
282
|
+
vite-elysia-forge build --outDir dist --target node
|
|
279
283
|
|
|
280
|
-
|
|
284
|
+
# Compile to standalone binary
|
|
285
|
+
vite-elysia-forge build --outFile server
|
|
281
286
|
|
|
282
|
-
|
|
283
|
-
vite-elysia-forge build
|
|
284
|
-
```
|
|
287
|
+
# Compile with custom entry
|
|
288
|
+
vite-elysia-forge build --entry src/api/index.ts --outFile myserver
|
|
285
289
|
|
|
286
|
-
|
|
290
|
+
# Bundle without minification
|
|
291
|
+
vite-elysia-forge build --outDir dist --no-minify
|
|
292
|
+
```
|
|
287
293
|
|
|
288
|
-
### 7.5
|
|
294
|
+
### 7.5 Deployment Architecture
|
|
289
295
|
|
|
290
|
-
|
|
296
|
+
**Separate deployment (recommended):**
|
|
291
297
|
|
|
292
|
-
```
|
|
293
|
-
|
|
298
|
+
```
|
|
299
|
+
┌─────────────────┐ ┌──────────────────┐
|
|
300
|
+
│ Static Host │ │ API Server │
|
|
301
|
+
│ (CDN/Pages) │◄────────┤ (VPS/Cloud) │
|
|
302
|
+
│ │ CORS │ │
|
|
303
|
+
│ dist/ │ │ dist/server.js │
|
|
304
|
+
└─────────────────┘ └──────────────────┘
|
|
294
305
|
```
|
|
295
306
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
### 7.6 CLI Reference
|
|
307
|
+
- Frontend: Build with `vite build` and deploy to Cloudflare Pages, Netlify, Vercel, etc.
|
|
308
|
+
- Backend: Build with `vite-elysia-forge build --outDir dist` and deploy to any server with Bun installed, or use the compiled binary
|
|
299
309
|
|
|
300
|
-
|
|
301
|
-
| :--------------- | :---: | :------------------ | :------------------------------------------ |
|
|
302
|
-
| `--api <path>` | `-a` | `src/server/api.ts` | Path to API entry file |
|
|
303
|
-
| `--static <dir>` | `-s` | `dist` | Output directory for static frontend assets |
|
|
304
|
-
| `--server <dir>` | `-o` | Same as `--static` | Output directory for server bundle |
|
|
305
|
-
| `--skip-vite` | | `false` | Skip the Vite frontend build |
|
|
306
|
-
| `--skip-server` | | `false` | Skip the server build |
|
|
310
|
+
**API-only deployment:**
|
|
307
311
|
|
|
308
|
-
|
|
312
|
+
If you have no frontend, just build and deploy the server:
|
|
309
313
|
|
|
310
314
|
```bash
|
|
311
|
-
vite-elysia-forge build --
|
|
315
|
+
vite-elysia-forge build --outDir dist
|
|
316
|
+
bun dist/server.js
|
|
312
317
|
```
|
|
313
318
|
|
|
314
319
|
## 8. Troubleshooting
|
|
@@ -349,7 +354,7 @@ If you see `Current adapter doesn't support WebSocket`, you need to enable WS mo
|
|
|
349
354
|
|
|
350
355
|
```ts
|
|
351
356
|
elysiaPlugin({
|
|
352
|
-
serverFile: "./
|
|
357
|
+
serverFile: "./server/api.ts",
|
|
353
358
|
ws: true,
|
|
354
359
|
});
|
|
355
360
|
```
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,39 +1,38 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Bun build target options.
|
|
4
|
+
*/
|
|
5
|
+
type BuildTarget = "bun" | "node" | "browser";
|
|
6
|
+
/**
|
|
7
|
+
* Build options for the server bundle.
|
|
4
8
|
*/
|
|
5
9
|
interface BuildOptions {
|
|
6
10
|
/**
|
|
7
11
|
* Path to the API entry file.
|
|
8
|
-
* @default "
|
|
12
|
+
* @default "server/api.ts"
|
|
9
13
|
*/
|
|
10
|
-
|
|
14
|
+
entry?: string;
|
|
11
15
|
/**
|
|
12
|
-
* Output directory for the
|
|
13
|
-
*
|
|
16
|
+
* Output directory for the server bundle (used with --outDir).
|
|
17
|
+
* Mutually exclusive with outFile.
|
|
14
18
|
*/
|
|
15
|
-
|
|
19
|
+
outDir?: string;
|
|
16
20
|
/**
|
|
17
|
-
* Output
|
|
18
|
-
*
|
|
19
|
-
* will be built to separate directories.
|
|
20
|
-
* @default "dist" (same as staticDir)
|
|
21
|
+
* Output file path for compiled standalone binary (used with --outFile).
|
|
22
|
+
* Mutually exclusive with outDir.
|
|
21
23
|
*/
|
|
22
|
-
|
|
24
|
+
outFile?: string;
|
|
23
25
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @default false
|
|
26
|
+
* Build target for Bun.build.
|
|
27
|
+
* @default "bun"
|
|
27
28
|
*/
|
|
28
|
-
|
|
29
|
+
target?: BuildTarget;
|
|
29
30
|
/**
|
|
30
|
-
* Whether to
|
|
31
|
-
*
|
|
32
|
-
* @default false
|
|
31
|
+
* Whether to minify the output.
|
|
32
|
+
* @default true
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
minify?: boolean;
|
|
35
35
|
}
|
|
36
|
-
declare function build(options?: BuildOptions
|
|
37
|
-
declare function buildCompile(options?: BuildOptions | string): Promise<void>;
|
|
36
|
+
declare function build(options?: BuildOptions): Promise<void>;
|
|
38
37
|
|
|
39
|
-
export { type BuildOptions,
|
|
38
|
+
export { type BuildOptions, type BuildTarget, build };
|
package/dist/cli.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
import {spawnSync}from'child_process';import {existsSync,mkdirSync,writeFileSync,unlinkSync,rmSync}from'fs';import {resolve,relative,sep}from'path';async function
|
|
3
|
-
import {
|
|
4
|
-
import { api } from ${JSON.stringify(
|
|
2
|
+
import {spawnSync}from'child_process';import {existsSync,mkdirSync,writeFileSync,unlinkSync,rmSync}from'fs';import {resolve,relative,sep,dirname}from'path';async function w(t={}){let o=t.entry||"server/api.ts",r=t.target||"bun",n=t.minify!==false,e=resolve(process.cwd(),o);existsSync(e)||(console.error(`\u274C API entry file "${o}" not found.`),console.error(' By default, vite-elysia-forge looks for "server/api.ts".'),console.error(" If your API is located elsewhere, please specify the path:"),console.error(" $ vite-elysia-forge build --entry <path-to-your-api-file>"),process.exit(1));let s=resolve(process.cwd(),".vef-temp");existsSync(s)||mkdirSync(s,{recursive:true});let i=resolve(s,".temp-prod.ts"),l=relative(s,e);l=l.split(sep).join("/"),l.startsWith(".")||(l="./"+l);let p=`
|
|
3
|
+
import { Elysia } from "elysia";
|
|
4
|
+
import { api } from ${JSON.stringify(l)};
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
const app = new Elysia();
|
|
7
|
+
if (api) app.use(api);
|
|
8
|
+
|
|
9
|
+
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000;
|
|
10
|
+
app.listen(port);
|
|
11
|
+
|
|
12
|
+
console.log(\`Production server running at http://\${app?.server?.hostname}:\${app?.server?.port}\`);
|
|
13
|
+
`;writeFileSync(i,p);try{if(t.outFile)await D(i,t.outFile,r,n);else {let u=t.outDir||"dist";await x(i,u,r,n);}}finally{existsSync(i)&&unlinkSync(i),existsSync(s)&&rmSync(s,{recursive:true,force:true});}}async function x(t,o,r,n){let e=resolve(process.cwd(),o);existsSync(e)||mkdirSync(e,{recursive:true}),console.log(`\u{1F4E6} Building server to "${o}/" (target: ${r})...`);try{let s=await Bun.build({entrypoints:[t],outdir:e,target:r,minify:n,naming:"server.js"});if(!s.success){console.error("\u274C Server build failed");for(let i of s.logs)console.error(i);process.exit(1);}console.log(`\u2705 Server built to "${o}/server.js"`),console.log(`
|
|
14
|
+
\u{1F4A1} To run: bun ${o}/server.js`);}catch(s){console.error("\u274C Failed to build server. Ensure you are running this command with Bun."),console.error(s),process.exit(1);}}async function D(t,o,r,n){let e=resolve(process.cwd(),o),s=dirname(e);existsSync(s)||mkdirSync(s,{recursive:true});let i=resolve(process.cwd(),".vef-temp-bundle");existsSync(i)||mkdirSync(i,{recursive:true}),console.log(`\u{1F4E6} Building and compiling server to "${o}" (target: ${r})...`);try{let l=await Bun.build({entrypoints:[t],outdir:i,target:r,minify:n,naming:"server.js"});if(!l.success){console.error("\u274C Server build failed");for(let f of l.logs)console.error(f);process.exit(1);}let p=resolve(i,"server.js"),u=spawnSync("bun",["build","--compile",p,"--outfile",e],{stdio:"inherit",env:{...process.env,NODE_ENV:"production"}});u.status!==0&&(console.error("\u274C Bun compile failed"),process.exit(u.status||1)),console.log(`\u2705 Compiled standalone binary: ${o}`),console.log(`
|
|
15
|
+
\u{1F4A1} To run: ./${o}`);}catch(l){console.error("\u274C Failed to compile server. Ensure you are running this command with Bun."),console.error(l),process.exit(1);}finally{existsSync(i)&&rmSync(i,{recursive:true,force:true});}}function O(t){let o={};for(let r=0;r<t.length;r++){let n=t[r],e=t[r+1];switch(n){case "--entry":case "-e":e&&!e.startsWith("-")&&(o.entry=e,r++);break;case "--outDir":case "-d":e&&!e.startsWith("-")&&(o.outDir=e,r++);break;case "--outFile":case "-o":e&&!e.startsWith("-")&&(o.outFile=e,r++);break;case "--target":case "-t":e&&!e.startsWith("-")&&(o.target=e,r++);break;case "--no-minify":o.minify=false;break}}return o}if(import.meta.main){let t=process.argv.slice(2),o=t[0],r=t.slice(1);if(o==="build"){let n=O(r);n.outDir&&n.outFile&&(console.error("\u274C Cannot use both --outDir and --outFile. Choose one."),process.exit(1)),w(n);}else console.log("Usage: vite-elysia-forge build [options]"),console.log(""),console.log("Build the Elysia server for production."),console.log(""),console.log("Options:"),console.log(" --entry, -e <path> Path to API entry file (default: server/api.ts)"),console.log(" --outDir, -d <dir> Output directory for bundled server.js"),console.log(" --outFile, -o <file> Output path for compiled standalone binary"),console.log(" --target, -t <target> Build target: bun, node, browser (default: bun)"),console.log(" --no-minify Disable minification"),console.log(""),console.log("Examples:"),console.log(" # Bundle server to dist/server.js"),console.log(" vite-elysia-forge build --outDir dist"),console.log(""),console.log(" # Bundle with node target"),console.log(" vite-elysia-forge build --outDir dist --target node"),console.log(""),console.log(" # Compile to standalone binary"),console.log(" vite-elysia-forge build --outFile server"),console.log(""),console.log(" # Compile with custom entry"),console.log(" vite-elysia-forge build --entry src/api/index.ts --outFile myserver");}export{w as build};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-elysia-forge",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "A Vite plugin to seamlessly integrate ElysiaJS for full-stack development with Bun runtime.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -36,10 +36,6 @@
|
|
|
36
36
|
"types": "./dist/index.d.ts",
|
|
37
37
|
"import": "./dist/index.js",
|
|
38
38
|
"require": "./dist/index.cjs"
|
|
39
|
-
},
|
|
40
|
-
"./production": {
|
|
41
|
-
"types": "./dist/production.d.ts",
|
|
42
|
-
"import": "./dist/production.js"
|
|
43
39
|
}
|
|
44
40
|
},
|
|
45
41
|
"files": [
|
package/dist/production.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options for starting the production server.
|
|
3
|
-
*/
|
|
4
|
-
interface ProductionOptions {
|
|
5
|
-
/**
|
|
6
|
-
* The port to listen on.
|
|
7
|
-
* @default 3000
|
|
8
|
-
*/
|
|
9
|
-
port?: number;
|
|
10
|
-
/**
|
|
11
|
-
* The directory containing the built frontend assets.
|
|
12
|
-
* @default "dist"
|
|
13
|
-
*/
|
|
14
|
-
distDir?: string;
|
|
15
|
-
/**
|
|
16
|
-
* The name of the HTML entry file.
|
|
17
|
-
* @default "index.html"
|
|
18
|
-
*/
|
|
19
|
-
htmlFile?: string;
|
|
20
|
-
/**
|
|
21
|
-
* The prefix for API routes. Requests starting with this prefix will be handled by the Elysia app.
|
|
22
|
-
* @default "/api"
|
|
23
|
-
*/
|
|
24
|
-
apiPrefix?: string;
|
|
25
|
-
/**
|
|
26
|
-
* The Elysia app instance or an object with a `handle` method.
|
|
27
|
-
*/
|
|
28
|
-
api: {
|
|
29
|
-
handle: (request: Request) => Promise<Response>;
|
|
30
|
-
} | any;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Starts a production server using Bun.serve.
|
|
34
|
-
*
|
|
35
|
-
* This function serves the static frontend assets from the distribution directory
|
|
36
|
-
* and handles API requests using the provided Elysia app instance.
|
|
37
|
-
*
|
|
38
|
-
* @param options - Configuration options for the production server.
|
|
39
|
-
* @throws {Error} If not running in a Bun environment.
|
|
40
|
-
*/
|
|
41
|
-
declare const startServer: (options: ProductionOptions) => void;
|
|
42
|
-
|
|
43
|
-
export { type ProductionOptions, startServer };
|