Skip to content

Commit

Permalink
refactor: updates
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Sep 11, 2024
1 parent d47d573 commit e681fb3
Show file tree
Hide file tree
Showing 22 changed files with 181 additions and 198 deletions.
1 change: 1 addition & 0 deletions eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default vuepress(
allow: [
'__dirname',
'_context',
'_pageData',
'_pageChunk',
'_registeredComponents',
],
Expand Down
25 changes: 9 additions & 16 deletions packages/bundler-vite/src/plugins/vuepressMarkdownPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { App } from '@vuepress/core'
import { parsePageContent, renderPageSfcBlocksToVue } from '@vuepress/core'
import { path } from '@vuepress/utils'
import { createPage, renderPageToVue } from '@vuepress/core'
import type { Plugin } from 'vite'

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

enforce: 'pre',

transform(code, id) {
async transform(code, id) {
if (!id.endsWith('.md')) return

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

// if the page content is not changed, render it to vue component directly
if (page?.content === code) {
return renderPageSfcBlocksToVue(page.sfcBlocks)
return renderPageToVue(page)
}

// parse the markdown content to sfc blocks and render it to vue component
const { sfcBlocks } = parsePageContent({
app,
// create a new page with the new content
const newPage = await createPage(app, {
content: code,
filePath: id,
filePathRelative: path.relative(app.dir.source(), id),
options: {},
})
return renderPageSfcBlocksToVue(sfcBlocks)
return renderPageToVue(newPage)
},

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

// parse the content to sfc blocks
const { sfcBlocks } = parsePageContent({
app,
// create a new page with the new content
const newPage = await createPage(app, {
content: code,
filePath: ctx.file,
filePathRelative: path.relative(app.dir.source(), ctx.file),
options: {},
})

ctx.read = () => renderPageSfcBlocksToVue(sfcBlocks)
ctx.read = () => renderPageToVue(newPage)
},
})
14 changes: 5 additions & 9 deletions packages/bundler-webpack/src/loaders/vuepressMarkdownLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ export interface VuepressMarkdownLoaderOptions {
export const vuepressMarkdownLoader: LoaderDefinitionFunction<VuepressMarkdownLoaderOptions> =
async function vuepressMarkdownLoader(source) {
// import esm dependencies
const [{ parsePageContent, renderPageSfcBlocksToVue }, { path }] =
await Promise.all([import('@vuepress/core'), import('@vuepress/utils')])
const { createPage, renderPageToVue } = await import('@vuepress/core')

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

// if the page content is not changed, render it to vue component directly
if (page?.content === source) {
return renderPageSfcBlocksToVue(page.sfcBlocks)
return renderPageToVue(page)
}

// parse the markdown content to sfc blocks and render it to vue component
const { sfcBlocks } = parsePageContent({
app,
// create a new page with the new content
const newPage = await createPage(app, {
content: source,
filePath: this.resourcePath,
filePathRelative: path.relative(app.dir.source(), this.resourcePath),
options: {},
})
return renderPageSfcBlocksToVue(sfcBlocks)
return renderPageToVue(newPage)
}
11 changes: 3 additions & 8 deletions packages/cli/src/commands/dev/handlePageAdd.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { App, Page } from '@vuepress/core'
import {
createPage,
preparePageChunk,
preparePageComponent,
prepareRoutes,
} from '@vuepress/core'
import { createPage, preparePageChunk, prepareRoutes } from '@vuepress/core'

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

// add the new page
app.pages.push(page)
app.pagesMap[filePath] = page

// prepare page files
await preparePageComponent(app, page)
// prepare page file
await preparePageChunk(app, page)

// prepare routes file
Expand Down
9 changes: 2 additions & 7 deletions packages/cli/src/commands/dev/handlePageChange.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import type { App, Page } from '@vuepress/core'
import {
createPage,
preparePageChunk,
preparePageComponent,
prepareRoutes,
} from '@vuepress/core'
import { createPage, preparePageChunk, prepareRoutes } from '@vuepress/core'

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

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

// prepare page files
await preparePageComponent(app, pageNew)
await preparePageChunk(app, pageNew)

const isPathChanged = pageOld.path !== pageNew.path
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/dev/handlePageUnlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const handlePageUnlink = async (

// remove the old page
app.pages.splice(pageIndex, 1)
delete app.pagesMap[filePath]

// re-prepare routes file
await prepareRoutes(app)
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const Content = defineComponent({
if (!props.path) return pageComponent.value
const route = resolveRoute(props.path)
return defineAsyncComponent(async () =>
route.loader().then(({ comp }) => comp),
route.loader().then((m) => m.default),
)
})

Expand Down
11 changes: 7 additions & 4 deletions packages/client/src/setupGlobalComputed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ export const setupGlobalComputed = (
if (__VUEPRESS_DEV__ && (import.meta.webpackHot || import.meta.hot)) {
__VUE_HMR_RUNTIME__.updatePageData = async (newPageData: PageData) => {
const oldPageChunk = await routes.value[newPageData.path].loader()
const newPageChunk = { comp: oldPageChunk.comp, data: newPageData }
const newPageChunk: PageChunk = {
default: oldPageChunk.default,
_pageData: newPageData,
}
routes.value[newPageData.path].loader = async () =>
Promise.resolve(newPageChunk)
if (
newPageData.path ===
router.currentRoute.value.meta._pageChunk?.data.path
router.currentRoute.value.meta._pageChunk?._pageData.path
) {
pageChunk.value = newPageChunk
}
Expand All @@ -67,8 +70,8 @@ export const setupGlobalComputed = (
const siteLocaleData = computed(() =>
resolvers.resolveSiteLocaleData(siteData.value, routeLocale.value),
)
const pageComponent = computed(() => pageChunk.value.comp)
const pageData = computed(() => pageChunk.value.data)
const pageComponent = computed(() => pageChunk.value.default)
const pageData = computed(() => pageChunk.value._pageData)
const pageFrontmatter = computed(() => pageData.value.frontmatter)
const pageHeadTitle = computed(() =>
resolvers.resolvePageHeadTitle(pageData.value, siteLocaleData.value),
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/types/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { Component } from 'vue'
import type { PageData } from '../types/index.js'

export interface PageChunk {
comp: Component
data: PageData
default: Component
_pageData: PageData
}

export type RouteMeta = Record<string, unknown>
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/app/appPrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { App } from '../types/index.js'
import {
prepareClientConfigs,
preparePageChunk,
preparePageComponent,
prepareRoutes,
prepareSiteData,
} from './prepare/index.js'
Expand All @@ -13,7 +12,6 @@ const log = debug('vuepress:core/app')
/**
* Prepare files for development or build
*
* - page components
* - page chunks
* - routes
* - site data
Expand All @@ -23,10 +21,7 @@ export const appPrepare = async (app: App): Promise<void> => {
log('prepare start')

await Promise.all([
...app.pages.flatMap((page) => [
preparePageComponent(app, page),
preparePageChunk(app, page),
]),
...app.pages.map(async (page) => preparePageChunk(app, page)),
prepareRoutes(app),
prepareSiteData(app),
prepareClientConfigs(app),
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/app/prepare/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './prepareClientConfigs.js'
export * from './preparePageChunk.js'
export * from './preparePageComponent.js'
export * from './prepareRoutes.js'
export * from './prepareSiteData.js'
32 changes: 4 additions & 28 deletions packages/core/src/app/prepare/preparePageChunk.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,11 @@
import { renderPageToVue } from '../../page/index.js'
import type { App, Page } from '../../types/index.js'

const HMR_CODE = `
if (import.meta.webpackHot) {
import.meta.webpackHot.accept()
if (__VUE_HMR_RUNTIME__.updatePageData) {
__VUE_HMR_RUNTIME__.updatePageData(data)
}
}
if (import.meta.hot) {
import.meta.hot.accept(({ data }) => {
__VUE_HMR_RUNTIME__.updatePageData(data)
})
}
`

/**
* Generate page chunk temp file of a single page
* Generate temp file the page does not have a source file
*/
export const preparePageChunk = async (app: App, page: Page): Promise<void> => {
// page chunk file content
let content = `\
import comp from ${JSON.stringify(page.componentFilePath)}
const data = JSON.parse(${JSON.stringify(JSON.stringify(page.data))})
export { comp, data }
`

// inject HMR code
if (app.env.isDev) {
content += HMR_CODE
if (page.filePath === null) {
await app.writeTemp(page.chunkFilePathRelative, renderPageToVue(page))
}

await app.writeTemp(page.chunkFilePathRelative, content)
}
17 changes: 0 additions & 17 deletions packages/core/src/app/prepare/preparePageComponent.ts

This file was deleted.

15 changes: 4 additions & 11 deletions packages/core/src/page/createPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { App, Page, PageOptions } from '../types/index.js'
import { inferPagePath } from './inferPagePath.js'
import { parsePageContent } from './parsePageContent.js'
import { resolvePageChunkInfo } from './resolvePageChunkInfo.js'
import { resolvePageComponentInfo } from './resolvePageComponentInfo.js'
import { resolvePageContent } from './resolvePageContent.js'
import { resolvePageDate } from './resolvePageDate.js'
import { resolvePageFileContent } from './resolvePageFileContent.js'
import { resolvePageFilePath } from './resolvePageFilePath.js'
import { resolvePageHtmlInfo } from './resolvePageHtmlInfo.js'
import { resolvePageLang } from './resolvePageLang.js'
Expand All @@ -27,7 +26,7 @@ export const createPage = async (
})

// read the raw file content according to the absolute file path
const content = await resolvePageFileContent({ filePath, options })
const content = await resolvePageContent({ filePath, options })

// render page content and extract information
const {
Expand Down Expand Up @@ -81,18 +80,14 @@ export const createPage = async (
path,
})

// resolve page component and extract headers & links
const { componentFilePath, componentFilePathRelative } =
resolvePageComponentInfo({
const { chunkFilePath, chunkFilePathRelative, chunkName } =
resolvePageChunkInfo({
app,
filePath,
filePathRelative,
htmlFilePathRelative,
})

const { chunkFilePath, chunkFilePathRelative, chunkName } =
resolvePageChunkInfo({ app, htmlFilePathRelative })

const page: Page = {
// page data
data: {
Expand Down Expand Up @@ -127,8 +122,6 @@ export const createPage = async (
// file info
filePath,
filePathRelative,
componentFilePath,
componentFilePathRelative,
chunkFilePath,
chunkFilePathRelative,
chunkName,
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/page/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export * from './createPage.js'
export * from './inferPagePath.js'
export * from './parsePageContent.js'
export * from './renderPageSfcBlocksToVue.js'
export * from './renderPageToVue.js'
export * from './resolvePageChunkInfo.js'
export * from './resolvePageComponentInfo.js'
export * from './resolvePageDate.js'
export * from './resolvePageFileContent.js'
export * from './resolvePageContent.js'
export * from './resolvePageFilePath.js'
export * from './resolvePageHtmlInfo.js'
export * from './resolvePageLang.js'
Expand Down
17 changes: 0 additions & 17 deletions packages/core/src/page/renderPageSfcBlocksToVue.ts

This file was deleted.

Loading

0 comments on commit e681fb3

Please sign in to comment.