-
Notifications
You must be signed in to change notification settings - Fork 925
/
app.ts
218 lines (201 loc) · 5.39 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { computed, h } from 'vue'
import type { CreateAppFunction, App, ComponentOptions } from 'vue'
import { createRouter, RouterView, START_LOCATION } from 'vue-router'
import type { Router, RouterHistory } from 'vue-router'
import { removeEndingSlash } from '@vuepress/shared'
import { clientAppEnhances } from '@internal/clientAppEnhances'
import { clientAppRootComponents } from '@internal/clientAppRootComponents'
import { clientAppSetups } from '@internal/clientAppSetups'
import { pagesComponent } from '@internal/pagesComponent'
import { pagesRoutes } from '@internal/pagesRoutes'
import {
siteData,
pageData,
resolvePageData,
pageFrontmatterSymbol,
resolvePageFrontmatter,
pageHeadSymbol,
resolvePageHead,
pageHeadTitleSymbol,
resolvePageHeadTitle,
pageLangSymbol,
resolvePageLang,
routeLocaleSymbol,
resolveRouteLocale,
themeDataSymbol,
resolveThemeData,
themeLocaleDataSymbol,
resolveThemeLocaleData,
siteLocaleDataSymbol,
resolveSiteLocaleData,
useUpdateHead,
} from './injections'
import { Content, OutboundLink } from './components'
import { withBase } from './utils'
export type AppCreator = CreateAppFunction<Element>
export type HistoryCreator = (base?: string) => RouterHistory
export type CreateVueAppResult = {
app: App
router: Router
}
/**
* Create a vue app
*
* Accepting different app creator and history creator, so it
* can be reused for both client side and server side
*/
export const createVueApp = async ({
appCreator,
historyCreator,
}: {
appCreator: AppCreator
historyCreator: HistoryCreator
}): Promise<CreateVueAppResult> => {
// options to create vue app
const appOptions: ComponentOptions = {
setup() {
// auto update head
useUpdateHead()
// invoke all clientAppSetups
for (const clientAppSetup of clientAppSetups) {
clientAppSetup()
}
return () => [
h(RouterView),
...clientAppRootComponents.map((comp) => h(comp)),
]
},
}
// create vue app
const app = appCreator(appOptions)
// create vue-router
const router = createRouter({
// TODO: it might be an issue of vue-router that have to remove the ending slash
history: historyCreator(removeEndingSlash(siteData.value.base)),
routes: pagesRoutes,
scrollBehavior: (to, from, savedPosition) => {
if (savedPosition) {
return savedPosition
}
if (to.hash) {
return { el: to.hash }
}
return { top: 0 }
},
})
// use vue-router
app.use(router)
router.beforeResolve(async (to, from) => {
if (to.path !== from.path || from === START_LOCATION) {
pageData.value = await resolvePageData(to.path)
}
})
// create global computed
const routeLocale = computed(() =>
resolveRouteLocale(siteData.value.locales, router.currentRoute.value.path)
)
const siteLocaleData = computed(() =>
resolveSiteLocaleData(siteData.value, routeLocale.value)
)
const themeData = computed(() => resolveThemeData(siteData.value))
const themeLocaleData = computed(() =>
resolveThemeLocaleData(siteLocaleData.value)
)
const pageFrontmatter = computed(() => resolvePageFrontmatter(pageData.value))
const pageHeadTitle = computed(() =>
resolvePageHeadTitle(pageData.value, siteLocaleData.value)
)
const pageHead = computed(() =>
resolvePageHead(
pageHeadTitle.value,
pageFrontmatter.value,
siteLocaleData.value
)
)
const pageLang = computed(() =>
resolvePageLang(pageFrontmatter.value, siteLocaleData.value)
)
// provide global computed
app.provide(routeLocaleSymbol, routeLocale)
app.provide(siteLocaleDataSymbol, siteLocaleData)
app.provide(themeDataSymbol, themeData)
app.provide(themeLocaleDataSymbol, themeLocaleData)
app.provide(pageFrontmatterSymbol, pageFrontmatter)
app.provide(pageHeadTitleSymbol, pageHeadTitle)
app.provide(pageHeadSymbol, pageHead)
app.provide(pageLangSymbol, pageLang)
// provide global data & helpers
Object.defineProperties(app.config.globalProperties, {
$routeLocale: {
get() {
return routeLocale.value
},
},
$site: {
get() {
return siteData.value
},
},
$siteLocale: {
get() {
return siteLocaleData.value
},
},
$theme: {
get() {
return themeData.value
},
},
$themeLocale: {
get() {
return themeLocaleData.value
},
},
$page: {
get() {
return pageData.value
},
},
$frontmatter: {
get() {
return pageFrontmatter.value
},
},
$lang: {
get() {
return pageLang.value
},
},
$headTitle: {
get() {
return pageHeadTitle.value
},
},
$withBase: {
get() {
return withBase
},
},
})
// register built-in components
/* eslint-disable vue/match-component-file-name */
app.component(
'ClientOnly',
__SSR__ ? () => null : (_, ctx) => ctx.slots.default?.()
)
app.component('Content', Content)
app.component('OutboundLink', OutboundLink)
/* eslint-enable vue/match-component-file-name */
// register all pages components
Object.entries(pagesComponent).forEach(([name, component]) => {
app.component(name, component)
})
// invoke all clientAppEnhances
for (const clientAppEnhance of clientAppEnhances) {
await clientAppEnhance({ app, router, siteData })
}
return {
app,
router,
}
}