mirror of https://github.com/vuejs/vitepress
parent
9069e4673b
commit
2bf64da748
@ -1,2 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
require('../dist')
|
||||
require('../dist').createServer().listen(3000, () => {
|
||||
console.log('listening at http://localhost:3000')
|
||||
})
|
||||
|
@ -0,0 +1,2 @@
|
||||
<div id="app"></div>
|
||||
<script type="module" src="/@app/index.js"></script>
|
@ -0,0 +1,23 @@
|
||||
import { createApp, ref, provide, h } from 'vue'
|
||||
import { Layout } from '/@theme/index.js'
|
||||
|
||||
const app = createApp({
|
||||
setup() {
|
||||
const path = ref(window.location.pathname)
|
||||
|
||||
// window.addEventListener('click', e => {
|
||||
// if (e.target.tagName === 'A') {
|
||||
// e.preventDefault()
|
||||
// if (e.target.href && e.target.href.indexOf(location.host)) {
|
||||
// history.pushState(null, '', e.target.href)
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
|
||||
provide('vitepress:path', path)
|
||||
|
||||
return () => h(Layout)
|
||||
}
|
||||
})
|
||||
|
||||
app.mount('#app')
|
@ -0,0 +1 @@
|
||||
export * from './server'
|
@ -0,0 +1,52 @@
|
||||
import path from 'path'
|
||||
import { createServer as createViteServer, cachedRead, Plugin } from 'vite'
|
||||
|
||||
const debug = require('debug')('vitepress')
|
||||
|
||||
// built ts files are placed into /dist
|
||||
const resolveAppFile = (file: string) =>
|
||||
path.join(__dirname, '../lib/app', file)
|
||||
|
||||
// TODO detect user configured theme
|
||||
const resolveThemeFile = (file: string) =>
|
||||
path.join(__dirname, '../lib/theme-default', file)
|
||||
|
||||
const VitePressPlugin: Plugin = ({ root, app }) => {
|
||||
app.use(async (ctx, next) => {
|
||||
// detect and serve vitepress app files
|
||||
if (ctx.path.startsWith('/@app')) {
|
||||
const file = ctx.path.replace(/^\/@app\/?/, '')
|
||||
ctx.type = path.extname(file)
|
||||
ctx.body = await cachedRead(resolveAppFile(file))
|
||||
|
||||
debug(`serving app file: ${ctx.url}`)
|
||||
return next()
|
||||
}
|
||||
|
||||
if (ctx.path.startsWith('/@theme')) {
|
||||
const file = ctx.path.replace(/^\/@theme\/?/, '')
|
||||
ctx.type = path.extname(file)
|
||||
ctx.body = await cachedRead(resolveThemeFile(file))
|
||||
|
||||
debug(`serving theme file: ${ctx.url}`)
|
||||
return next()
|
||||
}
|
||||
|
||||
if (ctx.path.endsWith('.md')) {
|
||||
debug(`serving .md: ${ctx.path}`)
|
||||
}
|
||||
|
||||
await next()
|
||||
// serve our index.html after vite history fallback
|
||||
if (ctx.url === '/index.html') {
|
||||
ctx.type = 'text/html'
|
||||
ctx.body = await cachedRead(resolveAppFile('index-dev.html'))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function createServer() {
|
||||
return createViteServer({
|
||||
plugins: [VitePressPlugin]
|
||||
})
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
<template>
|
||||
hello world
|
||||
</template>
|
@ -0,0 +1,3 @@
|
||||
import Layout from './Layout.vue'
|
||||
|
||||
export { Layout }
|
@ -1,25 +0,0 @@
|
||||
import { createServer, Plugin } from 'vite'
|
||||
import path from 'path'
|
||||
import { promises as fs } from 'fs'
|
||||
|
||||
const indexTemplate = fs.readFile(path.join(__dirname, '../theme/index.html'))
|
||||
|
||||
const VitePressPlugin: Plugin = ({ root, app }) => {
|
||||
app.use(async (ctx, next) => {
|
||||
// redirect request to index.html
|
||||
if (ctx.path === '/index.html') {
|
||||
ctx.body = await indexTemplate
|
||||
return
|
||||
}
|
||||
|
||||
return next()
|
||||
})
|
||||
|
||||
app.use(async (ctx, next) => {})
|
||||
}
|
||||
|
||||
createServer({
|
||||
plugins: [VitePressPlugin]
|
||||
}).listen(3000, () => {
|
||||
console.log('listening on http://localhost:3000')
|
||||
})
|
@ -1 +0,0 @@
|
||||
hello!
|
Loading…
Reference in new issue