@hamur/adapter-vue 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 +32 -2
- package/dist/chunks/browser-index-43pdsfp6.js +69 -0
- package/dist/chunks/browser-index-d1bxd40m.js +320 -0
- package/dist/chunks/browser-index-nhft0ewv.js +161 -0
- package/dist/src/adapter.js +402 -0
- package/dist/src/index.js +1348 -0
- package/dist/src/keepalive.js +6 -0
- package/dist/src/runtime/client.js +20 -0
- package/dist/src/runtime/island.js +45 -0
- package/dist/src/runtime/server.js +238 -0
- package/dist/types/src/adapter.d.ts +2 -0
- package/dist/types/src/async-data.d.ts +14 -0
- package/dist/types/src/client-component.d.ts +12 -0
- package/dist/types/src/client-fallback.d.ts +2 -0
- package/dist/types/src/components/AwaitValue.d.ts +15 -0
- package/dist/types/src/components/ClientFallback.server.d.ts +17 -0
- package/dist/types/src/components/ErrorBoundary.server.d.ts +21 -0
- package/dist/types/src/components/error-code.d.ts +1 -0
- package/dist/types/src/context.d.ts +65 -0
- package/dist/types/src/index.d.ts +329 -0
- package/dist/types/src/island.d.ts +68 -0
- package/dist/types/src/keepalive.d.ts +22 -0
- package/dist/types/src/lazy-hydration.d.ts +10 -0
- package/dist/types/src/markdown-page.d.ts +9 -0
- package/dist/types/src/markdown.d.ts +45 -0
- package/dist/types/src/plugin.d.ts +45 -0
- package/dist/types/src/route-boundary.d.ts +35 -0
- package/dist/types/src/route-boundary.server.d.ts +5 -0
- package/dist/types/src/runtime/client.d.ts +11 -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-island.d.ts +33 -0
- package/dist/types/src/svg.d.ts +10 -0
- package/package.json +46 -4
- package/src/adapter.ts +25 -0
- package/src/async-data.ts +77 -0
- package/src/client-component.ts +39 -0
- package/src/client-fallback.ts +2 -0
- package/src/components/Await.vue +30 -0
- package/src/components/AwaitValue.ts +41 -0
- package/src/components/ClientFallback.server.ts +41 -0
- package/src/components/ClientFallback.vue +31 -0
- package/src/components/ClientOnly.vue +16 -0
- package/src/components/DefaultErrorPage.vue +19 -0
- package/src/components/DevOnly.vue +8 -0
- package/src/components/ErrorBoundary.server.ts +69 -0
- package/src/components/ErrorBoundary.vue +32 -0
- package/src/components/Form.vue +85 -0
- package/src/components/Link.vue +200 -0
- package/src/components/Navigate.vue +16 -0
- package/src/components/Outlet.vue +28 -0
- package/src/components/error-code.ts +7 -0
- package/src/context.ts +391 -0
- package/src/index.ts +1528 -0
- package/src/island.ts +53 -0
- package/src/keepalive.ts +92 -0
- package/src/lazy-hydration.ts +53 -0
- package/src/markdown-page.ts +21 -0
- package/src/markdown.ts +80 -0
- package/src/plugin.ts +398 -0
- package/src/route-boundary.server.ts +29 -0
- package/src/route-boundary.ts +48 -0
- package/src/runtime/client.ts +137 -0
- package/src/runtime/hydration-dev.ts +38 -0
- package/src/runtime/hydration.ts +7 -0
- package/src/runtime/island.ts +60 -0
- package/src/runtime/server.ts +145 -0
- package/src/server-island.ts +38 -0
- package/src/svg.ts +28 -0
- package/src/vue.d.ts +5 -0
- package/src/webmcp.d.ts +18 -0
package/src/island.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { defineComponent, h, inject, useId } from "vue"
|
|
2
|
+
import type { Component, PropType } from "vue"
|
|
3
|
+
import { ISLAND_ELEMENT, renderIsland, type IslandOn } from "hamur/adapter"
|
|
4
|
+
import { AppKey } from "./context.ts"
|
|
5
|
+
|
|
6
|
+
const isServer = typeof document === "undefined"
|
|
7
|
+
|
|
8
|
+
export interface IslandProps {
|
|
9
|
+
/** The default export of a file under `app/islands/`. */
|
|
10
|
+
component: Component
|
|
11
|
+
props?: Record<string, unknown>
|
|
12
|
+
/** The island directive: when the island hydrates, `"only"` to mount it in the browser alone, or a directive a module adds; default `"load"`. */
|
|
13
|
+
on?: IslandOn
|
|
14
|
+
/** The island's id across swaps; a swap to a page with the same id keeps its element and state. */
|
|
15
|
+
persist?: string
|
|
16
|
+
/** With `persist`, a swap keeps the island's old props. */
|
|
17
|
+
persistProps?: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* On an island page, `component` rendered as a root of its own inside `<hamur-island>`, hydrated on its own in the browser;
|
|
22
|
+
* the `fallback` slot is what the server renders under `on="only"`, and the default slot is rendered on the server only and given
|
|
23
|
+
* to the island as its default slot, inside `<hamur-slot>`.
|
|
24
|
+
*/
|
|
25
|
+
export const Island = defineComponent({
|
|
26
|
+
name: "Island",
|
|
27
|
+
props: {
|
|
28
|
+
component: { type: [Object, Function] as PropType<IslandProps["component"]>, required: true },
|
|
29
|
+
props: { type: Object as PropType<IslandProps["props"]>, default: undefined },
|
|
30
|
+
on: { type: [String, Object] as PropType<IslandProps["on"]>, default: undefined },
|
|
31
|
+
persist: { type: String, default: undefined },
|
|
32
|
+
persistProps: { type: Boolean, default: false },
|
|
33
|
+
},
|
|
34
|
+
setup(props, { slots }) {
|
|
35
|
+
const app = inject(AppKey, null)
|
|
36
|
+
if (!app) throw new Error("Island must be rendered inside a Hamur app")
|
|
37
|
+
const id = useId()
|
|
38
|
+
// In the browser an Island only runs inside an island that hydrates: Vue patches every prop of a custom element while hydrating, so the element has none, and no children to compare.
|
|
39
|
+
if (!isServer) return () => h(ISLAND_ELEMENT)
|
|
40
|
+
const children = slots.default
|
|
41
|
+
const island = renderIsland(
|
|
42
|
+
app,
|
|
43
|
+
id,
|
|
44
|
+
props.component,
|
|
45
|
+
props.props ?? {},
|
|
46
|
+
props.on,
|
|
47
|
+
children ? { component: () => children() } : undefined,
|
|
48
|
+
{ persist: props.persist, persistProps: props.persistProps },
|
|
49
|
+
)
|
|
50
|
+
if (!island.html) return () => h(ISLAND_ELEMENT, island.attributes, slots.fallback?.())
|
|
51
|
+
return island.html.then((html) => () => h(ISLAND_ELEMENT, { ...island.attributes, innerHTML: html }))
|
|
52
|
+
},
|
|
53
|
+
})
|
package/src/keepalive.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Component,
|
|
3
|
+
InjectionKey,
|
|
4
|
+
KeepAlive,
|
|
5
|
+
PropType,
|
|
6
|
+
ShallowRef,
|
|
7
|
+
VNode,
|
|
8
|
+
defineComponent,
|
|
9
|
+
h,
|
|
10
|
+
provide,
|
|
11
|
+
shallowReactive,
|
|
12
|
+
shallowRef,
|
|
13
|
+
watchEffect,
|
|
14
|
+
} from "vue"
|
|
15
|
+
import type { KeptUnit, RenderEntry, RouteValue } from "hamur/adapter"
|
|
16
|
+
import type { Source, dataOf, routeValue } from "./context.ts"
|
|
17
|
+
|
|
18
|
+
/** What the outlet takes from the adapter's base: this module imports nothing at run time, so loading it splits no chunk of the base. */
|
|
19
|
+
export interface KeepAliveBase {
|
|
20
|
+
KeepAlive: typeof KeepAlive
|
|
21
|
+
defineComponent: typeof defineComponent
|
|
22
|
+
h: typeof h
|
|
23
|
+
provide: typeof provide
|
|
24
|
+
shallowReactive: typeof shallowReactive
|
|
25
|
+
shallowRef: typeof shallowRef
|
|
26
|
+
watchEffect: typeof watchEffect
|
|
27
|
+
RouteKey: InjectionKey<RouteValue>
|
|
28
|
+
DataKey: InjectionKey<ShallowRef<Record<string, unknown>>>
|
|
29
|
+
routeValue: typeof routeValue
|
|
30
|
+
dataOf: typeof dataOf
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type KeptOutlet = (
|
|
34
|
+
item: { kept?: KeptUnit[] },
|
|
35
|
+
below: RenderEntry | undefined,
|
|
36
|
+
child: VNode | undefined,
|
|
37
|
+
source: () => Source,
|
|
38
|
+
) => VNode
|
|
39
|
+
|
|
40
|
+
const SHOWN = "\0shown"
|
|
41
|
+
const exact = (key: string) => new RegExp(`^${key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`)
|
|
42
|
+
|
|
43
|
+
/** The outlet of `item` (an entry, or the tree's top level) in a `KeepAlive` that caches the current child under its keep-alive key and keeps the units of `item.kept`. */
|
|
44
|
+
export function createKeptOutlet({
|
|
45
|
+
KeepAlive,
|
|
46
|
+
defineComponent,
|
|
47
|
+
h,
|
|
48
|
+
provide,
|
|
49
|
+
shallowReactive,
|
|
50
|
+
shallowRef,
|
|
51
|
+
watchEffect,
|
|
52
|
+
RouteKey,
|
|
53
|
+
DataKey,
|
|
54
|
+
routeValue,
|
|
55
|
+
dataOf,
|
|
56
|
+
}: KeepAliveBase): KeptOutlet {
|
|
57
|
+
const keptPages = new Map<string, Component>()
|
|
58
|
+
|
|
59
|
+
/** One component per keep-alive key, named by it, so `KeepAlive`'s `include` drops exactly the units the core dropped; a hidden one keeps the route and data it was shown with. */
|
|
60
|
+
function keptPage(name: string): Component {
|
|
61
|
+
let page = keptPages.get(name)
|
|
62
|
+
if (!page) {
|
|
63
|
+
page = defineComponent({
|
|
64
|
+
name,
|
|
65
|
+
props: {
|
|
66
|
+
route: { type: Object as PropType<RouteValue>, required: true },
|
|
67
|
+
data: { type: Object as PropType<Record<string, unknown>>, required: true },
|
|
68
|
+
},
|
|
69
|
+
setup(props, { slots }) {
|
|
70
|
+
const route = shallowReactive({ ...props.route })
|
|
71
|
+
const data = shallowRef(props.data)
|
|
72
|
+
watchEffect(() => void Object.assign(route, props.route))
|
|
73
|
+
watchEffect(() => void (data.value = props.data))
|
|
74
|
+
provide(RouteKey, route)
|
|
75
|
+
provide(DataKey, data)
|
|
76
|
+
return () => slots.default?.()
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
keptPages.set(name, page)
|
|
80
|
+
}
|
|
81
|
+
return page
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return (item, below, child, source) => {
|
|
85
|
+
const key = below?.keepalive
|
|
86
|
+
const include = [...(item.kept ?? []).map((unit) => unit.key), ...(key === undefined ? [] : [key])].map(exact)
|
|
87
|
+
const ctx = source()
|
|
88
|
+
const view =
|
|
89
|
+
child && h(keptPage(key ?? SHOWN), { key, route: routeValue(ctx), data: dataOf(ctx) }, { default: () => child })
|
|
90
|
+
return h(KeepAlive, { include }, { default: () => view })
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { defineAsyncComponent, defineComponent, h, onBeforeUnmount, onMounted, provide, shallowRef } from "vue"
|
|
2
|
+
import type { Component, PropType } from "vue"
|
|
3
|
+
import { NeverHydratedKey } from "./context.ts"
|
|
4
|
+
import { HYDRATE_ELEMENT, hydrateOnAttribute, replayInteraction, whenTriggered } from "hamur/adapter"
|
|
5
|
+
import type { HydrateOn, HydrateOnOptions } from "hamur/adapter"
|
|
6
|
+
|
|
7
|
+
/** Hydration keeps the server markup in the element; a client render has put at most a comment there yet. */
|
|
8
|
+
const served = (element: Element) => element.firstElementChild !== null || (element.textContent ?? "") !== ""
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Renders the component on the server and, after hydration, keeps its server DOM until `on` fires; after a client
|
|
12
|
+
* navigation it mounts at once. Vue's `__asyncHydrate` loads the chunk before it asks a hydration strategy, so the gate is
|
|
13
|
+
* in the loader of a per-instance `defineAsyncComponent`.
|
|
14
|
+
*/
|
|
15
|
+
export function defineLazyHydrationComponent<T extends Component>(
|
|
16
|
+
on: HydrateOn,
|
|
17
|
+
loader: () => Promise<{ default: T }>,
|
|
18
|
+
options: Omit<HydrateOnOptions, "signal"> = {},
|
|
19
|
+
): T {
|
|
20
|
+
const attribute = hydrateOnAttribute(on)
|
|
21
|
+
return defineComponent({
|
|
22
|
+
name: "LazyHydration",
|
|
23
|
+
inheritAttrs: false,
|
|
24
|
+
props: { onHydrated: Function as PropType<() => void> },
|
|
25
|
+
setup(props, { attrs, slots }) {
|
|
26
|
+
if (on === "never") provide(NeverHydratedKey, true)
|
|
27
|
+
const wrapper = shallowRef<Element>()
|
|
28
|
+
let release: (() => void) | undefined
|
|
29
|
+
const gate = typeof window === "undefined" ? undefined : new Promise<void>((resolve) => (release = resolve))
|
|
30
|
+
const Inner = defineAsyncComponent({
|
|
31
|
+
loader: () => (gate ? gate.then(loader) : loader()).then((loaded) => loaded.default),
|
|
32
|
+
suspensible: false,
|
|
33
|
+
})
|
|
34
|
+
const controller = new AbortController()
|
|
35
|
+
let called = false
|
|
36
|
+
const mounted = () => {
|
|
37
|
+
if (called) return
|
|
38
|
+
called = true
|
|
39
|
+
props.onHydrated?.()
|
|
40
|
+
if (wrapper.value) replayInteraction(wrapper.value)
|
|
41
|
+
}
|
|
42
|
+
onMounted(() => {
|
|
43
|
+
const element = wrapper.value
|
|
44
|
+
if (!release || !element) return
|
|
45
|
+
if (served(element)) void whenTriggered(element, on, { ...options, signal: controller.signal }).then(release)
|
|
46
|
+
else release()
|
|
47
|
+
})
|
|
48
|
+
onBeforeUnmount(() => controller.abort())
|
|
49
|
+
return () =>
|
|
50
|
+
h(HYDRATE_ELEMENT, { on: attribute, ref: wrapper }, [h(Inner, { ...attrs, onVnodeMounted: mounted }, slots)])
|
|
51
|
+
},
|
|
52
|
+
}) as unknown as T
|
|
53
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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 = `${imports}import { defineComponent, h } from "vue";
|
|
7
|
+
import { MarkdownRenderer, useHead } from "@hamur/adapter-vue";
|
|
8
|
+
|
|
9
|
+
const value = ${value};
|
|
10
|
+
|
|
11
|
+
export default defineComponent({
|
|
12
|
+
name: "MarkdownPage",
|
|
13
|
+
setup() {
|
|
14
|
+
${head ? ` useHead(${head});\n` : ""} return () => h(MarkdownRenderer, ${props});
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
`
|
|
18
|
+
return { contents, extension: ".js" }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const markdownComponent = { module }
|
package/src/markdown.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Fragment, camelize, defineComponent, h, type Component, type PropType, type VNodeChild } from "vue"
|
|
2
|
+
import { markdownLink, markdownProse, markdownView, type MdView, type MdViewElement } from "hamur/adapter"
|
|
3
|
+
import type { MarkdownDocument, MdNode, MdProps } from "hamur/markdown"
|
|
4
|
+
import Link from "./components/Link.vue"
|
|
5
|
+
|
|
6
|
+
export interface MarkdownRendererProps {
|
|
7
|
+
/** A Markdown AST node, a list of nodes, or a document, which renders its `ast`. */
|
|
8
|
+
value: MdNode | MdNode[] | MarkdownDocument
|
|
9
|
+
/** Components by Markdown component name (kebab case); each gets the node's props, its default slot and a slot per named slot. */
|
|
10
|
+
components?: Record<string, Component>
|
|
11
|
+
/** Renders `html` nodes, writing what it returns as their inner HTML; without it they are left out. A Markdown page under config `markdown.html` passes the core's Trusted Types writer. */
|
|
12
|
+
raw?: (html: string) => string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type Components = Record<string, Component>
|
|
16
|
+
|
|
17
|
+
/** The props a component declares as written; the others fall through to its root element, so they pass the attribute checks first. */
|
|
18
|
+
function propsFor(component: Component, props: MdProps): Record<string, unknown> {
|
|
19
|
+
const options = (component as unknown as { props?: unknown }).props
|
|
20
|
+
const declared = new Set(
|
|
21
|
+
Array.isArray(options)
|
|
22
|
+
? (options as string[])
|
|
23
|
+
: typeof options === "object" && options !== null
|
|
24
|
+
? Object.keys(options)
|
|
25
|
+
: [],
|
|
26
|
+
)
|
|
27
|
+
const entries = Object.entries(props)
|
|
28
|
+
const [element] = markdownView([
|
|
29
|
+
"span",
|
|
30
|
+
Object.fromEntries(entries.filter(([name]) => !declared.has(camelize(name)))),
|
|
31
|
+
]) as MdViewElement[]
|
|
32
|
+
return { ...element!.attrs, ...Object.fromEntries(entries.filter(([name]) => declared.has(camelize(name)))) }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function render(view: MdView, raw: (html: string) => string, components: Components): VNodeChild {
|
|
36
|
+
if (typeof view === "string") return view
|
|
37
|
+
if ("html" in view) return h("hamur-html", { style: "display:contents", innerHTML: raw(view.html) })
|
|
38
|
+
const children = () => view.children.map((child) => render(child, raw, components))
|
|
39
|
+
if ("component" in view) {
|
|
40
|
+
const component = Object.hasOwn(components, view.component) ? components[view.component] : undefined
|
|
41
|
+
if (component) {
|
|
42
|
+
const slots = Object.fromEntries(
|
|
43
|
+
Object.entries(view.slots).map(([name, views]) => [
|
|
44
|
+
name,
|
|
45
|
+
() => views.map((slot) => render(slot, raw, components)),
|
|
46
|
+
]),
|
|
47
|
+
)
|
|
48
|
+
return h(
|
|
49
|
+
component,
|
|
50
|
+
propsFor(component, view.props),
|
|
51
|
+
view.children.length ? { ...slots, default: children } : slots,
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
return view.attrs ? h(view.component, view.attrs, children()) : h(Fragment, children())
|
|
55
|
+
}
|
|
56
|
+
const link = markdownLink(view)
|
|
57
|
+
if (link) return h(Link, { ...link.attrs, to: link.to }, { default: children })
|
|
58
|
+
return h(view.tag, view.attrs, children())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Renders the Markdown AST as Vue vnodes, an internal link as `Link`, a component node as its component in `components`. */
|
|
62
|
+
export const MarkdownRenderer = defineComponent({
|
|
63
|
+
name: "MarkdownRenderer",
|
|
64
|
+
props: {
|
|
65
|
+
value: { type: [String, Array, Object] as PropType<MarkdownRendererProps["value"]>, required: true },
|
|
66
|
+
components: { type: Object as PropType<Components>, default: undefined },
|
|
67
|
+
raw: { type: Function as PropType<(html: string) => string>, default: undefined },
|
|
68
|
+
},
|
|
69
|
+
setup(props) {
|
|
70
|
+
return () => {
|
|
71
|
+
const { value, components } = markdownProse(props.value, props.components ?? {})
|
|
72
|
+
return h(
|
|
73
|
+
Fragment,
|
|
74
|
+
markdownView(value, { html: props.raw !== undefined, components }).map((view) =>
|
|
75
|
+
render(view, props.raw!, components),
|
|
76
|
+
),
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
})
|