svelte 5.58.0 → 5.59.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 (100) hide show
  1. package/compiler/index.js +1 -1
  2. package/package.json +19 -1
  3. package/renderer.d.ts +1 -0
  4. package/src/compiler/errors.js +19 -0
  5. package/src/compiler/index.js +37 -2
  6. package/src/compiler/migrate/index.js +2 -1
  7. package/src/compiler/phases/1-parse/read/options.js +20 -0
  8. package/src/compiler/phases/2-analyze/index.js +21 -2
  9. package/src/compiler/phases/2-analyze/visitors/AnimateDirective.js +5 -0
  10. package/src/compiler/phases/2-analyze/visitors/Attribute.js +6 -2
  11. package/src/compiler/phases/2-analyze/visitors/BindDirective.js +4 -0
  12. package/src/compiler/phases/2-analyze/visitors/ExpressionTag.js +2 -1
  13. package/src/compiler/phases/2-analyze/visitors/HtmlTag.js +5 -0
  14. package/src/compiler/phases/2-analyze/visitors/OnDirective.js +6 -0
  15. package/src/compiler/phases/2-analyze/visitors/RegularElement.js +13 -6
  16. package/src/compiler/phases/2-analyze/visitors/SvelteBody.js +5 -0
  17. package/src/compiler/phases/2-analyze/visitors/SvelteDocument.js +5 -0
  18. package/src/compiler/phases/2-analyze/visitors/SvelteElement.js +5 -1
  19. package/src/compiler/phases/2-analyze/visitors/SvelteHead.js +5 -0
  20. package/src/compiler/phases/2-analyze/visitors/SvelteWindow.js +5 -0
  21. package/src/compiler/phases/2-analyze/visitors/Text.js +3 -1
  22. package/src/compiler/phases/2-analyze/visitors/TransitionDirective.js +6 -0
  23. package/src/compiler/phases/2-analyze/visitors/shared/element.js +3 -2
  24. package/src/compiler/phases/3-transform/client/transform-client.js +68 -1
  25. package/src/compiler/phases/3-transform/client/transform-template/index.js +4 -3
  26. package/src/compiler/phases/3-transform/client/transform-template/template.js +1 -1
  27. package/src/compiler/phases/3-transform/client/utils.js +3 -2
  28. package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +23 -13
  29. package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
  30. package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
  31. package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
  32. package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
  33. package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
  34. package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
  35. package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
  36. package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
  37. package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
  38. package/src/compiler/print/index.js +45 -57
  39. package/src/compiler/state.js +12 -0
  40. package/src/compiler/utils/builders.js +2 -2
  41. package/src/compiler/validate-options.js +47 -1
  42. package/src/index-client.js +3 -3
  43. package/src/internal/client/constants.js +15 -1
  44. package/src/internal/client/custom-renderer/containers.js +213 -0
  45. package/src/internal/client/custom-renderer/cross.js +145 -0
  46. package/src/internal/client/custom-renderer/index.js +28 -0
  47. package/src/internal/client/custom-renderer/state.js +59 -0
  48. package/src/internal/client/dev/css.js +3 -1
  49. package/src/internal/client/dev/debug.js +6 -6
  50. package/src/internal/client/dev/elements.js +9 -7
  51. package/src/internal/client/dev/tracing.js +67 -11
  52. package/src/internal/client/dev/validation.js +2 -1
  53. package/src/internal/client/dom/blocks/async.js +2 -1
  54. package/src/internal/client/dom/blocks/await.js +110 -19
  55. package/src/internal/client/dom/blocks/boundary.js +142 -88
  56. package/src/internal/client/dom/blocks/branches.js +94 -75
  57. package/src/internal/client/dom/blocks/css-props.js +31 -3
  58. package/src/internal/client/dom/blocks/each.js +166 -45
  59. package/src/internal/client/dom/blocks/html.js +19 -13
  60. package/src/internal/client/dom/blocks/slot.js +3 -3
  61. package/src/internal/client/dom/blocks/snippet.js +52 -3
  62. package/src/internal/client/dom/blocks/svelte-element.js +17 -6
  63. package/src/internal/client/dom/blocks/svelte-head.js +13 -4
  64. package/src/internal/client/dom/css.js +4 -4
  65. package/src/internal/client/dom/elements/attributes.js +91 -51
  66. package/src/internal/client/dom/elements/bindings/input.js +19 -10
  67. package/src/internal/client/dom/elements/bindings/select.js +13 -9
  68. package/src/internal/client/dom/elements/class.js +16 -5
  69. package/src/internal/client/dom/elements/events.js +153 -21
  70. package/src/internal/client/dom/elements/misc.js +3 -2
  71. package/src/internal/client/dom/elements/style.js +17 -6
  72. package/src/internal/client/dom/hydration.js +6 -6
  73. package/src/internal/client/dom/operations.js +435 -14
  74. package/src/internal/client/dom/reconciler.js +2 -2
  75. package/src/internal/client/dom/template.js +94 -66
  76. package/src/internal/client/error-handling.js +4 -12
  77. package/src/internal/client/errors.js +85 -2
  78. package/src/internal/client/index.js +9 -2
  79. package/src/internal/client/reactivity/async.js +257 -122
  80. package/src/internal/client/reactivity/batch.js +1124 -531
  81. package/src/internal/client/reactivity/deriveds.js +281 -177
  82. package/src/internal/client/reactivity/effects.js +87 -54
  83. package/src/internal/client/reactivity/sources.js +30 -124
  84. package/src/internal/client/reactivity/status.js +1 -1
  85. package/src/internal/client/render.js +60 -87
  86. package/src/internal/client/runtime.js +931 -153
  87. package/src/internal/client/validate.js +2 -2
  88. package/src/internal/disclose-version.js +1 -1
  89. package/src/internal/flags/custom-renderer-server.js +10 -0
  90. package/src/internal/flags/custom-renderer.js +3 -0
  91. package/src/internal/flags/index.js +6 -0
  92. package/src/internal/init-operations.js +3 -0
  93. package/src/internal/shared/errors.js +20 -0
  94. package/src/legacy/legacy-client.js +6 -2
  95. package/src/renderer/index-server.js +18 -0
  96. package/src/renderer/index.js +2 -0
  97. package/src/version.js +1 -1
  98. package/types/index.d.ts +245 -25
  99. package/types/index.d.ts.map +10 -1
  100. package/src/internal/client/reactivity/utils.js +0 -18
