prunelle 0.9.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 (96) hide show
  1. package/llms.txt +18 -18
  2. package/package.json +2 -2
  3. package/src/a11y/declaration.ts +4 -7
  4. package/src/build/bundle.ts +6 -2
  5. package/src/build/index.ts +3 -1
  6. package/src/build/island-promote.ts +42 -2
  7. package/src/build/islands.ts +59 -8
  8. package/src/build/manifest.ts +98 -0
  9. package/src/build/plugin-host.ts +11 -1
  10. package/src/build/prerender.ts +6 -1
  11. package/src/build/reach.ts +4 -3
  12. package/src/build/resolve.ts +5 -1
  13. package/src/build/routes.ts +18 -0
  14. package/src/cli/check-forms.ts +8 -0
  15. package/src/cli/check-links.ts +14 -5
  16. package/src/cli/check.ts +12 -6
  17. package/src/cli/commands.ts +5 -3
  18. package/src/cli/form-opaque-children.ts +40 -0
  19. package/src/compiler/analyze.ts +20 -14
  20. package/src/compiler/cacheability-private.ts +13 -3
  21. package/src/compiler/compliance-contrast.ts +48 -10
  22. package/src/compiler/compliance-rules.ts +72 -0
  23. package/src/compiler/compliance.ts +6 -66
  24. package/src/compiler/dom-attrs.ts +178 -22
  25. package/src/compiler/dom.ts +31 -44
  26. package/src/compiler/emit.ts +17 -6
  27. package/src/compiler/expr-context.ts +260 -207
  28. package/src/compiler/props-body.ts +263 -21
  29. package/src/compiler/props.ts +155 -5
  30. package/src/compiler/signal-reads.ts +142 -29
  31. package/src/compiler/spread-handlers.ts +25 -13
  32. package/src/compiler/ssr-emit.ts +3 -3
  33. package/src/compiler/ssr-helpers.ts +0 -5
  34. package/src/compiler/ssr.ts +4 -2
  35. package/src/dev/host-check.ts +2 -1
  36. package/src/dev/reload.ts +4 -1
  37. package/src/dom/attrs.ts +93 -25
  38. package/src/dom/extras.ts +89 -49
  39. package/src/dom/field-marks.ts +9 -1
  40. package/src/dom/index.ts +19 -13
  41. package/src/dom/loop.ts +259 -188
  42. package/src/dom/prefetch.ts +4 -1
  43. package/src/dom/regions.ts +25 -8
  44. package/src/dom/swap.ts +11 -10
  45. package/src/dom/trusted.ts +4 -4
  46. package/src/eco/declaration.ts +5 -1
  47. package/src/eco/index.ts +3 -0
  48. package/src/index.ts +3 -2
  49. package/src/plugins/config.ts +1 -1
  50. package/src/plugins/public.ts +4 -0
  51. package/src/plugins/types.ts +8 -1
  52. package/src/reactivity/graph.ts +244 -41
  53. package/src/reactivity/hmr.ts +14 -3
  54. package/src/reactivity/index.ts +219 -235
  55. package/src/reactivity/resource.ts +17 -3
  56. package/src/reactivity/seed.ts +118 -16
  57. package/src/reactivity/selector.ts +85 -47
  58. package/src/reactivity/server-seed.ts +31 -23
  59. package/src/reactivity/store.ts +316 -136
  60. package/src/server/action-protect.ts +3 -0
  61. package/src/server/action-registry.ts +5 -1
  62. package/src/server/cache-invalidation.ts +4 -3
  63. package/src/server/compress.ts +52 -17
  64. package/src/server/consent.ts +5 -3
  65. package/src/server/cookies.ts +27 -8
  66. package/src/server/cors.ts +98 -0
  67. package/src/server/csp-report.ts +8 -4
  68. package/src/server/health.ts +9 -0
  69. package/src/server/http.ts +3 -1
  70. package/src/server/index.ts +25 -13
  71. package/src/server/loader.ts +1 -0
  72. package/src/server/plugin-host.ts +41 -9
  73. package/src/server/plugin-route-shadow.ts +24 -0
  74. package/src/server/rate-limit.ts +13 -3
  75. package/src/server/region.ts +19 -2
  76. package/src/server/request-handler.ts +58 -22
  77. package/src/server/request-log.ts +1 -1
  78. package/src/server/route-cache.ts +14 -2
  79. package/src/server/route-table.ts +21 -11
  80. package/src/server/router.ts +2 -1
  81. package/src/server/routing.ts +100 -42
  82. package/src/server/sanitize-html.ts +3 -1
  83. package/src/server/serve-handlers.ts +10 -4
  84. package/src/server/served-file.ts +19 -14
  85. package/src/server/shared-store.ts +10 -1
  86. package/src/server/static-dir.ts +21 -3
  87. package/src/server/webhook.ts +6 -4
  88. package/src/ssr/context.ts +149 -51
  89. package/src/ssr/index.ts +41 -46
  90. package/src/ssr/region.ts +9 -3
  91. package/src/style.ts +12 -5
  92. package/src/test/browser.ts +3 -1
  93. package/src/test/build-reuse.ts +22 -7
  94. package/src/test/index.ts +7 -3
  95. package/src/ts-plugin/diagnostics.ts +56 -18
  96. package/ts-plugin.cjs +465 -131
