diff --git a/packages/page/package.json b/packages/page/package.json index fc72b4a..13d5aac 100644 --- a/packages/page/package.json +++ b/packages/page/package.json @@ -45,6 +45,7 @@ "debug": "^4.4.0" }, "devDependencies": { - "@types/debug": "^4.1.12" + "@types/debug": "^4.1.12", + "vite-plugin-minissg": "^4.1.3" } } diff --git a/packages/page/src/minissg.ts b/packages/page/src/minissg.ts index 772f161..93930be 100644 --- a/packages/page/src/minissg.ts +++ b/packages/page/src/minissg.ts @@ -1,7 +1,7 @@ +import type { Main } from 'vite-plugin-minissg' import type { Awaitable } from '../../vite-plugin-minissg/src/util' -import type * as minissg from '../../vite-plugin-minissg/src/module' -export type MainModule = Readonly<{ main: minissg.Main }> +export type MainModule = Readonly<{ main: Main }> export type Loaded = Awaitable export const hasMinissgMain = (x: object): x is MainModule => diff --git a/packages/page/src/page.ts b/packages/page/src/page.ts index 1f17e10..bb45105 100644 --- a/packages/page/src/page.ts +++ b/packages/page/src/page.ts @@ -1,6 +1,6 @@ import { Delay } from '@minissg/async' +import type { Content } from 'vite-plugin-minissg' import type { Awaitable, Null } from '../../vite-plugin-minissg/src/util' -import type * as minissg from '../../vite-plugin-minissg/src/module' import { type Pairs, type List, iteratePairs, listItems } from './items' import type { RelPath as RelPathTy } from './filename' import { PathSteps, emptyRelPath, copyRelPath } from './filename' @@ -33,7 +33,7 @@ interface CommonArg { url?: Readonly | string | Null parsePath?: ((this: This, path: string) => Awaitable) | Null paginatePath?: ((this: This, index: number) => Awaitable) | Null - render?: ((this: This, loaded: Load) => Awaitable) | Null + render?: ((this: This, loaded: Load) => Awaitable) | Null } type Loaded = Awaitable diff --git a/packages/page/src/page_base.ts b/packages/page/src/page_base.ts index a0b98a4..0ac574c 100644 --- a/packages/page/src/page_base.ts +++ b/packages/page/src/page_base.ts @@ -1,5 +1,5 @@ import { Delay } from '@minissg/async' -import type * as minissg from '../../vite-plugin-minissg/src/module' +import type { Content, Context, Module } from 'vite-plugin-minissg' import type { Awaitable } from '../../vite-plugin-minissg/src/util' import type { RelPath } from './filename' import type { Asset } from './asset' @@ -92,18 +92,18 @@ export abstract class PageBase { return this.stem.wrap(s => this.#tree?.findByStem('/' + s) ?? new Set()) } - main(context: Readonly): Awaitable { + main(context: Readonly): Awaitable { if (this.#tree == null) throw Error('main is unavailable') return this.#tree.main(context) } - render(module: Load): Awaitable { + render(module: Load): Awaitable { const mod: unknown = module if (mod == null) return mod if (typeof mod === 'string') return mod if (mod instanceof Uint8Array) return mod if (typeof mod !== 'object') return `[${typeof mod}]` - if ('default' in mod) return mod.default as minissg.Content + if ('default' in mod) return mod.default as Content // eslint-disable-next-line @typescript-eslint/unbound-method return Reflect.apply(Object.prototype.toString, mod, []) } diff --git a/packages/page/src/tree.ts b/packages/page/src/tree.ts index 09ad718..11b8937 100644 --- a/packages/page/src/tree.ts +++ b/packages/page/src/tree.ts @@ -1,5 +1,6 @@ import { Delay, Ivar, Memo } from '@minissg/async' -import type * as minissg from '../../vite-plugin-minissg/src/module' +import type { Context, Module } from 'vite-plugin-minissg' +import type { ModuleName } from '../../vite-plugin-minissg/src/module' import { type Awaitable, raise } from '../../vite-plugin-minissg/src/util' import type { FileName, RelPath } from './filename' import { PathSteps, concatName, concatFileName, emptyRelPath } from './filename' @@ -15,9 +16,9 @@ type Laziable = X | (() => X) interface Instance { readonly self: PublicTree - readonly moduleName: minissg.ModuleName - readonly stem: minissg.ModuleName - readonly variant: minissg.ModuleName + readonly moduleName: ModuleName + readonly stem: ModuleName + readonly variant: ModuleName readonly fileName: FileName readonly parent: Instance | undefined // undefined means root readonly root: Instance | undefined // undefined means self @@ -90,7 +91,7 @@ export class Tree { }) } - toContext(): Delay { + toContext(): Delay { return this.instance.get().wrap(inst => { const parentContext = inst.parent?.self.toContext() return (parentContext ?? Delay.resolve(undefined)).wrap(parent => { @@ -249,7 +250,7 @@ export class Tree { } private findParent( - context: Readonly | undefined + context: Readonly | undefined ): Delay | undefined> { for (let c = context; c != null; c = c.parent) { const tree = this.context.getTree(c.module) @@ -258,7 +259,7 @@ export class Tree { return Delay.resolve(undefined) } - async main(context: Readonly): Promise { + async main(context: Readonly): Promise { const inst = await this.instantiate(() => this.findParent(context.parent)) if (context.moduleName.path !== inst.moduleName.path) { throw Error(`module name mismatch: ${context.moduleName.path}`) diff --git a/packages/page/src/tree_context.ts b/packages/page/src/tree_context.ts index b325604..67f64e5 100644 --- a/packages/page/src/tree_context.ts +++ b/packages/page/src/tree_context.ts @@ -1,8 +1,7 @@ import { AsyncLocalStorage } from 'node:async_hooks' import { Memo } from '@minissg/async' -import type * as minissg from '../../vite-plugin-minissg/src/module' +import type { Context } from 'vite-plugin-minissg' import type { Awaitable } from '../../vite-plugin-minissg/src/util' -//import { FileName } from './filename' import type { PageBase } from './page_base' import type { PublicTree } from './tree' @@ -29,7 +28,7 @@ export class TreeContext { private static readonly loadedStorage = new AsyncLocalStorage>() - static run(context: minissg.Context, func: () => X): X { + static run(context: Context, func: () => X): X { return Memo.inContext(context, () => { return context.loaded != null ? this.loadedStorage.run(context.loaded, func) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index beabe58..17db957 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -234,6 +234,9 @@ importers: '@types/debug': specifier: ^4.1.12 version: 4.1.12 + vite-plugin-minissg: + specifier: workspace:^ + version: link:../vite-plugin-minissg packages/render-preact: dependencies: