-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
68 lines (62 loc) · 2.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createViteRuntime, type Plugin } from 'vite'
import type { ViteEnvApi } from '@vite-runtime/types'
import { createMiddleware } from '@hattip/adapter-node'
import path from 'node:path'
export const exampleFramework = (entrypoint: string): Plugin => {
let runtimeEnvPlugin: Plugin<ViteEnvApi> | undefined
let resolvedEntrypoint!: string
let teardown: (() => void | Promise<void>) | undefined
return {
name: 'example-framework',
configResolved(config) {
resolvedEntrypoint = path.resolve(config.root, entrypoint)
const runtimeEnvPlugins = config.plugins.filter(
(plugin) => plugin.api && 'createRuntime' in plugin.api
) as Plugin<ViteEnvApi>[]
if (runtimeEnvPlugins.length > 2) {
throw new Error('Multiple runtime env plugin should not be used')
}
if (runtimeEnvPlugins.length === 1) {
runtimeEnvPlugin = runtimeEnvPlugins[0]
}
},
async configureServer(server) {
const runtime = await (runtimeEnvPlugin
? runtimeEnvPlugin.api!.createRuntime(server)
: createViteRuntime(server))
const dispatchRequest =
'dispatchRequest' in runtime && runtime.dispatchRequest
? (request: Request) => {
return runtime.dispatchRequest!(resolvedEntrypoint, request)
}
: async (request: Request) => {
const module = (await runtime.executeUrl!(
resolvedEntrypoint
)) as { [Symbol.toStringTag]: 'Module' }
if (
!('default' in module) ||
typeof module.default !== 'function'
)
throw new Error('should have a default export of function type')
return module.default(request)
}
teardown = 'teardown' in runtime ? runtime.teardown : undefined
const middleware = createMiddleware(
async (ctx) => {
return await dispatchRequest(ctx.request)
},
{ alwaysCallNext: false }
)
server.middlewares.use((req, res, next) => {
if (req.url!.startsWith('/api/')) {
middleware(req, res, next)
return
}
next()
})
},
async buildEnd() {
await teardown?.()
}
}
}