Skip to content

Commit 6a70b79

Browse files
committed
refactor: redesign rpc interface
1 parent ede28bf commit 6a70b79

File tree

15 files changed

+167
-108
lines changed

15 files changed

+167
-108
lines changed

packages/devtools/src/app/backends/static.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ServerFunctionsDump } from '../../shared/types'
1+
import type { ServerFunctionsDump } from '../../node/rpc'
22
import type { Backend } from '../types/backend'
33
import { useRuntimeConfig } from '#app/nuxt'
44
import { parse } from 'structured-clone-es'
@@ -29,8 +29,8 @@ export function createStaticBackend(): Backend {
2929
connectionError: error,
3030
connect() {},
3131
functions: {
32-
getPayload: () => {
33-
return getDump.then(dump => dump.getPayload)
32+
'vite:get-payload': async () => {
33+
return getDump.then(dump => dump['vite:get-payload'])
3434
},
3535
},
3636
}

packages/devtools/src/app/backends/websocket.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { ClientFunctions, ServerFunctions } from '../../shared/types'
1+
import type { ServerFunctions } from '../../node/rpc'
2+
import type { ClientFunctions } from '../../shared/types'
23
import type { Backend } from '../types/backend'
34
import { createBirpc } from 'birpc'
45
import { parse, stringify } from 'structured-clone-es'
@@ -86,17 +87,17 @@ export function createWebSocketBackend(options: WebSocketBackendOptions): Backen
8687
},
8788
isDynamic: true,
8889
functions: {
89-
getPayload: async () => {
90+
'vite:get-payload': async () => {
9091
try {
91-
return await rpc.getPayload()
92+
return await rpc['vite:get-payload']()
9293
}
9394
catch (err) {
9495
error.value = err
9596
throw err
9697
}
9798
},
98-
openInEditor: (filename: string) => rpc.openInEditor.asEvent(filename),
99-
openInFinder: (filename: string) => rpc.openInFinder.asEvent(filename),
99+
'vite:open-in-editor': async (filename: string) => rpc['vite:open-in-editor'].asEvent(filename),
100+
'vite:open-in-finder': async (filename: string) => rpc['vite:open-in-finder'].asEvent(filename),
100101
},
101102
}
102103
}

packages/devtools/src/app/server/api/metadata.json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ws = createWsServer({
1010
}).then((ws) => {
1111
// Warm up the payload
1212
setTimeout(() => {
13-
ws.serverFunctions.getPayload()
13+
ws.serverFunctions['vite:get-payload']()
1414
}, 1)
1515
return ws
1616
})

packages/devtools/src/app/state/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { ref } from 'vue'
44
export const rawEvents = ref<any>([])
55

66
export async function fetchData(backend: Backend) {
7-
rawEvents.value = await backend.functions.getPayload()
7+
rawEvents.value = await backend.functions['vite:get-payload']()
88
}

packages/devtools/src/app/types/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Ref } from 'vue'
2-
import type { ServerFunctions } from '../../shared/types'
2+
import type { ServerFunctions } from '../../node/rpc'
33

44
export type Functions =
55
& Partial<ServerFunctions>
6-
& Pick<ServerFunctions, 'getPayload'>
6+
& Pick<ServerFunctions, 'vite:get-payload'>
77

88
export interface Backend {
99
name: string

packages/devtools/src/node/cli.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { ServerFunctionsDump } from '../shared/types'
1+
import type { ServerFunctionsDump } from './rpc'
22
import { existsSync } from 'node:fs'
3+
34
import fs from 'node:fs/promises'
45
import process from 'node:process'
5-
66
import c from 'ansis'
77
import cac from 'cac'
88
import { getPort } from 'get-port-please'
@@ -31,13 +31,13 @@ cli
3131
const cwd = process.cwd()
3232
const outDir = resolve(cwd, options.outDir)
3333

34-
const rpc = await import('./rpc').then(r => r.createServerFunctions({
35-
cwd,
36-
configFile: options.config,
37-
mode: 'build',
38-
}))
34+
const rpc = await import('./functions')
35+
.then(async r => await r.createServerFunctions({
36+
cwd,
37+
mode: 'build',
38+
}))
3939
const rpcDump: ServerFunctionsDump = {
40-
getPayload: await rpc.getPayload(),
40+
'vite:get-payload': await rpc['vite:get-payload'](),
4141
}
4242

4343
let baseURL = options.base
@@ -89,13 +89,12 @@ cli
8989

9090
const { server, ws } = await createHostServer({
9191
cwd: options.root,
92-
configFile: options.config,
9392
mode: 'dev',
9493
})
9594

9695
// Warm up the payload
9796
setTimeout(() => {
98-
ws.serverFunctions.getPayload()
97+
ws.serverFunctions['vite:get-payload']()
9998
}, 1)
10099

101100
server.listen(port, host, async () => {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { ServerFunctions } from './rpc/index'
2+
import { rpcFunctions } from './rpc/index'
3+
4+
export interface CreateServerFunctionsOptions {
5+
cwd: string
6+
mode: 'dev' | 'build'
7+
}
8+
9+
export async function createServerFunctions(options: CreateServerFunctionsOptions): Promise<ServerFunctions> {
10+
const functions = await Promise.all(
11+
rpcFunctions
12+
.map(async fn => [fn.name, (await fn.setup(options)).handler]),
13+
)
14+
return Object.fromEntries(functions)
15+
}

packages/devtools/src/node/rpc.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineRpcFunction } from '../types'
2+
3+
export const getPayload = defineRpcFunction({
4+
name: 'vite:get-payload',
5+
type: 'static',
6+
setup: () => ({
7+
handler: async () => ({
8+
timestamp: Date.now(),
9+
}),
10+
}),
11+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineRpcFunction } from '../types'
2+
3+
export const openInEditor = defineRpcFunction({
4+
name: 'vite:open-in-editor',
5+
type: 'action',
6+
setup: () => ({
7+
handler: async (path: string) => {
8+
await import('launch-editor').then(r => r.default(path))
9+
},
10+
}),
11+
})

0 commit comments

Comments
 (0)