Skip to content

Commit 6013474

Browse files
committed
feat(vue): add vitepress support
1 parent 998c20f commit 6013474

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

packages/vue/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from './variants/vitepress'
12
export * from './variants/vue'

packages/vue/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ export interface RegisterProviderOptions<P extends Platform> {
4141
platform: Platform
4242
analytics: VueAnalytics<P>
4343
}
44+
45+
export interface CreateVitePressAnalyticsInstanceOptions
46+
extends Omit<VueAnalyticsConstructorOptions, 'platform'> {}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { isBrowser } from '@bassist/utils'
2+
import { VueAnalytics } from '../analytics'
3+
import { getMajorVersion, getGlobalProperty } from '../utils'
4+
import type { Platform } from '@web-analytics/core'
5+
import type {
6+
CreateVitePressAnalyticsInstanceOptions,
7+
RegisterProviderOptions,
8+
VueInstance,
9+
} from '../types'
10+
11+
function registerProvider<P extends Platform>({
12+
analytics,
13+
platform,
14+
}: RegisterProviderOptions<P>) {
15+
return (
16+
app: VueInstance,
17+
options: CreateVitePressAnalyticsInstanceOptions
18+
) => {
19+
if (!isBrowser) return
20+
21+
// Synchronize the real properties with the prototype to the exported instance
22+
const analyticsInstance = new VueAnalytics<P>({ platform, ...options })
23+
for (const key in analyticsInstance) {
24+
if (Object.prototype.hasOwnProperty.call(analyticsInstance, key)) {
25+
// @ts-ignore
26+
analytics[key] = analyticsInstance[key]
27+
}
28+
}
29+
const prototype = Object.getPrototypeOf(analyticsInstance)
30+
Object.setPrototypeOf(analytics, prototype)
31+
32+
// Add a global property to Vue instance
33+
const property = getGlobalProperty(platform)
34+
const version = getMajorVersion(app)
35+
switch (version) {
36+
case 2: {
37+
app.prototype[property] = analytics
38+
break
39+
}
40+
case 3: {
41+
app.config.globalProperties[property] = analytics
42+
break
43+
}
44+
}
45+
}
46+
}
47+
48+
export function createVitePressBaiduAnalytics() {
49+
// @ts-ignore
50+
const baiduAnalytics: VueAnalytics<'baidu'> = {}
51+
52+
return {
53+
baiduAnalytics,
54+
registerBaiduAnalytics: registerProvider({
55+
analytics: baiduAnalytics,
56+
platform: 'baidu',
57+
}),
58+
}
59+
}
60+
61+
export function createVitePressCnzzAnalytics() {
62+
// @ts-ignore
63+
const cnzzAnalytics: VueAnalytics<'cnzz'> = {}
64+
65+
return {
66+
cnzzAnalytics,
67+
registerCnzzAnalytics: registerProvider({
68+
analytics: cnzzAnalytics,
69+
platform: 'cnzz',
70+
}),
71+
}
72+
}

0 commit comments

Comments
 (0)