prunelle 0.0.0-stage → 0.2.0
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/llms.txt +148 -0
- package/package.json +53 -4
- package/src/ambient.d.ts +6 -0
- package/src/attrs.ts +35 -0
- package/src/build/bundle.ts +613 -0
- package/src/build/carry.ts +57 -0
- package/src/build/client-entries.ts +107 -0
- package/src/build/client-plugin.ts +169 -0
- package/src/build/client-prune.ts +118 -0
- package/src/build/compile.ts +128 -0
- package/src/build/files.ts +156 -0
- package/src/build/first-load.ts +164 -0
- package/src/build/graph.ts +247 -0
- package/src/build/index.ts +614 -0
- package/src/build/is-inside.ts +12 -0
- package/src/build/islands.ts +641 -0
- package/src/build/manifest.ts +281 -0
- package/src/build/output.ts +84 -0
- package/src/build/plugin-host.ts +220 -0
- package/src/build/precompress.ts +139 -0
- package/src/build/prerender.ts +150 -0
- package/src/build/render-rules.ts +205 -0
- package/src/build/report.ts +247 -0
- package/src/build/resolve.ts +50 -0
- package/src/build/routes.ts +338 -0
- package/src/build/server-entry.ts +38 -0
- package/src/build/static.ts +163 -0
- package/src/build/status-pages.ts +127 -0
- package/src/cli/check-forms.ts +654 -0
- package/src/cli/check-links.ts +619 -0
- package/src/cli/check.ts +367 -0
- package/src/cli/commands.ts +265 -0
- package/src/cli/index.ts +70 -0
- package/src/cli/inspect.ts +335 -0
- package/src/cli/new.ts +41 -0
- package/src/cli/upgrade-names.ts +209 -0
- package/src/cli/upgrade.ts +354 -0
- package/src/compiler/analyze.ts +618 -0
- package/src/compiler/ast.ts +82 -0
- package/src/compiler/browser-globals.ts +127 -0
- package/src/compiler/compliance-rgaa.ts +502 -0
- package/src/compiler/compliance.ts +497 -0
- package/src/compiler/data-keys.ts +295 -0
- package/src/compiler/dom-attrs.ts +379 -0
- package/src/compiler/dom.ts +566 -0
- package/src/compiler/emit.ts +131 -0
- package/src/compiler/expr-context.ts +408 -0
- package/src/compiler/index.ts +8 -0
- package/src/compiler/interactivity.ts +366 -0
- package/src/compiler/mistakes.ts +499 -0
- package/src/compiler/node-env.ts +50 -0
- package/src/compiler/patch.ts +97 -0
- package/src/compiler/props.ts +460 -0
- package/src/compiler/scope.ts +568 -0
- package/src/compiler/server-only.ts +326 -0
- package/src/compiler/shared.ts +369 -0
- package/src/compiler/signal-reads.ts +242 -0
- package/src/compiler/slots.ts +227 -0
- package/src/compiler/ssr-helpers.ts +555 -0
- package/src/compiler/ssr-island.ts +90 -0
- package/src/compiler/ssr-prunelle-tags.ts +56 -0
- package/src/compiler/ssr.ts +491 -0
- package/src/context.ts +165 -0
- package/src/dev/child.ts +30 -0
- package/src/dev/index.ts +208 -0
- package/src/dev/modules.ts +68 -0
- package/src/dev/protocol.ts +21 -0
- package/src/dev/queue.ts +79 -0
- package/src/dev/reload.ts +157 -0
- package/src/dev/supervisor.ts +187 -0
- package/src/dev/watchers.ts +215 -0
- package/src/dom/action.ts +164 -0
- package/src/dom/attrs.ts +72 -0
- package/src/dom/claim.ts +120 -0
- package/src/dom/extras.ts +562 -0
- package/src/dom/index.ts +289 -0
- package/src/dom/island.ts +284 -0
- package/src/dom/loop.ts +450 -0
- package/src/dom/revive.ts +36 -0
- package/src/index.ts +22 -0
- package/src/jsx-runtime.ts +566 -0
- package/src/plugins/config.ts +86 -0
- package/src/plugins/context.ts +66 -0
- package/src/plugins/index.ts +14 -0
- package/src/plugins/load.ts +70 -0
- package/src/plugins/order.ts +111 -0
- package/src/plugins/public.ts +12 -0
- package/src/plugins/requires.ts +47 -0
- package/src/plugins/services.ts +35 -0
- package/src/plugins/types.ts +242 -0
- package/src/reactivity/graph.ts +249 -0
- package/src/reactivity/index.ts +533 -0
- package/src/reactivity/selector.ts +51 -0
- package/src/reactivity/store.ts +208 -0
- package/src/server/action-protect.ts +186 -0
- package/src/server/action-registry.ts +78 -0
- package/src/server/action.ts +498 -0
- package/src/server/app-entry.ts +23 -0
- package/src/server/assets.ts +136 -0
- package/src/server/compress.ts +560 -0
- package/src/server/consent.ts +253 -0
- package/src/server/cookies.ts +139 -0
- package/src/server/document.ts +271 -0
- package/src/server/env.ts +33 -0
- package/src/server/errors.ts +382 -0
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +508 -0
- package/src/server/index.ts +687 -0
- package/src/server/lifecycle.ts +75 -0
- package/src/server/loader.ts +118 -0
- package/src/server/page.ts +107 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +256 -0
- package/src/server/prerendered.ts +71 -0
- package/src/server/public.ts +19 -0
- package/src/server/rate-limit.ts +133 -0
- package/src/server/region.ts +96 -0
- package/src/server/request-body.ts +27 -0
- package/src/server/request-path.ts +73 -0
- package/src/server/request-scope.ts +22 -0
- package/src/server/route-table.ts +82 -0
- package/src/server/router.ts +261 -0
- package/src/server/routing.ts +377 -0
- package/src/server/security.ts +472 -0
- package/src/server/serve-handlers.ts +83 -0
- package/src/server/served-file.ts +128 -0
- package/src/server/standard-schema.ts +87 -0
- package/src/server/start.ts +99 -0
- package/src/server/static-dir.ts +167 -0
- package/src/server/stream.ts +192 -0
- package/src/ssr/context.ts +350 -0
- package/src/ssr/index.ts +596 -0
- package/src/ssr/navigation.ts +96 -0
- package/src/ssr/region.ts +171 -0
- package/src/style.ts +33 -0
- package/src/test/browser.ts +192 -0
- package/src/test/index.ts +191 -0
- package/src/unrenderable.ts +35 -0
- package/templates/_shared/AGENTS.md +10 -0
- package/templates/_shared/CLAUDE.md +1 -0
- package/templates/_shared/gitignore +3 -0
- package/templates/_shared/public/robots.txt +2 -0
- package/templates/_shared/src/routes/legal.tsx +13 -0
- package/templates/_shared/tsconfig.json +20 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +21 -0
- package/templates/app/prunelle.config.ts +11 -0
- package/templates/app/src/auth.ts +14 -0
- package/templates/app/src/routes/_layout.tsx +32 -0
- package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
- package/templates/app/src/routes/dashboard/index.tsx +13 -0
- package/templates/app/src/routes/index.tsx +15 -0
- package/templates/app/src/routes/login.tsx +34 -0
- package/templates/app/src/styles/app.css +7 -0
- package/templates/app/tests/app.test.ts +32 -0
- package/templates/blog/content/posts/welcome.md +9 -0
- package/templates/blog/package.json +22 -0
- package/templates/blog/prunelle.config.ts +23 -0
- package/templates/blog/src/posts.ts +26 -0
- package/templates/blog/src/routes/_layout.tsx +20 -0
- package/templates/blog/src/routes/blog/[slug].tsx +31 -0
- package/templates/blog/src/routes/index.tsx +33 -0
- package/templates/blog/src/styles/app.css +10 -0
- package/templates/fullstack/package.json +19 -0
- package/templates/fullstack/prunelle.config.ts +6 -0
- package/templates/fullstack/src/actions/greet.ts +19 -0
- package/templates/fullstack/src/components/Counter.tsx +10 -0
- package/templates/fullstack/src/components/GreetForm.tsx +27 -0
- package/templates/fullstack/src/routes/_layout.tsx +14 -0
- package/templates/fullstack/src/routes/api/health.ts +5 -0
- package/templates/fullstack/src/routes/index.tsx +13 -0
- package/templates/fullstack/src/styles/app.css +29 -0
- package/templates/fullstack/tests/app.test.ts +37 -0
- package/templates/minimal/package.json +19 -0
- package/templates/minimal/prunelle.config.ts +6 -0
- package/templates/minimal/src/components/Counter.tsx +10 -0
- package/templates/minimal/src/routes/_layout.tsx +14 -0
- package/templates/minimal/src/routes/index.tsx +11 -0
- package/templates/minimal/src/styles/app.css +22 -0
- package/README.md +0 -3
package/src/dom/index.ts
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// Prunelle minimal DOM & hydration core. Every island imports it, so what only some islands need lives apart: attribute bindings in prunelle/internal/dom-attrs, claiming SSR lists and inserts in dom-claim, lists in dom-loop, inserts and spreads in dom-extras.
|
|
2
|
+
|
|
3
|
+
import { batch, createOwner, disposeOwner, getOwner, onCleanup, runWithOwner } from '../reactivity/index'
|
|
4
|
+
import { assertRenderable } from '../unrenderable'
|
|
5
|
+
|
|
6
|
+
export { effect } from '../reactivity/index'
|
|
7
|
+
|
|
8
|
+
let hydrating = false
|
|
9
|
+
let hydrateNode: Node | null = null
|
|
10
|
+
let hydrateRoot: Element | null = null
|
|
11
|
+
|
|
12
|
+
export function isRangeEnd(node: Node): boolean {
|
|
13
|
+
return node.nodeType === 8 && (node as Comment).data === 'k]'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function hydrateCursor(): Node | null {
|
|
17
|
+
return hydrating ? hydrateNode : null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isHydrating(): boolean {
|
|
21
|
+
return hydrating
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Points the hydration cursor at `node` (claiming SSR nodes when `on`) and returns the cursor it replaced, for the caller to restore. */
|
|
25
|
+
export function moveCursor(on: boolean, node: Node | null): [boolean, Node | null] {
|
|
26
|
+
const prev: [boolean, Node | null] = [hydrating, hydrateNode]
|
|
27
|
+
hydrating = on
|
|
28
|
+
hydrateNode = node
|
|
29
|
+
return prev
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function hydrate(container: Element, render: () => Node | DocumentFragment | null, idPrefix?: string): () => void {
|
|
33
|
+
const owner = createOwner(undefined)
|
|
34
|
+
if (idPrefix !== undefined)
|
|
35
|
+
owner.id = { p: idPrefix, n: 0 }
|
|
36
|
+
const prevHydrating = hydrating
|
|
37
|
+
const prevNode = hydrateNode
|
|
38
|
+
const prevRoot = process.env.NODE_ENV !== 'production' ? hydrateRoot : null
|
|
39
|
+
hydrating = true
|
|
40
|
+
hydrateNode = container.firstChild
|
|
41
|
+
if (process.env.NODE_ENV !== 'production')
|
|
42
|
+
hydrateRoot = container
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
runWithOwner(owner, () => {
|
|
46
|
+
render()
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
disposeOwner(owner)
|
|
51
|
+
throw err
|
|
52
|
+
}
|
|
53
|
+
finally {
|
|
54
|
+
hydrating = prevHydrating
|
|
55
|
+
hydrateNode = prevNode
|
|
56
|
+
if (process.env.NODE_ENV !== 'production')
|
|
57
|
+
hydrateRoot = prevRoot
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return () => disposeOwner(owner)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function mount(container: Element, render: () => Node | DocumentFragment | null, idPrefix?: string): () => void {
|
|
64
|
+
const owner = createOwner(undefined)
|
|
65
|
+
if (idPrefix !== undefined)
|
|
66
|
+
owner.id = { p: idPrefix, n: 0 }
|
|
67
|
+
const prevHydrating = hydrating
|
|
68
|
+
hydrating = false
|
|
69
|
+
try {
|
|
70
|
+
const node = runWithOwner(owner, render)
|
|
71
|
+
container.textContent = ''
|
|
72
|
+
if (node !== null)
|
|
73
|
+
container.appendChild(node)
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
disposeOwner(owner)
|
|
77
|
+
throw err
|
|
78
|
+
}
|
|
79
|
+
finally {
|
|
80
|
+
hydrating = prevHydrating
|
|
81
|
+
}
|
|
82
|
+
return () => disposeOwner(owner)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function warnMismatch(node: Node, expectedTag: string, steppedOver: boolean): void {
|
|
86
|
+
const island = hydrateRoot?.getAttribute('c') ?? hydrateRoot?.tagName.toLowerCase() ?? 'unknown'
|
|
87
|
+
const found = node.nodeType === 1
|
|
88
|
+
? `<${(node as Element).tagName.toLowerCase()}>`
|
|
89
|
+
: node.nodeType === 3
|
|
90
|
+
? `#text(${JSON.stringify((node as Text).data.slice(0, 30))})`
|
|
91
|
+
: node.nodeType === 8
|
|
92
|
+
? `<!--${(node as Comment).data}-->`
|
|
93
|
+
: `nodeType(${node.nodeType})`
|
|
94
|
+
console.warn(`prunelle: hydration mismatch in island "${island}": expected <${expectedTag}>, found ${found}; ${steppedOver ? `hydrating the next <${expectedTag}>` : 'falling back to fresh clone'}.`)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function template(html: string, isSvg?: boolean): () => Node {
|
|
98
|
+
let cached: Node | undefined
|
|
99
|
+
const expectedTag = /^<([A-Za-z][^\s/>]*)/.exec(html)?.[1]?.toLowerCase()
|
|
100
|
+
|
|
101
|
+
const clone = (): Node => {
|
|
102
|
+
if (cached === undefined) {
|
|
103
|
+
const t = document.createElement('template')
|
|
104
|
+
t.innerHTML = isSvg ? `<svg>${html}</svg>` : html
|
|
105
|
+
cached = isSvg ? t.content.firstChild!.firstChild! : t.content.firstChild!
|
|
106
|
+
}
|
|
107
|
+
return cached.cloneNode(true)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return (): Node => {
|
|
111
|
+
if (hydrating && hydrateNode !== null) {
|
|
112
|
+
const stale = hydrateNode
|
|
113
|
+
// Stop at a closing range marker so a mismatch inside a branch never steals or replaces nodes outside it.
|
|
114
|
+
if (isRangeEnd(stale)) {
|
|
115
|
+
if (process.env.NODE_ENV !== 'production' && expectedTag !== undefined)
|
|
116
|
+
warnMismatch(stale, expectedTag, false)
|
|
117
|
+
hydrating = false
|
|
118
|
+
hydrateNode = null
|
|
119
|
+
return clone()
|
|
120
|
+
}
|
|
121
|
+
// A node injected before the markup (an extension, a password manager) is stepped over; no matching node at all means the server rendered something else.
|
|
122
|
+
let node: Node | null = stale
|
|
123
|
+
while (node !== null && !isRangeEnd(node) && expectedTag !== undefined && node.nodeName.toLowerCase() !== expectedTag)
|
|
124
|
+
node = node.nextSibling
|
|
125
|
+
if (node !== null && isRangeEnd(node))
|
|
126
|
+
node = null
|
|
127
|
+
if (process.env.NODE_ENV !== 'production' && node !== stale)
|
|
128
|
+
warnMismatch(stale, expectedTag!, node !== null)
|
|
129
|
+
if (node === null) {
|
|
130
|
+
hydrating = false
|
|
131
|
+
hydrateNode = null
|
|
132
|
+
const fresh = clone()
|
|
133
|
+
stale.parentNode?.replaceChild(fresh, stale)
|
|
134
|
+
return fresh
|
|
135
|
+
}
|
|
136
|
+
hydrateNode = node.nextSibling
|
|
137
|
+
return node
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return clone()
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Moves the hydration cursor past the SSR range of a root fragment's dynamic child (`<!--k[-->...<!--k]-->` or `<!---->text<!---->`) and returns its opening marker; null when not hydrating.
|
|
145
|
+
export function claimRange(): Node | null {
|
|
146
|
+
const start = hydrating ? hydrateNode : null
|
|
147
|
+
if (start === null || start.nodeType !== 8)
|
|
148
|
+
return null
|
|
149
|
+
hydrateNode = skipRange(start)?.nextSibling ?? null
|
|
150
|
+
return start
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// A text child of a root fragment, claimed from the SSR markup while hydrating; a dynamic one sits between `<!---->` markers.
|
|
154
|
+
export function fragText(data: string, dynamic?: boolean): Text {
|
|
155
|
+
if (dynamic) {
|
|
156
|
+
const start = claimRange()
|
|
157
|
+
if (start !== null)
|
|
158
|
+
return textAfter(start)
|
|
159
|
+
}
|
|
160
|
+
else if (hydrating && hydrateNode !== null && hydrateNode.nodeType === 3) {
|
|
161
|
+
const text = hydrateNode as Text
|
|
162
|
+
hydrateNode = text.nextSibling
|
|
163
|
+
return text
|
|
164
|
+
}
|
|
165
|
+
return document.createTextNode(data)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function frag(...nodes: Node[]): Node | null {
|
|
169
|
+
if (hydrating)
|
|
170
|
+
return null
|
|
171
|
+
const f = document.createDocumentFragment()
|
|
172
|
+
for (let i = 0; i < nodes.length; i++)
|
|
173
|
+
f.appendChild(nodes[i]!)
|
|
174
|
+
return f
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function firstText(el: Node): Text {
|
|
178
|
+
const first = el.firstChild
|
|
179
|
+
if (first !== null && first.nodeType === 3)
|
|
180
|
+
return first as Text
|
|
181
|
+
const text = document.createTextNode('')
|
|
182
|
+
el.insertBefore(text, first)
|
|
183
|
+
return text
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function textAfter(marker: Node): Text {
|
|
187
|
+
const next = marker.nextSibling
|
|
188
|
+
if (next !== null && next.nodeType === 3)
|
|
189
|
+
return next as Text
|
|
190
|
+
const text = document.createTextNode('')
|
|
191
|
+
marker.parentNode!.insertBefore(text, next)
|
|
192
|
+
return text
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Advance past an optional SSR text node (`<!-->text<!-->` or `<!--><!-->`) to the closing comment marker. */
|
|
196
|
+
export function skipText(marker: Node): Node | null {
|
|
197
|
+
const next = marker.nextSibling
|
|
198
|
+
return next !== null && next.nodeType === 3 ? next.nextSibling : next
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Advance past an SSR-bracketed dynamic range (`<!--k[-->...<!--k]-->` or `<!---->...<!---->`) to its closing comment marker. */
|
|
202
|
+
export function skipRange(marker: Node): Node | null {
|
|
203
|
+
const balanced = marker.nodeType === 8 && (marker as Comment).data === 'k['
|
|
204
|
+
let depth = balanced ? 1 : 0
|
|
205
|
+
let cur = marker.nextSibling
|
|
206
|
+
while (cur !== null) {
|
|
207
|
+
if (cur.nodeType === 8) {
|
|
208
|
+
if (!balanced)
|
|
209
|
+
return cur
|
|
210
|
+
const data = (cur as Comment).data
|
|
211
|
+
if (data === 'k[')
|
|
212
|
+
depth++
|
|
213
|
+
else if (data === 'k]' && --depth === 0)
|
|
214
|
+
return cur
|
|
215
|
+
}
|
|
216
|
+
cur = cur.nextSibling
|
|
217
|
+
}
|
|
218
|
+
return null
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function setText(node: Text, value: unknown): void {
|
|
222
|
+
if (process.env.NODE_ENV !== 'production')
|
|
223
|
+
assertRenderable(value, 'text')
|
|
224
|
+
const str = value == null || typeof value === 'boolean' ? '' : String(value)
|
|
225
|
+
if (node.data !== str)
|
|
226
|
+
node.data = str
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// ---- Delegated events
|
|
230
|
+
|
|
231
|
+
const delegated = new WeakMap<Document, Set<string>>()
|
|
232
|
+
|
|
233
|
+
function dispatchEvent(event: Event): void {
|
|
234
|
+
const key = `$$${event.type}`
|
|
235
|
+
let current: EventTarget | null = null
|
|
236
|
+
const prevDesc = Object.getOwnPropertyDescriptor(event, 'currentTarget')
|
|
237
|
+
Object.defineProperty(event, 'currentTarget', { configurable: true, get: () => current })
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
const path = event.composedPath?.()
|
|
241
|
+
const usePath = path !== undefined && path.length > 0
|
|
242
|
+
let i = 0
|
|
243
|
+
let node = (usePath ? path[0] : event.target) as (EventTarget & Record<string, ((e: Event) => void) | undefined>) | null | undefined
|
|
244
|
+
while (node != null) {
|
|
245
|
+
const handler = node[key]
|
|
246
|
+
if (typeof handler === 'function') {
|
|
247
|
+
current = node
|
|
248
|
+
batch(() => handler(event))
|
|
249
|
+
if (event.cancelBubble)
|
|
250
|
+
return
|
|
251
|
+
}
|
|
252
|
+
node = (usePath ? path[++i] : (node as unknown as Node).parentNode) as (EventTarget & Record<string, ((e: Event) => void) | undefined>) | null | undefined
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
if (prevDesc !== undefined)
|
|
257
|
+
Object.defineProperty(event, 'currentTarget', prevDesc)
|
|
258
|
+
else
|
|
259
|
+
delete (event as unknown as Record<string, unknown>).currentTarget
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
export function delegate(events: string[]): void {
|
|
264
|
+
if (typeof document === 'undefined')
|
|
265
|
+
return
|
|
266
|
+
let set = delegated.get(document)
|
|
267
|
+
if (set === undefined) {
|
|
268
|
+
set = new Set()
|
|
269
|
+
delegated.set(document, set)
|
|
270
|
+
}
|
|
271
|
+
for (let i = 0; i < events.length; i++) {
|
|
272
|
+
const type = events[i]!
|
|
273
|
+
if (!set.has(type)) {
|
|
274
|
+
set.add(type)
|
|
275
|
+
document.addEventListener(type, dispatchEvent)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function on(el: Node, event: string, handler: ((e: Event) => void) | false | null | undefined): void {
|
|
281
|
+
if (typeof handler !== 'function')
|
|
282
|
+
return
|
|
283
|
+
const fn = (e: Event) => {
|
|
284
|
+
batch(() => handler(e))
|
|
285
|
+
}
|
|
286
|
+
el.addEventListener(event, fn)
|
|
287
|
+
if (getOwner() !== undefined)
|
|
288
|
+
onCleanup(() => el.removeEventListener(event, fn))
|
|
289
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// Native <pru-island> Custom Element: the build defines which strategies an app uses, so the loader drops the code of the others.
|
|
2
|
+
|
|
3
|
+
import type { hydrate, mount } from './index'
|
|
4
|
+
|
|
5
|
+
declare const PRUNELLE_VISIBLE: boolean | undefined
|
|
6
|
+
declare const PRUNELLE_IDLE: boolean | undefined
|
|
7
|
+
declare const PRUNELLE_INTERACTION: boolean | undefined
|
|
8
|
+
declare const PRUNELLE_MEDIA: boolean | undefined
|
|
9
|
+
declare const PRUNELLE_CLIENT_ONLY: boolean | undefined
|
|
10
|
+
/** The URL `prunelle build` gave the prop revivers; left undefined, they load from source. */
|
|
11
|
+
declare const PRUNELLE_REVIVE: string | undefined
|
|
12
|
+
|
|
13
|
+
// Only props holding a Date, Map, Set or another tagged value load the revivers: plain JSON parses with no extra file.
|
|
14
|
+
const reviver = (): Promise<typeof import('./revive')> => typeof PRUNELLE_REVIVE === 'undefined' ? import('./revive') : import(PRUNELLE_REVIVE)
|
|
15
|
+
|
|
16
|
+
// An island chunk also re-exports the DOM runtime it imports (`writeClientEntries`), `mount` only in an app with a client-only island.
|
|
17
|
+
type IslandModule = { default: (props: Record<string, unknown>) => Node | DocumentFragment | null, hydrate: typeof hydrate, mount?: typeof mount }
|
|
18
|
+
|
|
19
|
+
/** Finds this island's own `<script type="application/json" data-pru-props>` direct child. */
|
|
20
|
+
function findOwnPropsScript(root: Element): Element | null {
|
|
21
|
+
for (let child = root.lastElementChild; child !== null; child = child.previousElementSibling) {
|
|
22
|
+
if (child.tagName === 'SCRIPT' && child.hasAttribute('data-pru-props'))
|
|
23
|
+
return child
|
|
24
|
+
}
|
|
25
|
+
return null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Finds this island's own `<pru-slot>` without descending into nested `<pru-island>` boundaries. */
|
|
29
|
+
function findOwnSlot(root: Element): Element | null {
|
|
30
|
+
for (let child = root.firstElementChild; child !== null; child = child.nextElementSibling) {
|
|
31
|
+
const tag = child.tagName
|
|
32
|
+
if (tag === 'PRU-SLOT')
|
|
33
|
+
return child
|
|
34
|
+
if (tag === 'PRU-ISLAND')
|
|
35
|
+
continue
|
|
36
|
+
const nested = findOwnSlot(child)
|
|
37
|
+
if (nested !== null)
|
|
38
|
+
return nested
|
|
39
|
+
}
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function extractOwnSlot(root: Element): Element | null {
|
|
44
|
+
for (let child = root.lastElementChild; child !== null; child = child.previousElementSibling) {
|
|
45
|
+
if (child.tagName === 'TEMPLATE' && child.hasAttribute('data-pru-slot')) {
|
|
46
|
+
const slot = document.createElement('pru-slot')
|
|
47
|
+
slot.appendChild((child as HTMLTemplateElement).content)
|
|
48
|
+
child.remove()
|
|
49
|
+
return slot
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return findOwnSlot(root)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Collects box-generating descendant elements for IntersectionObserver, stepping through `display: contents` wrappers. */
|
|
56
|
+
function collectVisibleTargets(root: Element, out: Element[]): void {
|
|
57
|
+
for (let child = root.firstElementChild; child !== null; child = child.nextElementSibling) {
|
|
58
|
+
const tag = child.tagName
|
|
59
|
+
if (tag === 'SCRIPT' || tag === 'TEMPLATE')
|
|
60
|
+
continue
|
|
61
|
+
const isContents = tag === 'PRU-SLOT'
|
|
62
|
+
|| tag === 'PRU-ISLAND'
|
|
63
|
+
|| (typeof getComputedStyle === 'function' && getComputedStyle(child).display === 'contents')
|
|
64
|
+
if (isContents)
|
|
65
|
+
collectVisibleTargets(child, out)
|
|
66
|
+
else
|
|
67
|
+
out.push(child)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function replayEvent(root: Element, ev: Event, target: EventTarget | null): void {
|
|
72
|
+
if (target === null || typeof target.dispatchEvent !== 'function')
|
|
73
|
+
return
|
|
74
|
+
if (typeof (target as Node).nodeType === 'number' && !root.contains(target as Node))
|
|
75
|
+
return
|
|
76
|
+
const init: EventInit = { bubbles: ev.bubbles, cancelable: ev.cancelable, composed: ev.composed }
|
|
77
|
+
let clone: Event
|
|
78
|
+
if (ev.type === 'keydown') {
|
|
79
|
+
const ke = ev as KeyboardEvent
|
|
80
|
+
clone = new KeyboardEvent(ev.type, { ...init, key: ke.key, code: ke.code, altKey: ke.altKey, ctrlKey: ke.ctrlKey, shiftKey: ke.shiftKey, metaKey: ke.metaKey })
|
|
81
|
+
}
|
|
82
|
+
else if (ev.type === 'click' || ev.type === 'pointerdown') {
|
|
83
|
+
const me = ev as MouseEvent
|
|
84
|
+
const Ctor = ev.type === 'pointerdown' && typeof PointerEvent === 'function' ? PointerEvent : MouseEvent
|
|
85
|
+
clone = new Ctor(ev.type, { ...init, button: me.button, buttons: me.buttons, clientX: me.clientX, clientY: me.clientY, altKey: me.altKey, ctrlKey: me.ctrlKey, shiftKey: me.shiftKey, metaKey: me.metaKey })
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
clone = typeof FocusEvent === 'function' ? new FocusEvent(ev.type, init) : new Event(ev.type, init)
|
|
89
|
+
}
|
|
90
|
+
target.dispatchEvent(clone)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isAllowedChunkUrl(chunk: string): boolean {
|
|
94
|
+
// A browser reads `//host` and `/\host` as another site.
|
|
95
|
+
if (chunk[0] === '/')
|
|
96
|
+
return chunk[1] !== '/' && chunk[1] !== '\\'
|
|
97
|
+
if (typeof location !== 'undefined' && location.origin && location.origin !== 'null') {
|
|
98
|
+
try {
|
|
99
|
+
const parsed = new URL(chunk, location.href)
|
|
100
|
+
return parsed.origin === location.origin
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return false
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return !/^(?:javascript|data|vbscript):/i.test(chunk)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let clientIslandIdSeq = 0
|
|
110
|
+
|
|
111
|
+
export function defineIslandElement(): void {
|
|
112
|
+
if (typeof customElements === 'undefined' || customElements.get('pru-island'))
|
|
113
|
+
return
|
|
114
|
+
|
|
115
|
+
class PrunelleIsland extends HTMLElement {
|
|
116
|
+
_d: (() => void) | undefined
|
|
117
|
+
_c: (() => void) | undefined
|
|
118
|
+
_p: string | undefined
|
|
119
|
+
_slot: Element | undefined
|
|
120
|
+
_s = false
|
|
121
|
+
_t = 0
|
|
122
|
+
|
|
123
|
+
_clearStrategy(): void {
|
|
124
|
+
this._c?.()
|
|
125
|
+
this._c = undefined
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
connectedCallback(): void {
|
|
129
|
+
if (this._s)
|
|
130
|
+
return
|
|
131
|
+
this._s = true
|
|
132
|
+
const token = ++this._t
|
|
133
|
+
|
|
134
|
+
const h = this.getAttribute('h')
|
|
135
|
+
if ((typeof PRUNELLE_VISIBLE === 'undefined' || PRUNELLE_VISIBLE) && h === 'visible') {
|
|
136
|
+
if (typeof IntersectionObserver === 'undefined') {
|
|
137
|
+
void this._l(false, token)
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
const targets: Element[] = []
|
|
141
|
+
collectVisibleTargets(this, targets)
|
|
142
|
+
let restoreDisplay: (() => void) | undefined
|
|
143
|
+
if (targets.length === 0) {
|
|
144
|
+
const display = typeof getComputedStyle === 'function' ? getComputedStyle(this).display : 'contents'
|
|
145
|
+
if (display === 'contents' || display === '') {
|
|
146
|
+
const prev = this.style.display
|
|
147
|
+
this.style.display = 'inline'
|
|
148
|
+
restoreDisplay = () => { this.style.display = prev }
|
|
149
|
+
}
|
|
150
|
+
targets.push(this)
|
|
151
|
+
}
|
|
152
|
+
const io = new IntersectionObserver((entries) => {
|
|
153
|
+
if (entries.some(e => e.isIntersecting))
|
|
154
|
+
void this._l(false, token)
|
|
155
|
+
}, { rootMargin: '200px' })
|
|
156
|
+
this._c = () => {
|
|
157
|
+
io.disconnect()
|
|
158
|
+
restoreDisplay?.()
|
|
159
|
+
}
|
|
160
|
+
for (const target of targets)
|
|
161
|
+
io.observe(target)
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
if ((typeof PRUNELLE_IDLE === 'undefined' || PRUNELLE_IDLE) && h === 'idle') {
|
|
165
|
+
const g = globalThis as { requestIdleCallback?: (cb: () => void) => number, cancelIdleCallback?: (id: number) => void }
|
|
166
|
+
const run = () => void this._l(false, token)
|
|
167
|
+
if (g.requestIdleCallback) {
|
|
168
|
+
const id = g.requestIdleCallback(run)
|
|
169
|
+
this._c = () => g.cancelIdleCallback?.(id)
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
const id = setTimeout(run, 1)
|
|
173
|
+
this._c = () => clearTimeout(id)
|
|
174
|
+
}
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
if ((typeof PRUNELLE_INTERACTION === 'undefined' || PRUNELLE_INTERACTION) && h === 'interaction') {
|
|
178
|
+
const evs = ['pointerdown', 'click', 'focusin', 'keydown']
|
|
179
|
+
const queued: { ev: Event, target: EventTarget | null }[] = []
|
|
180
|
+
let loading = false
|
|
181
|
+
const stop = () => {
|
|
182
|
+
for (const e of evs) this.removeEventListener(e, go, true)
|
|
183
|
+
}
|
|
184
|
+
const go = (ev: Event) => {
|
|
185
|
+
queued.push({ ev, target: ev.target })
|
|
186
|
+
if (!loading) {
|
|
187
|
+
loading = true
|
|
188
|
+
void this._l(false, token, true, () => {
|
|
189
|
+
for (let i = 0; i < queued.length; i++)
|
|
190
|
+
replayEvent(this, queued[i]!.ev, queued[i]!.target)
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
for (const e of evs) this.addEventListener(e, go, true)
|
|
195
|
+
this._c = stop
|
|
196
|
+
return
|
|
197
|
+
}
|
|
198
|
+
if ((typeof PRUNELLE_MEDIA === 'undefined' || PRUNELLE_MEDIA) && h !== null && h.startsWith('media(')) {
|
|
199
|
+
const mq = matchMedia(h.slice(6, -1))
|
|
200
|
+
if (mq.matches) {
|
|
201
|
+
void this._l(false, token)
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
const go = () => {
|
|
205
|
+
if (mq.matches)
|
|
206
|
+
void this._l(false, token)
|
|
207
|
+
}
|
|
208
|
+
mq.addEventListener('change', go)
|
|
209
|
+
this._c = () => mq.removeEventListener('change', go)
|
|
210
|
+
}
|
|
211
|
+
return
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
void this._l(h === 'client', token)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
disconnectedCallback(): void {
|
|
218
|
+
// Defer disposal by a microtask so synchronous DOM moves (e.g. SPA persistent islands) stay alive!
|
|
219
|
+
queueMicrotask(() => {
|
|
220
|
+
if (!this.isConnected) {
|
|
221
|
+
this._t++
|
|
222
|
+
this._clearStrategy()
|
|
223
|
+
this._d?.()
|
|
224
|
+
this._d = undefined
|
|
225
|
+
this._s = false
|
|
226
|
+
}
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async _l(clientOnly: boolean, token: number, keepStrategy = false, after?: () => void): Promise<void> {
|
|
231
|
+
if (!keepStrategy)
|
|
232
|
+
this._clearStrategy()
|
|
233
|
+
const chunk = this.getAttribute('c')
|
|
234
|
+
if (!chunk || !isAllowedChunkUrl(chunk) || token !== this._t || !this.isConnected || this._d !== undefined) {
|
|
235
|
+
this._clearStrategy()
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
let dispose: (() => void) | undefined
|
|
240
|
+
let mod: IslandModule | undefined
|
|
241
|
+
try {
|
|
242
|
+
const ps = findOwnPropsScript(this)
|
|
243
|
+
if (ps !== null) {
|
|
244
|
+
this._p = ps.textContent || '{}'
|
|
245
|
+
ps.remove()
|
|
246
|
+
}
|
|
247
|
+
const raw = this._p ?? this.getAttribute('p')
|
|
248
|
+
const [loaded, revive] = await Promise.all([import(chunk) as Promise<IslandModule>, raw?.includes('"__k":') ? reviver() : undefined])
|
|
249
|
+
mod = loaded
|
|
250
|
+
if (!this.isConnected || token !== this._t || this._d !== undefined) {
|
|
251
|
+
this._clearStrategy()
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
this._clearStrategy()
|
|
255
|
+
|
|
256
|
+
const props = (raw ? revive?.parseIslandProps(raw) ?? JSON.parse(raw) : {}) as Record<string, unknown>
|
|
257
|
+
const slot = extractOwnSlot(this) ?? this._slot ?? null
|
|
258
|
+
if (slot !== null) {
|
|
259
|
+
this._slot = slot
|
|
260
|
+
props.children = slot
|
|
261
|
+
}
|
|
262
|
+
const idPrefix = this.getAttribute('i') ?? `c${clientIslandIdSeq++}-`
|
|
263
|
+
const render = () => loaded.default(props)
|
|
264
|
+
dispose = (typeof PRUNELLE_CLIENT_ONLY === 'undefined' || PRUNELLE_CLIENT_ONLY) && clientOnly
|
|
265
|
+
? loaded.mount!(this, render, idPrefix)
|
|
266
|
+
: loaded.hydrate(this, render, idPrefix)
|
|
267
|
+
if (!this.isConnected || token !== this._t) {
|
|
268
|
+
dispose()
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
this._d = dispose
|
|
272
|
+
after?.()
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
this._clearStrategy()
|
|
276
|
+
dispose?.()
|
|
277
|
+
// The chunk URL is a hash; in dev the component's own name says which island failed.
|
|
278
|
+
console.error(`[pru-island] failed to hydrate ${process.env.NODE_ENV !== 'production' && mod?.default?.name ? `<${mod.default.name}> (${chunk})` : chunk}:`, err)
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
customElements.define('pru-island', PrunelleIsland)
|
|
284
|
+
}
|