prunelle 0.8.0 → 0.10.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.
Files changed (209) hide show
  1. package/llms.txt +68 -51
  2. package/package.json +7 -2
  3. package/src/a11y/declaration.ts +10 -17
  4. package/src/a11y/index.ts +69 -125
  5. package/src/animate.ts +127 -0
  6. package/src/build/asides.ts +51 -0
  7. package/src/build/bundle.ts +94 -136
  8. package/src/build/client-entries.ts +6 -6
  9. package/src/build/client-outputs.ts +75 -0
  10. package/src/build/client-plugin.ts +9 -4
  11. package/src/build/ecoindex.ts +4 -2
  12. package/src/build/files.ts +18 -0
  13. package/src/build/graph.ts +14 -6
  14. package/src/build/index.ts +23 -11
  15. package/src/build/island-promote.ts +42 -2
  16. package/src/build/islands.ts +63 -9
  17. package/src/build/key-types.ts +31 -0
  18. package/src/build/manifest.ts +117 -1
  19. package/src/build/plugin-host.ts +13 -1
  20. package/src/build/prerender.ts +8 -3
  21. package/src/build/reach.ts +18 -4
  22. package/src/build/report.ts +1 -1
  23. package/src/build/resolve.ts +5 -1
  24. package/src/build/route-types.ts +16 -0
  25. package/src/build/routes.ts +42 -15
  26. package/src/build/seed-ids.ts +65 -0
  27. package/src/build/seed-writes.ts +111 -0
  28. package/src/build/static-headers.ts +2 -2
  29. package/src/build/static.ts +1 -1
  30. package/src/cli/check-cache.ts +51 -0
  31. package/src/cli/check-consent.ts +95 -0
  32. package/src/cli/check-forms.ts +10 -2
  33. package/src/cli/check-links-patterns.ts +101 -0
  34. package/src/cli/check-links.ts +30 -105
  35. package/src/cli/check-regions.ts +185 -0
  36. package/src/cli/check-security.ts +50 -6
  37. package/src/cli/check.ts +49 -21
  38. package/src/cli/commands.ts +138 -16
  39. package/src/cli/docs.ts +21 -2
  40. package/src/cli/form-opaque-children.ts +40 -0
  41. package/src/cli/index.ts +1 -1
  42. package/src/cli/inspect.ts +2 -0
  43. package/src/cli/mcp-tools.ts +2 -2
  44. package/src/cli/new.ts +9 -5
  45. package/src/cli/upgrade-names.ts +14 -2
  46. package/src/cli/upgrade.ts +3 -0
  47. package/src/compiler/analyze.ts +22 -16
  48. package/src/compiler/cacheability-private.ts +13 -3
  49. package/src/compiler/cacheability.ts +24 -2
  50. package/src/compiler/compliance-contrast.ts +322 -0
  51. package/src/compiler/compliance-empty.ts +24 -0
  52. package/src/compiler/compliance-rgaa-content.ts +95 -0
  53. package/src/compiler/compliance-rgaa-criteria.ts +45 -0
  54. package/src/compiler/compliance-rgaa-pages.ts +140 -0
  55. package/src/compiler/compliance-rgaa.ts +19 -0
  56. package/src/compiler/compliance-rules.ts +72 -0
  57. package/src/compiler/compliance-third-party.ts +59 -0
  58. package/src/compiler/compliance-urls.ts +52 -0
  59. package/src/compiler/compliance.ts +61 -93
  60. package/src/compiler/cookie-inventory.ts +119 -0
  61. package/src/compiler/dom-attrs.ts +178 -22
  62. package/src/compiler/dom.ts +31 -44
  63. package/src/compiler/emit.ts +17 -6
  64. package/src/compiler/expr-context.ts +260 -207
  65. package/src/compiler/foreign-imports.ts +5 -3
  66. package/src/compiler/mistakes.ts +18 -9
  67. package/src/compiler/props-body.ts +263 -21
  68. package/src/compiler/props-pattern.ts +211 -0
  69. package/src/compiler/props.ts +324 -343
  70. package/src/compiler/signal-reads.ts +142 -29
  71. package/src/compiler/spread-handlers.ts +25 -13
  72. package/src/compiler/ssr-emit.ts +510 -0
  73. package/src/compiler/ssr-helpers.ts +165 -121
  74. package/src/compiler/ssr.ts +28 -441
  75. package/src/context.ts +14 -1
  76. package/src/dev/child.ts +1 -1
  77. package/src/dev/host-check.ts +2 -1
  78. package/src/dev/hot-islands.ts +71 -0
  79. package/src/dev/index.ts +31 -18
  80. package/src/dev/protocol.ts +1 -1
  81. package/src/dev/queue.ts +3 -2
  82. package/src/dev/reload.ts +5 -2
  83. package/src/dev/supervisor.ts +3 -1
  84. package/src/dev/watchers.ts +6 -2
  85. package/src/dom/action.ts +97 -14
  86. package/src/dom/attrs.ts +93 -25
  87. package/src/dom/extras.ts +91 -50
  88. package/src/dom/field-marks.ts +32 -0
  89. package/src/dom/hot.ts +9 -0
  90. package/src/dom/index.ts +44 -18
  91. package/src/dom/island-hot.ts +29 -0
  92. package/src/dom/island.ts +13 -2
  93. package/src/dom/loop.ts +259 -188
  94. package/src/dom/prefetch.ts +135 -0
  95. package/src/dom/regions.ts +515 -0
  96. package/src/dom/swap.ts +246 -0
  97. package/src/dom/trusted.ts +22 -0
  98. package/src/dom/upload.ts +44 -0
  99. package/src/eco/declaration.ts +44 -41
  100. package/src/eco/index.ts +8 -5
  101. package/src/href.ts +31 -0
  102. package/src/index.ts +12 -5
  103. package/src/plugins/config.ts +1 -1
  104. package/src/plugins/context.ts +7 -1
  105. package/src/plugins/daily.ts +27 -0
  106. package/src/plugins/index.ts +3 -3
  107. package/src/plugins/order.ts +1 -1
  108. package/src/plugins/public.ts +12 -5
  109. package/src/plugins/services.ts +88 -14
  110. package/src/plugins/types.ts +128 -10
  111. package/src/reactivity/graph.ts +244 -41
  112. package/src/reactivity/hmr.ts +101 -0
  113. package/src/reactivity/index.ts +243 -239
  114. package/src/reactivity/resource.ts +129 -0
  115. package/src/reactivity/seed.ts +141 -0
  116. package/src/reactivity/selector.ts +85 -47
  117. package/src/reactivity/server-seed.ts +202 -0
  118. package/src/reactivity/store.ts +331 -97
  119. package/src/rgpd/draft.ts +171 -0
  120. package/src/rgpd/forms.ts +85 -0
  121. package/src/rgpd/index.ts +57 -0
  122. package/src/server/action-flash.ts +1 -0
  123. package/src/server/action-protect.ts +3 -0
  124. package/src/server/action-registry.ts +11 -9
  125. package/src/server/action.ts +51 -19
  126. package/src/server/app-secret.ts +14 -8
  127. package/src/server/cache-invalidation.ts +97 -0
  128. package/src/server/compress.ts +55 -18
  129. package/src/server/consent.ts +21 -5
  130. package/src/server/cookie-inventory.ts +78 -0
  131. package/src/server/cookies.ts +77 -8
  132. package/src/server/cors.ts +98 -0
  133. package/src/server/csp-report.ts +90 -0
  134. package/src/server/discard.ts +13 -0
  135. package/src/server/document.ts +2 -2
  136. package/src/server/env.ts +1 -1
  137. package/src/server/errors.ts +9 -2
  138. package/src/server/field-errors.ts +91 -0
  139. package/src/server/form-schema.ts +16 -11
  140. package/src/server/health.ts +41 -11
  141. package/src/server/http.ts +28 -11
  142. package/src/server/index.ts +122 -307
  143. package/src/server/loader.ts +2 -0
  144. package/src/server/page.ts +3 -9
  145. package/src/server/plugin-host.ts +66 -16
  146. package/src/server/plugin-route-shadow.ts +24 -0
  147. package/src/server/public.ts +14 -3
  148. package/src/server/rate-limit.ts +49 -10
  149. package/src/server/region.ts +22 -2
  150. package/src/server/request-handler.ts +406 -0
  151. package/src/server/request-log.ts +94 -0
  152. package/src/server/route-cache.ts +55 -8
  153. package/src/server/route-input.ts +54 -0
  154. package/src/server/route-table.ts +53 -27
  155. package/src/server/router.ts +2 -1
  156. package/src/server/routing.ts +104 -45
  157. package/src/server/sanitize-html.ts +4 -2
  158. package/src/server/security.ts +38 -8
  159. package/src/server/serve-handlers.ts +16 -6
  160. package/src/server/served-file.ts +19 -14
  161. package/src/server/shared-store.ts +323 -0
  162. package/src/server/start.ts +12 -5
  163. package/src/server/static-dir.ts +34 -12
  164. package/src/server/stream.ts +2 -1
  165. package/src/server/surface.ts +39 -0
  166. package/src/server/webhook.ts +50 -0
  167. package/src/server/workers.ts +168 -0
  168. package/src/ssr/consent-banner.ts +20 -16
  169. package/src/ssr/consent-texts.ts +40 -0
  170. package/src/ssr/context.ts +174 -53
  171. package/src/ssr/cookie-table.ts +75 -0
  172. package/src/ssr/index.ts +63 -58
  173. package/src/ssr/navigation.ts +161 -14
  174. package/src/ssr/region.ts +9 -3
  175. package/src/style.ts +12 -5
  176. package/src/test/browser.ts +53 -23
  177. package/src/test/build-reuse.ts +85 -0
  178. package/src/test/index.ts +51 -8
  179. package/src/test/mount.ts +53 -0
  180. package/src/test/outside.ts +94 -0
  181. package/src/ts-plugin/diagnostics.ts +56 -18
  182. package/templates/_shared/AGENTS.md +1 -1
  183. package/templates/_shared/Dockerfile +22 -0
  184. package/templates/_shared/dockerignore +9 -0
  185. package/templates/_shared/gitignore +2 -0
  186. package/templates/app/.env.example +3 -1
  187. package/templates/app/compose.yaml +32 -0
  188. package/templates/app/db/migrations/001_notes.sql +7 -0
  189. package/templates/app/db/schema.ts +11 -0
  190. package/templates/app/package.json +1 -0
  191. package/templates/app/prunelle.config.ts +6 -0
  192. package/templates/app/src/actions/notes.ts +26 -0
  193. package/templates/app/src/auth.ts +4 -2
  194. package/templates/app/src/db.ts +5 -0
  195. package/templates/app/src/routes/dashboard/index.tsx +26 -1
  196. package/templates/app/src/routes/privacy.tsx +6 -2
  197. package/templates/app/tests/app.test.ts +14 -3
  198. package/templates/plugin/AGENTS.md +11 -0
  199. package/templates/plugin/CLAUDE.md +1 -0
  200. package/templates/plugin/README.md +27 -0
  201. package/templates/plugin/gitignore +2 -0
  202. package/templates/plugin/llms.txt +19 -0
  203. package/templates/plugin/package.json +33 -0
  204. package/templates/plugin/src/index.ts +40 -0
  205. package/templates/plugin/tests/fixture/prunelle.config.ts +4 -0
  206. package/templates/plugin/tests/fixture/src/routes/index.tsx +11 -0
  207. package/templates/plugin/tests/plugin.test.ts +42 -0
  208. package/templates/plugin/tsconfig.json +19 -0
  209. package/ts-plugin.cjs +4058 -2246
