1
1
import { defineCustomElement , getCurrentInstance , h } from 'vue' ;
2
2
3
+ import type { DevToolsHook , DevToolsHooks } from '@vue/devtools' ;
3
4
import type { Component , ComponentInternalInstance } from 'vue' ;
4
5
import type { InitVueAppOption } from '~/web/init-vue-app' ;
5
6
6
7
import { initVueApp } from '~/web/init-vue-app' ;
7
8
8
9
type ComponentInstance = ComponentInternalInstance & { provides : ComponentInternalInstance [ 'appContext' ] [ 'provides' ] } ;
9
10
10
- export const createElementInstance = ( component : Component , options : InitVueAppOption ) => {
11
+ declare global {
12
+ interface Window {
13
+ __VUE_DEVTOOLS_GLOBAL_HOOK__ : DevToolsHook & { Vue : Component } ;
14
+ }
15
+ }
16
+
17
+ export const createElementInstance = ( component : Component , { name, ...options } : InitVueAppOption & { name : string } ) => {
11
18
return defineCustomElement ( {
12
19
setup ( props ) {
13
20
const app = initVueApp ( component , options ) ;
@@ -16,6 +23,24 @@ export const createElementInstance = (component: Component, options: InitVueAppO
16
23
if ( inst ) {
17
24
Object . assign ( inst . appContext , app . _context ) ;
18
25
Object . assign ( inst . provides , app . _context . provides ) ;
26
+
27
+ // Add support for Vue Devtools
28
+ if ( import . meta. env . DEV && window . __VUE_DEVTOOLS_GLOBAL_HOOK__ ) {
29
+ app . _container = document . querySelector ( name ) ;
30
+ app . _instance = inst ;
31
+
32
+ const types = {
33
+ Comment : Symbol ( 'v-cmt' ) ,
34
+ Fragment : Symbol ( 'v-fgt' ) ,
35
+ Static : Symbol ( 'v-stc' ) ,
36
+ Text : Symbol ( 'v-txt' ) ,
37
+ } ;
38
+
39
+ window . __VUE_DEVTOOLS_GLOBAL_HOOK__ . emit ( 'app:init' as DevToolsHooks , app , app . version , types ) ;
40
+ window . __VUE_DEVTOOLS_GLOBAL_HOOK__ . Vue = app ;
41
+
42
+ console . info ( 'Vue Devtools is enabled.' ) ;
43
+ }
19
44
}
20
45
21
46
return ( ) => h ( component , props ) ;
0 commit comments