1
- import { defineConfig , mergeConfig } from "vite" ;
2
-
3
- import react from "@vitejs/plugin-react" ;
4
- import basicSsl from "@vitejs/plugin-basic-ssl" ;
5
- import path from "node:path" ;
6
1
import fs from "node:fs/promises" ;
2
+ import path from "node:path" ;
7
3
import devServer from "@hono/vite-dev-server" ;
4
+ import basicSsl from "@vitejs/plugin-basic-ssl" ;
5
+ import react from "@vitejs/plugin-react" ;
6
+ import { defineConfig , mergeConfig , type Plugin } from "vite" ;
8
7
9
8
const OUT_DIR = ".vercel" ;
10
9
@@ -37,6 +36,71 @@ const vercelConfigPlugin = () => ({
37
36
} ,
38
37
} ) ;
39
38
39
+ /**
40
+ * Generate the vercel specific code within the server entry file.
41
+ *
42
+ * ```ts
43
+ * import { handle } from "hono/vercel";
44
+ *
45
+ * ...
46
+ *
47
+ * const handler = handle(app);
48
+ * export const GET = handler;
49
+ * export const POST = handler;
50
+ * export const PATCH = handler;
51
+ * export const PUT = handler;
52
+ * export const OPTIONS = handler;
53
+ * ```
54
+ *
55
+ */
56
+ const vercelEntryPlugin = ( ) : Plugin => {
57
+ let entry : string ;
58
+ let resolvedEntryPath : string ;
59
+ let projectRoot : string ;
60
+
61
+ return {
62
+ name : "vercel-entry" ,
63
+ configResolved ( config ) {
64
+ if ( config . build . lib ) {
65
+ const e = config . build . lib . entry ;
66
+ if ( typeof e === "string" ) {
67
+ entry = e ;
68
+ } else {
69
+ throw new Error ( "Entry must be a string path" ) ;
70
+ }
71
+ }
72
+
73
+ projectRoot = config . root ;
74
+ resolvedEntryPath = path . normalize ( path . resolve ( projectRoot , entry ) ) ;
75
+ } ,
76
+ async load ( id ) {
77
+ const normalizedId = path . normalize ( path . resolve ( projectRoot , id ) ) ;
78
+
79
+ if ( normalizedId === resolvedEntryPath ) {
80
+ try {
81
+ const content = await fs . readFile ( resolvedEntryPath , "utf-8" ) ;
82
+ const transformedContent = [
83
+ 'import { handle } from "hono/vercel";' ,
84
+ content ,
85
+ "const handler = handle(app);" ,
86
+ "export const GET = handler;" ,
87
+ "export const POST = handler;" ,
88
+ "export const PATCH = handler;" ,
89
+ "export const PUT = handler;" ,
90
+ "export const OPTIONS = handler;" ,
91
+ ] . join ( "\n" ) ;
92
+
93
+ return transformedContent ;
94
+ } catch ( e ) {
95
+ this . error ( `Failed to process entry file ${ entry } : ${ e } ` ) ;
96
+ }
97
+ }
98
+
99
+ return null ;
100
+ } ,
101
+ } ;
102
+ } ;
103
+
40
104
/**
41
105
* Vite is handling both the building of our final assets and also running the
42
106
* dev server which gives us HMR for both SSR'd templates and client React code.
@@ -62,7 +126,7 @@ const vercelConfigPlugin = () => ({
62
126
* paths within the code. This is something that could be improved to make
63
127
* the build script less brittle.
64
128
*
65
- * [1]: <https://vercel.com/docs/build-output-api>
129
+ * [1]: <https://vercel.com/docs/build-output-api>
66
130
*/
67
131
export default defineConfig ( ( { mode, command } ) => {
68
132
const baseConfig = {
@@ -101,13 +165,15 @@ export default defineConfig(({ mode, command }) => {
101
165
name : "server" ,
102
166
formats : [ "umd" ] ,
103
167
} ,
168
+ plugins : [ vercelConfigPlugin ( ) ] ,
104
169
rollupOptions : {
105
170
output : {
106
171
entryFileNames : "output/functions/index.func/index.js" ,
107
172
} ,
108
- plugins : [ vercelConfigPlugin ( ) ] ,
173
+ // plugins: [vercelEntryPlugin("src/server/index.tsx" )],
109
174
} ,
110
175
} ,
176
+ plugins : [ vercelEntryPlugin ( ) ] ,
111
177
} ;
112
178
113
179
const devConfig = {
0 commit comments