@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.
Files changed (106) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +31 -2
  3. package/dist/chunks/browser-index-324qgh1c.js +190 -0
  4. package/dist/chunks/browser-index-evqp5a71.js +64 -0
  5. package/dist/chunks/browser-index-xc6081e3.js +9 -0
  6. package/dist/src/adapter.js +480 -0
  7. package/dist/src/index.js +1161 -0
  8. package/dist/src/runtime/client.js +65 -0
  9. package/dist/src/runtime/island.js +72 -0
  10. package/dist/src/runtime/server.js +142 -0
  11. package/dist/types/src/activation.d.ts +15 -0
  12. package/dist/types/src/adapter.d.ts +2 -0
  13. package/dist/types/src/async-data.d.ts +21 -0
  14. package/dist/types/src/boundary-error.d.ts +9 -0
  15. package/dist/types/src/client-only.d.ts +6 -0
  16. package/dist/types/src/context.d.ts +87 -0
  17. package/dist/types/src/deferred.svelte.d.ts +6 -0
  18. package/dist/types/src/effect.svelte.d.ts +2 -0
  19. package/dist/types/src/hydrating.d.ts +4 -0
  20. package/dist/types/src/index.d.ts +270 -0
  21. package/dist/types/src/island-html.d.ts +13 -0
  22. package/dist/types/src/keepalive.d.ts +11 -0
  23. package/dist/types/src/lazy-hydration.d.ts +25 -0
  24. package/dist/types/src/markdown-page.d.ts +9 -0
  25. package/dist/types/src/plugin.d.ts +52 -0
  26. package/dist/types/src/runtime/client.d.ts +12 -0
  27. package/dist/types/src/runtime/hmr.d.ts +28 -0
  28. package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
  29. package/dist/types/src/runtime/hydration.d.ts +8 -0
  30. package/dist/types/src/runtime/island.d.ts +8 -0
  31. package/dist/types/src/runtime/server.d.ts +12 -0
  32. package/dist/types/src/server-component.d.ts +3 -0
  33. package/dist/types/src/store.d.ts +7 -0
  34. package/dist/types/src/svg.d.ts +10 -0
  35. package/package.json +43 -4
  36. package/src/Announcer.svelte +26 -0
  37. package/src/Await.svelte +43 -0
  38. package/src/AwaitAsync.svelte +42 -0
  39. package/src/BodyAttrs.svelte +7 -0
  40. package/src/Caught.svelte +41 -0
  41. package/src/ClientComponent.svelte +35 -0
  42. package/src/ClientFallback.svelte +46 -0
  43. package/src/ClientOnly.svelte +22 -0
  44. package/src/DefaultErrorPage.svelte +15 -0
  45. package/src/DevOnly.svelte +12 -0
  46. package/src/ErrorBoundary.svelte +14 -0
  47. package/src/FallbackEffect.svelte +9 -0
  48. package/src/Form.svelte +96 -0
  49. package/src/HeadLink.svelte +7 -0
  50. package/src/HeadMeta.svelte +7 -0
  51. package/src/HeadScript.svelte +7 -0
  52. package/src/HeadStyle.svelte +7 -0
  53. package/src/HeadTitle.svelte +6 -0
  54. package/src/HtmlAttrs.svelte +7 -0
  55. package/src/HttpHeader.svelte +7 -0
  56. package/src/HttpStatusCode.svelte +7 -0
  57. package/src/Island.svelte +59 -0
  58. package/src/IslandChildren.svelte +7 -0
  59. package/src/KeptOutlet.svelte +46 -0
  60. package/src/KeptPage.svelte +55 -0
  61. package/src/LazyHydration.svelte +54 -0
  62. package/src/Link.svelte +209 -0
  63. package/src/LoadingIndicator.svelte +18 -0
  64. package/src/MarkdownNodes.svelte +36 -0
  65. package/src/MarkdownRenderer.svelte +20 -0
  66. package/src/MarkdownSlots.svelte +33 -0
  67. package/src/Navigate.svelte +10 -0
  68. package/src/Nest.svelte +83 -0
  69. package/src/Outlet.svelte +31 -0
  70. package/src/Rethrow.svelte +7 -0
  71. package/src/Root.svelte +29 -0
  72. package/src/RouteAnnouncer.svelte +47 -0
  73. package/src/Scoped.svelte +11 -0
  74. package/src/ServerComponent.svelte +19 -0
  75. package/src/ServerComponentClient.svelte +64 -0
  76. package/src/ServerIsland.svelte +26 -0
  77. package/src/Slotted.svelte +11 -0
  78. package/src/Thrown.svelte +9 -0
  79. package/src/Time.svelte +23 -0
  80. package/src/activation.ts +51 -0
  81. package/src/adapter.ts +24 -0
  82. package/src/async-data.ts +105 -0
  83. package/src/boundary-error.ts +77 -0
  84. package/src/client-only.ts +8 -0
  85. package/src/context.ts +227 -0
  86. package/src/deferred.svelte.ts +33 -0
  87. package/src/effect.svelte.ts +7 -0
  88. package/src/env.d.ts +3 -0
  89. package/src/hydrating.ts +18 -0
  90. package/src/index.ts +1337 -0
  91. package/src/island-html.ts +58 -0
  92. package/src/keepalive.ts +12 -0
  93. package/src/lazy-hydration.ts +89 -0
  94. package/src/markdown-page.ts +17 -0
  95. package/src/plugin.ts +558 -0
  96. package/src/runtime/client.ts +61 -0
  97. package/src/runtime/hmr.ts +177 -0
  98. package/src/runtime/hydration-dev.ts +22 -0
  99. package/src/runtime/hydration.ts +7 -0
  100. package/src/runtime/island.ts +94 -0
  101. package/src/runtime/server.ts +87 -0
  102. package/src/server-component.ts +13 -0
  103. package/src/store.ts +31 -0
  104. package/src/svelte.d.ts +5 -0
  105. package/src/svg.ts +30 -0
  106. 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
+ }
@@ -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 }