wip: rewrites for dynamic routes

pull/2005/head
Evan You 2 years ago
parent 24507105b1
commit 34b149ba25

@ -9,7 +9,6 @@ import { bundle, okMark, failMark } from './bundle'
import { createRequire } from 'module'
import { pathToFileURL } from 'url'
import pkgDir from 'pkg-dir'
import { resolveRoutes } from '../plugins/dynamicRoutesPlugin'
export async function build(
root?: string,
@ -32,16 +31,9 @@ export async function build(
}
try {
const [dynamicRoutes] = await resolveRoutes(siteConfig.dynamicRoutes)
const allPages = [
...siteConfig.pages,
...dynamicRoutes.map((r) => r.path)
]
const { clientResult, serverResult, pageToHashMap } = await bundle(
siteConfig,
buildOptions,
allPages
buildOptions
)
const entryPath = path.join(siteConfig.tempDir, 'app.js')
@ -73,7 +65,7 @@ export async function build(
const hashMapString = JSON.stringify(JSON.stringify(pageToHashMap))
await Promise.all(
['404.md', ...allPages]
['404.md', ...siteConfig.pages]
.map((page) => siteConfig.rewrites.map[page] || page)
.map((page) =>
renderPage(

@ -20,8 +20,7 @@ export const failMark = '\x1b[31m✖\x1b[0m'
// bundles the VitePress app for both client AND server.
export async function bundle(
config: SiteConfig,
options: BuildOptions,
allPages: string[]
options: BuildOptions
): Promise<{
clientResult: RollupOutput
serverResult: RollupOutput
@ -35,7 +34,7 @@ export async function bundle(
// the loading is done via filename conversion rules so that the
// metadata doesn't need to be included in the main chunk.
const input: Record<string, string> = {}
allPages.forEach((file) => {
config.pages.forEach((file) => {
// page filename conversion
// foo/bar.md -> foo_bar.md
const alias = config.rewrites.map[file] || file

@ -15,7 +15,11 @@ import {
} from 'vite'
import { DEFAULT_THEME_PATH } from './alias'
import type { MarkdownOptions } from './markdown/markdown'
import { dynamicRouteRE } from './plugins/dynamicRoutesPlugin'
import {
dynamicRouteRE,
resolveRoutes,
type ResolvedRouteConfig
} from './plugins/dynamicRoutesPlugin'
import {
APPEARANCE_KEY,
type Awaitable,
@ -179,7 +183,7 @@ export interface SiteConfig<ThemeConfig = any>
cacheDir: string
tempDir: string
pages: string[]
dynamicRoutes: string[]
dynamicRoutes: readonly [ResolvedRouteConfig[], Record<string, string[]>]
rewrites: {
map: Record<string, string | undefined>
inv: Record<string, string | undefined>
@ -252,7 +256,11 @@ export async function resolveConfig(
).sort()
const pages = allMarkdownFiles.filter((p) => !dynamicRouteRE.test(p))
const dynamicRoutes = allMarkdownFiles.filter((p) => dynamicRouteRE.test(p))
const dynamicRouteFiles = allMarkdownFiles.filter((p) =>
dynamicRouteRE.test(p)
)
const dynamicRoutes = await resolveRoutes(dynamicRouteFiles)
pages.push(...dynamicRoutes[0].map((r) => r.path))
const rewriteEntries = Object.entries(userConfig.rewrites || {})
const rewrites = rewriteEntries.length

@ -55,7 +55,9 @@ export async function createMarkdownToVueRenderFn(
file: string,
publicDir: string
): Promise<MarkdownCompileResult> => {
const alias = siteConfig?.rewrites.map[file.slice(srcDir.length + 1)]
const alias =
siteConfig?.rewrites.map[file] || // virtual dynamic path file
siteConfig?.rewrites.map[file.slice(srcDir.length + 1)]
file = alias ? path.join(srcDir, alias) : file
const relativePath = slash(path.relative(srcDir, file))
const dir = path.dirname(file)

@ -201,7 +201,9 @@ export async function createVitePressPlugin(
server.middlewares.use((req, res, next) => {
if (req.url) {
const page = req.url.replace(/[?#].*$/, '').slice(site.base.length)
req.url = req.url.replace(page, rewrites.inv[page] || page)
if (rewrites.inv[page]) {
req.url = req.url.replace(page, rewrites.inv[page]!)
}
}
next()
})

@ -41,8 +41,8 @@ export const dynamicRoutesPlugin = async (
config: SiteConfig
): Promise<Plugin> => {
let server: ViteDevServer
let routes = config.dynamicRoutes
let [resolvedRoutes, routeFileToModulesMap] = await resolveRoutes(routes)
let routes = config.dynamicRoutes[0].map(r => r.route)
let [resolvedRoutes, routeFileToModulesMap] = config.dynamicRoutes
// TODO: make this more efficient by only reloading the invalidated route
// TODO: invlidate modules for paths that are no longer present

Loading…
Cancel
Save