@hamur/adapter-preact 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +35 -2
- package/dist/chunks/browser-index-89d71ade.js +118 -0
- package/dist/chunks/browser-index-ezte15pp.js +67 -0
- package/dist/chunks/browser-index-hsy7tnbg.js +21 -0
- package/dist/chunks/browser-index-k3m2axbw.js +274 -0
- package/dist/src/adapter.js +456 -0
- package/dist/src/error-page.js +391 -0
- package/dist/src/index.js +1506 -0
- package/dist/src/keepalive.js +6 -0
- package/dist/src/runtime/client.js +96 -0
- package/dist/src/runtime/island.js +33 -0
- package/dist/src/runtime/server.js +95 -0
- package/dist/types/src/activation.d.ts +15 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +13 -0
- package/dist/types/src/client-component.d.ts +11 -0
- package/dist/types/src/client-fallback.d.ts +2 -0
- package/dist/types/src/context.d.ts +81 -0
- package/dist/types/src/error-page.d.ts +1 -0
- package/dist/types/src/head.d.ts +5 -0
- package/dist/types/src/index.d.ts +429 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/island.d.ts +19 -0
- package/dist/types/src/jsx-locator.d.ts +6 -0
- package/dist/types/src/keepalive.d.ts +26 -0
- package/dist/types/src/lazy-hydration.d.ts +12 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +12 -0
- package/dist/types/src/plugin.d.ts +36 -0
- package/dist/types/src/route-boundary.d.ts +41 -0
- package/dist/types/src/runtime/client.d.ts +10 -0
- package/dist/types/src/runtime/hmr.d.ts +31 -0
- package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
- package/dist/types/src/runtime/hydration.d.ts +8 -0
- package/dist/types/src/runtime/island.d.ts +8 -0
- package/dist/types/src/runtime/server.d.ts +11 -0
- package/dist/types/src/server-island.d.ts +10 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +71 -4
- package/src/activation.ts +51 -0
- package/src/adapter.ts +35 -0
- package/src/async-data.ts +65 -0
- package/src/client-component.ts +23 -0
- package/src/client-fallback.ts +2 -0
- package/src/context.ts +372 -0
- package/src/env.d.ts +3 -0
- package/src/error-page.ts +1 -0
- package/src/head.ts +34 -0
- package/src/index.ts +1868 -0
- package/src/island-html.ts +58 -0
- package/src/island.ts +56 -0
- package/src/jsx-locator.ts +199 -0
- package/src/keepalive.ts +100 -0
- package/src/lazy-hydration.ts +101 -0
- package/src/markdown-page.ts +18 -0
- package/src/markdown.ts +58 -0
- package/src/plugin.ts +231 -0
- package/src/route-boundary.ts +51 -0
- package/src/runtime/client.ts +85 -0
- package/src/runtime/hmr.ts +141 -0
- package/src/runtime/hydration-dev.ts +8 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +37 -0
- package/src/runtime/server.ts +102 -0
- package/src/server-island.ts +35 -0
- package/src/svg.ts +27 -0
- package/src/webmcp.d.ts +18 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ComponentType, FunctionComponent } from "preact";
|
|
2
|
+
interface Slot {
|
|
3
|
+
impl: ComponentType<any>;
|
|
4
|
+
listeners: Set<() => void>;
|
|
5
|
+
wrapper: FunctionComponent<any>;
|
|
6
|
+
guard: FunctionComponent<any>;
|
|
7
|
+
inner: FunctionComponent<any>;
|
|
8
|
+
}
|
|
9
|
+
export interface HotModule {
|
|
10
|
+
readonly id: string;
|
|
11
|
+
signature: string;
|
|
12
|
+
version: string;
|
|
13
|
+
/** The key of every guard: bumped to remount the module's components. */
|
|
14
|
+
generation: number;
|
|
15
|
+
/** Set from a swap until the renders it caused are done: a render error below the wrapper then remounts, then reloads. */
|
|
16
|
+
swapped: boolean;
|
|
17
|
+
retried: boolean;
|
|
18
|
+
/** The namespaces of the modules it imports: a new one means that module ran again. */
|
|
19
|
+
dependencies: readonly unknown[];
|
|
20
|
+
next?: {
|
|
21
|
+
signature: string;
|
|
22
|
+
version: string;
|
|
23
|
+
dependencies: readonly unknown[];
|
|
24
|
+
};
|
|
25
|
+
slots: Map<string, Slot>;
|
|
26
|
+
component<P>(name: string, impl: ComponentType<P>): ComponentType<P>;
|
|
27
|
+
commit(): void;
|
|
28
|
+
}
|
|
29
|
+
/** One per module version, `previous` is what the module's last version returned. */
|
|
30
|
+
export declare function hotModule(previous: HotModule | undefined, id: string, signature: string, version: string, dependencies?: readonly unknown[]): HotModule;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** What `hydration-dev.ts` hands the client runtime in `hamur dev`; a production client never sets it. */
|
|
2
|
+
export interface HydrationWatch {
|
|
3
|
+
/** The root hydrated: its DOM is compared with the server markup saved before. */
|
|
4
|
+
done(): void;
|
|
5
|
+
}
|
|
6
|
+
export declare const devHydration: {
|
|
7
|
+
watch?: (root: Element) => HydrationWatch;
|
|
8
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IslandContext } from "hamur/adapter";
|
|
2
|
+
/** Hydrates the island's server HTML in `el` as a root of its own. */
|
|
3
|
+
export declare function hydrateIsland(el: Element, component: unknown, props: Record<string, unknown>, ctx: IslandContext): void;
|
|
4
|
+
/** Renders the island into `el` in place of the fallback it holds. */
|
|
5
|
+
export declare function mountIsland(el: Element, component: unknown, props: Record<string, unknown>, ctx: IslandContext): void;
|
|
6
|
+
export declare function unmountIsland(el: Element): void;
|
|
7
|
+
/** Renders into the island's root again with new props, which keeps its state. */
|
|
8
|
+
export declare function updateIsland(el: Element, props: Record<string, unknown>): void;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ComponentChild, ComponentType } from "preact";
|
|
2
|
+
import type { ComponentRenderContext, RenderContext, RenderResult } from "hamur/adapter";
|
|
3
|
+
export declare function render(ctx: RenderContext): Promise<RenderResult>;
|
|
4
|
+
/** `component` as a root of its own, with each island inside it rendered in place. */
|
|
5
|
+
export declare function renderComponent(component: unknown, props: Record<string, unknown>, ctx: ComponentRenderContext): Promise<{
|
|
6
|
+
html: string;
|
|
7
|
+
}>;
|
|
8
|
+
/** The server side of a client-only component renders nothing. */
|
|
9
|
+
export declare function clientComponent(): () => ComponentChild;
|
|
10
|
+
/** The server side of a server-only component: the component itself inside `hamur-component`, its props checked as the client sends them. */
|
|
11
|
+
export declare function serverComponent(name: string, component: ComponentType<Record<string, unknown>>): (props: Record<string, unknown>) => ComponentChild;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ComponentChild } from "preact";
|
|
2
|
+
export interface ServerIslandProps<P extends Record<string, unknown> = Record<string, unknown>> {
|
|
3
|
+
/** The name of a component under `app/server-islands/`. */
|
|
4
|
+
name: string;
|
|
5
|
+
props?: P;
|
|
6
|
+
/** Rendered with the page; replaced by the island's HTML once the browser has it. */
|
|
7
|
+
fallback?: ComponentChild;
|
|
8
|
+
}
|
|
9
|
+
/** A component rendered on the server in its own request; the page holds its fallback, the core swaps the answer in, and Preact never renders or hydrates it. */
|
|
10
|
+
export declare function ServerIsland<P extends Record<string, unknown> = Record<string, unknown>>({ name, props, fallback, }: ServerIslandProps<P>): ComponentChild;
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hamur/adapter-preact",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/productdevbook/hamur.git",
|
|
9
|
+
"directory": "packages/adapter-preact"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
14
|
+
"bun": "./src/index.ts",
|
|
15
|
+
"hamur-source": "./src/index.ts",
|
|
16
|
+
"node": "./dist/src/index.js",
|
|
17
|
+
"default": "./src/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"./adapter": {
|
|
20
|
+
"types": "./dist/types/src/adapter.d.ts",
|
|
21
|
+
"bun": "./src/adapter.ts",
|
|
22
|
+
"hamur-source": "./src/adapter.ts",
|
|
23
|
+
"node": "./dist/src/adapter.js",
|
|
24
|
+
"default": "./src/adapter.ts"
|
|
25
|
+
},
|
|
26
|
+
"./error-page": {
|
|
27
|
+
"types": "./dist/types/src/error-page.d.ts",
|
|
28
|
+
"bun": "./src/error-page.ts",
|
|
29
|
+
"hamur-source": "./src/error-page.ts",
|
|
30
|
+
"node": "./dist/src/error-page.js",
|
|
31
|
+
"default": "./src/error-page.ts"
|
|
32
|
+
},
|
|
33
|
+
"./runtime/server": {
|
|
34
|
+
"types": "./dist/types/src/runtime/server.d.ts",
|
|
35
|
+
"bun": "./src/runtime/server.ts",
|
|
36
|
+
"hamur-source": "./src/runtime/server.ts",
|
|
37
|
+
"node": "./dist/src/runtime/server.js",
|
|
38
|
+
"default": "./src/runtime/server.ts"
|
|
39
|
+
},
|
|
40
|
+
"./runtime/client": {
|
|
41
|
+
"types": "./dist/types/src/runtime/client.d.ts",
|
|
42
|
+
"bun": "./src/runtime/client.ts",
|
|
43
|
+
"hamur-source": "./src/runtime/client.ts",
|
|
44
|
+
"node": "./dist/src/runtime/client.js",
|
|
45
|
+
"default": "./src/runtime/client.ts"
|
|
46
|
+
},
|
|
47
|
+
"./runtime/keepalive": {
|
|
48
|
+
"types": "./dist/types/src/keepalive.d.ts",
|
|
49
|
+
"bun": "./src/keepalive.ts",
|
|
50
|
+
"hamur-source": "./src/keepalive.ts",
|
|
51
|
+
"node": "./dist/src/keepalive.js",
|
|
52
|
+
"default": "./src/keepalive.ts"
|
|
53
|
+
},
|
|
54
|
+
"./runtime/island": {
|
|
55
|
+
"types": "./dist/types/src/runtime/island.d.ts",
|
|
56
|
+
"bun": "./src/runtime/island.ts",
|
|
57
|
+
"hamur-source": "./src/runtime/island.ts",
|
|
58
|
+
"node": "./dist/src/runtime/island.js",
|
|
59
|
+
"default": "./src/runtime/island.ts"
|
|
60
|
+
},
|
|
61
|
+
"./package.json": "./package.json"
|
|
62
|
+
},
|
|
63
|
+
"files": [
|
|
64
|
+
"dist",
|
|
65
|
+
"src"
|
|
66
|
+
],
|
|
67
|
+
"dependencies": {},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"preact": "^11.0.2",
|
|
70
|
+
"preact-render-to-string": "^6.8.1",
|
|
71
|
+
"hamur": "^0.0.2"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** The shown state of one keep-alive wrapper and the `onPageActivated`/`onPageDeactivated` functions under it. */
|
|
2
|
+
export interface Activation {
|
|
3
|
+
hidden: boolean
|
|
4
|
+
readonly parent: Activation | undefined
|
|
5
|
+
readonly children: Set<Activation>
|
|
6
|
+
readonly on: Set<() => void>
|
|
7
|
+
readonly off: Set<() => void>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function activation(parent: Activation | undefined, hidden: boolean): Activation {
|
|
11
|
+
const own: Activation = { hidden, parent, children: new Set(), on: new Set(), off: new Set() }
|
|
12
|
+
parent?.children.add(own)
|
|
13
|
+
return own
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function detach(own: Activation): void {
|
|
17
|
+
own.parent?.children.delete(own)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Whether a page under `own` is on screen: no wrapper above it is hidden. */
|
|
21
|
+
export function shown(own: Activation | undefined): boolean {
|
|
22
|
+
return !own || (!own.hidden && shown(own.parent))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let queue: Array<{ shown: boolean; fn: () => void }> | undefined
|
|
26
|
+
|
|
27
|
+
/** Runs `fn` once the commit is done; within one commit every deactivation runs before any activation, so a hide comes before the show it makes room for. */
|
|
28
|
+
export function later(isShown: boolean, fn: () => void): void {
|
|
29
|
+
if (!queue) {
|
|
30
|
+
queue = []
|
|
31
|
+
queueMicrotask(() => {
|
|
32
|
+
const run = queue!
|
|
33
|
+
queue = undefined
|
|
34
|
+
for (const item of run) if (!item.shown) item.fn()
|
|
35
|
+
for (const item of run) if (item.shown) item.fn()
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
queue.push({ shown: isShown, fn })
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function fire(own: Activation, isShown: boolean): void {
|
|
42
|
+
for (const fn of isShown ? own.on : own.off) later(isShown, fn)
|
|
43
|
+
for (const child of own.children) if (!child.hidden) fire(child, isShown)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function setHidden(own: Activation, hidden: boolean): void {
|
|
47
|
+
if (own.hidden === hidden) return
|
|
48
|
+
const before = shown(own)
|
|
49
|
+
own.hidden = hidden
|
|
50
|
+
if (shown(own) !== before) fire(own, !before)
|
|
51
|
+
}
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ADAPTER_CONTRACT, defineAdapter } from "hamur/adapter"
|
|
2
|
+
import { clientComponentModule, serverComponentModule } from "hamur/adapter/build"
|
|
3
|
+
import {
|
|
4
|
+
preactDebugPlugin,
|
|
5
|
+
preactHydrationPlugin,
|
|
6
|
+
preactLocatorPlugin,
|
|
7
|
+
preactPlugin,
|
|
8
|
+
preactProductionPlugin,
|
|
9
|
+
} from "./plugin.ts"
|
|
10
|
+
import { svgComponent } from "./svg.ts"
|
|
11
|
+
import { markdownComponent } from "./markdown-page.ts"
|
|
12
|
+
|
|
13
|
+
export default defineAdapter({
|
|
14
|
+
name: "preact",
|
|
15
|
+
contract: ADAPTER_CONTRACT,
|
|
16
|
+
extensions: [".tsx", ".jsx"],
|
|
17
|
+
clientEntry: "@hamur/adapter-preact/runtime/client",
|
|
18
|
+
serverEntry: "@hamur/adapter-preact/runtime/server",
|
|
19
|
+
keepAliveOutlet: "@hamur/adapter-preact/runtime/keepalive",
|
|
20
|
+
islandEntry: "@hamur/adapter-preact/runtime/island",
|
|
21
|
+
transforms: (ctx) =>
|
|
22
|
+
ctx.dev
|
|
23
|
+
? [
|
|
24
|
+
preactLocatorPlugin(ctx),
|
|
25
|
+
...(ctx.target === "client" ? [preactPlugin(ctx), preactHydrationPlugin(), preactDebugPlugin()] : []),
|
|
26
|
+
]
|
|
27
|
+
: [preactProductionPlugin(ctx)],
|
|
28
|
+
jsx: { runtime: "automatic", importSource: "preact" },
|
|
29
|
+
errorPage: "@hamur/adapter-preact/error-page",
|
|
30
|
+
peers: ["preact", "preact-render-to-string"],
|
|
31
|
+
svgComponent,
|
|
32
|
+
markdownComponent,
|
|
33
|
+
serverComponent: { module: (component) => serverComponentModule(component, "@hamur/adapter-preact/runtime") },
|
|
34
|
+
clientComponent: { module: (component) => clientComponentModule(component, "@hamur/adapter-preact/runtime") },
|
|
35
|
+
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { AppContext, AsyncDataOptions, FetchOptions } from "hamur/app"
|
|
2
|
+
import { isPreviewMode } from "hamur/adapter"
|
|
3
|
+
import type { AsyncDataHandler, FetchRequest, GetCachedData, GetCachedDataCause, HandlerReference } from "hamur/adapter"
|
|
4
|
+
|
|
5
|
+
export const valueOf = <T>(value: T | (() => T)): T => (typeof value === "function" ? (value as () => T)() : value)
|
|
6
|
+
|
|
7
|
+
// A function is never a body to send, so a function `query` or `body` is a getter.
|
|
8
|
+
export function requestOf({ query, body, ...rest }: FetchRequest): FetchOptions {
|
|
9
|
+
const request: FetchOptions = rest
|
|
10
|
+
const resolvedQuery = valueOf(query)
|
|
11
|
+
const resolvedBody = valueOf(body)
|
|
12
|
+
if (resolvedQuery !== undefined) request.query = resolvedQuery
|
|
13
|
+
if (resolvedBody !== undefined) request.body = resolvedBody
|
|
14
|
+
return request
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The restored payload entry's data while the app hydrates, else `undefined`. */
|
|
18
|
+
export function defaultGetCachedData<T>(key: string, app: AppContext): T | undefined {
|
|
19
|
+
if (!app.asyncData.hydrating || isPreviewMode(app)) return undefined
|
|
20
|
+
const entry = app.asyncData.get<T>(key)
|
|
21
|
+
return entry?.status === "success" ? entry.data : undefined
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** `refresh()`, `execute()` and a `watch` change: ask `getCachedData` with their cause, run `handler` when it has nothing. */
|
|
25
|
+
export function refreshCached<T, R>(
|
|
26
|
+
app: AppContext,
|
|
27
|
+
key: string,
|
|
28
|
+
handler: AsyncDataHandler<R>,
|
|
29
|
+
options: AsyncDataOptions<T, R>,
|
|
30
|
+
getCachedData: GetCachedData<T>,
|
|
31
|
+
cause: GetCachedDataCause,
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
const cached = getCachedData(key, app, { cause })
|
|
34
|
+
if (cached !== undefined) {
|
|
35
|
+
app.asyncData.fetch<T, R>(key, handler, { ...options, cached })
|
|
36
|
+
return Promise.resolve()
|
|
37
|
+
}
|
|
38
|
+
if (!app.asyncData.has(key)) app.asyncData.fetch<T, R>(key, handler, { ...options, immediate: false })
|
|
39
|
+
return app.asyncData.refresh(key, handler)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const kept = new WeakMap<object, Set<string>>()
|
|
43
|
+
|
|
44
|
+
// Nuxt purges no entry whose caller reads it back through its own getCachedData: the entry is that cache.
|
|
45
|
+
export function keepCached<T>(app: AppContext, key: string, getCachedData: GetCachedData<T>): void {
|
|
46
|
+
if (getCachedData === defaultGetCachedData) return
|
|
47
|
+
let keys = kept.get(app.asyncData)
|
|
48
|
+
if (!keys) kept.set(app.asyncData, (keys = new Set()))
|
|
49
|
+
if (keys.has(key)) return
|
|
50
|
+
keys.add(key)
|
|
51
|
+
app.asyncData.subscribe(key, () => {})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The run's signal, combined with a `signal` the caller gave to `useFetch`. */
|
|
55
|
+
export const signalOf = (own: AbortSignal | undefined, run: AbortSignal): AbortSignal =>
|
|
56
|
+
own ? AbortSignal.any([own, run]) : run
|
|
57
|
+
|
|
58
|
+
/** The call of `useAsyncData(fn, input)`: in process with the request's event on the server, through the stub in the browser. */
|
|
59
|
+
export const callHandler =
|
|
60
|
+
<R>(app: AppContext, fn: HandlerReference, input: unknown): AsyncDataHandler<R> =>
|
|
61
|
+
({ signal }) =>
|
|
62
|
+
(fn as (input: unknown, init: { signal: AbortSignal; event: AppContext["event"] }) => Promise<R>)(input, {
|
|
63
|
+
signal,
|
|
64
|
+
event: app.event,
|
|
65
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createContext, createElement } from "preact"
|
|
2
|
+
import type { ComponentChild, ComponentChildren, ComponentType } from "preact"
|
|
3
|
+
import { Suspense, lazy } from "preact/compat"
|
|
4
|
+
import { useContext, useEffect, useState } from "preact/hooks"
|
|
5
|
+
|
|
6
|
+
/** Set by `ClientOnly` around its children; a client-only component below shows its `fallback` while its chunk loads. */
|
|
7
|
+
export const ClientOnlyContext = createContext<{ fallback: ComponentChildren } | null>(null)
|
|
8
|
+
|
|
9
|
+
type Props = Record<string, unknown>
|
|
10
|
+
|
|
11
|
+
/** The client side of a client-only component: nothing in the hydration render, the file's chunk once mounted. */
|
|
12
|
+
export function clientComponent(
|
|
13
|
+
load: () => Promise<{ default: ComponentType<Props> }>,
|
|
14
|
+
): (props: Props) => ComponentChild {
|
|
15
|
+
const Loaded = lazy(load) as ComponentType<Props>
|
|
16
|
+
return function ClientComponent(props) {
|
|
17
|
+
const scope = useContext(ClientOnlyContext)
|
|
18
|
+
const [hydrated, setHydrated] = useState(scope !== null)
|
|
19
|
+
useEffect(() => setHydrated(true), [])
|
|
20
|
+
if (!hydrated) return null
|
|
21
|
+
return createElement(Suspense, { fallback: scope?.fallback ?? null }, createElement(Loaded, props))
|
|
22
|
+
}
|
|
23
|
+
}
|