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 ( ) ; onMounted ( ( ) => { watch ( ( ) => site . value . lang , ( lang ) => { document . documentElement . lang = lang ; } , { immediate : true } ) ; } ) ; if ( import . meta . env . PROD ) { 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 ) ; if ( inBrowser ) { useUpdateHead ( router . route , data . site ) ; } app . component ( 'Content' , Content ) ; app . component ( 'ClientOnly' , ClientOnly ) ; app . component ( 'Debug' , import . meta . env . PROD ? ( ) => null : defineAsyncComponent ( ( ) => import ( './components/Debug.vue' ) ) ) ; Object . defineProperty ( app . config . globalProperties , '$frontmatter' , { get ( ) { return data . frontmatter . value ; } } ) ; if ( Theme . enhanceApp ) { Theme . enhanceApp ( { app , router , siteData : siteDataRef } ) ; } return { app , router } ; } function newApp ( ) { return import . meta . env . PROD ? createSSRApp ( VitePressApp ) : createClientApp ( VitePressApp ) ; } function newRouter ( ) { let isInitialPageLoad = inBrowser ; let initialPath ; return createRouter ( ( path ) => { let pageFilePath = pathToFile ( path ) ; if ( isInitialPageLoad ) { initialPath = pageFilePath ; } if ( isInitialPageLoad || initialPath === pageFilePath ) { pageFilePath = pageFilePath . replace ( /\.js$/ , '.lean.js' ) ; } if ( inBrowser ) { isInitialPageLoad = false ; return import ( pageFilePath ) ; } return require ( pageFilePath ) ; } , NotFound ) ; } function handleHMR ( router ) { if ( import . meta . hot ) { import . meta . hot ! . on ( 'vitepress:pageData' , ( payload ) => { if ( shouldHotReload ( payload ) ) { router . route . data = payload . pageData ; } } ) ; } } function shouldHotReload ( payload ) { const payloadPath = payload . path . replace ( /(\bindex)?\.md$/ , '' ) ; const locationPath = location . pathname . replace ( /(\bindex)?\.html$/ , '' ) ; return payloadPath === locationPath ; } if ( inBrowser ) { const { app , router } = createApp ( ) ; router . go ( ) . then ( ( ) => { app . mount ( '#app' ) ; } ) ; }