Skip to content

Commit

Permalink
refactor: simplify client path mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 31, 2020
1 parent f62f4a4 commit 4f0c903
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 44 deletions.
29 changes: 0 additions & 29 deletions src/client/app/exports.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/client/app/index.html

This file was deleted.

29 changes: 29 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// exports in this file are exposed to themes and md files via 'vitepress'
// so the user can do `import { useRoute, useSiteData } from 'vitepress'`

// generic types
export type { Router, Route } from './app/router'

// theme types
export * from './app/theme'

// composables
export { useRouter, useRoute } from './app/router'
export { useSiteData } from './app/composables/siteData'
export { useSiteDataByRoute } from './app/composables/siteDataByRoute'
export { usePageData } from './app/composables/pageData'
export { useFrontmatter } from './app/composables/frontmatter'

// utilities
export { inBrowser, joinPath } from './app/utils'

// components
export { Content } from './app/components/Content'

import { ComponentOptions } from 'vue'
import _Debug from './app/components/Debug.vue'
const Debug = _Debug as ComponentOptions
export { Debug }

// default theme
export { default as DefaultTheme } from './theme-default'
5 changes: 2 additions & 3 deletions src/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
"lib": ["ESNext", "DOM"],
"types": ["vite"],
"paths": {
"/@theme/*": ["theme-default/*"],
"/@default-theme/*": ["theme-default/*"],
"/@shared/*": ["shared/*"],
"/@types/*": ["../../types/*"],
"vitepress": ["app/exports.ts"]
"/@theme/*": ["theme-default/*"],
"vitepress": ["index.ts"]
}
},
"include": [
Expand Down
11 changes: 4 additions & 7 deletions src/node/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@ export const DEFAULT_THEME_PATH = path.join(
export const SITE_DATA_ID = '@siteData'
export const SITE_DATA_REQUEST_PATH = '/' + SITE_DATA_ID

// this is a path resolver that is passed to vite
// so that we can resolve custom requests that start with /@app or /@theme
// we also need to map file paths back to their public served paths so that
// vite HMR can send the correct update notifications to the client.
export function resolveAliases(
root: string,
themeDir: string,
userConfig: UserConfig
): AliasOptions {
const paths: Record<string, string> = {
...userConfig.alias,
'/@app': APP_PATH,
'/@theme': themeDir,
'/@default-theme': DEFAULT_THEME_PATH,
'/@shared': SHARED_PATH,
[SITE_DATA_ID]: SITE_DATA_REQUEST_PATH
}
Expand All @@ -40,7 +34,10 @@ export function resolveAliases(
find: p,
replacement: paths[p]
})),
{ find: /^vitepress$/, replacement: `${APP_PATH}/exports.js` }
{
find: /^vitepress$/,
replacement: path.join(__dirname, '../client/index')
}
]

let isLinked = false
Expand Down
10 changes: 7 additions & 3 deletions src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,16 @@ export function createVitePressPlugin(

configureServer(server) {
// serve our index.html after vite history fallback
const indexPath = `/@fs/${path.join(APP_PATH, 'index.html')}`
return () => {
// @ts-ignore
server.app.use((req, _, next) => {
server.app.use((req, res, next) => {
if (req.url!.endsWith('.html')) {
req.url = indexPath
res.statusCode = 200
res.end(
`<div id="app"></div>\n` +
`<script type="module" src="/@fs/${APP_PATH}/index.js"></script>`
)
return
}
next()
})
Expand Down

0 comments on commit 4f0c903

Please sign in to comment.