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

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

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

@ -55,7 +55,9 @@ export async function createMarkdownToVueRenderFn(
file: string, file: string,
publicDir: string publicDir: string
): Promise<MarkdownCompileResult> => { ): 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 file = alias ? path.join(srcDir, alias) : file
const relativePath = slash(path.relative(srcDir, file)) const relativePath = slash(path.relative(srcDir, file))
const dir = path.dirname(file) const dir = path.dirname(file)

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

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

Loading…
Cancel
Save