package/src/dom/extras.ts CHANGED
@@ -5,11 +5,12 @@ import type { CatchProps } from '../context'
5
5
  import type { Owned } from '../reactivity/graph'
6
6
  import type { Owner } from '../reactivity/index'
7
7
  import { DOM_PROPS, isBlockedAttr } from '../attrs'
8
- import { batch, createOwner, disposeOwner, effect, getOwner, onCleanup, onEffectError, runOwned, runWithOwner, signal, untrack } from '../reactivity/index'
8
+ import { batch, createOwner, disposeOwner, domEffect, getOwner, onCleanup, onEffectError, runOwned, runWithOwner, signal, untrack } from '../reactivity/index'
9
9
  import { assertRenderable } from '../unrenderable'
10
10
  import { setAttr, setClass, setProp, setStyle } from './attrs'
11
11
  import { claimInsert } from './claim'
12
12
  import { claimRange, delegate, hydrateCursor, isHydrating, moveCursor, skipRange } from './index'
13
+ import { trustedHtml } from './trusted'
13
14
 
14
15
  // A literal rather than a split string, so the bundler drops it from an island that imports this module for its chunk alone (#3683).
15
16
  const DELEGATED_EVENTS = new Set(['beforeinput', 'click', 'dblclick', 'focusin', 'focusout', 'input', 'keydown', 'keyup', 'mousedown', 'mouseup', 'pointerdown', 'pointerup', 'touchstart', 'touchend'])