@@ -17,8 +17,8 @@ import type { ExprContext, HydrationMarker, MapCallMatch } from './shared'
17
17
  import { analyzeModule, getExprContext, loadDeclarations, parseTsx } from './analyze'
18
18
  import { walk } from './ast'
19
19
  import { createDevSourceAttr } from './dev-source'
20
- import { collectStaticStyleNames, compileElementAttributes, compileRawTextChildren, formatSetTextOp, resolveComponentCallee } from './dom-attrs'
21
- import { emitWalkStatements, moduleHeader, SVG_CHILD_TAGS } from './emit'
20
+ import { coalesceLeafEffects, collectStaticStyleNames, compileElementAttributes, compileRawTextChildren, formatSetTextOp, pushTextSlotOp, resolveComponentCallee } from './dom-attrs'
21
+ import { emitWalkStatements, moduleHeader, runtimeAlias, SVG_CHILD_TAGS } from './emit'
22
22
  import { SourceEditor } from './patch'
23
23
  import { compileParamList, createPropCompiler, isStaticPropExpr, withoutBindings } from './props'
24
24
  import { extractBlockKeyExpr, extractKeyExpr, literalArrayConsts, rewriteFreeIdentifiers } from './scope'
@@ -62,6 +62,7 @@ interface DomCompilerCtx {
62
62
  templates: { name: string, html: string, isSvg?: boolean }[]
63
63
  delegatedEvents: Set<string>
64
64
  svgDepth: number
65
+ alias: (name: string) => string
65
66
  use: (name: string) => string
66
67
  freshVar: (prefix?: string) => string
67
68
  nextVarId: () => number
@@ -72,6 +73,7 @@ interface DomCompilerCtx {
72
73
 
73
74
  interface ElementTreeState {
74
75
  ops: string[]
76
+ depKeys: Map<number, string>
75
77
  walkPlan: WalkItem[]
76
78
  childRefs: Map<string, string>
77
79
  textSlotsByParent: Map<string, Set<number>>
@@ -239,7 +241,7 @@ function compileLoopCall(
239
241
  rowRewrites.set(idxParam, `${idxSig}.value`)
240
242
 
241
243
  const rawRowBody = match.isJsxBody ? compileRootJsx(ctx, match.body as JSXElement | JSXFragment, rowRewrites) : ctx.compileExpression(match.body, rowRewrites)
242
- const rowBody = ctx.withPrelude(rawRowBody, match.body, prelude)
244
+ const rowBody = staticKeyInserts(ctx, ctx.withPrelude(rawRowBody, match.body, prelude))
243
245
  const rowParams = idxSig !== undefined ? `${rowSig}, ${idxSig}` : rowSig
244
246
  return {
245
247
  isStatic: false,
@@ -247,6 +249,14 @@ function compileLoopCall(
247
249
  }
248
250
  }
249
251
 
252
+ // The key rewrite leaves `insert(el, () => _k0, ...)` where a child reads only the hoisted key: a bare name tracks nothing, so it is inserted once.
253
+ function staticKeyInserts(ctx: DomCompilerCtx, rowBody: string): string {
254
+ const insertName = ctx.alias('insert')
255
+ if (!rowBody.includes(`${insertName}(`))
256
+ return rowBody
257
+ return rowBody.replace(new RegExp(`\\b${insertName}\\((\\w+), \\(\\) => ([A-Za-z_$][\\w$]*), `, 'g'), (_, el: string, name: string) => `${ctx.use('insertStatic')}(${el}, ${name}, `)
258
+ }
259
+
250
260
  function emitTextSlotOp(
251
261
  ctx: DomCompilerCtx,
252
262
  tree: ElementTreeState,
@@ -280,7 +290,7 @@ function emitTextSlotOp(
280
290
  }
281
291
  tree.ops.push(`const ${txtVar} = ${ctx.use('textAfter')}(${markerVar});`)
282
292
  }
283
- tree.ops.push(formatSetTextOp(txtVar, exprCode, expr, scopeRewrites, ctx.use))
293
+ pushTextSlotOp(tree.ops, tree.depKeys, txtVar, exprCode, expr, scopeRewrites, ctx.use)
284
294
  return { html, nextIndex }
285
295
  }
286
296
 
@@ -295,17 +305,8 @@ function buildElementHtml(
295
305
  const rawTextTag = RAW_TEXT_ELEMENTS.has(tag) ? tag as 'style' | 'script' : undefined
296
306
  const postChildOps: string[] = []
297
307
  const attrHtml = compileElementAttributes({
298
- el,
299
- tag,
300
- elVar,
301
- scopeRewrites,
302
- staticStyleNames: ctx.staticStyleNames,
303
- ops: tree.ops,
304
- postChildOps,
305
- delegatedEvents: ctx.delegatedEvents,
306
- use: ctx.use,
307
- freshVar: ctx.freshVar,
308
- compileExpression: ctx.compileExpression,
308
+ el, tag, elVar, scopeRewrites, staticStyleNames: ctx.staticStyleNames, ops: tree.ops, depKeys: tree.depKeys,
309
+ postChildOps, delegatedEvents: ctx.delegatedEvents, use: ctx.use, freshVar: ctx.freshVar, compileExpression: ctx.compileExpression,
309
310
  })
310
311
 
311
312
  let html = `<${tag}${ctx.devSrc(el, tag)}${attrHtml}>`
@@ -331,15 +332,17 @@ function buildElementHtml(
331
332
  return v
332
333
  }
333
334
 
334
- const emitInsert = (valueCode: string, marker: HydrationMarker, open: string, close: string): void => {
335
+ // A value that reads no signal is inserted once, without an effect.
336
+ const emitInsert = (valueCode: string, marker: HydrationMarker, open: string, close: string, isStatic = false): void => {
337
+ const call = isStatic ? `${ctx.use('insertStatic')}(${elVar}, ${valueCode}` : `${ctx.use('insert')}(${elVar}, () => ${valueCode}`
335
338
  if (marker === 'none') {
336
- tree.ops.push(`${ctx.use('insert')}(${elVar}, () => ${valueCode}, null, null);`)
339
+ tree.ops.push(`${call}, null, null);`)
337
340
  }
338
341
  else if (marker === 'range-open') {
339
342
  const startVar = requestTreeChild(ctx, tree, elVar, domIndex)
340
343
  html += open
341
344
  domIndex++
342
- tree.ops.push(`${ctx.use('insert')}(${elVar}, () => ${valueCode}, null, ${startVar});`)
345
+ tree.ops.push(`${call}, null, ${startVar});`)
343
346
  }
344
347
  else {
345
348
  const startVar = requestTreeChild(ctx, tree, elVar, domIndex)
@@ -347,7 +350,7 @@ function buildElementHtml(
347
350
  html += open + close
348
351
  addSlot(tree.rangeSlotsByParent, elVar, domIndex + 1)
349
352
  domIndex += 2
350
- tree.ops.push(`${ctx.use('insert')}(${elVar}, () => ${valueCode}, ${endVar}, ${startVar});`)
353
+ tree.ops.push(`${call}, ${endVar}, ${startVar});`)
351
354
  }
352
355
  }
353
356
 
@@ -417,7 +420,7 @@ function buildElementHtml(
417
420
 
418
421
  const exprCode = ctx.compileExpression(slot.expr, scopeRewrites)
419
422
  if (nodeCapable) {
420
- emitInsert(exprCode, marker, open, close)
423
+ emitInsert(exprCode, marker, open, close, isStaticPropExpr(slot.expr, scopeRewrites))
421
424
  }
422
425
  else {
423
426
  const res = emitTextSlotOp(ctx, tree, elVar, domIndex, marker, open, close, exprCode, slot.expr, scopeRewrites)
@@ -439,16 +442,13 @@ function buildElementHtml(
439
442
  function compileElementTree(ctx: DomCompilerCtx, rootEl: JSXElement, scopeRewrites: ReadonlyMap<string, string>): string {
440
443
  const rootVar = ctx.freshVar('_e')
441
444
  const tree: ElementTreeState = {
442
- ops: [],
443
- walkPlan: [],
444
- childRefs: new Map(),
445
- textSlotsByParent: new Map(),
446
- rangeSlotsByParent: new Map(),
447
- varAliases: new Map(),
445
+ ops: [], depKeys: new Map(), walkPlan: [], childRefs: new Map(),
446
+ textSlotsByParent: new Map(), rangeSlotsByParent: new Map(), varAliases: new Map(),
448
447
  }
449
448
 
450
449
  const html = buildElementHtml(ctx, tree, rootEl, rootVar, scopeRewrites)
451
450
  resolveElementAliases(tree)
451
+ coalesceLeafEffects(tree.ops, tree.depKeys, ctx.alias)
452
452
 
453
453
  const rootTag = jsxTagName(rootEl.openingElement.name)
454
454
  const tmplName = ctx.freshVar('_t')
@@ -582,9 +582,10 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
582
582
  const counters = new Map<string, number>()
583
583
  let varCounter = 0
584
584
 
585
+ const alias = runtimeAlias(code)
585
586
  const use = (name: string): string => {
586
587
  used.add(name)
587
- return `__${name}`
588
+ return alias(name)
588
589
  }
589
590
  const freshVar = (prefix = '_e'): string => {
590
591
  let n = counters.get(prefix) ?? 0
@@ -627,22 +628,8 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
627
628
  }
628
629
 
629
630
  const ctx: DomCompilerCtx = {
630
- code,
631
- analysis,
632
- jsxNodes,
633
- exprContext,
634
- staticArrays,
635
- staticStyleNames,
636
- devSrc,
637
- templates,
638
- delegatedEvents,
639
- svgDepth: 0,
640
- use,
641
- freshVar,
642
- nextVarId: () => varCounter++,
643
- compileExpression,
644
- compileLoopRow,
645
- withPrelude,
631
+ code, analysis, jsxNodes, exprContext, staticArrays, staticStyleNames, devSrc, templates, delegatedEvents,
632
+ svgDepth: 0, alias, use, freshVar, nextVarId: () => varCounter++, compileExpression, compileLoopRow, withPrelude,
646
633
  }
647
634
 
648
635
  walk(program, (node) => {
@@ -659,7 +646,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
659
646
  return undefined
660
647
  })
661
648
 
662
- const header = moduleHeader(used, templates, delegatedEvents)
649
+ const header = moduleHeader(used, templates, delegatedEvents, alias)
663
650
  if (header !== '')
664
651
  editor.prepend(header)
665
652
 
@@ -22,14 +22,25 @@ export const DELEGATED_EVENTS = new Set([
22
22
  // Helpers kept out of the dom core, which every island loads: a page pays for them only when one of its islands binds them.
23
23
  const ATTR_HELPERS = ['setAttr', 'setClass', 'setProp', 'setSelectValues', 'setStyle']
24
24
  const CLAIM_HELPERS = ['eachStatic']
25
- const EXTRA_HELPERS = ['catchBoundary', 'fragInsert', 'insert', 'mergeProps', 'omit', 'propDefault', 'setInnerHtml', 'show', 'spread']
25
+ const EXTRA_HELPERS = ['catchBoundary', 'fragInsert', 'insert', 'insertStatic', 'mergeProps', 'omit', 'propDefault', 'setInnerHtml', 'show', 'spread']
26
26
 
27
27
  function takeHelpers(used: Set<string>, names: readonly string[]): string[] {
28
28
  return names.filter(name => used.delete(name))
29
29
  }
30
30
 
31
+ /** The local name a runtime helper is imported under: `__esc`, with underscores added when the app's own source already uses that name. */
32
+ export function runtimeAlias(code: string): (name: string) => string {
33
+ const mentioned = new Set(code.match(/__[\w$]+/g))
34
+ return (name) => {
35
+ let alias = `__${name}`
36
+ while (mentioned.has(alias))
37
+ alias += '_'
38
+ return alias
39
+ }
40
+ }
41
+
31
42
  /** The import lines, hoisted templates and delegation call a compiled module starts with. Marks the helpers it needs in `used`. */
32
- export function moduleHeader(used: Set<string>, templates: readonly { name: string, html: string, isSvg?: boolean }[], delegatedEvents: ReadonlySet<string>): string {
43
+ export function moduleHeader(used: Set<string>, templates: readonly { name: string, html: string, isSvg?: boolean }[], delegatedEvents: ReadonlySet<string>, alias: (name: string) => string = name => `__${name}`): string {
33
44
  let header = ''
34
45
  if (templates.length > 0)
35
46
  used.add('template')
@@ -43,18 +54,18 @@ export function moduleHeader(used: Set<string>, templates: readonly { name: stri
43
54
  const usedExtras = takeHelpers(used, EXTRA_HELPERS)
44
55
  for (const [names, module] of [[[...used].sort(), 'dom'], [usedAttrs, 'dom-attrs'], [usedClaims, 'dom-claim'], [usedExtras, 'dom-extras']] as const) {
45
56
  if (names.length > 0)
46
- header += `import { ${names.map(name => `${name} as __${name}`).join(', ')} } from 'prunelle/internal/${module}';\n`
57
+ header += `import { ${names.map(name => `${name} as ${alias(name)}`).join(', ')} } from 'prunelle/internal/${module}';\n`
47
58
  }
48
59
  // Bun groups modules by the islands reaching them: an island binding attributes or claims reaches the extras too, so the extras other islands use share that chunk instead of costing the page a request of their own (#3683).
49
60
  if (usedExtras.length === 0 && usedAttrs.length + usedClaims.length > 0)
50
61
  header += `import 'prunelle/internal/dom-extras';\n`
51
62
  if (hasLoop)
52
- header += `import { loop as __loop } from 'prunelle/internal/dom-loop';\n`
63
+ header += `import { loop as ${alias('loop')} } from 'prunelle/internal/dom-loop';\n`
53
64
  }
54
65
  for (const t of templates)
55
- header += `const ${t.name} = /* @__PURE__ */ __template(${JSON.stringify(t.html)}${t.isSvg ? ', true' : ''});\n`
66
+ header += `const ${t.name} = /* @__PURE__ */ ${alias('template')}(${JSON.stringify(t.html)}${t.isSvg ? ', true' : ''});\n`
56
67
  if (delegatedEvents.size > 0)
57
- header += `__delegate(${JSON.stringify([...delegatedEvents].sort())});\n`
68
+ header += `${alias('delegate')}(${JSON.stringify([...delegatedEvents].sort())});\n`
58
69
  return header
59
70
  }
60
71