Skip to content

Commit 44e18e0

Browse files
committedFeb 17, 2024
chore(devtool): adds devtool support to wc
1 parent 1551d1f commit 44e18e0

File tree

8 files changed

+861
-244
lines changed

8 files changed

+861
-244
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
},
5353
"dependencies": {
5454
"@dvcol/web-extension-utils": "^2.3.4",
55+
"@vue/devtools": "^7.0.15",
5556
"naive-ui": "^2.37.3",
5657
"pinia": "^2.1.7",
5758
"vue": "^3.4.14",

‎pnpm-lock.yaml

+833-96
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/services/tmdb-client/tmdb-client.service.ts

-8
This file was deleted.

‎src/services/trakt-client/trakt-client.service.ts

-9
This file was deleted.

‎src/services/tvdb-client/tvdb-client.service.ts

-8
This file was deleted.

‎src/stores/settings.store.ts

-121
This file was deleted.

‎src/web/create-wc.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { defineCustomElement, getCurrentInstance, h } from 'vue';
22

3+
import type { DevToolsHook, DevToolsHooks } from '@vue/devtools';
34
import type { Component, ComponentInternalInstance } from 'vue';
45
import type { InitVueAppOption } from '~/web/init-vue-app';
56

67
import { initVueApp } from '~/web/init-vue-app';
78

89
type ComponentInstance = ComponentInternalInstance & { provides: ComponentInternalInstance['appContext']['provides'] };
910

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 }) => {
1118
return defineCustomElement({
1219
setup(props) {
1320
const app = initVueApp(component, options);
@@ -16,6 +23,24 @@ export const createElementInstance = (component: Component, options: InitVueAppO
1623
if (inst) {
1724
Object.assign(inst.appContext, app._context);
1825
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+
}
1944
}
2045

2146
return () => h(component, props);

‎src/web/define-component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const defineComponent = async (options: DefineOption = {}, component: Web
1111
} else {
1212
const [{ createElementInstance }, { lazyComponent }] = await Promise.all([import('~/web/create-wc'), import('~/utils/lazy.utils')]);
1313
const ContainerComponent = lazyComponent(() => import('~/components/web/ContainerComponent.ce.vue'));
14-
const TraktExtensionWc = createElementInstance(ContainerComponent, options);
14+
const TraktExtensionWc = createElementInstance(ContainerComponent, { name: component, ...options });
1515
customElements.define(component, TraktExtensionWc);
1616
}
1717
};

0 commit comments

Comments
 (0)
Please sign in to comment.