Skip to content

Commit 3bb73dc

Browse files
committed
refactor: improve hmr code injection
1 parent e7b4e27 commit 3bb73dc

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

packages/core/src/app/prepare/prepareRoutes.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
import { normalizeRoutePath } from '@vuepress/shared'
22
import type { App, Page } from '../../types/index.js'
33

4+
const ROUTES_VAR_NAME = 'routes'
5+
const REDIRECTS_VAR_NAME = 'redirects'
6+
47
const HMR_CODE = `
58
if (import.meta.webpackHot) {
69
import.meta.webpackHot.accept()
7-
if (__VUE_HMR_RUNTIME__.updateRoutes) {
8-
__VUE_HMR_RUNTIME__.updateRoutes(routes)
9-
}
10-
if (__VUE_HMR_RUNTIME__.updateRedirects) {
11-
__VUE_HMR_RUNTIME__.updateRedirects(redirects)
12-
}
10+
__VUE_HMR_RUNTIME__.updateRoutes?.(${ROUTES_VAR_NAME})
11+
__VUE_HMR_RUNTIME__.updateRedirects?.(${REDIRECTS_VAR_NAME}})
1312
}
1413
1514
if (import.meta.hot) {
16-
import.meta.hot.accept(({ routes, redirects }) => {
17-
__VUE_HMR_RUNTIME__.updateRoutes(routes)
18-
__VUE_HMR_RUNTIME__.updateRedirects(redirects)
15+
import.meta.hot.accept((m) => {
16+
__VUE_HMR_RUNTIME__.updateRoutes?.(m.${ROUTES_VAR_NAME})
17+
__VUE_HMR_RUNTIME__.updateRedirects?.(m.${REDIRECTS_VAR_NAME})
1918
})
2019
}
2120
`
@@ -47,7 +46,7 @@ const resolvePageRedirects = ({ path, pathInferred }: Page): string[] => {
4746
export const prepareRoutes = async (app: App): Promise<void> => {
4847
// routes file content
4948
let content = `\
50-
export const redirects = JSON.parse(${JSON.stringify(
49+
export const ${REDIRECTS_VAR_NAME} = JSON.parse(${JSON.stringify(
5150
JSON.stringify(
5251
Object.fromEntries(
5352
app.pages.flatMap((page) =>
@@ -57,7 +56,7 @@ export const redirects = JSON.parse(${JSON.stringify(
5756
),
5857
)})
5958
60-
export const routes = Object.fromEntries([
59+
export const ${ROUTES_VAR_NAME} = Object.fromEntries([
6160
${app.pages
6261
.map(
6362
({ chunkFilePath, chunkName, path, routeMeta }) =>

packages/core/src/app/prepare/prepareSiteData.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { App } from '../../types/index.js'
22

3+
const SITE_DATA_VAR_NAME = 'siteData'
4+
35
const HMR_CODE = `
46
if (import.meta.webpackHot) {
57
import.meta.webpackHot.accept()
6-
if (__VUE_HMR_RUNTIME__.updateSiteData) {
7-
__VUE_HMR_RUNTIME__.updateSiteData(siteData)
8-
}
8+
__VUE_HMR_RUNTIME__.updateSiteData?.(${SITE_DATA_VAR_NAME})
99
}
1010
1111
if (import.meta.hot) {
12-
import.meta.hot.accept(({ siteData }) => {
13-
__VUE_HMR_RUNTIME__.updateSiteData(siteData)
12+
import.meta.hot.accept((m) => {
13+
__VUE_HMR_RUNTIME__.updateSiteData?.(m.${SITE_DATA_VAR_NAME})
1414
})
1515
}
1616
`
@@ -20,7 +20,7 @@ if (import.meta.hot) {
2020
*/
2121
export const prepareSiteData = async (app: App): Promise<void> => {
2222
let content = `\
23-
export const siteData = JSON.parse(${JSON.stringify(
23+
export const ${SITE_DATA_VAR_NAME} = JSON.parse(${JSON.stringify(
2424
JSON.stringify(app.siteData),
2525
)})
2626
`

packages/core/src/page/renderPageToVue.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isString } from '@vuepress/shared'
2-
import type { Page } from '../types/index.js'
2+
import type { App, Page } from '../types/index.js'
33

44
const TEMPLATE_WRAPPER_TAG_OPEN = '<div>'
55
const TEMPLATE_WRAPPER_TAG_CLOSE = '</div>'
@@ -10,21 +10,32 @@ const SCRIPT_TAG_CLOSE = '</script>'
1010
const SCRIPT_TAG_OPEN_LANG_TS_REGEX = /<\s*script[^>]*\blang=['"]ts['"][^>]*/
1111
const SCRIPT_TAG_OPEN_LANG_TS = '<script lang="ts">'
1212

13+
const PAGE_DATA_CODE_VAR_NAME = '_pageData'
14+
const PAGE_DATA_CODE_TEMPLATE_OUTLET = '__PAGE_DATA__'
15+
const PAGE_DATA_CODE_TEMPLATE = `export const ${PAGE_DATA_CODE_VAR_NAME} = JSON.parse(${PAGE_DATA_CODE_TEMPLATE_OUTLET})`
16+
1317
const HMR_CODE = `
1418
if (import.meta.webpackHot) {
1519
import.meta.webpackHot.accept()
16-
if (__VUE_HMR_RUNTIME__.updatePageData) {
17-
__VUE_HMR_RUNTIME__.updatePageData(_pageData)
18-
}
20+
__VUE_HMR_RUNTIME__.updatePageData?.(${PAGE_DATA_CODE_VAR_NAME})
1921
}
2022
2123
if (import.meta.hot) {
22-
import.meta.hot.accept(({ _pageData }) => {
23-
__VUE_HMR_RUNTIME__.updatePageData(_pageData)
24+
import.meta.hot.accept((m) => {
25+
__VUE_HMR_RUNTIME__.updatePageData?.(m.${PAGE_DATA_CODE_VAR_NAME})
2426
})
2527
}
2628
`
2729

30+
/**
31+
* Util to resolve the page data code
32+
*/
33+
const resolvePageDataCode = (data: Page['data']): string =>
34+
PAGE_DATA_CODE_TEMPLATE.replace(
35+
PAGE_DATA_CODE_TEMPLATE_OUTLET,
36+
JSON.stringify(JSON.stringify(data)),
37+
)
38+
2839
/**
2940
* Util to resolve the open tag of script block
3041
*/
@@ -43,7 +54,10 @@ const resolveScriptTagOpen = (sfcBlocks: Page['sfcBlocks']): string => {
4354
/**
4455
* Render page to vue component
4556
*/
46-
export const renderPageToVue = ({ data, sfcBlocks }: Page): string => {
57+
export const renderPageToVue = (
58+
app: App,
59+
{ data, sfcBlocks }: Page,
60+
): string => {
4761
// #688: wrap the content of `<template>` with a `<div>` to avoid some potential issues of fragment component
4862
const templateContent =
4963
sfcBlocks.template &&
@@ -56,12 +70,13 @@ export const renderPageToVue = ({ data, sfcBlocks }: Page): string => {
5670
].join('')
5771

5872
// inject page data code and HMR code into the script content
59-
const pageDataCode = `export const _pageData = JSON.parse(${JSON.stringify(JSON.stringify(data))})`
73+
const scriptTagOpen = resolveScriptTagOpen(sfcBlocks)
74+
const pageDataCode = resolvePageDataCode(data)
6075
const scriptContent = [
61-
resolveScriptTagOpen(sfcBlocks),
76+
scriptTagOpen,
6277
sfcBlocks.script?.contentStripped,
6378
pageDataCode,
64-
HMR_CODE,
79+
app.env.isDev && HMR_CODE,
6580
sfcBlocks.script?.tagClose ?? SCRIPT_TAG_CLOSE,
6681
]
6782
.filter(isString)

0 commit comments

Comments
 (0)