perf: add debug log

This commit is contained in:
pengzhanbo 2024-05-05 20:24:53 +08:00
parent 7ff8fbe093
commit 251ad8848d
10 changed files with 44 additions and 11 deletions

View File

@ -36,6 +36,7 @@
"vuepress": "2.0.0-rc.9"
},
"dependencies": {
"@pengzhanbo/utils": "^1.1.2",
"chokidar": "^3.6.0",
"create-filter": "^1.0.1",
"fast-glob": "^3.3.2",

View File

@ -1,9 +1,10 @@
import { fs } from 'vuepress/utils'
import { colors, fs, logger } from 'vuepress/utils'
import type { Plugin } from 'vuepress/core'
import chokidar from 'chokidar'
import { createFilter } from 'create-filter'
import grayMatter from 'gray-matter'
import jsonToYaml from 'json2yaml'
import { promiseParallel } from '@pengzhanbo/utils'
import type {
AutoFrontmatterOptions,
FrontmatterArray,
@ -13,6 +14,8 @@ import type {
import { readMarkdown, readMarkdownList } from './readFiles.js'
import { ensureArray, isEmptyObject } from './utils.js'
const PLUGIN_NAME = '@vuepress-plume/plugin-auto-frontmatter'
export function autoFrontmatterPlugin({
include = ['**/*.md'],
exclude = ['.vuepress/**/*', 'node_modules'],
@ -70,11 +73,16 @@ export function autoFrontmatterPlugin({
}
return {
name: '@vuepress-plume/plugin-auto-frontmatter',
name: PLUGIN_NAME,
onInitialized: async (app) => {
const start = performance.now()
const markdownList = await readMarkdownList(app.dir.source(), globFilter)
for (const file of markdownList)
await formatMarkdown(file)
await promiseParallel(
markdownList.map(file => () => formatMarkdown(file)),
64,
)
if (app.env.isDebug)
logger.info(`\n[${colors.green(PLUGIN_NAME)}] Init time spent: ${(performance.now() - start).toFixed(2)}ms`)
},
onWatched: async (app, watchers) => {
const watcher = chokidar.watch('**/*.md', {

View File

@ -1,4 +1,5 @@
import type { App, Page } from 'vuepress/core'
import { colors, logger } from 'vuepress/utils'
import type { BlogPostData, BlogPostDataItem } from '../shared/index.js'
import type { PluginOption } from './plugin.js'
@ -30,6 +31,8 @@ function getTimestamp(time: Date): number {
const EXCERPT_SPLIT = '<!-- more -->'
export async function preparedBlogData(app: App, pageFilter: (id: string) => boolean, options: PluginOption): Promise<void> {
const start = performance.now()
let pages = app.pages.filter((page) => {
return page.filePathRelative && pageFilter(page.filePathRelative)
})
@ -94,4 +97,7 @@ export const extraBlogData = JSON.parse(${JSON.stringify(
content += HMR_CODE
await app.writeTemp('internal/blogData.js', content)
if (app.env.isDebug)
logger.info(`\n[${colors.green('@vuepress-plume/plugin-blog-data')}] prepare blog data time spent: ${(performance.now() - start).toFixed(2)}ms`)
}

View File

@ -7,7 +7,7 @@ export function notesDataPlugin(options: NotesDataOptions | NotesDataOptions[]):
return (app: App) => {
return {
name: '@vuepress-plume/plugin-notes-data',
clientConfigFile: path.resolve(
clientConfigFile: path.join(
getDirname(import.meta.url),
'../client/clientConfig.js',
),

View File

@ -1,4 +1,4 @@
import { path } from 'vuepress/utils'
import { colors, logger, path } from 'vuepress/utils'
import type { App } from 'vuepress/core'
import * as chokidar from 'chokidar'
import { createFilter } from 'create-filter'
@ -66,6 +66,7 @@ function resolvedNotesData(app: App, options: NotesDataOptions, result: NotesDat
}
export async function prepareNotesData(app: App, options: NotesDataOptions | NotesDataOptions[]) {
const start = performance.now()
const notesData: NotesData = {}
const allOptions = ensureArray<NotesDataOptions>(options)
@ -78,6 +79,12 @@ export const notesData = ${JSON.stringify(notesData, null, 2)}
content += HMR_CODE
await app.writeTemp('internal/notesData.js', content)
if (app.env.isDebug) {
logger.info(
`\n[${colors.green('@vuepress-plume/plugin-notes-data')}] prepare notes data time spent: ${(performance.now() - start).toFixed(2)}ms`,
)
}
}
export function watchNotesData(app: App, watchers: any[], options: NotesDataOptions | NotesDataOptions[]): void {

View File

@ -1,6 +1,7 @@
import type { App, Page } from 'vuepress/core'
import MiniSearch from 'minisearch'
import pMap from 'p-map'
import { colors, logger } from 'vuepress/utils'
import type { SearchOptions, SearchPluginOptions } from '../shared/index.js'
export interface SearchIndexOptions {
@ -47,11 +48,18 @@ export async function prepareSearchIndex({
isSearchable,
searchOptions,
}: SearchIndexOptions) {
const start = performance.now()
const pages = isSearchable ? app.pages.filter(isSearchable) : app.pages
await pMap(pages, p => indexFile(p, searchOptions), {
concurrency: 64,
})
await writeTemp(app)
if (app.env.isDebug) {
logger.info(
`\n[${colors.green('@vuepress-plume/plugin-search')}] prepare search time spent: ${(performance.now() - start).toFixed(2)}ms`,
)
}
}
export async function onSearchIndexUpdated(

View File

@ -16,12 +16,12 @@ yarn add @vuepress-plume/plugin-shikiji
``` js
// .vuepress/config.[jt]s
import { shikijiPlugin } from '@vuepress-plume/plugin-shikiji'
import { shikiPlugin } from '@vuepress-plume/plugin-shikiji'
export default {
// ...
plugins: [
shikijiPlugin()
shikiPlugin()
]
// ...
}

View File

@ -1,6 +1,6 @@
import { shikiPlugin } from './shikijiPlugin.js'
import { shikiPlugin } from './shikiPlugin.js'
export * from './shikijiPlugin.js'
export * from './shikiPlugin.js'
export * from './types.js'
export default shikiPlugin

View File

@ -1,7 +1,7 @@
import type { Plugin, PluginObject } from 'vuepress/core'
import { getDirname, path } from 'vuepress/utils'
import { highlight } from './highlight.js'
import type { HighlighterOptions } from './types'
import type { HighlighterOptions } from './types.js'
export type ShikiPluginOptions = HighlighterOptions

3
pnpm-lock.yaml generated
View File

@ -116,6 +116,9 @@ importers:
plugins/plugin-auto-frontmatter:
dependencies:
'@pengzhanbo/utils':
specifier: ^1.1.2
version: 1.1.2
chokidar:
specifier: ^3.6.0
version: 3.6.0