@@ -31,7 +32,7 @@ export function setInnerHtml(el: Element, value: unknown): void {
31
32
  else
32
33
  html = String(value)
33
34
  if (el.innerHTML !== html)
34
- el.innerHTML = html
35
+ el.innerHTML = trustedHtml(html)
35
36
  }
36
37
 
37
38
  const EVENT_PROP = /^on([A-Z]\w*)$/
@@ -83,8 +84,50 @@ export function omit(props: Record<string, unknown>, excluded: string[]): Record
83
84
  return out
84
85
  }
85
86
 
87
+ function applySpreadProp(el: Element, name: string, value: unknown): void {
88
+ if (name === 'class')
89
+ setClass(el, value)
90
+ else if (name === 'style')
91
+ setStyle(el as HTMLElement, value)
92
+ else if (DOM_PROPS.has(name))
93
+ setProp(el, name, value)
94
+ else
95
+ setAttr(el, name, value)
96
+ }
97
+
98
+ function clearRemovedProps(el: Element, props: Record<string, unknown>, prevVals: Map<string, unknown>): void {
99
+ for (const k of prevVals.keys()) {
100
+ if (Object.hasOwn(props, k))
101
+ continue
102
+ prevVals.delete(k)
103
+ if (k === 'class' || k === 'className') {
104
+ const other = k === 'class' ? 'className' : 'class'
105
+ if (Object.hasOwn(props, other))
106
+ prevVals.delete(other)
107
+ else
108
+ setClass(el, '')
109
+ }
110
+ else if (k === 'style') {
111
+ setStyle(el as HTMLElement, null)
112
+ }
113
+ else {
114
+ const name = k === 'htmlFor' ? 'for' : k
115
+ if (name === 'for') {
116
+ const other = k === 'for' ? 'htmlFor' : 'for'
117
+ if (Object.hasOwn(props, other)) {
118
+ prevVals.delete(other)
119
+ continue
120
+ }
121
+ }
122
+ if (DOM_PROPS.has(name))
123
+ setProp(el, name, name === 'value' ? '' : false)
124
+ setAttr(el, name, null)
125
+ }
126
+ }
127
+ }
128
+
86
129
  export function spread(el: Element, getProps: () => Record<string, unknown> | null | undefined): void {
87
- let prevKeys: string[] = []
130
+ const prevVals = new Map<string, unknown>()
88
131
  let lastRef: unknown
89
132
  const elRec = el as unknown as Record<string, unknown>
90
133
  // Delegated events share document-level dispatch with compiled handlers so stopPropagation() runs child->parent.
@@ -114,32 +157,15 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
114
157
  for (const [type, entry] of delegated)
115
158
  releaseDelegated(type, entry)
116
159
  delegated.clear()
160
+ prevVals.clear()
117
161
  })
