diff --git a/packages/page/package.json b/packages/page/package.json index f21ae3b..5d1126e 100644 --- a/packages/page/package.json +++ b/packages/page/package.json @@ -32,7 +32,13 @@ "keywords": [ "vite-plugin-minissg" ], + "dependencies": { + "debug": "^4.3.4" + }, "peerDependencies": { "vite-plugin-minissg": "^3.0.0" + }, + "devDependencies": { + "@types/debug": "^4.1.12" } } diff --git a/packages/page/src/debug.ts b/packages/page/src/debug.ts new file mode 100644 index 0000000..1dda1e3 --- /dev/null +++ b/packages/page/src/debug.ts @@ -0,0 +1,38 @@ +import createDebug from 'debug' +import type { Awaitable } from '../../vite-plugin-minissg/src/util' + +const d = createDebug('minissg:page') +export const debug = d.enabled ? d : undefined + +const sleep = (msec: number): PromiseLike => + new Promise(resolve => { + setTimeout(() => { + resolve(undefined) + }, msec) + }) + +const debugTimerPeriod = 3000 + +const debugTimerImpl = ( + task: () => PromiseLike, + callback: ( + debug: typeof d, + dt: number, + when: 'start' | 'middle' | 'end' + ) => Awaitable +): PromiseLike => { + const promise = task().then(x => ({ done: x })) + const t1 = performance.now() + return Promise.resolve(callback(d, 0, 'start')).then(async () => { + for (;;) { + const result = await Promise.race([promise, sleep(debugTimerPeriod)]) + const t2 = performance.now() + await callback(d, t2 - t1, result != null ? 'end' : 'middle') + if (result != null) return result.done + } + }) +} + +export const debugTimer = d.enabled + ? debugTimerImpl + : (f: () => PromiseLike) => f() diff --git a/packages/page/src/tree.ts b/packages/page/src/tree.ts index af225f3..f84e953 100644 --- a/packages/page/src/tree.ts +++ b/packages/page/src/tree.ts @@ -1,6 +1,7 @@ import { AsyncLocalStorage } from 'node:async_hooks' import type * as minissg from '../../vite-plugin-minissg/src/module' import { type Awaitable, raise } from '../../vite-plugin-minissg/src/util' +import { debugTimer } from './debug' import { isAbsURL, defineProperty, createObject, objectAssign } from './util' import { type Delay, delay } from './delay' import { Memo } from './memo' @@ -329,7 +330,17 @@ class TreeNodeImpl { const render1 = async (): R => await this.module.render(mod) const render2 = async (): R => await currentNode.run(this, render1) const render3 = async (): R => await this.memo.run(render2) - return { default: delay(render3) } + const render = debugTimer(render3, (debug, dt, when) => { + const path = `/${c.moduleName.path}` + if (when === 'start') { + debug('start rendering %s', path) + } else if (when === 'middle') { + debug('now rendering %s (+%s sec)', path, (dt / 1000).toFixed(3)) + } else { + debug('rendering %s finished (%s sec)', path, (dt / 1000).toFixed(3)) + } + }) + return { default: delay(render) } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d6ab764..9be5eba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -220,9 +220,16 @@ importers: packages/page: dependencies: + debug: + specifier: ^4.3.4 + version: 4.3.4 vite-plugin-minissg: specifier: workspace:^ version: link:../vite-plugin-minissg + devDependencies: + '@types/debug': + specifier: ^4.1.12 + version: 4.1.12 packages/render-preact: dependencies: