Skip to content

Commit fe6e240

Browse files
committed
refactor: move types
1 parent a2725d7 commit fe6e240

38 files changed

Lines changed: 150 additions & 156 deletions

packages/devtools-kit/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"vite": "catalog:build"
3636
},
3737
"devDependencies": {
38-
"tsdown": "catalog:build"
38+
"tsdown": "catalog:build",
39+
"vite": "catalog:build"
3940
}
4041
}

packages/devtools-kit/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const one = 1
1+
export type * from './types'
2+
export * from './utils/rpc'
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
export interface DevToolsPluginOptions {
2-
setup: () => void | Promise<void>
3-
}
1+
import type { DevToolsPluginOptions } from './vite'
2+
import 'vite'
43

54
// Extend Vite's Plugin interface
65
declare module 'vite' {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import './extend'
2+
3+
export * from './rpc'
4+
export * from './utils'
5+
export * from './vite'
Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RolldownLogsManager } from '../rolldown/logs-manager'
1+
import type { EntriesToObject, Thenable } from './utils'
22

33
/**
44
* Type of the RPC function,
@@ -8,8 +8,6 @@ import type { RolldownLogsManager } from '../rolldown/logs-manager'
88
*/
99
export type RpcFunctionType = 'static' | 'action' | 'query'
1010

11-
export type Thenable<T> = T | Promise<T>
12-
1311
export interface RpcFunctionSetupResult<
1412
ARGS extends any[],
1513
RETURN = void,
@@ -33,39 +31,16 @@ export interface RpcFunctionDefinition<
3331
export interface RpcContext {
3432
cwd: string
3533
mode: 'dev' | 'build'
36-
manager: RolldownLogsManager
37-
}
38-
39-
export type EntriesToObject<T extends readonly [string, any][]> = {
40-
[K in T[number] as K[0]]: K[1]
34+
meta?: any
4135
}
4236

43-
export type DefinitionsToFunctions<T extends readonly RpcFunctionDefinition<any, any, any>[]> = EntriesToObject<{
37+
export type RpcDefinitionsToFunctions<T extends readonly RpcFunctionDefinition<any, any, any>[]> = EntriesToObject<{
4438
[K in keyof T]: [T[K]['name'], Awaited<ReturnType<T[K]['setup']>>['handler']]
4539
}>
4640

47-
export type FilterDefinitions<
41+
export type RpcDefinitionsFilter<
4842
T extends readonly RpcFunctionDefinition<any, any, any>[],
4943
Type extends RpcFunctionType,
5044
> = {
5145
[K in keyof T]: T[K] extends { type: Type } ? T[K] : never
5246
}
53-
54-
// type a = DefinitionsToFunctions<
55-
// FilterDefinitions<[
56-
// {
57-
// name: 'a'
58-
// type: 'static'
59-
// setup: () => ({
60-
// handler: () => void
61-
// })
62-
// },
63-
// {
64-
// name: 'b'
65-
// type: 'action'
66-
// setup: () => ({
67-
// handler: (a: string) => void
68-
// })
69-
// },
70-
// ], 'action' | 'static'>
71-
// >
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type Thenable<T> = T | Promise<T>
2+
3+
export type EntriesToObject<T extends readonly [string, any][]> = {
4+
[K in T[number] as K[0]]: K[1]
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { RpcContext } from './rpc'
2+
3+
export interface DevToolsPluginOptions {
4+
setup: () => void | Promise<void>
5+
}
6+
7+
export interface DevToolsSetupContext {
8+
rpc: RpcContext
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { RpcContext, RpcFunctionDefinition, RpcFunctionType } from '../types'
2+
3+
export function defineRpcFunction<
4+
NAME extends string,
5+
TYPE extends RpcFunctionType,
6+
ARGS extends any[],
7+
RETURN = void,
8+
>(
9+
definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN>,
10+
): RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN> {
11+
return definition
12+
}
13+
14+
export async function getRpcHandler<
15+
NAME extends string,
16+
TYPE extends RpcFunctionType,
17+
ARGS extends any[],
18+
RETURN = void,
19+
>(
20+
definition: RpcFunctionDefinition<NAME, TYPE, ARGS, RETURN>,
21+
context: RpcContext,
22+
): Promise<(...args: ARGS) => RETURN> {
23+
if (definition.handler) {
24+
return definition.handler
25+
}
26+
const result = definition.__resolved ??= await definition.setup(context)
27+
return result.handler
28+
}

packages/devtools-kit/tsconfig.json

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { defineConfig } from 'tsdown'
22

3-
export default defineConfig([
4-
{
5-
entry: [
6-
'src/index.ts',
7-
],
8-
clean: true,
9-
fixedExtension: true,
10-
dts: true,
11-
},
12-
])
3+
export default defineConfig({
4+
entry: [
5+
'src/index.ts',
6+
],
7+
clean: true,
8+
tsconfig: '../../tsconfig.pkgs.json',
9+
fixedExtension: true,
10+
dts: true,
11+
})

0 commit comments

Comments
 (0)