118
162
 
119
- effect(() => {
163
+ domEffect(() => {
120
164
  const types = new Set<string>()
121
165
  const props = getProps() ?? {}
122
166
  const nextKeys = Object.keys(props)
123
167
 
124
- for (let i = 0; i < prevKeys.length; i++) {
125
- const k = prevKeys[i]!
126
- if (k in props)
127
- continue
128
- if (k === 'class' || k === 'className') {
129
- setClass(el, '')
130
- }
131
- else if (k === 'style') {
132
- setStyle(el as HTMLElement, null)
133
- }
134
- else if (k !== 'key' && k !== 'ref' && k !== '$hydrate' && k !== 'children') {
135
- const name = k === 'htmlFor' ? 'for' : k
136
- if (!isBlockedAttr(name)) {
137
- if (DOM_PROPS.has(name))
138
- setProp(el, name, name === 'value' ? '' : false)
139
- setAttr(el, name, null)
140
- }
141
- }
142
- }
168
+ clearRemovedProps(el, props, prevVals)
143
169
 
144
170
  for (let i = 0; i < nextKeys.length; i++) {
145
171
  const k = nextKeys[i]!
@@ -167,7 +193,7 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
167
193
  delegate([type])
168
194
  elRec[key] = v
169
195
  }
170
- else if (elRec[key] === entry[0]) {
196
+ else if (elRec[key] === entry[0] && entry[0] !== v) {
171
197
  elRec[key] = v
172
198
  entry[0] = v
173
199
  }
@@ -182,14 +208,12 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
182
208
  const name = k === 'className' ? 'class' : k === 'htmlFor' ? 'for' : k
183
209
  if (isBlockedAttr(name))
184
210
  continue
185
- if (name === 'class')
186
- setClass(el, v)
187
- else if (name === 'style')
188
- setStyle(el as HTMLElement, v)
189
- else if (DOM_PROPS.has(name))
190
- setProp(el, name, v)
191
- else
192
- setAttr(el, name, v)
211
+ // A reactive store() style object may mutate in place while keeping the same reference; setStyle diffs per property.
212
+ const isStyleObj = name === 'style' && v !== null && typeof v === 'object'
213
+ if (!isStyleObj && prevVals.has(k) && Object.is(prevVals.get(k), v))
214
+ continue
215
+ prevVals.set(k, v)
216
+ applySpreadProp(el, name, v)
193
217
  }
194
218
 
195
219
  for (const [type, entry] of delegated) {
@@ -204,7 +228,6 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
204
228
  el.removeEventListener(type, listen)
205
229
  }
206
230
  }
207
- prevKeys = nextKeys
208
231
  })
209
232
  }
210
233
 
@@ -282,35 +305,53 @@ function claimTextNode(claimed: Node | null, str: string): Text | undefined {
282
305
  }
283
306
 
