Skip to content

Commit a19d945

Browse files
committed
feat(client): add vue-devtools support
1 parent 2ace258 commit a19d945

File tree

8 files changed

+91
-3
lines changed

8 files changed

+91
-3
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module.exports = {
66
__VUEPRESS_DEV__: 'readonly',
77
__VUEPRESS_SSR__: 'readonly',
88
__VUE_HMR_RUNTIME__: 'readonly',
9+
__VUE_OPTIONS_API__: 'readonly',
10+
__VUE_PROD_DEVTOOLS__: 'readonly',
911
},
1012
overrides: [
1113
{

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"shiki",
3333
"slugify",
3434
"unmount",
35+
"vuejs",
3536
"vuepress",
3637
"vueuse",
3738
"zoomable"

packages/@vuepress/client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"copy": "cpx \"src/**/*.{d.ts,css}\" lib"
3030
},
3131
"dependencies": {
32+
"@vue/devtools-api": "^6.0.0-beta.21.1",
3233
"@vuepress/shared": "2.0.0-beta.33",
3334
"vue": "^3.2.26",
3435
"vue-router": "^4.0.12"

packages/@vuepress/client/src/app.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from './composables'
2323
import { provideGlobalComputed } from './provideGlobalComputed'
2424
import { registerGlobalComponents } from './registerGlobalComponents'
25+
import { setupDevtools } from './setupDevtools'
2526

2627
/**
2728
* - use `createApp` in dev mode
@@ -83,9 +84,14 @@ export const createVueApp: CreateVueAppFunction = async () => {
8384
}
8485
})
8586

86-
// global computed and global components
87-
provideGlobalComputed(app, router)
87+
// global components and computed
8888
registerGlobalComponents(app)
89+
const globalComputed = provideGlobalComputed(app, router)
90+
91+
// setup devtools in dev mode
92+
if (__VUEPRESS_DEV__ || __VUE_PROD_DEVTOOLS__) {
93+
setupDevtools(app, globalComputed)
94+
}
8995

9096
// invoke all clientAppEnhances
9197
for (const clientAppEnhance of clientAppEnhances) {

packages/@vuepress/client/src/provideGlobalComputed.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,45 @@ import {
1919
} from './composables'
2020
import type {
2121
PageData,
22+
PageDataRef,
2223
PageFrontmatter,
24+
PageFrontmatterRef,
25+
PageHead,
26+
PageHeadRef,
2327
PageHeadTitle,
28+
PageHeadTitleRef,
2429
PageLang,
30+
PageLangRef,
2531
RouteLocale,
32+
RouteLocaleRef,
2633
SiteData,
34+
SiteDataRef,
2735
SiteLocaleData,
36+
SiteLocaleDataRef,
2837
} from './composables'
2938
import { withBase } from './utils'
3039

40+
/**
41+
* Vuepress client global computed
42+
*/
43+
export interface GlobalComputed {
44+
pageData: PageDataRef
45+
pageFrontmatter: PageFrontmatterRef
46+
pageHead: PageHeadRef
47+
pageHeadTitle: PageHeadTitleRef
48+
pageLang: PageLangRef
49+
routeLocale: RouteLocaleRef
50+
siteData: SiteDataRef
51+
siteLocaleData: SiteLocaleDataRef
52+
}
53+
3154
/**
3255
* Create and provide global computed
3356
*/
34-
export const provideGlobalComputed = (app: App, router: Router): void => {
57+
export const provideGlobalComputed = (
58+
app: App,
59+
router: Router
60+
): GlobalComputed => {
3561
// create global computed
3662
const routeLocale = computed(() =>
3763
resolveRouteLocale(siteData.value.locales, router.currentRoute.value.path)
@@ -63,6 +89,7 @@ export const provideGlobalComputed = (app: App, router: Router): void => {
6389
// provide global helpers
6490
Object.defineProperties(app.config.globalProperties, {
6591
$frontmatter: { get: () => pageFrontmatter.value },
92+
$head: { get: () => pageHead.value },
6693
$headTitle: { get: () => pageHeadTitle.value },
6794
$lang: { get: () => pageLang.value },
6895
$page: { get: () => pageData.value },
@@ -71,11 +98,23 @@ export const provideGlobalComputed = (app: App, router: Router): void => {
7198
$siteLocale: { get: () => siteLocaleData.value },
7299
$withBase: { get: () => withBase },
73100
})
101+
102+
return {
103+
pageData,
104+
pageFrontmatter,
105+
pageHead,
106+
pageHeadTitle,
107+
pageLang,
108+
routeLocale,
109+
siteData,
110+
siteLocaleData,
111+
}
74112
}
75113

76114
declare module '@vue/runtime-core' {
77115
export interface ComponentCustomProperties {
78116
$frontmatter: PageFrontmatter
117+
$head: PageHead
79118
$headTitle: PageHeadTitle
80119
$lang: PageLang
81120
$page: PageData
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { setupDevtoolsPlugin } from '@vue/devtools-api'
2+
import type { App } from 'vue'
3+
import type { GlobalComputed } from './provideGlobalComputed'
4+
5+
export const setupDevtools = (
6+
app: App,
7+
globalComputed: GlobalComputed
8+
): void => {
9+
setupDevtoolsPlugin(
10+
{
11+
app,
12+
id: 'org.vuejs.vuepress',
13+
label: 'VuePress',
14+
packageName: '@vuepress/client',
15+
homepage: 'https://v2.vuepress.vuejs.org',
16+
logo: 'https://v2.vuepress.vuejs.org/images/hero.png',
17+
componentStateTypes: ['VuePress'],
18+
},
19+
(api) => {
20+
api.on.inspectComponent((payload) => {
21+
payload.instanceData.state.push(
22+
...Object.entries(globalComputed).map(([name, item]) => ({
23+
type: 'VuePress',
24+
key: name,
25+
editable: false,
26+
value: item.value,
27+
}))
28+
)
29+
})
30+
}
31+
)
32+
}

packages/@vuepress/client/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ declare const __VUEPRESS_VERSION__: string
22
declare const __VUEPRESS_DEV__: boolean
33
declare const __VUEPRESS_SSR__: boolean
44
declare const __VUE_HMR_RUNTIME__: Record<string, any>
5+
declare const __VUE_OPTIONS_API__: boolean
6+
declare const __VUE_PROD_DEVTOOLS__: boolean

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,6 +2879,11 @@
28792879
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.0.0-beta.20.1.tgz#5b499647e929c35baf2a66a399578f9aa4601142"
28802880
integrity sha512-R2rfiRY+kZugzWh9ZyITaovx+jpU4vgivAEAiz80kvh3yviiTU3CBuGuyWpSwGz9/C7TkSWVM/FtQRGlZ16n8Q==
28812881

2882+
"@vue/devtools-api@^6.0.0-beta.21.1":
2883+
version "6.0.0-beta.21.1"
2884+
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.0.0-beta.21.1.tgz#f1410f53c42aa67fa3b01ca7bdba891f69d7bc97"
2885+
integrity sha512-FqC4s3pm35qGVeXRGOjTsRzlkJjrBLriDS9YXbflHLsfA9FrcKzIyWnLXoNm+/7930E8rRakXuAc2QkC50swAw==
2886+
28822887
28832888
version "3.2.26"
28842889
resolved "https://registry.yarnpkg.com/@vue/reactivity-transform/-/reactivity-transform-3.2.26.tgz#6d8f20a4aa2d19728f25de99962addbe7c4d03e9"

0 commit comments

Comments
 (0)