Skip to content

Commit 34b149b

Browse files
committed
wip: rewrites for dynamic routes
1 parent 2450710 commit 34b149b

File tree

6 files changed

+23
-20
lines changed

6 files changed

+23
-20
lines changed

src/node/build/build.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { bundle, okMark, failMark } from './bundle'
99
import { createRequire } from 'module'
1010
import { pathToFileURL } from 'url'
1111
import pkgDir from 'pkg-dir'
12-
import { resolveRoutes } from '../plugins/dynamicRoutesPlugin'
1312

1413
export async function build(
1514
root?: string,
@@ -32,16 +31,9 @@ export async function build(
3231
}
3332

3433
try {
35-
const [dynamicRoutes] = await resolveRoutes(siteConfig.dynamicRoutes)
36-
const allPages = [
37-
...siteConfig.pages,
38-
...dynamicRoutes.map((r) => r.path)
39-
]
40-
4134
const { clientResult, serverResult, pageToHashMap } = await bundle(
4235
siteConfig,
43-
buildOptions,
44-
allPages
36+
buildOptions
4537
)
4638

4739
const entryPath = path.join(siteConfig.tempDir, 'app.js')
@@ -73,7 +65,7 @@ export async function build(
7365
const hashMapString = JSON.stringify(JSON.stringify(pageToHashMap))
7466

7567
await Promise.all(
76-
['404.md', ...allPages]
68+
['404.md', ...siteConfig.pages]
7769
.map((page) => siteConfig.rewrites.map[page] || page)
7870
.map((page) =>
7971
renderPage(

src/node/build/bundle.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export const failMark = '\x1b[31m✖\x1b[0m'
2020
// bundles the VitePress app for both client AND server.
2121
export async function bundle(
2222
config: SiteConfig,
23-
options: BuildOptions,
24-
allPages: string[]
23+
options: BuildOptions
2524
): Promise<{
2625
clientResult: RollupOutput
2726
serverResult: RollupOutput
@@ -35,7 +34,7 @@ export async function bundle(
3534
// the loading is done via filename conversion rules so that the
3635
// metadata doesn't need to be included in the main chunk.
3736
const input: Record<string, string> = {}
38-
allPages.forEach((file) => {
37+
config.pages.forEach((file) => {
3938
// page filename conversion
4039
// foo/bar.md -> foo_bar.md
4140
const alias = config.rewrites.map[file] || file

src/node/config.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {
1515
} from 'vite'
1616
import { DEFAULT_THEME_PATH } from './alias'
1717
import type { MarkdownOptions } from './markdown/markdown'
18-
import { dynamicRouteRE } from './plugins/dynamicRoutesPlugin'
18+
import {
19+
dynamicRouteRE,
20+
resolveRoutes,
21+
type ResolvedRouteConfig
22+
} from './plugins/dynamicRoutesPlugin'
1923
import {
2024
APPEARANCE_KEY,
2125
type Awaitable,
@@ -179,7 +183,7 @@ export interface SiteConfig<ThemeConfig = any>
179183
cacheDir: string
180184
tempDir: string
181185
pages: string[]
182-
dynamicRoutes: string[]
186+
dynamicRoutes: readonly [ResolvedRouteConfig[], Record<string, string[]>]
183187
rewrites: {
184188
map: Record<string, string | undefined>
185189
inv: Record<string, string | undefined>
@@ -252,7 +256,11 @@ export async function resolveConfig(
252256
).sort()
253257

254258
const pages = allMarkdownFiles.filter((p) => !dynamicRouteRE.test(p))
255-
const dynamicRoutes = allMarkdownFiles.filter((p) => dynamicRouteRE.test(p))
259+
const dynamicRouteFiles = allMarkdownFiles.filter((p) =>
260+
dynamicRouteRE.test(p)
261+
)
262+
const dynamicRoutes = await resolveRoutes(dynamicRouteFiles)
263+
pages.push(...dynamicRoutes[0].map((r) => r.path))
256264

257265
const rewriteEntries = Object.entries(userConfig.rewrites || {})
258266
const rewrites = rewriteEntries.length

src/node/markdownToVue.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export async function createMarkdownToVueRenderFn(
5555
file: string,
5656
publicDir: string
5757
): Promise<MarkdownCompileResult> => {
58-
const alias = siteConfig?.rewrites.map[file.slice(srcDir.length + 1)]
58+
const alias =
59+
siteConfig?.rewrites.map[file] || // virtual dynamic path file
60+
siteConfig?.rewrites.map[file.slice(srcDir.length + 1)]
5961
file = alias ? path.join(srcDir, alias) : file
6062
const relativePath = slash(path.relative(srcDir, file))
6163
const dir = path.dirname(file)

src/node/plugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ export async function createVitePressPlugin(
201201
server.middlewares.use((req, res, next) => {
202202
if (req.url) {
203203
const page = req.url.replace(/[?#].*$/, '').slice(site.base.length)
204-
req.url = req.url.replace(page, rewrites.inv[page] || page)
204+
if (rewrites.inv[page]) {
205+
req.url = req.url.replace(page, rewrites.inv[page]!)
206+
}
205207
}
206208
next()
207209
})

src/node/plugins/dynamicRoutesPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export const dynamicRoutesPlugin = async (
4141
config: SiteConfig
4242
): Promise<Plugin> => {
4343
let server: ViteDevServer
44-
let routes = config.dynamicRoutes
45-
let [resolvedRoutes, routeFileToModulesMap] = await resolveRoutes(routes)
44+
let routes = config.dynamicRoutes[0].map(r => r.route)
45+
let [resolvedRoutes, routeFileToModulesMap] = config.dynamicRoutes
4646

4747
// TODO: make this more efficient by only reloading the invalidated route
4848
// TODO: invlidate modules for paths that are no longer present

0 commit comments

Comments
 (0)