vite-elysia-forge 0.0.6 → 0.0.8
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 +91 -64
- package/dist/cli.js +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,43 +1,40 @@
|
|
|
1
1
|
# vite-elysia-forge
|
|
2
2
|
|
|
3
|
-
<
|
|
4
|
-
|
|
5
|
-
<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
<!-- Vite symbol -->
|
|
13
|
-
<polygon points="50,20 70,60 50,100 30,60" fill="url(#viteGradient)" />
|
|
14
|
-
|
|
15
|
-
<!-- Arrow connecting to Elysia -->
|
|
16
|
-
<line x1="80" y1="60" x2="120" y2="60" stroke="#646cff" stroke-width="3" marker-end="url(#arrowhead)" />
|
|
17
|
-
<defs>
|
|
18
|
-
<marker id="arrowhead" markerWidth="10" markerHeight="10" refX="9" refY="3" orient="auto">
|
|
19
|
-
<polygon points="0 0, 10 3, 0 6" fill="#646cff" />
|
|
20
|
-
</marker>
|
|
21
|
-
</defs>
|
|
22
|
-
|
|
23
|
-
<!-- Elysia symbol (simplified) -->
|
|
24
|
-
<circle cx="150" cy="30" r="15" fill="#e0db55" />
|
|
25
|
-
<circle cx="150" cy="90" r="15" fill="#e0db55" />
|
|
26
|
-
<rect x="135" y="50" width="30" height="20" fill="#e0db55" />
|
|
27
|
-
</svg>
|
|
28
|
-
|
|
29
|
-
Vite middleware plugin that hot-reloads an Elysia API module and forwards `/api` requests to it during local development.
|
|
30
|
-
|
|
31
|
-
## Installation
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="https://vitejs.dev/logo.svg" alt="Vite" width="60" height="60" />
|
|
5
|
+
<img src="https://elysiajs.com/assets/elysia.svg" alt="Elysia" width="60" height="60" />
|
|
6
|
+
<img src="https://bun.sh/logo.svg" alt="Bun" width="60" height="60" />
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
A [Vite](https://vite.dev/) middleware plugin that hot-reloads an [Elysia](https://elysiajs.com/) API module and forwards `/api` requests to it during local development. Powered by [Bun](https://bun.sh/).
|
|
10
|
+
|
|
11
|
+
## 1. Installation
|
|
32
12
|
|
|
33
13
|
```bash
|
|
34
|
-
bun install
|
|
14
|
+
bun install vite-elysia-forge
|
|
35
15
|
```
|
|
36
16
|
|
|
37
|
-
## Quick Start
|
|
17
|
+
## 2. Quick Start
|
|
18
|
+
|
|
19
|
+
### 2.1 Create Your API Handler
|
|
38
20
|
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
Place your Elysia handler at `src/server/api.ts` (default path):
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Elysia } from "elysia";
|
|
25
|
+
|
|
26
|
+
export const api = new Elysia({
|
|
27
|
+
prefix: "/api",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
api.get("/", () => "hello from elysia");
|
|
31
|
+
|
|
32
|
+
export default api;
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2.2 Configure Vite
|
|
36
|
+
|
|
37
|
+
Register the plugin in your Vite config:
|
|
41
38
|
|
|
42
39
|
```ts
|
|
43
40
|
import { defineConfig } from "vite";
|
|
@@ -46,85 +43,115 @@ import elysiaPlugin from "vite-elysia-forge";
|
|
|
46
43
|
export default defineConfig({
|
|
47
44
|
plugins: [
|
|
48
45
|
elysiaPlugin({
|
|
49
|
-
serverFile: "/server/api.ts",
|
|
46
|
+
serverFile: "./src/server/api.ts",
|
|
50
47
|
}),
|
|
51
48
|
],
|
|
52
49
|
});
|
|
53
50
|
```
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
## Starting the App
|
|
52
|
+
### 2.3 Start Development
|
|
58
53
|
|
|
59
|
-
|
|
54
|
+
Run Vite as usual and access your API at `/api/*` routes. The plugin will automatically reload the Elysia module when files change.
|
|
60
55
|
|
|
61
56
|
```bash
|
|
62
|
-
bun run
|
|
63
|
-
bun start
|
|
57
|
+
bun run dev
|
|
64
58
|
```
|
|
65
59
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
## Configuration
|
|
69
|
-
|
|
70
|
-
| Option | Type | Default | Description |
|
|
71
|
-
| ------------ | ------ | ------------ | ------------------------------------------------------- |
|
|
72
|
-
| `serverURL` | string | `"/server/"` | URL prefix to your API module (leading slash required). |
|
|
73
|
-
| `serverFile` | string | `"api.ts"` | Filename of the Elysia module to load and hot-reload. |
|
|
60
|
+
## 3. API Module Requirements
|
|
74
61
|
|
|
75
|
-
|
|
62
|
+
Your API module must export an Elysia instance with a `handle(request: Request) => Promise<Response>` method.
|
|
76
63
|
|
|
77
|
-
|
|
64
|
+
### 3.1 Basic Example
|
|
78
65
|
|
|
79
66
|
```ts
|
|
67
|
+
import { Elysia } from "elysia";
|
|
68
|
+
|
|
80
69
|
export const api = new Elysia({
|
|
81
70
|
prefix: "/api",
|
|
82
71
|
});
|
|
83
72
|
|
|
84
73
|
api.get("/", () => "hello from elysia");
|
|
74
|
+
api.get("/users", () => ["user1", "user2"]);
|
|
85
75
|
|
|
86
76
|
export default api;
|
|
87
77
|
```
|
|
88
78
|
|
|
89
|
-
##
|
|
79
|
+
## 4. Integration with @elysiajs/openapi
|
|
90
80
|
|
|
91
|
-
|
|
81
|
+
To use the [@elysiajs/openapi plugin](https://elysiajs.com/patterns/openapi), add the following to your `tsconfig.json`:
|
|
92
82
|
|
|
93
|
-
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"compilerOptions": {
|
|
86
|
+
"types": ["bun-types"],
|
|
87
|
+
"typeRoots": ["node_modules"]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 4.1 Example with `fromTypes`
|
|
93
|
+
|
|
94
|
+
It is recommended to pre-generate the declaration file (`.d.ts`) to provide type declaration to the generator.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { Elysia, t } from "elysia";
|
|
98
|
+
import { openapi, fromTypes } from "@elysiajs/openapi";
|
|
99
|
+
|
|
100
|
+
const app = new Elysia().use(
|
|
101
|
+
openapi({
|
|
102
|
+
references: fromTypes("server/api"),
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 5. Production Deployment
|
|
108
|
+
|
|
109
|
+
### 5.1 Build Configuration
|
|
110
|
+
|
|
111
|
+
Update your `package.json` scripts:
|
|
94
112
|
|
|
95
113
|
```json
|
|
96
114
|
{
|
|
97
115
|
"scripts": {
|
|
116
|
+
"dev": "vite",
|
|
98
117
|
"build": "vite-elysia-forge build",
|
|
99
118
|
"start": "bun dist/server.js"
|
|
100
119
|
}
|
|
101
120
|
}
|
|
102
121
|
```
|
|
103
122
|
|
|
104
|
-
|
|
123
|
+
If your API is located elsewhere, specify the path:
|
|
105
124
|
|
|
106
125
|
```bash
|
|
107
|
-
|
|
126
|
+
vite-elysia-forge build src/my-api.ts
|
|
108
127
|
```
|
|
109
128
|
|
|
110
|
-
|
|
129
|
+
### 5.2 Building for Production
|
|
111
130
|
|
|
112
|
-
|
|
113
|
-
2. Automatically generate a temporary entry file that imports your API from `src/server/api.ts` (default).
|
|
114
|
-
3. Bundle the server into a single file at `dist/server.js`.
|
|
115
|
-
|
|
116
|
-
If your API is located elsewhere, you can specify the path:
|
|
131
|
+
Run the build command:
|
|
117
132
|
|
|
118
133
|
```bash
|
|
119
|
-
|
|
134
|
+
bun run build
|
|
120
135
|
```
|
|
121
136
|
|
|
122
|
-
|
|
137
|
+
This command performs the following steps:
|
|
138
|
+
|
|
139
|
+
1. Runs `vite build` to compile your frontend to `dist/`
|
|
140
|
+
2. Automatically generates a temporary entry file that imports your API from `src/server/api.ts`
|
|
141
|
+
3. Bundles the server into a single file at `dist/server.js`
|
|
142
|
+
|
|
143
|
+
### 5.3 Starting the Production Server
|
|
144
|
+
|
|
145
|
+
Start the server with:
|
|
123
146
|
|
|
124
147
|
```bash
|
|
125
148
|
bun start
|
|
126
149
|
```
|
|
127
150
|
|
|
128
|
-
## Authors
|
|
151
|
+
## 6. Authors
|
|
129
152
|
|
|
130
153
|
- Chijioke Udokporo ([@chijiokeudokporo](https://github.com/chijioke-udokporo))
|
|
154
|
+
|
|
155
|
+
## 7. License
|
|
156
|
+
|
|
157
|
+
MIT
|
package/dist/cli.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
import {spawnSync}from'child_process';import {existsSync,mkdirSync,writeFileSync,unlinkSync}from'fs';import {resolve,relative,sep}from'path';async function
|
|
2
|
+
import {spawnSync}from'child_process';import {existsSync,mkdirSync,writeFileSync,unlinkSync,rmSync}from'fs';import {resolve,relative,sep}from'path';async function b(o="src/server/api.ts"){let c=resolve(process.cwd(),o);existsSync(c)||(console.error(`\u274C API entry file "${o}" not found.`),console.error(' By default, vite-elysia-forge looks for "src/server/api.ts".'),console.error(" If your API is located elsewhere, please specify the path:"),console.error(" $ vite-elysia-forge build <path-to-your-api-file>"),process.exit(1));let t=spawnSync("bun",["x","vite","build"],{stdio:"inherit",env:{...process.env,NODE_ENV:"production"}});t.status!==0&&(console.error("\u274C Vite build failed"),process.exit(t.status||1));let e=resolve(process.cwd(),".output");existsSync(e)||mkdirSync(e,{recursive:true});let s=resolve(e,".temp-prod.ts"),r=relative(e,c);r=r.split(sep).join("/"),r.startsWith(".")||(r="./"+r);let a=`
|
|
3
3
|
import { startServer } from "vite-elysia-forge/production";
|
|
4
|
-
import { api } from "${
|
|
4
|
+
import { api } from "${r}";
|
|
5
5
|
|
|
6
6
|
startServer({
|
|
7
7
|
api,
|
|
8
8
|
port: process.env.PORT ? parseInt(process.env.PORT) : 3000,
|
|
9
9
|
distDir: "dist",
|
|
10
10
|
});
|
|
11
|
-
`;writeFileSync(s,a);try{let i=await Bun.build({entrypoints:[s],outdir:"dist",target:"bun",minify:!0,naming:"server.js"});if(!i.success){console.error("\u274C Server build failed");for(let
|
|
11
|
+
`;writeFileSync(s,a);try{let i=await Bun.build({entrypoints:[s],outdir:"dist",target:"bun",minify:!0,naming:"server.js"});if(!i.success){console.error("\u274C Server build failed");for(let u of i.logs)console.error(u);process.exit(1);}}catch(i){console.error("\u274C Failed to build server. Ensure you are running this command with Bun."),console.error(i),process.exit(1);}finally{existsSync(s)&&unlinkSync(s),existsSync(e)&&rmSync(e,{recursive:true,force:true});}}if(import.meta.main){let o=process.argv.slice(2);if(o[0]==="build"){let t=o[1];b(t);}else console.log("Usage: vite-elysia-forge build [api-entry]"),console.log(" api-entry: Path to your API entry file (default: src/server/api.ts)");}export{b as build};
|