forked from vuejs/vitepress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
151 lines (125 loc) · 3.6 KB
/
index.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
import {
App,
createApp as createClientApp,
createSSRApp,
defineAsyncComponent,
h,
onMounted,
watch
} from 'vue'
import { inBrowser, pathToFile } from './utils'
import { Router, RouterSymbol, createRouter } from './router'
import { siteDataRef, useData } from './data'
import { useUpdateHead } from './composables/head'
import Theme from '/@theme/index'
import { usePrefetch } from './composables/preFetch'
import { dataSymbol, initData } from './data'
import { Content } from './components/Content'
import { ClientOnly } from './components/ClientOnly'
const NotFound = Theme.NotFound || (() => '404 Not Found')
const VitePressApp = {
name: 'VitePressApp',
setup() {
const { site } = useData()
// change the language on the HTML element based on the current lang
onMounted(() => {
watch(
() => site.value.lang,
(lang: string) => {
document.documentElement.lang = lang
},
{ immediate: true }
)
})
if (import.meta.env.PROD) {
// in prod mode, enable intersectionObserver based pre-fetch
usePrefetch()
}
return () => h(Theme.Layout)
}
}
export function createApp() {
const router = newRouter()
handleHMR(router)
const app = newApp()
app.provide(RouterSymbol, router)
const data = initData(router.route)
app.provide(dataSymbol, data)
// install global components
app.component('Content', Content)
app.component('ClientOnly', ClientOnly)
app.component(
'Debug',
import.meta.env.PROD
? () => null
: defineAsyncComponent(() => import('./components/Debug.vue'))
)
// expose $frontmatter
Object.defineProperty(app.config.globalProperties, '$frontmatter', {
get() {
return data.frontmatter.value
}
})
if (Theme.enhanceApp) {
Theme.enhanceApp({
app,
router,
siteData: siteDataRef
})
}
return { app, router, data }
}
function newApp(): App {
return import.meta.env.PROD
? createSSRApp(VitePressApp)
: createClientApp(VitePressApp)
}
function newRouter(): Router {
let isInitialPageLoad = inBrowser
let initialPath: string
return createRouter((path) => {
let pageFilePath = pathToFile(path)
if (isInitialPageLoad) {
initialPath = pageFilePath
}
// use lean build if this is the initial page load or navigating back
// to the initial loaded path (the static vnodes already adopted the
// static content on that load so no need to re-fetch the page)
if (isInitialPageLoad || initialPath === pageFilePath) {
pageFilePath = pageFilePath.replace(/\.js$/, '.lean.js')
}
// in browser: native dynamic import
if (inBrowser) {
isInitialPageLoad = false
return import(/*@vite-ignore*/ pageFilePath)
}
// SSR: sync require
// @ts-ignore
return require(pageFilePath)
}, NotFound)
}
function handleHMR(router: Router): void {
// update route.data on HMR updates of active page
if (import.meta.hot) {
// hot reload pageData
import.meta.hot!.on('vitepress:pageData', (payload) => {
if (shouldHotReload(payload)) {
router.route.data = payload.pageData
}
})
}
}
function shouldHotReload(payload: any): boolean {
const payloadPath = payload.path.replace(/(\bindex)?\.md$/, '')
const locationPath = location.pathname.replace(/(\bindex)?\.html$/, '')
return payloadPath === locationPath
}
if (inBrowser) {
const { app, router, data } = createApp()
// wait until page component is fetched before mounting
router.go().then(() => {
// dynamically update head tags
useUpdateHead(router.route, data.site)
app.mount('#app')
})
}