diff --git a/src/client/app/composables/siteData.ts b/src/client/app/composables/siteData.ts index 7829e317..8b6d7f71 100644 --- a/src/client/app/composables/siteData.ts +++ b/src/client/app/composables/siteData.ts @@ -16,7 +16,7 @@ function parse(data: string): SiteData { // hmr if (import.meta.hot) { - import.meta.hot!.accept('/@siteData', (m) => { + import.meta.hot.accept('/@siteData', (m) => { siteDataRef.value = parse(m.default) }) } diff --git a/src/client/theme-default/components/LastUpdated.vue b/src/client/theme-default/components/LastUpdated.vue index d33ab0a2..9dfce320 100644 --- a/src/client/theme-default/components/LastUpdated.vue +++ b/src/client/theme-default/components/LastUpdated.vue @@ -12,8 +12,6 @@ import { useSiteDataByRoute, usePageData } from 'vitepress' const site = useSiteDataByRoute() const page = usePageData() -const datetime = ref('') - const hasLastUpdated = computed(() => { const lu = site.value.themeConfig.lastUpdated @@ -22,12 +20,11 @@ const hasLastUpdated = computed(() => { const prefix = computed(() => { const p = site.value.themeConfig.lastUpdated - return p === true ? 'Last Updated' : p }) -onMounted(() => { - datetime.value = new Date(page.value.lastUpdated).toLocaleString('en-US') +const datetime = computed(() => { + return new Date(page.value.lastUpdated).toLocaleString('en-US') }) diff --git a/src/node/cli.ts b/src/node/cli.ts index bd21caf8..eef10966 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -15,13 +15,7 @@ if (root) { if (!command || command === 'dev') { createServer(root, argv) - .then((server) => { - return server.listen().then(() => { - console.log( - `listening at http://localhost:${server.config.server.port}` - ) - }) - }) + .then((server) => server.listen()) .catch((err) => { console.error(chalk.red(`failed to start server. error:\n`), err) process.exit(1) diff --git a/src/node/markdownToVue.ts b/src/node/markdownToVue.ts index 6f7a2a4a..cc94049c 100644 --- a/src/node/markdownToVue.ts +++ b/src/node/markdownToVue.ts @@ -8,12 +8,7 @@ import { PageData, HeadConfig } from '../../types/shared' import slash from 'slash' const debug = require('debug')('vitepress:md') -const cache = new LRUCache({ max: 1024 }) - -interface MarkdownCompileCachedResult extends MarkdownCompileResult { - tagsWithPageData: string - tagsWithoutPageData: string -} +const cache = new LRUCache({ max: 1024 }) interface MarkdownCompileResult { vueSrc: string @@ -26,17 +21,13 @@ export function createMarkdownToVueRenderFn( ) { const md = createMarkdownRenderer(options) - return ( - src: string, - file: string, - injectData = true - ): MarkdownCompileResult => { + return (src: string, file: string): MarkdownCompileResult => { const relativePath = slash(path.relative(root, file)) const cached = cache.get(src) if (cached) { debug(`[cache hit] ${relativePath}`) - return pickResult(cached, injectData) + return cached } const start = Date.now() @@ -44,8 +35,6 @@ export function createMarkdownToVueRenderFn( const { content, data: frontmatter } = matter(src) const { html, data } = md.render(content) - const vueSrc = `\n` - // TODO validate data.links? const pageData: PageData = { title: inferTitle(frontmatter, content), @@ -54,38 +43,21 @@ export function createMarkdownToVueRenderFn( headers: data.headers, relativePath, // TODO use git timestamp? - lastUpdated: fs.statSync(file).mtimeMs + lastUpdated: Math.round(fs.statSync(file).mtimeMs) } - const tagsWithPageData = genPageDataCode( - data.hoistedTags || [], - pageData - ).join('\n') - - const tagsWithoutPageData = (data.hoistedTags || []).join('\n') + const vueSrc = + genPageDataCode(data.hoistedTags || [], pageData).join('\n') + + `\n` debug(`[render] ${file} in ${Date.now() - start}ms.`) const result = { vueSrc, - pageData, - tagsWithPageData, - tagsWithoutPageData + pageData } cache.set(src, result) - return pickResult(result, injectData) - } -} - -function pickResult( - res: MarkdownCompileCachedResult, - injectData: boolean -): MarkdownCompileResult { - return { - vueSrc: - res.vueSrc + - (injectData ? res.tagsWithPageData : res.tagsWithoutPageData), - pageData: res.pageData + return result } } diff --git a/src/node/plugin.ts b/src/node/plugin.ts index 2039e5a6..d38deb66 100644 --- a/src/node/plugin.ts +++ b/src/node/plugin.ts @@ -17,7 +17,6 @@ export function createVitePressPlugin( }) let siteData = initialSiteData - let stringifiedData = JSON.stringify(JSON.stringify(initialSiteData)) const vitePressPlugin: Plugin = { name: 'vitepress', @@ -37,7 +36,7 @@ export function createVitePressPlugin( load(id) { if (id === SITE_DATA_REQUEST_PATH) { - return `export default ${stringifiedData}` + return `export default ${JSON.stringify(JSON.stringify(siteData))}` } }, @@ -62,30 +61,22 @@ export function createVitePressPlugin( }, async handleHotUpdate(file, mods, read, server) { + // handle config hmr if (file === configPath) { const newData = await resolveSiteData(root) - stringifiedData = JSON.stringify(JSON.stringify(newData)) if (newData.base !== siteData.base) { console.warn( `[vitepress]: config.base has changed. Please restart the dev server.` ) } siteData = newData - return + return [server.moduleGraph.getModuleById(SITE_DATA_REQUEST_PATH)!] } // hot reload .md files as .vue files if (file.endsWith('.md')) { const content = await read() - const { pageData, vueSrc } = markdownToVue( - content.toString(), - file, - // do not inject pageData on HMR - // it leads to plugin-vue to think