Skip to content

Commit b7ab325

Browse files
committed
wip: make embedding work
1 parent 604b61a commit b7ab325

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+342
-709
lines changed
Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,67 @@
1-
import type { BirpcReturn } from 'birpc'
1+
import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/client'
2+
import type { BirpcOptions, BirpcReturn } from 'birpc'
23
import type { ConnectionMeta, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions } from '../types'
34
import { createRpcClient } from '@vitejs/devtools-rpc'
45
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client'
56

6-
function isNumeric(str: string | number) {
7+
function isNumeric(str: string | number | undefined) {
8+
if (str == null)
9+
return false
710
return `${+str}` === `${str}`
811
}
912

10-
export async function getDevToolsRpc(baseURL = '/__vite_devtools__/api/'): Promise<BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>> {
11-
const metadata = await fetch(`${baseURL}metadata.json`)
12-
.then(r => r.json()) as ConnectionMeta
13+
export interface DevToolsRpcClientOptions {
14+
connectionMeta?: ConnectionMeta
15+
baseURL?: string[]
16+
wsOptions?: Partial<WebSocketRpcClientOptions>
17+
rpcOptions?: Partial<BirpcOptions<DevToolsRpcServerFunctions>>
18+
}
19+
20+
export async function getDevToolsRpcClient(
21+
options: DevToolsRpcClientOptions = {},
22+
): Promise<{
23+
connectionMeta: ConnectionMeta
24+
rpc: BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>
25+
}> {
26+
const {
27+
baseURL = '/__vite_devtools__/api/',
28+
} = options
29+
const urls = Array.isArray(baseURL) ? baseURL : [baseURL]
30+
let connectionMeta: ConnectionMeta | undefined = options.connectionMeta
31+
32+
if (!connectionMeta) {
33+
const errors: Error[] = []
34+
for (const url of urls) {
35+
try {
36+
connectionMeta = await fetch(`${url}connection.json`)
37+
.then(r => r.json()) as ConnectionMeta
38+
break
39+
}
40+
catch (e) {
41+
errors.push(e as Error)
42+
}
43+
}
44+
if (!connectionMeta) {
45+
throw new Error(`Failed to get connection meta from ${urls.join(', ')}`, {
46+
cause: errors,
47+
})
48+
}
49+
}
1350

14-
const url = isNumeric(metadata.websocket)
15-
? `${location.protocol.replace('http', 'ws')}//${location.hostname}:${metadata.websocket}`
16-
: metadata.websocket as string
51+
const url = isNumeric(connectionMeta.websocket)
52+
? `${location.protocol.replace('http', 'ws')}//${location.hostname}:${connectionMeta.websocket}`
53+
: connectionMeta.websocket as string
1754

1855
const rpc = createRpcClient<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>({}, {
1956
preset: createWsRpcPreset({
2057
url,
58+
...options.wsOptions,
2159
}),
60+
...options.rpcOptions,
2261
})
2362

24-
return rpc
63+
return {
64+
connectionMeta,
65+
rpc,
66+
}
2567
}

packages/devtools-kit/src/types/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './rpc'
22
export * from './rpc-augments'
3-
export * from './rpc-core-functions'
43
export * from './utils'
54
export * from './views'
65
export * from './vite-augment'

packages/devtools-kit/src/types/rpc-core-functions.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

packages/devtools-kit/src/types/rpc.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { DevToolsRpcServerFunctions } from './rpc-augments'
22
import type { EntriesToObject, Thenable } from './utils'
3+
import type { DevToolsNodeContext } from './vite-plugin'
4+
5+
export type { BirpcFn, BirpcReturn } from 'birpc'
36

47
/**
58
* Type of the RPC function,
@@ -10,6 +13,7 @@ import type { EntriesToObject, Thenable } from './utils'
1013
export type RpcFunctionType = 'static' | 'action' | 'query'
1114

1215
export interface RpcFunctionsHost {
16+
context: DevToolsNodeContext
1317
readonly functions: DevToolsRpcServerFunctions
1418
readonly definitions: Map<string, RpcFunctionDefinition<string, any, any, any>>
1519
register: (fn: RpcFunctionDefinition<string, any, any, any>) => void
@@ -32,19 +36,12 @@ export interface RpcFunctionDefinition<
3236
> {
3337
name: NAME
3438
type: TYPE
35-
setup: (context: RpcContext) => Thenable<RpcFunctionSetupResult<ARGS, RETURN>>
39+
setup: (context: DevToolsNodeContext) => Thenable<RpcFunctionSetupResult<ARGS, RETURN>>
3640
handler?: (...args: ARGS) => RETURN
3741
__resolved?: RpcFunctionSetupResult<ARGS, RETURN>
3842
__promise?: Thenable<RpcFunctionSetupResult<ARGS, RETURN>>
3943
}
4044

41-
export interface RpcContext {
42-
cwd: string
43-
mode: 'dev' | 'build'
44-
functions: RpcFunctionsHost
45-
meta?: any
46-
}
47-
4845
export type RpcDefinitionsToFunctions<T extends readonly RpcFunctionDefinition<any, any, any>[]> = EntriesToObject<{
4946
[K in keyof T]: [T[K]['name'], Awaited<ReturnType<T[K]['setup']>>['handler']]
5047
}>

packages/devtools-kit/src/types/views.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface DevToolsDockHost {
22
register: (entry: DevToolsDockEntry) => void
3+
values: () => DevToolsDockEntry[]
34
}
45

56
export interface DevToolsDockEntryBase {

packages/devtools-kit/src/types/vite-plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ResolvedConfig } from 'vite'
1+
import type { ResolvedConfig, ViteDevServer } from 'vite'
22
import type { RpcFunctionsHost } from './rpc'
33
import type { DevToolsDockHost } from './views'
44

@@ -12,13 +12,14 @@ export interface DevToolsPluginOptions {
1212
dev?: DevToolsCapabilities | boolean
1313
build?: DevToolsCapabilities | boolean
1414
}
15-
setup: (context: DevToolsSetupContext) => void | Promise<void>
15+
setup: (context: DevToolsNodeContext) => void | Promise<void>
1616
}
1717

18-
export interface DevToolsSetupContext {
18+
export interface DevToolsNodeContext {
1919
readonly cwd: string
2020
readonly mode: 'dev' | 'build'
2121
readonly viteConfig: ResolvedConfig
22+
readonly viteServer?: ViteDevServer
2223
rpc: RpcFunctionsHost
2324
docks: DevToolsDockHost
2425
}

packages/devtools-kit/src/utils/rpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RpcContext, RpcFunctionDefinition, RpcFunctionType } from '../types'
1+
import type { DevToolsNodeContext, RpcFunctionDefinition, RpcFunctionType } from '../types'
22

33
export function defineRpcFunction<
44
NAME extends string,
@@ -18,7 +18,7 @@ export async function getRpcHandler<
1818
RETURN = void,
1919
>(
2020
definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN>,
21-
context: RpcContext,
21+
context: DevToolsNodeContext,
2222
): Promise<(...args: ARGS) => RETURN> {
2323
if (definition.handler) {
2424
return definition.handler

packages/devtools-rpc/package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
],
2020
"sideEffects": false,
2121
"exports": {
22-
".": "./dist/index.mjs",
23-
"./presets": "./dist/presets/index.mjs",
24-
"./presets/ws/client": "./dist/presets/ws/client.mjs",
25-
"./presets/ws/server": "./dist/presets/ws/server.mjs"
22+
".": "./dist/index.js",
23+
"./presets": "./dist/presets/index.js",
24+
"./presets/ws/client": "./dist/presets/ws/client.js",
25+
"./presets/ws/server": "./dist/presets/ws/server.js",
26+
"./package.json": "./package.json"
2627
},
27-
"main": "./dist/index.mjs",
28-
"types": "./dist/index.d.mts",
28+
"main": "./dist/index.js",
29+
"module": "./dist/index.js",
30+
"types": "./dist/index.d.ts",
2931
"files": [
3032
"dist"
3133
],
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { defineConfig } from 'tsdown'
22

3-
export default defineConfig([
4-
{
5-
entry: [
6-
'src/index.ts',
7-
],
8-
tsconfig: '../../tsconfig.pkgs.json',
9-
clean: true,
10-
fixedExtension: true,
11-
dts: true,
3+
export default defineConfig({
4+
entry: {
5+
'index': 'src/index.ts',
6+
'presets/ws/client': 'src/presets/ws/client.ts',
7+
'presets/ws/server': 'src/presets/ws/server.ts',
8+
'presets/index': 'src/presets/index.ts',
129
},
13-
{
14-
entry: ['src/presets/ws/client.ts', 'src/presets/ws/server.ts', 'src/presets/index.ts'],
15-
tsconfig: '../../tsconfig.pkgs.json',
16-
clean: true,
17-
fixedExtension: true,
18-
outDir: 'dist/presets',
19-
dts: true,
20-
},
21-
])
10+
tsconfig: '../../tsconfig.pkgs.json',
11+
clean: true,
12+
dts: true,
13+
exports: true,
14+
})

packages/devtools-vite/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
],
1919
"sideEffects": false,
2020
"exports": {
21-
".": "./dist/index.js"
21+
".": "./dist/index.js",
22+
"./dirs": "./dist/dirs.js",
23+
"./package.json": "./package.json"
2224
},
2325
"main": "./dist/index.js",
26+
"module": "./dist/index.js",
2427
"types": "./dist/index.d.ts",
2528
"files": [
2629
"dist"
@@ -52,6 +55,7 @@
5255
"p-limit": "catalog:deps",
5356
"pathe": "catalog:deps",
5457
"publint": "catalog:deps",
58+
"sirv": "catalog:",
5559
"split2": "catalog:deps",
5660
"structured-clone-es": "catalog:deps",
5761
"tinyglobby": "catalog:deps",

0 commit comments

Comments
 (0)