Skip to content

Commit a4348c6

Browse files
committed
feat!: rename interface of client script
1 parent cb2844a commit a4348c6

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

packages/core/src/client/webcomponents/state/state.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { DevToolsDockEntry } from '@vitejs/devtools-kit'
2-
import type { ImportScriptContext } from '@vitejs/devtools-kit/client'
2+
import type { DockClientScriptContext } from '@vitejs/devtools-kit/client'
33
import type { Ref } from 'vue'
44
import type { DevToolsDockState } from '../components/DockProps'
55
import { computed, reactive } from 'vue'
66

77
export function useStateHandlers(state: Ref<DevToolsDockState>) {
8-
function importScript(entry: DevToolsDockEntry): Promise<(context: ImportScriptContext) => void | Promise<void>> {
9-
const id = entry.id
8+
function importScript(entry: DevToolsDockEntry): Promise<(context: DockClientScriptContext) => void | Promise<void>> {
9+
const id = `${entry.type}:${entry.id}`
1010
return import(/* @vite-ignore */ ['/.devtools', 'imports'].join('-'))
1111
.then((module) => {
1212
const importsMap = module.importsMap as Record<string, () => Promise<() => void>>
@@ -17,21 +17,21 @@ export function useStateHandlers(state: Ref<DevToolsDockState>) {
1717
return importFn()
1818
})
1919
.catch((error) => {
20-
// TODO: maybe popup a error toast here?
21-
// TODO: A unified logger API
20+
// TODO: maybe popup a error toast here?
21+
// TODO: A unified logger API
2222
console.error('[VITE DEVTOOLS] Error executing import action', error)
2323
return Promise.reject(error)
2424
})
2525
}
2626

27-
function selectDockEntry(entry?: DevToolsDockEntry) {
27+
async function selectDockEntry(entry?: DevToolsDockEntry) {
2828
if (!entry) {
2929
state.value.open = false
3030
state.value.dockEntry = undefined
3131
return
3232
}
3333

34-
const scriptContext: ImportScriptContext = reactive({
34+
const scriptContext: DockClientScriptContext = reactive({
3535
dockEntry: entry,
3636
// @ts-expect-error cast for unwraping
3737
dockState: computed<'active' | 'inactive'>({
@@ -52,16 +52,14 @@ export function useStateHandlers(state: Ref<DevToolsDockState>) {
5252

5353
// If it's an action, run and return (early exit)
5454
if (entry?.type === 'action') {
55-
return importScript(entry).then(fn => fn(scriptContext))
55+
return await importScript(entry).then(fn => fn(scriptContext))
5656
}
5757

5858
state.value.dockEntry = entry
5959
state.value.open = true
6060

6161
// If has import script, run it
62-
if (entry.import) {
63-
importScript(entry).then(fn => fn(scriptContext))
64-
}
62+
await importScript(entry).then(fn => fn?.(scriptContext))
6563
}
6664

6765
return {

packages/core/src/node/plugins/server.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
1+
import type { ClientScriptEntry, DevToolsNodeContext } from '@vitejs/devtools-kit'
22
import type { Plugin } from 'vite'
33
import { createDevToolsContext } from '../context'
44
import { createDevToolsMiddleware } from '../server'
@@ -32,10 +32,23 @@ export function DevToolsServer(): Plugin {
3232
throw new Error('DevTools context is not initialized')
3333
}
3434
const docks = Array.from(context.docks.values())
35-
const imports = docks.map(i => i.import ? { id: i.id, ...i.import } : undefined).filter(x => !!x)
35+
const map = new Map<string, ClientScriptEntry>()
36+
for (const dock of docks) {
37+
const id = `${dock.type}:${dock.id}`
38+
if (dock.type === 'action') {
39+
map.set(id, dock.action)
40+
}
41+
else if (dock.type === 'custom-render') {
42+
map.set(id, dock.renderer)
43+
}
44+
else if (dock.type === 'iframe' && dock.clientScript) {
45+
map.set(id, dock.clientScript)
46+
}
47+
}
3648
return [
3749
`export const importsMap = {`,
38-
...imports.map(i => ` ${JSON.stringify(i.id)}: () => import(${JSON.stringify(i.importFrom)}).then(r => r[${JSON.stringify(i.importName)}]),`),
50+
...[...Object.entries(map)]
51+
.map(([id, { importFrom, importName }]) => ` ${JSON.stringify(id)}: () => import(${JSON.stringify(importFrom)}).then(r => r[${JSON.stringify(importName)}]),`),
3952
'}',
4053
].join('\n')
4154
}

packages/kit/src/client/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export interface DevToolsRpcClientOptions {
1717
rpcOptions?: Partial<BirpcOptions<DevToolsRpcServerFunctions>>
1818
}
1919

20-
export interface ImportScriptContext {
20+
/**
21+
* Context for client scripts running in dock entries
22+
*/
23+
export interface DockClientScriptContext {
2124
/**
2225
* The dock entry info of the current dock item
2326
*/
@@ -26,6 +29,13 @@ export interface ImportScriptContext {
2629
* The current state of the dock
2730
*/
2831
dockState: 'active' | 'inactive'
32+
/**
33+
* Type of the client environment
34+
*
35+
* 'embedded' - running inside an embedded floating panel
36+
* 'standalone' - running inside a standlone window (no user app)
37+
*/
38+
clientType: 'embedded' | 'standalone'
2939
/**
3040
* Function to hide the panel, if applicable
3141
*/

packages/kit/src/types/views.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ export interface DevToolsViewIframe extends DevToolsDockEntryBase {
6161
*/
6262
frameId?: string
6363
/**
64-
* Optional script to import into the iframe
64+
* Optional client script to import into the iframe
6565
*/
66-
import?: ClientScriptEntry
66+
clientScript?: ClientScriptEntry
6767
}
6868

6969
export interface DevToolsViewAction extends DevToolsDockEntryBase {
7070
type: 'action'
71-
import: ClientScriptEntry
71+
action: ClientScriptEntry
7272
}
7373

7474
export interface DevToolsViewCustomRender extends DevToolsDockEntryBase {
7575
type: 'custom-render'
76-
import: ClientScriptEntry
76+
renderer: ClientScriptEntry
7777
}
7878

7979
export type DevToolsDockEntry = DevToolsViewIframe | DevToolsViewAction | DevToolsViewCustomRender

0 commit comments

Comments
 (0)