Skip to content

Commit e681fb3

Browse files
committed
refactor: updates
1 parent d47d573 commit e681fb3

22 files changed

+181
-198
lines changed

eslint.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default vuepress(
1919
allow: [
2020
'__dirname',
2121
'_context',
22+
'_pageData',
2223
'_pageChunk',
2324
'_registeredComponents',
2425
],
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { App } from '@vuepress/core'
2-
import { parsePageContent, renderPageSfcBlocksToVue } from '@vuepress/core'
3-
import { path } from '@vuepress/utils'
2+
import { createPage, renderPageToVue } from '@vuepress/core'
43
import type { Plugin } from 'vite'
54

65
/**
@@ -11,26 +10,23 @@ export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({
1110

1211
enforce: 'pre',
1312

14-
transform(code, id) {
13+
async transform(code, id) {
1514
if (!id.endsWith('.md')) return
1615

1716
// get the matched page by file path (id)
1817
const page = app.pagesMap[id]
1918

2019
// if the page content is not changed, render it to vue component directly
2120
if (page?.content === code) {
22-
return renderPageSfcBlocksToVue(page.sfcBlocks)
21+
return renderPageToVue(page)
2322
}
2423

25-
// parse the markdown content to sfc blocks and render it to vue component
26-
const { sfcBlocks } = parsePageContent({
27-
app,
24+
// create a new page with the new content
25+
const newPage = await createPage(app, {
2826
content: code,
2927
filePath: id,
30-
filePathRelative: path.relative(app.dir.source(), id),
31-
options: {},
3228
})
33-
return renderPageSfcBlocksToVue(sfcBlocks)
29+
return renderPageToVue(newPage)
3430
},
3531

3632
async handleHotUpdate(ctx) {
@@ -39,15 +35,12 @@ export const vuepressMarkdownPlugin = ({ app }: { app: App }): Plugin => ({
3935
// read the source code
4036
const code = await ctx.read()
4137

42-
// parse the content to sfc blocks
43-
const { sfcBlocks } = parsePageContent({
44-
app,
38+
// create a new page with the new content
39+
const newPage = await createPage(app, {
4540
content: code,
4641
filePath: ctx.file,
47-
filePathRelative: path.relative(app.dir.source(), ctx.file),
48-
options: {},
4942
})
5043

51-
ctx.read = () => renderPageSfcBlocksToVue(sfcBlocks)
44+
ctx.read = () => renderPageToVue(newPage)
5245
},
5346
})

packages/bundler-webpack/src/loaders/vuepressMarkdownLoader.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export interface VuepressMarkdownLoaderOptions {
1111
export const vuepressMarkdownLoader: LoaderDefinitionFunction<VuepressMarkdownLoaderOptions> =
1212
async function vuepressMarkdownLoader(source) {
1313
// import esm dependencies
14-
const [{ parsePageContent, renderPageSfcBlocksToVue }, { path }] =
15-
await Promise.all([import('@vuepress/core'), import('@vuepress/utils')])
14+
const { createPage, renderPageToVue } = await import('@vuepress/core')
1615

1716
// get app instance from loader options
1817
const { app } = this.getOptions()
@@ -22,16 +21,13 @@ export const vuepressMarkdownLoader: LoaderDefinitionFunction<VuepressMarkdownLo
2221

2322
// if the page content is not changed, render it to vue component directly
2423
if (page?.content === source) {
25-
return renderPageSfcBlocksToVue(page.sfcBlocks)
24+
return renderPageToVue(page)
2625
}
2726

28-
// parse the markdown content to sfc blocks and render it to vue component
29-
const { sfcBlocks } = parsePageContent({
30-
app,
27+
// create a new page with the new content
28+
const newPage = await createPage(app, {
3129
content: source,
3230
filePath: this.resourcePath,
33-
filePathRelative: path.relative(app.dir.source(), this.resourcePath),
34-
options: {},
3531
})
36-
return renderPageSfcBlocksToVue(sfcBlocks)
32+
return renderPageToVue(newPage)
3733
}

packages/cli/src/commands/dev/handlePageAdd.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import type { App, Page } from '@vuepress/core'
2-
import {
3-
createPage,
4-
preparePageChunk,
5-
preparePageComponent,
6-
prepareRoutes,
7-
} from '@vuepress/core'
2+
import { createPage, preparePageChunk, prepareRoutes } from '@vuepress/core'
83

94
/**
105
* Event handler for page add event
@@ -28,9 +23,9 @@ export const handlePageAdd = async (
2823

2924
// add the new page
3025
app.pages.push(page)
26+
app.pagesMap[filePath] = page
3127

32-
// prepare page files
33-
await preparePageComponent(app, page)
28+
// prepare page file
3429
await preparePageChunk(app, page)
3530

3631
// prepare routes file

packages/cli/src/commands/dev/handlePageChange.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import type { App, Page } from '@vuepress/core'
2-
import {
3-
createPage,
4-
preparePageChunk,
5-
preparePageComponent,
6-
prepareRoutes,
7-
} from '@vuepress/core'
2+
import { createPage, preparePageChunk, prepareRoutes } from '@vuepress/core'
83

94
/**
105
* Event handler for page change event
@@ -31,9 +26,9 @@ export const handlePageChange = async (
3126

3227
// replace the old page with the new page
3328
app.pages.splice(pageIndex, 1, pageNew)
29+
app.pagesMap[filePath] = pageNew
3430

3531
// prepare page files
36-
await preparePageComponent(app, pageNew)
3732
await preparePageChunk(app, pageNew)
3833

3934
const isPathChanged = pageOld.path !== pageNew.path

packages/cli/src/commands/dev/handlePageUnlink.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const handlePageUnlink = async (
2020

2121
// remove the old page
2222
app.pages.splice(pageIndex, 1)
23+
delete app.pagesMap[filePath]
2324

2425
// re-prepare routes file
2526
await prepareRoutes(app)

packages/client/src/components/Content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const Content = defineComponent({
2222
if (!props.path) return pageComponent.value
2323
const route = resolveRoute(props.path)
2424
return defineAsyncComponent(async () =>
25-
route.loader().then(({ comp }) => comp),
25+
route.loader().then((m) => m.default),
2626
)
2727
})
2828

packages/client/src/setupGlobalComputed.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ export const setupGlobalComputed = (
4747
if (__VUEPRESS_DEV__ && (import.meta.webpackHot || import.meta.hot)) {
4848
__VUE_HMR_RUNTIME__.updatePageData = async (newPageData: PageData) => {
4949
const oldPageChunk = await routes.value[newPageData.path].loader()
50-
const newPageChunk = { comp: oldPageChunk.comp, data: newPageData }
50+
const newPageChunk: PageChunk = {
51+
default: oldPageChunk.default,
52+
_pageData: newPageData,
53+
}
5154
routes.value[newPageData.path].loader = async () =>
5255
Promise.resolve(newPageChunk)
5356
if (
5457
newPageData.path ===
55-
router.currentRoute.value.meta._pageChunk?.data.path
58+
router.currentRoute.value.meta._pageChunk?._pageData.path
5659
) {
5760
pageChunk.value = newPageChunk
5861
}
@@ -67,8 +70,8 @@ export const setupGlobalComputed = (
6770
const siteLocaleData = computed(() =>
6871
resolvers.resolveSiteLocaleData(siteData.value, routeLocale.value),
6972
)
70-
const pageComponent = computed(() => pageChunk.value.comp)
71-
const pageData = computed(() => pageChunk.value.data)
73+
const pageComponent = computed(() => pageChunk.value.default)
74+
const pageData = computed(() => pageChunk.value._pageData)
7275
const pageFrontmatter = computed(() => pageData.value.frontmatter)
7376
const pageHeadTitle = computed(() =>
7477
resolvers.resolvePageHeadTitle(pageData.value, siteLocaleData.value),

packages/client/src/types/routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { Component } from 'vue'
22
import type { PageData } from '../types/index.js'
33

44
export interface PageChunk {
5-
comp: Component
6-
data: PageData
5+
default: Component
6+
_pageData: PageData
77
}
88

99
export type RouteMeta = Record<string, unknown>

packages/core/src/app/appPrepare.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { App } from '../types/index.js'
33
import {
44
prepareClientConfigs,
55
preparePageChunk,
6-
preparePageComponent,
76
prepareRoutes,
87
prepareSiteData,
98
} from './prepare/index.js'
@@ -13,7 +12,6 @@ const log = debug('vuepress:core/app')
1312
/**
1413
* Prepare files for development or build
1514
*
16-
* - page components
1715
* - page chunks
1816
* - routes
1917
* - site data
@@ -23,10 +21,7 @@ export const appPrepare = async (app: App): Promise<void> => {
2321
log('prepare start')
2422

2523
await Promise.all([
26-
...app.pages.flatMap((page) => [
27-
preparePageComponent(app, page),
28-
preparePageChunk(app, page),
29-
]),
24+
...app.pages.map(async (page) => preparePageChunk(app, page)),
3025
prepareRoutes(app),
3126
prepareSiteData(app),
3227
prepareClientConfigs(app),

0 commit comments

Comments
 (0)