Skip to content

Commit 735efa4

Browse files
committed
feat(core): add simple support for action type
1 parent b37bab7 commit 735efa4

File tree

7 files changed

+86
-10
lines changed

7 files changed

+86
-10
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"cac": "catalog:deps",
5858
"debug": "catalog:deps",
5959
"launch-editor": "catalog:deps",
60+
"mlly": "catalog:deps",
6061
"open": "catalog:deps",
6162
"pathe": "catalog:deps",
6263
"sirv": "catalog:deps",

packages/core/playground/vite.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export default defineConfig({
3939
id: 'local',
4040
type: 'iframe',
4141
url: 'https://antfu.me',
42+
43+
})
44+
45+
ctx.docks.register({
46+
type: 'action',
47+
import: ctx.utils.clientEntryFromSimpleFunction(() => {
48+
// eslint-disable-next-line no-alert
49+
alert('Hello, world!')
50+
}),
51+
id: 'local2',
52+
title: 'Local2',
53+
icon: 'https://upload.wikimedia.org/wikipedia/commons/9/95/Vue.js_Logo_2.svg',
4254
})
4355
},
4456
},

packages/core/src/client/webcomponents/components/Dock.vue

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup lang="ts">
2+
import type { ClientScriptEntry, DevToolsDockEntry } from '@vitejs/devtools-kit'
23
import type { DockProps } from './DockProps'
34
import { useEventListener, useScreenSafeArea } from '@vueuse/core'
45
import { computed, onMounted, reactive, ref, toRefs, useTemplateRef, watchEffect } from 'vue'
@@ -213,6 +214,32 @@ const panelStyle = computed(() => {
213214
return style
214215
})
215216
217+
// TODO: use a visual module to host all the imports
218+
function importScript(entry: ClientScriptEntry) {
219+
return import(/* @vite-ignore */ entry.importFrom)
220+
.then((module) => {
221+
return module[entry.importName || 'default']
222+
})
223+
.catch((error) => {
224+
// TODO: maybe popup a error toast here?
225+
console.error('[VITE DEVTOOLS] Error importing action', error)
226+
return Promise.reject(error)
227+
})
228+
}
229+
230+
function onSelected(entry: DevToolsDockEntry) {
231+
if (entry?.type === 'action') {
232+
return importScript(entry.import).then(fn => fn())
233+
}
234+
235+
state.value.dockEntry = entry
236+
237+
// TODO: handle for each type of entry
238+
if ('import' in entry && entry.import) {
239+
importScript(entry.import)
240+
}
241+
}
242+
216243
onMounted(() => {
217244
bringUp()
218245
recalculateCounter.value++
@@ -250,7 +277,7 @@ onMounted(() => {
250277
:class="isMinimized ? 'opacity-0' : 'opacity-100'"
251278
:is-vertical="isVertical"
252279
:selected="state.dockEntry"
253-
@select="s => state.dockEntry = s"
280+
@select="onSelected"
254281
/>
255282
</div>
256283
</div>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { DevToolsNodeUtils } from '@vitejs/devtools-kit'
2+
import { toDataURL } from 'mlly'
3+
4+
export const ContextUtils: DevToolsNodeUtils = {
5+
clientEntryFromSimpleFunction: (fn: () => void) => {
6+
const code = `const fn = ${fn.toString()}; export default fn`
7+
return {
8+
importFrom: toDataURL(code),
9+
importName: 'default',
10+
}
11+
},
12+
}

packages/core/src/node/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
22
import type { ResolvedConfig, ViteDevServer } from 'vite'
33
import Debug from 'debug'
4+
import { ContextUtils } from './context-utils'
45
import { DevToolsDockHost } from './host-docks'
56
import { RpcFunctionsHost } from './host-functions'
67
import { DevToolsViewHost } from './host-views'
@@ -22,6 +23,7 @@ export async function createDevToolsContext(
2223
rpc: undefined!,
2324
docks: undefined!,
2425
views: undefined!,
26+
utils: ContextUtils,
2527
}
2628
const rpcHost = new RpcFunctionsHost(context)
2729
const docksHost = new DevToolsDockHost(context)

packages/kit/src/types/views.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ export interface DevToolsDockEntryBase {
2424
icon: string
2525
}
2626

27+
export interface ClientScriptEntry {
28+
/**
29+
* The filepath or module name to import from
30+
*/
31+
importFrom: string
32+
/**
33+
* The name to import the module as
34+
*
35+
* @default 'default'
36+
*/
37+
importName?: string
38+
}
39+
2740
export interface DevToolsViewIframe extends DevToolsDockEntryBase {
2841
type: 'iframe'
2942
url: string
@@ -33,21 +46,25 @@ export interface DevToolsViewIframe extends DevToolsDockEntryBase {
3346
* When not provided, it would be treated as a unique frame.
3447
*/
3548
frameId?: string
49+
/**
50+
* Optional script to import into the iframe
51+
*/
52+
import?: ClientScriptEntry
3653
}
3754

3855
export interface DevToolsViewWebComponent extends DevToolsDockEntryBase {
3956
type: 'webcomponent'
40-
from: string
41-
import: string
57+
import: ClientScriptEntry
4258
}
4359

4460
export interface DevToolsViewAction extends DevToolsDockEntryBase {
4561
type: 'action'
46-
importFrom: string
47-
/**
48-
* @default 'default'
49-
*/
50-
importName?: string
62+
import: ClientScriptEntry
63+
}
64+
65+
export interface DevToolsViewCustomRender extends DevToolsDockEntryBase {
66+
type: 'custom-render'
67+
import: ClientScriptEntry
5168
}
5269

53-
export type DevToolsDockEntry = DevToolsViewIframe | DevToolsViewWebComponent | DevToolsViewAction
70+
export type DevToolsDockEntry = DevToolsViewIframe | DevToolsViewWebComponent | DevToolsViewAction | DevToolsViewCustomRender

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

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

55
export interface DevToolsCapabilities {
66
rpc?: boolean
@@ -23,6 +23,11 @@ export interface DevToolsNodeContext {
2323
rpc: RpcFunctionsHost
2424
docks: DevToolsDockHost
2525
views: DevToolsViewHost
26+
utils: DevToolsNodeUtils
27+
}
28+
29+
export interface DevToolsNodeUtils {
30+
clientEntryFromSimpleFunction: (fn: () => void) => ClientScriptEntry
2631
}
2732

2833
export interface ConnectionMeta {

0 commit comments

Comments
 (0)