@@ -1,6 +1,6 @@
1
- /** @import { Blocker, Effect, Source, Value } from '#client' */
1
+ /** @import { Effect, Source, Value } from '#client' */
2
2
  /** @import { Batch } from './batch.js' */
3
- import { DESTROYED, STALE_REACTION } from '#client/constants';
3
+ import { DESTROYED, EFFECT_TRANSPARENT, STALE_REACTION } from '#client/constants';
4
4
  import { DEV } from 'esm-env';
5
5
  import {
6
6
  component_context,
@@ -9,22 +9,26 @@ import {
9
9
  set_component_context,
10
10
  set_dev_stack
11
11
  } from '../context.js';
12
+ import { current_renderer, set_renderer } from '../custom-renderer/state.js';
13
+ import { custom_renderers_flag } from '../../flags/index.js';
12
14
  import { invoke_error_boundary } from '../error-handling.js';
13
15
  import {
14
16
  active_effect,
15
17
  active_reaction,
18
+ get,
16
19
  set_active_effect,
17
20
  set_active_reaction
18
21
  } from '../runtime.js';
19
22
  import { current_batch } from './batch.js';
20
23
  import {
21
- async_derived,
22
24
  reactivity_loss_tracker,
23
25
  derived,
24
26
  derived_safe_equal,
25
- set_reactivity_loss_tracker
27
+ set_reactivity_loss_tracker,
28
+ async_expression,
29
+ is_uninitialized
26
30
  } from './deriveds.js';
27
- import { aborted } from './effects.js';
31
+ import { async_effect, block, branch } from './effects.js';
28
32
 
29
33
  /**
30
34
  * @param {Blocker[]} blockers
@@ -33,12 +37,8 @@ import { aborted } from './effects.js';
33
37
  * @param {(values: Value[]) => any} fn
34
38
  */
35
39
  export function flatten(blockers, sync, async, fn) {
36
- const d = is_runes() ? derived : derived_safe_equal;
37
-
38
- // Filter out already-settled blockers - no need to wait for them
39
- var pending = blockers.filter((b) => !b.settled);
40
-
41
- var deriveds = sync.map(d);
40
+ var parent = /** @type {Effect} */ (active_effect);
41
+ var deriveds = sync.map(is_runes() ? derived : derived_safe_equal);
42
42
 
43
43
  if (DEV) {
44
44
  deriveds.forEach((d, i) => {
@@ -52,69 +52,67 @@ export function flatten(blockers, sync, async, fn) {
52
52
  });
53
53
  }
54
54
 
55
- if (async.length === 0 && pending.length === 0) {
56
- fn(deriveds);
57
- return;
58
- }
59
-
60
- var parent = /** @type {Effect} */ (active_effect);
55
+ // Filter out already-settled blockers - no need to wait for them
56
+ var pending = blockers.filter((b) => !b.settled);
61
57
 
62
- var restore = capture();
63
- var blocker_promise =
64
- pending.length === 1
65
- ? pending[0].promise
66
- : pending.length > 1
67
- ? Promise.all(pending.map((b) => b.promise))
68
- : null;
58
+ if (pending.length > 0) {
59
+ var restore = capture();
60
+ var decrement_pending = increment_pending();
61
+ var p = pending.length === 1 ? pending[0].promise : Promise.all(pending.map((b) => b.promise));
69
62
 
70
- /**
71
- * @param {Source[]} async
72
- */
73
- function finish(async) {
74
- if ((parent.f & DESTROYED) !== 0) {
75
- return;
76
- }
63
+ p.then(() => {
64
+ if ((parent.f & DESTROYED) !== 0) {
65
+ return;
66
+ }
77
67
 
78
- restore();
68
+ // Keep the caller's context, but use the script's batch to read its first values.
69
+ restore(pending[0].run.get_batch());
79
70
 
80
- try {
81
- fn([...deriveds, ...async]);
82
- } catch (error) {
83
- invoke_error_boundary(error, parent);
84
- }
71
+ try {
72
+ run();
73
+ } catch (error) {
74
+ invoke_error_boundary(error, parent);
75
+ }
85
76
 
86
- unset_context();
77
+ unset_context();
78
+ }).finally(decrement_pending);
79
+ } else {
80
+ run();
87
81
  }
88
82
 
89
- var decrement_pending = increment_pending();
83
+ function run() {
84
+ if (blockers.some((b) => b.failed)) return;
90
85
 
91
- // Fast path: blockers but no async expressions
92
- if (async.length === 0) {
93
- /** @type {Promise<any>} */ (blocker_promise).then(() => finish([])).finally(decrement_pending);
94
- return;
95
- }
86
+ if (async.length === 0) {
87
+ fn(deriveds);
88
+ return;
89
+ }
96
90
 
97
- // Full path: has async expressions
98
- function run() {
99
- Promise.all(async.map((expression) => async_derived(expression)))
100
- .then(finish)
101
- .catch((error) => invoke_error_boundary(error, parent))
102
- .finally(decrement_pending);
103
- }
91
+ var signals = async.map((fn) => async_expression(fn));
104
92
 
105
- if (blocker_promise) {
106
- blocker_promise.then(() => {
107
- if ((parent.f & DESTROYED) !== 0) {
108
- decrement_pending();
109
- return;
93
+ /** @type {Effect | null} */
94
+ var effect = null;
95
+
96
+ block(() => {
97
+ for (var s of signals) {
98
+ if (is_uninitialized(s)) {
99
+ get(s);
100
+ return;
101
+ }
110
102
  }
111
103
 
112
- restore();
113
- run();
114
- unset_context();
115
- });
116
- } else {
117
- run();
104
+ if (effect !== null) {
105
+ /** @type {Batch} */ (current_batch).revalidate_branch(effect);
106
+ } else {
107
+ effect = branch(() => {
108
+ try {
109
+ fn([...deriveds, ...signals]);
110
+ } catch (error) {
111
+ invoke_error_boundary(error, parent);
112
+ }
113
+ });
114
+ }
115
+ }, EFFECT_TRANSPARENT);
118
116
  }
119
117
  }
120
118
 
@@ -136,21 +134,29 @@ export function capture() {
136
134
  var previous_reaction = active_reaction;
137
135
  var previous_component_context = component_context;
138
136
  var previous_batch = /** @type {Batch} */ (current_batch);
137
+ var previous_renderer = custom_renderers_flag ? current_renderer : null;
139
138
 
140
139
  if (DEV) {
141
140
  var previous_dev_stack = dev_stack;
142
141
  }
143
142
 
144
- return function restore(activate_batch = true) {
143
+ /**
144
+ * @param {Batch | null} [batch] the batch to continue in, if not the one that was active before
145
+ */
146
+ return function restore(batch = previous_batch) {
145
147
  set_active_effect(previous_effect);
146
148
  set_active_reaction(previous_reaction);
147
149
  set_component_context(previous_component_context);
148
150
 
149
- if (activate_batch && (previous_effect.f & DESTROYED) === 0) {
150
- // TODO we only need optional chaining here because `{#await ...}` blocks
151
+ if (custom_renderers_flag) {
152
+ set_renderer(previous_renderer);
153
+ }
154
+
155
+ if (batch !== null && (previous_effect.f & DESTROYED) === 0) {
156
+ // A batch that was merged into another one continues as that one.
157
+ // TODO we only need the check here because `{#await ...}` blocks
151
158
  // are anomalous. Once we retire them we can get rid of it
152
- previous_batch?.activate();
153
- previous_batch?.apply();
159
+ merge_target(batch).activate();
154
160
  }
155
161
 
156
162
  if (DEV) {
@@ -298,6 +304,9 @@ export function unset_context(deactivate_batch = true) {
298
304
  set_active_effect(null);
299
305
  set_active_reaction(null);
300
306
  set_component_context(null);
307
+ if (custom_renderers_flag) {
308
+ set_renderer(null);
309
+ }
301
310
  if (deactivate_batch) current_batch?.deactivate();
302
311
 
303
312
  if (DEV) {
@@ -307,78 +316,184 @@ export function unset_context(deactivate_batch = true) {
307
316
  }
308
317
 
309
318
  /**
310
- * @param {Array<() => void | Promise<void>>} thunks
319
+ * The script's state while a `run` step executes.
320
+ * Async expressions set `batch` when they deliver their first value. `continued` holds callbacks
321
+ * to run after the script resumes, and `signals` lists the expressions in creation order.
322
+ * @type {Runner | null}
311
323
  */
312
- export function run(thunks) {
313
- const restore = capture();
324
+ export let current_runner = null;
314
325
 
315
- const decrement_pending = increment_pending();
326
+ class Runner {
327
+ /** @type {ReturnType<typeof capture>} */
328
+ #restore;
316
329
 
317
- var active = /** @type {Effect} */ (active_effect);
330
+ /**
331
+ * Other batches that share this run
332
+ * @type {Set<Batch>}
333
+ */
334
+ #adopters = new Set();
318
335
 
319
- /** @type {null | { error: any }} */
320
- var errored = null;
336
+ /** @type {Batch} */
337
+ #batch;
321
338
 
322
- /** @param {any} error */
323
- const handle_error = (error) => {
324
- errored = { error }; // wrap in object in case a promise rejects with a falsy value
339
+ /** @type {Array<() => void>} */
340
+ continued = [];
325
341
 
326
- if (!aborted(active)) {
327
- invoke_error_boundary(error, active);
328
- }
329
- };
342
+ /**
343
+ * Async deriveds created during this run
344
+ * @type {Source[]}
345
+ */
346
+ signals = [];
330
347
 
331
- var promise = Promise.resolve(thunks[0]()).catch(handle_error);
348
+ /** Whether the full initialization promise chain is still pending. */
349
+ running = true;
332
350
 
333
- /** @type {Blocker} */
334
- var blocker = { promise, settled: false };
335
- var blockers = [blocker];
351
+ constructor() {
352
+ // TODO this should be unnecessary! these should just be class initializers. but
353
+ // something somewhere (vitest?) must be doing some transpilation that breaks classes
354
+ this.#batch = /** @type {Batch} */ (current_batch);
355
+ this.#restore = capture();
356
+ }
336
357
 
337
- promise.finally(() => {
338
- blocker.settled = true;
339
- unset_context();
340
- });
358
+ /** The batch to continue in, even if the original fork was discarded. */
359
+ get_batch() {
360
+ if (!merge_target(this.#batch).linked) {
361
+ for (const b of this.#adopters) {
362
+ var target = merge_target(b);
363
+ if (target.linked) this.#batch = target;
364
+ }
365
+ }
341
366
 
342
- for (const fn of thunks.slice(1)) {
343
- promise = promise
344
- .then(() => {
345
- restore();
367
+ return this.#batch;
368
+ }
346
369
 
347
- try {
348
- if (errored) {
349
- throw errored.error;
350
- }
370
+ restore() {
371
+ this.#restore(this.get_batch());
372
+ }
351
373
 
352
- if (aborted(active)) {
353
- throw STALE_REACTION;
354
- }
374
+ /**
375
+ *
376
+ * @param {Array<() => void | Promise<void>>} thunks
377
+ */
378
+ run(thunks) {
379
+ var active = /** @type {Effect} */ (active_effect);
355
380
 
356
- return fn();
381
+ var promise = Promise.resolve(this.#invoke(thunks[0]));
382
+ var blockers = [new Blocker(this, promise)];
383
+
384
+ for (const fn of thunks.slice(1)) {
385
+ promise = promise.then(() => {
386
+ if ((active.f & DESTROYED) !== 0) {
387
+ throw STALE_REACTION;
388
+ }
389
+
390
+ try {
391
+ this.restore();
392
+ return this.#invoke(fn);
357
393
  } finally {
358
394
  // We gotta unset context directly in case the function returns a promise, in which case
359
- // unset_context in .finally() would be too late ...
395
+ // unset_context in .finally() would be too late
360
396
  unset_context();
397
+ // Release buffered results now that the script has used the previous step's first values.
398
+ this.#continue();
399
+ }
400
+ });
401
+
402
+ blockers.push(new Blocker(this, promise));
403
+ }
404
+
405
+ promise = promise
406
+ .catch((error) => {
407
+ if ((active.f & DESTROYED) === 0) {
408
+ this.restore();
409
+
410
+ try {
411
+ invoke_error_boundary(error, active);
412
+ } finally {
413
+ unset_context();
414
+ }
361
415
  }
362
416
  })
363
- .catch(handle_error);
417
+ // The last step has no following step to release its buffered results.
418
+ .finally(() => this.#continue());
364
419
 
365
- const blocker = { promise, settled: false };
366
- blockers.push(blocker);
420
+ async_effect(() => {
421
+ // Inside an effect so later adopting branches can rerun this
422
+ var batch = /** @type {Batch} */ (current_batch);
423
+ this.#adopters.add(batch);
424
+ var decrement_pending = increment_pending();
367
425
 
368
- promise.finally(() => {
369
- blocker.settled = true;
370
- // ... but we also need it after such a promise has resolved in case it restores our context
371
- unset_context();
426
+ promise.finally(() => {
427
+ // An adopting batch must also run effects created after it adopted the script's branch.
428
+ var target = merge_target(batch);
429
+
430
+ if (target.linked && (active.f & DESTROYED) === 0) {
431
+ target.revalidate_branch(active);
432
+ }
433
+
434
+ decrement_pending();
435
+
436
+ this.running = false;
437
+ });
372
438
  });
439
+
440
+ return blockers;
441
+ }
442
+
443
+ /**
444
+ * Switch away from the previous batch (which is now obsolete)
445
+ * to another one
446
+ * @param {Batch} batch
447
+ */
448
+ switch(batch) {
449
+ this.#batch = batch;
450
+ }
451
+
452
+ #continue() {
453
+ for (const fn of this.continued.splice(0)) fn();
373
454
  }
374
455
 
375
- promise
376
- // wait one more tick, so that template effects are
377
- // guaranteed to run before `$effect(...)`
378
- .then(() => Promise.resolve())
379
- .finally(decrement_pending);
456
+ /**
457
+ * @param {() => void | Promise<void>} fn
458
+ */
459
+ #invoke(fn) {
460
+ var previous_runner = current_runner;
461
+ current_runner = this;
380
462
 
381
- return blockers;
463
+ try {
464
+ return fn();
465
+ } finally {
466
+ current_runner = previous_runner;
467
+ }
468
+ }
469
+ }
470
+
471
+ export class Blocker {
472
+ settled = false;
473
+ failed = false;
474
+
475
+ /**
476
+ * @param {Runner} run
477
+ * @param {Promise<any>} promise
478
+ */
479
+ constructor(run, promise) {
480
+ this.run = run;
481
+ this.promise = promise
482
+ .catch(() => {
483
+ this.failed = true;
484
+ })
485
+ .finally(() => {
486
+ this.settled = true;
487
+ unset_context();
488
+ });
489
+ }
490
+ }
491
+
492
+ /**
493
+ * @param {Array<() => void | Promise<void>>} thunks
494
+ */
495
+ export function run(thunks) {
496
+ return new Runner().run(thunks);
382
497
  }
383
498
 
384
499
  /**
@@ -389,19 +504,39 @@ export function wait(blockers) {
389
504
  }
390
505
 
391
506
  /**
392
- * @returns {(skip?: boolean) => void}
507
+ * Adds pending work to the current batch, and to the boundary of the active effect. If the batch is merged
508
+ * into another one before the work is done, it's that batch's from then on (see `Batch#merge`)
509
+ * @returns {() => void}
393
510
  */
394
511
  export function increment_pending() {
395
512
  var effect = /** @type {Effect} */ (active_effect);
396
513
  var boundary = effect.b; // undefined if called outside the render tree, e.g. a standalone $effect.root
397
- var batch = /** @type {Batch} */ (current_batch);
398
- var blocking = !!boundary?.is_rendered();
514
+ var batch = merge_target(/** @type {Batch} */ (current_batch));
399
515
 
400
- boundary?.update_pending_count(1, batch);
401
- batch.increment(blocking, effect);
516
+ /** The nearest boundary with a pending snippet, if there is one */
517
+ var pending = boundary;
518
+
519
+ while (pending && !pending.is_pending) {
520
+ pending = pending.parent;
521
+ }
522
+
523
+ batch.increment(effect);
524
+ pending?.increment(effect, batch);
525
+ boundary?.update_effect_pending(1);
402
526
 
403
527
  return () => {
404
- boundary?.update_pending_count(-1, batch);
405
- batch.decrement(blocking, effect);
528
+ merge_target(batch).decrement(effect);
529
+ // The boundary keeps counts under the originating batch, even if it was merged.
530
+ pending?.decrement(effect, batch);
531
+ boundary?.update_effect_pending(-1);
406
532
  };
407
533
  }
534
+
535
+ /**
536
+ * The batch that `batch` was merged into, if any (transitively), else `batch` itself
537
+ * @param {Batch} batch
538
+ */
539
+ export function merge_target(batch) {
540
+ while (batch.merged !== null) batch = batch.merged;
541
+ return batch;
542
+ }