Skip to content

Commit 0bf04d4

Browse files
committed
feat(vite): init rpc
1 parent b1a1fd1 commit 0bf04d4

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type {} from '@vitejs/devtools'
2+
import type { DevToolsRpcClient } from '@vitejs/devtools-kit/client'
3+
import type {} from '../../node/rpc'
4+
import { useRuntimeConfig } from '#app/nuxt'
5+
import { getDevToolsRpcClient } from '@vitejs/devtools-kit/client'
6+
import { reactive, shallowRef } from 'vue'
7+
8+
export const rpcConnectionState = reactive<{
9+
connected: boolean
10+
error: Error | null
11+
}>({
12+
connected: false,
13+
error: null,
14+
})
15+
16+
const rpc = shallowRef<DevToolsRpcClient>(undefined!)
17+
18+
export async function connect() {
19+
const runtimeConfig = useRuntimeConfig()
20+
try {
21+
rpc.value = await getDevToolsRpcClient({
22+
baseURL: [
23+
'/.devtools/',
24+
runtimeConfig.app.baseURL,
25+
],
26+
connectionMeta: runtimeConfig.app.connection,
27+
wsOptions: {
28+
onConnected: () => {
29+
rpcConnectionState.connected = true
30+
},
31+
onError: (e) => {
32+
rpcConnectionState.error = e
33+
},
34+
onDisconnected: () => {
35+
rpcConnectionState.connected = false
36+
},
37+
},
38+
rpcOptions: {
39+
onGeneralError: (e, name) => {
40+
rpcConnectionState.error = e
41+
console.error(`[vite-devtools] RPC error on executing "${name}":`)
42+
},
43+
onFunctionError: (e, name) => {
44+
rpcConnectionState.error = e
45+
console.error(`[vite-devtools] RPC error on executing "${name}":`)
46+
},
47+
},
48+
})
49+
50+
rpcConnectionState.connected = true
51+
}
52+
catch (e) {
53+
rpcConnectionState.error = e as Error
54+
}
55+
}
56+
57+
export function useRpc() {
58+
return rpc
59+
}

packages/vite/src/modules/rpc.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
2+
import { DevToolsServer } from '../../../core/src/node/plugins/server'
3+
import { rpcFunctions } from '../node/rpc'
4+
5+
export default defineNuxtModule({
6+
meta: {
7+
name: 'devtools-rpc',
8+
configKey: 'devtoolsRpc',
9+
},
10+
setup() {
11+
addVitePlugin({
12+
name: 'vite:devtools:vite',
13+
devtools: {
14+
setup(ctx) {
15+
for (const fn of rpcFunctions) {
16+
ctx.rpc.register(fn)
17+
}
18+
},
19+
},
20+
})
21+
22+
addVitePlugin(DevToolsServer())
23+
},
24+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineRpcFunction } from '@vitejs/devtools-kit'
2+
3+
export const viteHi = defineRpcFunction({
4+
name: 'vite:hi',
5+
type: 'query',
6+
setup: (context) => {
7+
return {
8+
handler: async () => {
9+
return {
10+
message: 'Hi from server.',
11+
cwd: context.cwd,
12+
timestamp: Date.now(),
13+
}
14+
},
15+
}
16+
},
17+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { RpcDefinitionsToFunctions } from '@vitejs/devtools-kit'
2+
import { viteHi } from './functions/hi'
3+
import '@vitejs/devtools-kit'
4+
5+
export const rpcFunctions = [
6+
viteHi,
7+
] as const
8+
9+
export type ServerFunctions = RpcDefinitionsToFunctions<typeof rpcFunctions>
10+
11+
declare module '@vitejs/devtools-kit' {
12+
export interface DevToolsRpcServerFunctions extends ServerFunctions {}
13+
}

packages/vite/src/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default defineNuxtConfig({
1414
'@unocss/nuxt',
1515
'@nuxt/eslint',
1616
'nuxt-eslint-auto-explicit-import',
17+
'./modules/rpc',
1718
],
1819

1920
alias,

0 commit comments

Comments
 (0)