Skip to content

Commit

Permalink
perf(theme): add debug log (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored Oct 1, 2024
1 parent 341267b commit 84c1ae3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 5 deletions.
6 changes: 5 additions & 1 deletion plugins/plugin-md-power/src/node/enhance/imageSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import http from 'node:https'
import { URL } from 'node:url'
import { isLinkExternal, isLinkHttp } from '@vuepress/helper'
import imageSize from 'image-size'
import { fs, path } from 'vuepress/utils'
import { fs, logger, path } from 'vuepress/utils'
import { resolveAttrs } from '../utils/resolveAttrs.js'

interface ImgSize {
Expand Down Expand Up @@ -36,10 +36,14 @@ export async function imageSizePlugin(
return

if (type === 'all') {
const start = performance.now()
try {
await scanRemoteImageSize(app)
}
catch {}
if (app.env.isDebug) {
logger.info(`[vuepress-plugin-md-power] imageSizePlugin: scan all images time spent: ${performance.now() - start}ms`)
}
}

const imageRule = md.renderer.rules.image!
Expand Down
6 changes: 6 additions & 0 deletions theme/src/node/autoFrontmatter/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import grayMatter from 'gray-matter'
import jsonToYaml from 'json2yaml'
import { fs, hash, path } from 'vuepress/utils'
import { getThemeConfig } from '../loadConfig/index.js'
import { logger } from '../utils/index.js'
import { readMarkdown, readMarkdownList } from './readFile.js'
import { resolveOptions } from './resolveOptions.js'

Expand Down Expand Up @@ -87,6 +88,7 @@ export function initAutoFrontmatter(
}

export async function generateAutoFrontmatter(app: App) {
const start = performance.now()
if (!generate)
return

Expand All @@ -106,6 +108,10 @@ export async function generateAutoFrontmatter(app: App) {
)

await generate.updateCache(app)

if (app.env.isDebug) {
logger.info(`Generate auto frontmatter: ${(performance.now() - start).toFixed(2)}ms`)
}
}

export async function watchAutoFrontmatter(app: App, watchers: any[]) {
Expand Down
14 changes: 14 additions & 0 deletions theme/src/node/loadConfig/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deepMerge } from '@pengzhanbo/utils'
import { watch } from 'chokidar'
import { path } from 'vuepress/utils'
import { resolveLocaleOptions } from '../config/resolveLocaleOptions.js'
import { logger } from '../utils/index.js'
import { compiler } from './compiler.js'
import { findConfigPath } from './findConfigPath.js'

Expand Down Expand Up @@ -39,6 +40,7 @@ export async function initConfigLoader(
defaultConfig: ThemeConfig,
{ configFile, onChange }: InitConfigLoaderOptions = {},
) {
const start = performance.now()
const { encrypt, autoFrontmatter, ...localeOptions } = defaultConfig
loader = {
configFile,
Expand All @@ -55,19 +57,31 @@ export async function initConfigLoader(
},
}

const findStart = performance.now()
loader.configFile = await findConfigPath(app, configFile)
if (app.env.isDebug) {
logger.info(`Find config path: ${(performance.now() - findStart).toFixed(2)}ms`)
}

if (onChange) {
loader.changeEvents.push(onChange)
}

const loadStart = performance.now()
const { config, dependencies = [] } = await loader.load()
if (app.env.isDebug) {
logger.info(`theme config call load: ${(performance.now() - loadStart).toFixed(2)}ms`)
}
loader.loaded = true
loader.dependencies = [...dependencies]
updateResolvedConfig(app, config)

loader.whenLoaded.forEach(fn => fn(loader!.resolvedConfig))
loader.whenLoaded = []

if (app.env.isDebug) {
logger.info(`Load config: ${(performance.now() - start).toFixed(2)}ms`)
}
}

export function watchConfigFile(app: App, watchers: any[], onChange: ChangeEvent) {
Expand Down
6 changes: 6 additions & 0 deletions theme/src/node/prepare/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { App } from 'vuepress'
import { watch } from 'chokidar'
import { getThemeConfig } from '../loadConfig/index.js'
import { logger } from '../utils/index.js'
import { prepareArticleTagColors } from './prepareArticleTagColor.js'
import { preparedBlogData } from './prepareBlogData.js'
import { prepareEncrypt } from './prepareEncrypt.js'
Expand All @@ -10,6 +11,7 @@ import { prepareSidebar } from './prepareSidebar.js'
export async function prepareData(
app: App,
): Promise<void> {
const start = performance.now()
const { localeOptions, encrypt } = getThemeConfig()
await Promise.all([
prepareArticleTagColors(app),
Expand All @@ -18,6 +20,10 @@ export async function prepareData(
prepareEncrypt(app, encrypt),
prepareIcons(app, localeOptions),
])

if (app.env.isDebug) {
logger.info(`Prepare data: ${(performance.now() - start).toFixed(2)}ms`)
}
}

export function watchPrepare(
Expand Down
8 changes: 7 additions & 1 deletion theme/src/node/prepare/prepareArticleTagColor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { App } from 'vuepress'
import { toArray } from '@pengzhanbo/utils'
import { nanoid, resolveContent, writeTemp } from '../utils/index.js'
import { logger, nanoid, resolveContent, writeTemp } from '../utils/index.js'

export type TagsColorsItem = readonly [
string, // normal color
Expand Down Expand Up @@ -33,9 +33,15 @@ export const PRESET: TagsColorsItem[] = [
const cache: Record<number, string> = {}

export async function prepareArticleTagColors(app: App): Promise<void> {
const start = performance.now()
const { js, css } = genCode(app)
await writeTemp(app, 'internal/articleTagColors.css', css)
await writeTemp(app, 'internal/articleTagColors.js', js)
if (app.env.isDebug) {
logger.info(
`Generate article tag colors: ${(performance.now() - start).toFixed(2)}ms`,
)
}
}

export function genCode(app: App): { js: string, css: string } {
Expand Down
6 changes: 5 additions & 1 deletion theme/src/node/prepare/prepareEncrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Page } from 'vuepress/core'
import type { PlumeThemeEncrypt, PlumeThemePageData } from '../../shared/index.js'
import { isNumber, isString, random, toArray } from '@pengzhanbo/utils'
import { genSaltSync, hashSync } from 'bcrypt-ts'
import { hash, resolveContent, writeTemp } from '../utils/index.js'
import { hash, logger, resolveContent, writeTemp } from '../utils/index.js'

export type EncryptConfig = readonly [
boolean, // global
Expand All @@ -18,6 +18,7 @@ const separator = ':'
let contentHash = ''

export async function prepareEncrypt(app: App, encrypt?: PlumeThemeEncrypt) {
const start = performance.now()
const currentHash = encrypt ? hash(JSON.stringify(encrypt)) : ''

if (!contentHash || contentHash !== currentHash) {
Expand All @@ -28,6 +29,9 @@ export async function prepareEncrypt(app: App, encrypt?: PlumeThemeEncrypt) {
})
await writeTemp(app, 'internal/encrypt.js', content)
}
if (app.env.isDebug) {
logger.info(`Generate encrypt: ${(performance.now() - start).toFixed(2)}ms`)
}
}

const salt = () => genSaltSync(random(8, 16))
Expand Down
15 changes: 15 additions & 0 deletions theme/src/node/prepare/prepareIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function isIconify(icon: any): icon is string {
}

export async function prepareIcons(app: App, localeOptions: PlumeThemeLocaleOptions) {
const start = performance.now()
if (!isInstalled) {
await writeTemp(app, JS_FILENAME, resolveContent(app, { name: 'icons', content: '{}' }))
return
Expand All @@ -54,6 +55,12 @@ export async function prepareIcons(app: App, localeOptions: PlumeThemeLocaleOpti
collectMap[collect].push(name)
})

if (app.env.isDebug) {
logger.info(`Generate icons with pages and theme config: ${(performance.now() - start).toFixed(2)}ms`)
}

const collectStart = performance.now()

if (!locate) {
const mod = await interopDefault(import('@iconify/json'))
locate = mod.locate
Expand All @@ -67,6 +74,10 @@ export async function prepareIcons(app: App, localeOptions: PlumeThemeLocaleOpti
logger.warn(`[iconify] Unknown icons: ${unknownList.join(', ')}`)
}

if (app.env.isDebug) {
logger.info(`Generate icons with iconify collect: ${(performance.now() - collectStart).toFixed(2)}ms`)
}

let cssCode = ''
const map: Record<string, string> = {}
for (const [iconName, { className, content, background }] of entries(cache)) {
Expand All @@ -82,6 +93,10 @@ export async function prepareIcons(app: App, localeOptions: PlumeThemeLocaleOpti
before: `import './iconify.css'`,
})),
])

if (app.env.isDebug) {
logger.info(`Generate icons total time: ${(performance.now() - start).toFixed(2)}ms`)
}
}

function getIconsWithPage(page: Page): string[] {
Expand Down
7 changes: 6 additions & 1 deletion theme/src/node/prepare/prepareSidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ import {
isPlainObject,
removeLeadingSlash,
} from '@vuepress/helper'
import { normalizeLink, resolveContent, writeTemp } from '../utils/index.js'
import { logger, normalizeLink, resolveContent, writeTemp } from '../utils/index.js'

export async function prepareSidebar(app: App, localeOptions: PlumeThemeLocaleOptions) {
const start = performance.now()
const sidebar = getAllSidebar(localeOptions)
sidebar.__auto__ = getSidebarData(app, sidebar)
await writeTemp(app, 'internal/sidebar.js', resolveContent(app, { name: 'sidebar', content: sidebar }))

if (app.env.isDebug) {
logger.info(`Generate sidebar: ${(performance.now() - start).toFixed(2)}ms`)
}
}

function getSidebarData(
Expand Down
7 changes: 6 additions & 1 deletion theme/src/node/prepare/prepareThemeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@ import type { App } from 'vuepress'
import type { PlumeThemeData, PlumeThemeLocaleOptions, PlumeThemePluginOptions } from '../../shared/index.js'
import { resolveImageSize } from 'vuepress-plugin-md-power'
import { resolveThemeData } from '../config/resolveThemeData.js'
import { resolveContent, writeTemp } from '../utils/index.js'
import { logger, resolveContent, writeTemp } from '../utils/index.js'

export async function prepareThemeData(
app: App,
localeOptions: PlumeThemeLocaleOptions,
pluginOptions: PlumeThemePluginOptions,
): Promise<void> {
const start = performance.now()
const resolvedThemeData = resolveThemeData(app, localeOptions)

await resolveProfileImage(app, resolvedThemeData, pluginOptions)

const content = resolveContent(app, { name: 'themeData', content: resolvedThemeData })
await writeTemp(app, 'internal/themePlumeData.js', content)

if (app.env.isDebug) {
logger.info(`Generate theme data: ${(performance.now() - start).toFixed(2)}ms`)
}
}

async function resolveProfileImage(
Expand Down

0 comments on commit 84c1ae3

Please sign in to comment.