284
307
  export function insert(parent: Node, value: () => unknown, marker: Node | null = null, startMarker?: Node | null): void {
308
+ domEffect(inserter(parent, value, marker, startMarker))
309
+ }
310
+
311
+ // A child the compiler proved reads no signal (a hoisted loop key, a plain const): rendered once, with no effect to allocate or dispose.
312
+ export function insertStatic(parent: Node, value: unknown, marker: Node | null = null, startMarker?: Node | null): void {
313
+ inserter(parent, () => value, marker, startMarker)()
314
+ }
315
+
316
+ function inserter(parent: Node, value: () => unknown, marker: Node | null, startMarker: Node | null | undefined): () => void {
285
317
  let current: Node | Node[] | null = null
286
318
  let textNode: Text | undefined
287
319
  let firstRun = true
288
320
 
289
- effect(() => {
290
- let res: { result: unknown, marker: Node | null, claimed: Node | null, nodes: Node[] | undefined, consumed: boolean }
321
+ return () => {
322
+ let next: unknown
323
+ let claimedNode: Node | null = null
324
+ let ssrNodes: Node[] | undefined
325
+ let consumed = false
291
326
  if (firstRun) {
292
327
  firstRun = false
293
- let effMarker = marker
294
328
  let effStart = startMarker
295
- if (effStart === undefined && effMarker !== null && effMarker.nodeType === 8) {
296
- const close = skipRange(effMarker)
329
+ if (effStart === undefined && marker !== null && marker.nodeType === 8) {
330
+ const close = skipRange(marker)
297
331
  if (close !== null) {
298
- effStart = effMarker
299
- effMarker = close
332
+ effStart = marker
333
+ marker = close
300
334
  }
301
335
  }
302
- res = claimInsert(parent, effMarker, value, effStart)
303
- marker = res.marker
336
+ if (isHydrating()) {
337
+ const res = claimInsert(parent, marker, value, effStart)
338
+ marker = res.marker
339
+ next = res.result
340
+ claimedNode = res.claimed
341
+ ssrNodes = res.nodes
342
+ consumed = res.consumed
343
+ }
344
+ else {
345
+ next = value()
346
+ }
304
347
  }
305
348
  else {
306
- res = { result: value(), marker, claimed: null, nodes: undefined, consumed: false }
349
+ next = value()
307
350
  }
308
- const next = res.result
309
- const ssrNodes = res.nodes
310
351
  // An insert born in a fragment follows its marker into the parent the fragment was appended to.
311
352
  const host = marker?.parentNode ?? parent
312
353
 
313
- if (next === null && res.consumed && ssrNodes !== undefined) {
354
+ if (next === null && consumed && ssrNodes !== undefined) {
314
355
  current = ssrNodes
315
356
  return
316
357
  }
@@ -325,14 +366,14 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
325
366
  return
326
367
  }
327
368
 
328
- if (typeof next === 'string' || typeof next === 'number') {
369
+ if (typeof next === 'string' || typeof next === 'number' || typeof next === 'bigint') {
329
370
  const str = String(next)
330
371
  if (current !== null) {
331
372
  clearRendered(current)
332
373
  current = null
333
374
  }
334
375
  if (textNode === undefined) {
335
- const claimed = claimTextNode(res.claimed, str)
376
+ const claimed = claimTextNode(claimedNode, str)
336
377
  if (claimed !== undefined) {
337
378
  textNode = claimed
338
379
  if (ssrNodes !== undefined)
@@ -390,7 +431,7 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
390
431
  clearRendered(current)
391
432
  current = insertAll(host, marker, next, ssrNodes)
392
433
  }
393
- })
434
+ }
394
435
  }
395
436
 
396
437
  // A dynamic child of a root fragment: hydrates against its SSR range, or renders between two comments that travel with the fragment, so whoever removes the fragment's nodes removes what it renders later.
@@ -475,7 +516,7 @@ export function show(
475
516
  nodes = []
476
517
  }
477
518
 
478
- effect(() => {
519
+ domEffect(() => {
479
520
  const cond = Boolean(when())
480
521
  const nextBranch: 1 | 2 = cond ? 1 : 2
481
522
  if (branch === nextBranch)
@@ -0,0 +1,32 @@
1
+ // Loaded by `useAction` only once a submit fails with field errors or left marks to undo, so a form that never fails ships none of it.
2
+
3
+ // Each field `errors` names gets `aria-invalid` and is described by the element of its form whose whole text is its message: the first after it, else the last before it (RGAA 11.10, 11.11). A field the app or `Field` marked itself is left alone; `data-pru-err` keeps what its `aria-describedby` was, to undo the marks at the next submit.
4
+ export function markFields(form: HTMLFormElement, errors: Record<string, string> = {}): void {
5
+ const all = [...form.querySelectorAll('*')]
6
+ for (const field of [...form.elements] as HTMLInputElement[]) {
7
+ const was = field.getAttribute('data-pru-err')
8
+ if (was !== null) {
9
+ field.removeAttribute('data-pru-err')
10
+ field.removeAttribute('aria-invalid')
11
+ was ? field.setAttribute('aria-describedby', was) : field.removeAttribute('aria-describedby')
12
+ }
13
+ const message = Object.hasOwn(errors, field.name) && errors[field.name]
14
+ if (!message || field.hasAttribute('aria-invalid'))
15
+ continue
16
+ const shows = (el: Element) => el.textContent!.trim() === message
17
+ const shown = all.find(el => shows(el) && field.compareDocumentPosition(el) & 4) ?? all.findLast(shows)
18
+ const before = field.getAttribute('aria-describedby') || ''
19
+ field.setAttribute('data-pru-err', before)
20
+ field.setAttribute('aria-invalid', 'true')
21
+ if (shown)
22
+ field.setAttribute('aria-describedby', `${before} ${shown.id ||= freeId(form, field.name)}`.trim())
23
+ }
24
+ }
25
+
26
+ // An id no element of the document holds yet, so two forms with a same-named field do not share one.
27
+ function freeId(form: HTMLFormElement, name: string): string {
28
+ const base = `pru-err-${name.replace(/\s+/g, '-')}`
29
+ let id = base
30
+ for (let n = 1; form.ownerDocument.getElementById(id); n++) id = `${base}-${n}`
31
+ return id
32
+ }
package/src/dom/hot.ts ADDED
@@ -0,0 +1,9 @@
1
+ // Dev only: the island chunks `prunelle dev` build export `hotMount` from here, which also puts the hooks that keep signals across a swap on the page.
2
+
3
+ import { hooks } from '../reactivity/hmr'
4
+ import { mount } from './index'
5
+
6
+ /** Renders an island again over the DOM it has, its signals starting from the values the render `previous` disposed ended with. */
7
+ export function hotMount(container: Element, render: () => Node | DocumentFragment | null, idPrefix: string | undefined, previous: () => void): () => void {
8
+ return hooks.restore(previous, () => mount(container, render, idPrefix))
9
+ }
package/src/dom/index.ts CHANGED
@@ -1,11 +1,14 @@
1
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
2
 
3
+ import type { HmrHost } from '../reactivity/hmr'
3
4
  import { batch, createOwner, disposeOwner, getOwner, onCleanup, runWithOwner } from '../reactivity/index'
4
5
  import { assertRenderable } from '../unrenderable'
6
+ import { trustedHtml } from './trusted'
5
7
 
6
- export { effect } from '../reactivity/index'
8
+ export { domEffect as effect } from '../reactivity/index'
7
9
 
8
10
  let hydrating = false
11
+ let mounting = false
9
12
  let hydrateNode: Node | null = null
10
13
  let hydrateRoot: Element | null = null
11
14
  let mismatchHook: ((parent: Node, stale: Node, node: Node) => void) | null = null
@@ -28,6 +31,11 @@ export function isHydrating(): boolean {
28
31
  return hydrating
29
32
  }
30
33
 
34
+ /** True while a client-only island renders for the first time. */
35
+ export function isMounting(): boolean {
36
+ return mounting
37
+ }
38
+
31
39
  /** Points the hydration cursor at `node` (claiming SSR nodes when `on`) and returns the cursor it replaced, for the caller to restore. */
32
40
  export function moveCursor(on: boolean, node: Node | null): [boolean, Node | null] {
33
41
  const prev: [boolean, Node | null] = [hydrating, hydrateNode]
@@ -49,7 +57,7 @@ export function hydrate(container: Element, render: () => Node | DocumentFragmen
49
57
  hydrateRoot = container
50
58
 
51
59
  try {
52
- const node = runWithOwner(owner, render)
60
+ const node = runWithOwner(owner, process.env.NODE_ENV !== 'production' ? (globalThis as HmrHost).__pruHmr?.wrap(render) ?? render : render)
53
61
  if (node !== null && node.parentNode === null)
54
62
  container.appendChild(node)
55
63
  }
@@ -64,7 +72,8 @@ export function hydrate(container: Element, render: () => Node | DocumentFragmen
64
72
  hydrateRoot = prevRoot
65
73
  }
66
74
 
67
- return () => disposeOwner(owner)
75
+ const dispose = () => disposeOwner(owner)
76
+ return process.env.NODE_ENV !== 'production' ? (globalThis as HmrHost).__pruHmr?.keep(dispose) ?? dispose : dispose
68
77
  }
69
78
 
70
79
  export function mount(container: Element, render: () => Node | DocumentFragment | null, idPrefix?: string): () => void {
@@ -72,9 +81,11 @@ export function mount(container: Element, render: () => Node | DocumentFragment
72
81
  if (idPrefix !== undefined)
73
82
  owner.id = { p: idPrefix, n: 0 }
74
83
  const prevHydrating = hydrating
84
+ const prevMounting = mounting
75
85
  hydrating = false
86
+ mounting = true
76
87
  try {
77
- const node = runWithOwner(owner, render)
88
+ const node = runWithOwner(owner, process.env.NODE_ENV !== 'production' ? (globalThis as HmrHost).__pruHmr?.wrap(render) ?? render : render)
78
89
  container.textContent = ''
79
90
  if (node !== null)
80
91
  container.appendChild(node)
@@ -85,8 +96,10 @@ export function mount(container: Element, render: () => Node | DocumentFragment
85
96
  }
86
97
  finally {
87
98
  hydrating = prevHydrating
99
+ mounting = prevMounting
88
100
  }
89
- return () => disposeOwner(owner)
101
+ const dispose = () => disposeOwner(owner)
102
+ return process.env.NODE_ENV !== 'production' ? (globalThis as HmrHost).__pruHmr?.keep(dispose) ?? dispose : dispose
90
103
  }
91
104
 
92
105
  function warnMismatch(node: Node, expectedTag: string, steppedOver: boolean): void {
@@ -108,7 +121,7 @@ export function template(html: string, isSvg?: boolean): () => Node {
108
121
  const clone = (): Node => {
109
122
  if (cached === undefined) {
110
123
  const t = document.createElement('template')
111
- t.innerHTML = isSvg ? `<svg>${html}</svg>` : html
124
+ t.innerHTML = trustedHtml(isSvg ? `<svg>${html}</svg>` : html)
112
125
  cached = isSvg ? t.content.firstChild!.firstChild! : t.content.firstChild!
113
126
  }
114
127
  return cached.cloneNode(true)
@@ -260,11 +273,17 @@ export function setText(node: Text, value: unknown): void {
260
273
  const delegated = new WeakMap<Document, Set<string>>()
261
274
 
262
275
  function dispatchEvent(event: Event): void {
276
+ // A swapped island's chunk carries its own copy of this runtime, and with it another listener on the document.
277
+ if (process.env.NODE_ENV !== 'production') {
278
+ const seen = event as Event & Record<symbol, boolean>
279
+ if (seen[Symbol.for('prunelle.dispatched')])
280
+ return
281
+ seen[Symbol.for('prunelle.dispatched')] = true
282
+ }
263
283
  const key = `$$${event.type}`
264
- let current: EventTarget | null = null
265
- const prevDesc = Object.getOwnPropertyDescriptor(event, 'currentTarget')
266
- Object.defineProperty(event, 'currentTarget', { configurable: true, get: () => current })
267
- const errors: unknown[] = []
284
+ let current: EventTarget | undefined
285
+ let prevDesc: PropertyDescriptor | undefined
286
+ let errors: unknown[] | undefined
268
287
 
269
288
  try {
270
289
  const path = event.composedPath?.()
@@ -274,12 +293,16 @@ function dispatchEvent(event: Event): void {
274
293
  while (node != null) {
275
294
  const handler = node[key]
276
295
  if (typeof handler === 'function') {
296
+ if (current === undefined) {
297
+ prevDesc = Object.getOwnPropertyDescriptor(event, 'currentTarget')
298
+ Object.defineProperty(event, 'currentTarget', { configurable: true, get: () => current ?? null })
299
+ }
277
300
  current = node
278
301
  try {
279
302
  batch(() => handler(event))
280
303
  }
281
304
  catch (err) {
282
- errors.push(err)
305
+ ;(errors ??= []).push(err)
283
306
  }
284
307
  if (event.cancelBubble)
285
308
  break
@@ -288,16 +311,19 @@ function dispatchEvent(event: Event): void {
288
311
  }
289
312
  }
290
313
  finally {
291
- if (prevDesc !== undefined)
292
- Object.defineProperty(event, 'currentTarget', prevDesc)
293
- else
294
- delete (event as unknown as Record<string, unknown>).currentTarget
314
+ if (current !== undefined) {
315
+ if (prevDesc !== undefined)
316
+ Object.defineProperty(event, 'currentTarget', prevDesc)
317
+ else
318
+ delete (event as unknown as Record<string, unknown>).currentTarget
319
+ }
295
320
  }
296
321
  // Every handler ran: the first error is thrown as before, the others are reported.
297
- for (let i = 1; i < errors.length; i++)
298
- reportError(errors[i])
299
- if (errors.length > 0)
322
+ if (errors !== undefined) {
323
+ for (let i = 1; i < errors.length; i++)
324
+ reportError(errors[i])
300
325
  throw errors[0]
326
+ }
301
327
  }
302
328
 
303
329
  export function delegate(events: string[]): void {
@@ -0,0 +1,29 @@
1
+ // Dev only: `prunelle dev` swaps a hydrated island's code in the open page. island.ts reaches this behind `process.env.NODE_ENV !== 'production'`, so a production build holds none of it.
2
+
3
+ import type { hotMount } from './hot'
4
+
5
+ /** What the loader keeps of a hydrated island, for the swap to render it again. */
6
+ export interface HotRender { props: Record<string, unknown>, idPrefix: string }
7
+
8
+ export interface HotIsland extends HTMLElement {
9
+ _d: (() => void) | undefined
10
+ _hr: HotRender | undefined
11
+ }
12
+
13
+ type HotChunk = { default: (props: Record<string, unknown>) => Node | DocumentFragment | null, hotMount: typeof hotMount }
14
+
15
+ /** Points `el` at the chunk the edit built and, when it has hydrated, renders it again from the new code with the values its signals held. */
16
+ export async function hotSwapIsland(el: HotIsland, chunk: string): Promise<void> {
17
+ el.setAttribute('c', chunk)
18
+ const previous = el._d
19
+ const kept = el._hr
20
+ // One still waiting for its strategy loads the new chunk when that fires.
21
+ if (previous === undefined || kept === undefined)
22
+ return
23
+ const loaded = await import(chunk) as HotChunk
24
+ if (el._d !== previous || !el.isConnected)
25
+ return
26
+ previous()
27
+ el._d = undefined
28
+ el._d = loaded.hotMount(el, () => loaded.default(kept.props), kept.idPrefix, previous)
29
+ }
package/src/dom/island.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  // Native <pru-island> Custom Element: the build defines which strategies an app uses, so the loader drops the code of the others.
2
2
 
3
3
  import type { hydrate, mount } from './index'
4
+ import type { HotIsland, HotRender } from './island-hot'
5
+ import { hotSwapIsland } from './island-hot'
4
6
 
5
7
  declare const PRUNELLE_VISIBLE: boolean | undefined
6
8
  declare const PRUNELLE_IDLE: boolean | undefined
@@ -146,6 +148,9 @@ function watchInteraction(root: Element, onTrigger: (after: () => void) => Promi
146
148
  function isAllowedChunkUrl(chunk: string): boolean {
147
149
  if (/[\u0000-\u0020\u007F]/.test(chunk))
148
150
  return false
151
+ // A browser reads `//host` and `/\host` as another site; any other path from `/` is this one, and the chunks prunelle writes are such paths.
152
+ if (chunk[0] === '/' && chunk[1] !== '/' && chunk[1] !== '\\')
153
+ return true
149
154
  if (typeof location !== 'undefined' && location.origin && location.origin !== 'null') {
150
155
  try {
151
156
  return new URL(chunk, location.href).origin === location.origin
@@ -154,9 +159,8 @@ function isAllowedChunkUrl(chunk: string): boolean {
154
159
  return false
155
160
  }
156
161
  }
157
- // A browser reads `//host` and `/\host` as another site.
158
162
  if (chunk[0] === '/')
159
- return chunk[1] !== '/' && chunk[1] !== '\\'
163
+ return false
160
164
  return !/^(?:javascript|data|vbscript):/i.test(chunk)
161
165
  }
162
166
 
@@ -202,6 +206,8 @@ export function defineIslandElement(): void {
202
206
  _slot: Element | undefined
203
207
  _s = false
204
208
  _t = 0
209
+ declare _hr?: HotRender
210
+ declare _h?: (chunk: string) => Promise<void>
205
211
 
206
212
  _clearStrategy(token?: number): void {
207
213
  if (token !== undefined && token !== this._t)
@@ -324,6 +330,8 @@ export function defineIslandElement(): void {
324
330
  return true
325
331
  }
326
332
  this._d = dispose
333
+ if (process.env.NODE_ENV !== 'production')
334
+ this._hr = { props, idPrefix }
327
335
  // Queued behind the component's `onMount` callbacks, so a listener it binds there hears the replayed event.
328
336
  if (after)
329
337
  queueMicrotask(after)
@@ -340,5 +348,8 @@ export function defineIslandElement(): void {
340
348
  }
341
349
  }
342
350
 
351
+ if (process.env.NODE_ENV !== 'production')
352
+ PrunelleIsland.prototype._h = function (this: PrunelleIsland, chunk) { return hotSwapIsland(this as unknown as HotIsland, chunk) }
353
+
343
354
  customElements.define('pru-island', PrunelleIsland)
344
355
  }