@hamur/adapter-svelte 0.0.0-stage → 0.0.1
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 +31 -2
- package/dist/chunks/browser-index-324qgh1c.js +190 -0
- package/dist/chunks/browser-index-evqp5a71.js +64 -0
- package/dist/chunks/browser-index-xc6081e3.js +9 -0
- package/dist/src/adapter.js +480 -0
- package/dist/src/index.js +1161 -0
- package/dist/src/runtime/client.js +65 -0
- package/dist/src/runtime/island.js +72 -0
- package/dist/src/runtime/server.js +142 -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 +21 -0
- package/dist/types/src/boundary-error.d.ts +9 -0
- package/dist/types/src/client-only.d.ts +6 -0
- package/dist/types/src/context.d.ts +87 -0
- package/dist/types/src/deferred.svelte.d.ts +6 -0
- package/dist/types/src/effect.svelte.d.ts +2 -0
- package/dist/types/src/hydrating.d.ts +4 -0
- package/dist/types/src/index.d.ts +270 -0
- package/dist/types/src/island-html.d.ts +13 -0
- package/dist/types/src/keepalive.d.ts +11 -0
- package/dist/types/src/lazy-hydration.d.ts +25 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/plugin.d.ts +52 -0
- package/dist/types/src/runtime/client.d.ts +12 -0
- package/dist/types/src/runtime/hmr.d.ts +28 -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 +12 -0
- package/dist/types/src/server-component.d.ts +3 -0
- package/dist/types/src/store.d.ts +7 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +43 -4
- package/src/Announcer.svelte +26 -0
- package/src/Await.svelte +43 -0
- package/src/AwaitAsync.svelte +42 -0
- package/src/BodyAttrs.svelte +7 -0
- package/src/Caught.svelte +41 -0
- package/src/ClientComponent.svelte +35 -0
- package/src/ClientFallback.svelte +46 -0
- package/src/ClientOnly.svelte +22 -0
- package/src/DefaultErrorPage.svelte +15 -0
- package/src/DevOnly.svelte +12 -0
- package/src/ErrorBoundary.svelte +14 -0
- package/src/FallbackEffect.svelte +9 -0
- package/src/Form.svelte +96 -0
- package/src/HeadLink.svelte +7 -0
- package/src/HeadMeta.svelte +7 -0
- package/src/HeadScript.svelte +7 -0
- package/src/HeadStyle.svelte +7 -0
- package/src/HeadTitle.svelte +6 -0
- package/src/HtmlAttrs.svelte +7 -0
- package/src/HttpHeader.svelte +7 -0
- package/src/HttpStatusCode.svelte +7 -0
- package/src/Island.svelte +59 -0
- package/src/IslandChildren.svelte +7 -0
- package/src/KeptOutlet.svelte +46 -0
- package/src/KeptPage.svelte +55 -0
- package/src/LazyHydration.svelte +54 -0
- package/src/Link.svelte +209 -0
- package/src/LoadingIndicator.svelte +18 -0
- package/src/MarkdownNodes.svelte +36 -0
- package/src/MarkdownRenderer.svelte +20 -0
- package/src/MarkdownSlots.svelte +33 -0
- package/src/Navigate.svelte +10 -0
- package/src/Nest.svelte +83 -0
- package/src/Outlet.svelte +31 -0
- package/src/Rethrow.svelte +7 -0
- package/src/Root.svelte +29 -0
- package/src/RouteAnnouncer.svelte +47 -0
- package/src/Scoped.svelte +11 -0
- package/src/ServerComponent.svelte +19 -0
- package/src/ServerComponentClient.svelte +64 -0
- package/src/ServerIsland.svelte +26 -0
- package/src/Slotted.svelte +11 -0
- package/src/Thrown.svelte +9 -0
- package/src/Time.svelte +23 -0
- package/src/activation.ts +51 -0
- package/src/adapter.ts +24 -0
- package/src/async-data.ts +105 -0
- package/src/boundary-error.ts +77 -0
- package/src/client-only.ts +8 -0
- package/src/context.ts +227 -0
- package/src/deferred.svelte.ts +33 -0
- package/src/effect.svelte.ts +7 -0
- package/src/env.d.ts +3 -0
- package/src/hydrating.ts +18 -0
- package/src/index.ts +1337 -0
- package/src/island-html.ts +58 -0
- package/src/keepalive.ts +12 -0
- package/src/lazy-hydration.ts +89 -0
- package/src/markdown-page.ts +17 -0
- package/src/plugin.ts +558 -0
- package/src/runtime/client.ts +61 -0
- package/src/runtime/hmr.ts +177 -0
- package/src/runtime/hydration-dev.ts +22 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +94 -0
- package/src/runtime/server.ts +87 -0
- package/src/server-component.ts +13 -0
- package/src/store.ts +31 -0
- package/src/svelte.d.ts +5 -0
- package/src/svg.ts +30 -0
- package/src/webmcp.d.ts +18 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { markWaiting } from "hamur/adapter"
|
|
2
|
+
import type { AppContext } from "hamur/app"
|
|
3
|
+
|
|
4
|
+
type Outcome = { value: unknown } | { error: unknown }
|
|
5
|
+
|
|
6
|
+
const outcomes = new WeakMap<Promise<unknown>, Outcome>()
|
|
7
|
+
let current: Promise<void>[] | undefined
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The HTML of an island whose root has finished rendering (or the attributes of a server island), or `undefined` while it renders, recorded in `pending`
|
|
11
|
+
* so the render is done again once it has settled. A failed island throws where its `Island` renders, so the nearest
|
|
12
|
+
* route boundary shows the error. An island's HTML is always rendered in its place, never spliced into the markup.
|
|
13
|
+
*/
|
|
14
|
+
export function settledIsland<T = string>(
|
|
15
|
+
promise: Promise<T>,
|
|
16
|
+
pending: Promise<void>[] | undefined = current,
|
|
17
|
+
): T | undefined {
|
|
18
|
+
const outcome = outcomes.get(promise)
|
|
19
|
+
if (outcome) {
|
|
20
|
+
if ("error" in outcome) throw outcome.error
|
|
21
|
+
return outcome.value as T
|
|
22
|
+
}
|
|
23
|
+
pending?.push(
|
|
24
|
+
promise.then(
|
|
25
|
+
(value) => void outcomes.set(promise, { value }),
|
|
26
|
+
(error: unknown) => void outcomes.set(promise, { error }),
|
|
27
|
+
),
|
|
28
|
+
)
|
|
29
|
+
return undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Runs a synchronous render whose `Island`s record the islands still rendering in `pending`. */
|
|
33
|
+
export function collectingIslands<T>(pending: Promise<void>[], render: () => T): T {
|
|
34
|
+
const outer = current
|
|
35
|
+
current = pending
|
|
36
|
+
try {
|
|
37
|
+
return render()
|
|
38
|
+
} finally {
|
|
39
|
+
current = outer
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A pass of `renderInPasses` that met islands still rendering waits for them and is dropped, so the next pass renders them in place. */
|
|
44
|
+
export async function islandsSettled(app: AppContext, pending: Promise<void>[]): Promise<void> {
|
|
45
|
+
if (!pending.length) return
|
|
46
|
+
await Promise.all(pending)
|
|
47
|
+
markWaiting(app.asyncData)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** An island's own root, rendered again until every island inside it has settled. */
|
|
51
|
+
export async function renderSettled<T>(render: (pending: Promise<void>[]) => T | Promise<T>): Promise<T> {
|
|
52
|
+
for (;;) {
|
|
53
|
+
const pending: Promise<void>[] = []
|
|
54
|
+
const result = await collectingIslands(pending, () => render(pending))
|
|
55
|
+
if (!pending.length) return result
|
|
56
|
+
await Promise.all(pending)
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/keepalive.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { activation, detach, setHidden } from "./activation.ts"
|
|
2
|
+
import type { routeValue } from "./context.ts"
|
|
3
|
+
|
|
4
|
+
/** What `KeptOutlet.svelte` takes from the adapter's base as its `base` prop: it imports nothing from the base at run time, so loading it splits no chunk of the base. */
|
|
5
|
+
export interface KeepAliveBase {
|
|
6
|
+
APP_KEY: symbol
|
|
7
|
+
ACTIVATION_KEY: symbol
|
|
8
|
+
routeValue: typeof routeValue
|
|
9
|
+
activation: typeof activation
|
|
10
|
+
detach: typeof detach
|
|
11
|
+
setHidden: typeof setHidden
|
|
12
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Component } from "svelte"
|
|
2
|
+
import { hydrateOnAttribute, markWaiting } from "hamur/adapter"
|
|
3
|
+
import { NEVER_HYDRATED_KEY } from "./context.ts"
|
|
4
|
+
import type { HydrateOn, HydrateOnOptions } from "hamur/adapter"
|
|
5
|
+
import type { AppContext } from "hamur/app"
|
|
6
|
+
import LazyHydration from "./LazyHydration.svelte"
|
|
7
|
+
import { keptServerHTML } from "./hydrating.ts"
|
|
8
|
+
|
|
9
|
+
type Props = Record<string, unknown>
|
|
10
|
+
|
|
11
|
+
export interface Lazy {
|
|
12
|
+
on: HydrateOn
|
|
13
|
+
/** `{ on }` on the server, empty in the browser: Svelte reads an `on` attribute or spread key there as an event handler. */
|
|
14
|
+
attributes: Record<string, string>
|
|
15
|
+
options: Omit<HydrateOnOptions, "signal">
|
|
16
|
+
load(): Promise<Component<Props>>
|
|
17
|
+
/** The component's server HTML, `""` while its module loads on the server and in a client render, a marker while the page hydrates. */
|
|
18
|
+
html(props: Props, context: Map<unknown, unknown>, app: AppContext | undefined): string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type RenderChild = (component: Component<Props>, props: Props, context: Map<unknown, unknown>) => string
|
|
22
|
+
|
|
23
|
+
const isServer = typeof document === "undefined"
|
|
24
|
+
const pending = new Set<Promise<unknown>>()
|
|
25
|
+
let renderChild: RenderChild | undefined
|
|
26
|
+
/** Set by the server entry, so `svelte/server` stays out of the browser build. */
|
|
27
|
+
export function setLazyRenderer(render: RenderChild): void {
|
|
28
|
+
renderChild = render
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The server renders synchronously, so `render` awaits the lazy modules still loading before each pass. */
|
|
32
|
+
export function lazyModules(): Promise<unknown> | undefined {
|
|
33
|
+
return pending.size ? Promise.allSettled(pending) : undefined
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Renders the component on the server and, after hydration, keeps its server DOM until `on` fires; after a client navigation it mounts at once. */
|
|
37
|
+
export function defineLazyHydrationComponent<P extends Props>(
|
|
38
|
+
on: HydrateOn,
|
|
39
|
+
loader: () => Promise<{ default: Component<P> }>,
|
|
40
|
+
options: Omit<HydrateOnOptions, "signal"> = {},
|
|
41
|
+
): Component<P & { onHydrated?: () => void }> {
|
|
42
|
+
const attribute = hydrateOnAttribute(on)
|
|
43
|
+
let resolved: Component<Props> | undefined
|
|
44
|
+
let failed: { error: unknown } | undefined
|
|
45
|
+
let loading: Promise<Component<Props>> | undefined
|
|
46
|
+
const load = (): Promise<Component<Props>> => {
|
|
47
|
+
if (loading) return loading
|
|
48
|
+
const current = (loading = loader().then(
|
|
49
|
+
(loaded) => (resolved = loaded.default as Component<Props>),
|
|
50
|
+
(error: unknown) => {
|
|
51
|
+
if (isServer) failed = { error }
|
|
52
|
+
loading = undefined
|
|
53
|
+
throw error
|
|
54
|
+
},
|
|
55
|
+
))
|
|
56
|
+
if (isServer) {
|
|
57
|
+
pending.add(current)
|
|
58
|
+
void current.then(
|
|
59
|
+
() => pending.delete(current),
|
|
60
|
+
() => pending.delete(current),
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
return current
|
|
64
|
+
}
|
|
65
|
+
if (isServer) void load().catch(() => {})
|
|
66
|
+
const lazy: Lazy = {
|
|
67
|
+
on,
|
|
68
|
+
attributes: isServer ? { on: attribute } : {},
|
|
69
|
+
options,
|
|
70
|
+
load,
|
|
71
|
+
html(props, context, app) {
|
|
72
|
+
if (!isServer) return keptServerHTML()
|
|
73
|
+
if (failed) throw failed.error
|
|
74
|
+
if (resolved && renderChild)
|
|
75
|
+
return renderChild(resolved, props, on === "never" ? new Map(context).set(NEVER_HYDRATED_KEY, true) : context)
|
|
76
|
+
if (app?.asyncData) markWaiting(app.asyncData)
|
|
77
|
+
void load().catch(() => {})
|
|
78
|
+
return ""
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
const bound = (anchor: unknown, props: object) =>
|
|
82
|
+
(LazyHydration as unknown as (anchor: unknown, props: object) => unknown)(
|
|
83
|
+
anchor,
|
|
84
|
+
new Proxy(props, {
|
|
85
|
+
get: (target, key, receiver) => (key === "lazy" ? lazy : Reflect.get(target, key, receiver)),
|
|
86
|
+
}),
|
|
87
|
+
)
|
|
88
|
+
return bound as never
|
|
89
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { markdownPageCode } from "hamur/adapter/build"
|
|
2
|
+
import type { MarkdownSource } from "hamur/adapter"
|
|
3
|
+
|
|
4
|
+
function module(source: MarkdownSource): { contents: string; extension: string } {
|
|
5
|
+
const { imports, value, props, head } = markdownPageCode(source)
|
|
6
|
+
const contents = `<script>
|
|
7
|
+
${imports}import { MarkdownRenderer, useHead } from "@hamur/adapter-svelte";
|
|
8
|
+
|
|
9
|
+
const value = ${value};${head ? `\n useHead(${head});` : ""}
|
|
10
|
+
</script>
|
|
11
|
+
|
|
12
|
+
<MarkdownRenderer {...${props}} />
|
|
13
|
+
`
|
|
14
|
+
return { contents, extension: ".svelte" }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const markdownComponent = { module }
|