svelte 5.57.3 → 5.58.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/README.md +1 -1
  2. package/compiler/index.js +1 -1
  3. package/elements.d.ts +1 -2
  4. package/package.json +2 -2
  5. package/src/compiler/phases/1-parse/acorn.js +4 -14
  6. package/src/compiler/phases/1-parse/index.js +8 -1
  7. package/src/compiler/phases/1-parse/read/context.js +3 -2
  8. package/src/compiler/phases/1-parse/read/expression.js +2 -4
  9. package/src/compiler/phases/1-parse/utils/bracket.js +6 -0
  10. package/src/compiler/phases/2-analyze/css/css-prune.js +0 -8
  11. package/src/compiler/phases/2-analyze/index.js +2 -1
  12. package/src/compiler/phases/2-analyze/visitors/AwaitExpression.js +17 -15
  13. package/src/compiler/phases/3-transform/client/transform-template/index.js +27 -9
  14. package/src/compiler/phases/3-transform/client/utils.js +3 -2
  15. package/src/compiler/phases/3-transform/client/visitors/EachBlock.js +1 -1
  16. package/src/compiler/phases/3-transform/client/visitors/Fragment.js +13 -1
  17. package/src/compiler/phases/3-transform/client/visitors/HtmlTag.js +10 -2
  18. package/src/compiler/phases/3-transform/client/visitors/RegularElement.js +11 -2
  19. package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +15 -7
  20. package/src/compiler/phases/3-transform/client/visitors/SlotElement.js +19 -1
  21. package/src/compiler/phases/3-transform/client/visitors/SvelteComponent.js +0 -1
  22. package/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js +1 -1
  23. package/src/compiler/phases/3-transform/client/visitors/shared/element.js +1 -1
  24. package/src/compiler/phases/3-transform/client/visitors/shared/events.js +1 -1
  25. package/src/compiler/phases/3-transform/client/visitors/shared/utils.js +1 -1
  26. package/src/compiler/phases/3-transform/server/visitors/ExpressionStatement.js +8 -0
  27. package/src/compiler/phases/3-transform/server/visitors/Fragment.js +6 -1
  28. package/src/compiler/phases/3-transform/server/visitors/TitleElement.js +1 -1
  29. package/src/compiler/phases/3-transform/server/visitors/shared/element.js +2 -1
  30. package/src/compiler/phases/3-transform/server/visitors/shared/utils.js +1 -1
  31. package/src/compiler/phases/patterns.js +0 -1
  32. package/src/compiler/print/index.js +49 -73
  33. package/src/compiler/state.js +1 -1
  34. package/src/compiler/utils/builders.js +0 -1
  35. package/src/compiler/utils/mapped_code.js +1 -12
  36. package/src/constants.js +0 -8
  37. package/src/index-client.js +3 -3
  38. package/src/internal/client/constants.js +8 -1
  39. package/src/internal/client/dev/debug.js +6 -6
  40. package/src/internal/client/dev/elements.js +25 -9
  41. package/src/internal/client/dev/tracing.js +67 -11
  42. package/src/internal/client/dom/blocks/async.js +2 -1
  43. package/src/internal/client/dom/blocks/await.js +109 -20
  44. package/src/internal/client/dom/blocks/boundary.js +107 -70
  45. package/src/internal/client/dom/blocks/branches.js +17 -23
  46. package/src/internal/client/dom/blocks/each.js +88 -13
  47. package/src/internal/client/dom/blocks/html.js +44 -10
  48. package/src/internal/client/dom/blocks/slot.js +19 -1
  49. package/src/internal/client/dom/blocks/snippet.js +13 -0
  50. package/src/internal/client/dom/blocks/svelte-component.js +1 -1
  51. package/src/internal/client/dom/blocks/svelte-element.js +2 -1
  52. package/src/internal/client/dom/elements/attributes.js +4 -3
  53. package/src/internal/client/dom/elements/bindings/input.js +19 -10
  54. package/src/internal/client/dom/elements/bindings/select.js +3 -7
  55. package/src/internal/client/dom/elements/events.js +27 -2
  56. package/src/internal/client/dom/hydration.js +4 -1
  57. package/src/internal/client/dom/operations.js +2 -3
  58. package/src/internal/client/dom/template.js +0 -1
  59. package/src/internal/client/error-handling.js +4 -12
  60. package/src/internal/client/errors.js +18 -2
  61. package/src/internal/client/index.js +0 -1
  62. package/src/internal/client/reactivity/async.js +249 -123
  63. package/src/internal/client/reactivity/batch.js +1109 -538
  64. package/src/internal/client/reactivity/deriveds.js +281 -177
  65. package/src/internal/client/reactivity/effects.js +44 -28
  66. package/src/internal/client/reactivity/equality.js +0 -9
  67. package/src/internal/client/reactivity/props.js +0 -1
  68. package/src/internal/client/reactivity/sources.js +33 -125
  69. package/src/internal/client/reactivity/status.js +1 -1
  70. package/src/internal/client/render.js +11 -0
  71. package/src/internal/client/runtime.js +925 -152
  72. package/src/internal/client/validate.js +2 -2
  73. package/src/internal/server/renderer.js +52 -118
  74. package/src/internal/shared/utils.js +12 -0
  75. package/src/motion/spring.js +1 -1
  76. package/src/motion/tweened.js +1 -1
  77. package/src/reactivity/create-subscriber.js +1 -1
  78. package/src/version.js +1 -1
  79. package/types/index.d.ts +3 -3
  80. package/types/index.d.ts.map +1 -1
  81. package/src/internal/client/reactivity/utils.js +0 -18
@@ -1,9 +1,10 @@
1
1
  /** @import { ArrayExpression, Expression, Literal, ObjectExpression } from 'estree' */
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext, ComponentServerTransformState } from '../../types.js' */
4
+ /** @import { ExpressionMetadata } from '../../../../nodes.js' */
4
5
  import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
5
6
  import { binding_properties } from '../../../../bindings.js';
6
- import { ExpressionMetadata, is_custom_element_node } from '../../../../nodes.js';
7
+ import { is_custom_element_node } from '../../../../nodes.js';
7
8
  import { regex_starts_with_newline } from '../../../../patterns.js';
8
9
  import * as b from '#compiler/builders';
9
10
  import {
@@ -1,6 +1,7 @@
1
1
  /** @import { Expression, Identifier, Node, Statement, BlockStatement, ArrayExpression } from 'estree' */
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext, ServerTransformState } from '../../types.js' */
4
+ /** @import { ExpressionMetadata } from '../../../../nodes.js' */
4
5
 
5
6
  import { escape_html } from '../../../../../../escaping.js';
6
7
  import {
@@ -13,7 +14,6 @@ import * as b from '#compiler/builders';
13
14
  import { sanitize_template_string } from '../../../../../utils/sanitize_template_string.js';
14
15
  import { regex_whitespaces_strict } from '../../../../patterns.js';
15
16
  import { has_await_expression, save } from '../../../../../utils/ast.js';
16
- import { ExpressionMetadata } from '../../../../nodes.js';
17
17
 
18
18
  /** Opens an if/each block, so that we can remove nodes in the case of a mismatch */
19
19
  export const block_open = b.literal(BLOCK_OPEN);
@@ -1,4 +1,3 @@
1
- export const regex_whitespace = /\s/;
2
1
  export const regex_whitespaces = /\s+/;
3
2
  export const regex_starts_with_newline = /^\r?\n/;
4
3
  export const regex_starts_with_whitespace = /^\s/;
@@ -3,6 +3,7 @@
3
3
  import * as esrap from 'esrap';
4
4
  import ts from 'esrap/languages/ts';
5
5
  import { is_void } from '../../utils.js';
6
+ import { regex_ends_with_whitespaces, regex_starts_with_whitespaces } from '../phases/patterns.js';
6
7
 
7
8
  /** Threshold for when content should be formatted on separate lines */
8
9
  const LINE_BREAK_THRESHOLD = 50;
@@ -81,7 +82,9 @@ function escape_identifier(name) {
81
82
  */
82
83
  export function print(ast, options = undefined) {
83
84
  const comments = (ast.type === 'Root' && ast.comments) || [];
84
- const state = { preserve_whitespace: 0 };
85
+ const state = {
86
+ preserve_whitespace: ast.type === 'Root' && ast.options?.preserveWhitespace ? 1 : 0
87
+ };
85
88
  const css_comments =
86
89
  (ast.type === 'Root' ? ast.css?.comments : ast.type === 'StyleSheet' ? ast.comments : null) ||
87
90
  [];
@@ -122,7 +125,8 @@ function block(context, node, preserve_whitespace = false, allow_inline = false)
122
125
  return;
123
126
  }
124
127
 
125
- if (allow_inline && !child_context.multiline) {
128
+ // Wrapping at fragment edges is safe even when adjacent children must stay together.
129
+ if (allow_inline && !child_context.multiline && child_context.measure() <= LINE_BREAK_THRESHOLD) {
126
130
  context.append(child_context);
127
131
  } else {
128
132
  context.indent();
@@ -260,7 +264,11 @@ function print_element(node, context, comments, state) {
260
264
  const name = node.name.toLowerCase();
261
265
  const preserve =
262
266
  (node.type === 'RegularElement' || node.type === 'TitleElement') &&
263
- (name === 'pre' || name === 'textarea' || name === 'title');
267
+ (name === 'pre' ||
268
+ name === 'textarea' ||
269
+ name === 'title' ||
270
+ name === 'script' ||
271
+ name === 'style');
264
272
 
265
273
  if (preserve) state.preserve_whitespace += 1;
266
274
  base_element(node, context, comments, state);
@@ -552,12 +560,14 @@ const svelte_visitors = (comments, state) => ({
552
560
  for (const item of [node.module, node.instance, node.fragment, node.css]) {
553
561
  if (!item) continue;
554
562
 
555
- if (started) {
563
+ if (started && state.preserve_whitespace === 0) {
556
564
  context.margin();
557
565
  context.newline();
558
566
  }
559
567
 
560
- context.visit(item);
568
+ const child_context = context.new();
569
+ child_context.visit(item);
570
+ context.append(child_context);
561
571
  started = true;
562
572
  }
563
573
  },
@@ -566,7 +576,7 @@ const svelte_visitors = (comments, state) => ({
566
576
  context.write('<script');
567
577
  attributes(node, node.attributes, context, comments);
568
578
  context.write('>');
569
- block(context, node.content, state.preserve_whitespace > 0);
579
+ block(context, node.content);
570
580
  context.write('</script>');
571
581
  },
572
582
 
@@ -574,7 +584,6 @@ const svelte_visitors = (comments, state) => ({
574
584
  if (state.preserve_whitespace > 0) {
575
585
  for (const child of node.nodes) {
576
586
  context.visit(child);
577
- context.multiline ||= child.type === 'Text' && /[\r\n]/.test(child.data);
578
587
  }
579
588
  return;
580
589
  }
@@ -583,9 +592,9 @@ const svelte_visitors = (comments, state) => ({
583
592
  const last = node.nodes.at(-1);
584
593
  const has_surrounding_whitespace =
585
594
  first?.type === 'Text' &&
586
- /^\s/.test(first.data) &&
595
+ regex_starts_with_whitespaces.test(first.raw) &&
587
596
  last?.type === 'Text' &&
588
- /\s$/.test(last.data);
597
+ regex_ends_with_whitespaces.test(last.raw);
589
598
 
590
599
  /** @type {{ nodes: AST.SvelteNode[]; leading_whitespace: boolean }[]} */
591
600
  const items = [];
@@ -603,62 +612,40 @@ const svelte_visitors = (comments, state) => ({
603
612
  };
604
613
 
605
614
  for (let i = 0; i < node.nodes.length; i += 1) {
606
- let child_node = node.nodes[i];
607
-
615
+ const child_node = node.nodes[i];
608
616
  const prev = node.nodes[i - 1];
609
617
  const next = node.nodes[i + 1];
610
618
 
611
- if (child_node.type === 'Text') {
612
- child_node = { ...child_node }; // always clone, so we can safely mutate
619
+ if (child_node.type !== 'Text') {
620
+ sequence.push(child_node);
621
+ continue;
622
+ }
613
623
 
614
- child_node.data = child_node.data.replace(/[^\S]+/g, ' ');
624
+ let raw = child_node.raw;
615
625
 
616
- // trim fragment
617
- if (i === 0) {
618
- child_node.data = child_node.data.trimStart();
619
- }
626
+ // Like the compiler, trim fragment edges, but preserve whitespace inside
627
+ // text and next to expressions. In particular, do not trim non-breaking spaces.
628
+ if (i === 0) raw = raw.replace(regex_starts_with_whitespaces, '');
629
+ if (i === node.nodes.length - 1) raw = raw.replace(regex_ends_with_whitespaces, '');
630
+ if (raw === '') continue;
620
631
 
621
- if (i === node.nodes.length - 1) {
622
- child_node.data = child_node.data.trimEnd();
623
- }
624
-
625
- if (child_node.data === '') {
626
- continue;
627
- }
632
+ // Only split sequences where there was collapsible whitespace already.
633
+ // Inserting a newline between adjacent elements would introduce a space.
634
+ if (prev?.type !== 'ExpressionTag' && regex_starts_with_whitespaces.test(raw)) {
635
+ flush();
636
+ leading_whitespace = true;
637
+ raw = raw.replace(regex_starts_with_whitespaces, '');
638
+ }
628
639
 
629
- if (child_node.data.startsWith(' ') && prev && prev.type !== 'ExpressionTag') {
630
- flush();
631
- leading_whitespace = true;
632
- child_node.data = child_node.data.trimStart();
633
- }
640
+ const trailing_whitespace =
641
+ next?.type !== 'ExpressionTag' && regex_ends_with_whitespaces.test(raw);
634
642
 
635
- if (child_node.data !== '') {
636
- sequence.push({ ...child_node, data: child_node.data });
643
+ if (trailing_whitespace) raw = raw.replace(regex_ends_with_whitespaces, '');
644
+ if (raw !== '') sequence.push({ ...child_node, raw });
637
645
 
638
- if (child_node.data.endsWith(' ') && next && next.type !== 'ExpressionTag') {
639
- flush();
640
- child_node.data = child_node.data.trimStart();
641
- }
642
- }
643
- } else {
644
- const is_block_element =
645
- child_node.type === 'RegularElement' ||
646
- child_node.type === 'Component' ||
647
- child_node.type === 'SvelteBody' ||
648
- child_node.type === 'SvelteHead' ||
649
- child_node.type === 'SvelteFragment' ||
650
- child_node.type === 'SvelteBoundary' ||
651
- child_node.type === 'SvelteDocument' ||
652
- child_node.type === 'SvelteSelf' ||
653
- child_node.type === 'SvelteWindow' ||
654
- child_node.type === 'SvelteComponent' ||
655
- child_node.type === 'SvelteElement' ||
656
- child_node.type === 'SlotElement' ||
657
- child_node.type === 'TitleElement';
658
-
659
- if (is_block_element && sequence.length > 0) flush();
660
- sequence.push(child_node);
661
- if (is_block_element) flush();
646
+ if (trailing_whitespace) {
647
+ flush();
648
+ leading_whitespace = true;
662
649
  }
663
650
  }
664
651
 
@@ -707,10 +694,7 @@ const svelte_visitors = (comments, state) => ({
707
694
 
708
695
  AnimateDirective(node, context) {
709
696
  context.write(`animate:${node.name}`);
710
- if (
711
- node.expression !== null &&
712
- !(node.expression.type === 'Identifier' && node.expression.name === node.name)
713
- ) {
697
+ if (node.expression !== null) {
714
698
  context.write('={');
715
699
  context.visit(node.expression);
716
700
  context.write('}');
@@ -994,10 +978,7 @@ const svelte_visitors = (comments, state) => ({
994
978
  for (const modifier of node.modifiers) {
995
979
  context.write(`|${modifier}`);
996
980
  }
997
- if (
998
- node.expression !== null &&
999
- !(node.expression.type === 'Identifier' && node.expression.name === node.name)
1000
- ) {
981
+ if (node.expression !== null) {
1001
982
  context.write('={');
1002
983
  context.visit(node.expression);
1003
984
  context.write('}');
@@ -1131,7 +1112,8 @@ const svelte_visitors = (comments, state) => ({
1131
1112
  },
1132
1113
 
1133
1114
  Text(node, context) {
1134
- context.write(node.data);
1115
+ context.write(node.raw);
1116
+ context.multiline ||= /[\r\n]/.test(node.raw);
1135
1117
  },
1136
1118
 
1137
1119
  TitleElement(node, context) {
@@ -1144,10 +1126,7 @@ const svelte_visitors = (comments, state) => ({
1144
1126
  for (const modifier of node.modifiers) {
1145
1127
  context.write(`|${modifier}`);
1146
1128
  }
1147
- if (
1148
- node.expression !== null &&
1149
- !(node.expression.type === 'Identifier' && node.expression.name === node.name)
1150
- ) {
1129
+ if (node.expression !== null) {
1151
1130
  context.write('={');
1152
1131
  context.visit(node.expression);
1153
1132
  context.write('}');
@@ -1156,10 +1135,7 @@ const svelte_visitors = (comments, state) => ({
1156
1135
 
1157
1136
  UseDirective(node, context) {
1158
1137
  context.write(`use:${node.name}`);
1159
- if (
1160
- node.expression !== null &&
1161
- !(node.expression.type === 'Identifier' && node.expression.name === node.name)
1162
- ) {
1138
+ if (node.expression !== null) {
1163
1139
  context.write('={');
1164
1140
  context.visit(node.expression);
1165
1141
  context.write('}');
@@ -174,6 +174,6 @@ export function adjust(state) {
174
174
  }
175
175
 
176
176
  ignore_stack = [];
177
- ignore_map.clear();
177
+ ignore_map = new Map(); // faster than .clear()
178
178
  cached_ignore_snapshot = null;
179
179
  }
@@ -1,5 +1,4 @@
1
1
  /** @import * as ESTree from 'estree' */
2
- import { walk } from 'zimmerframe';
3
2
  import { regex_is_valid_identifier } from '../phases/patterns.js';
4
3
  import { sanitize_template_string } from './sanitize_template_string.js';
5
4
  import { has_await_expression } from './ast.js';
@@ -5,6 +5,7 @@
5
5
  /** @import { Source } from '../preprocess/private.js' */
6
6
  /** @import { DecodedSourceMap, SourceMapSegment, RawSourceMap } from '@jridgewell/remapping' */
7
7
  import remapping from '@jridgewell/remapping';
8
+ import { push_array } from '../../internal/shared/utils.js';
8
9
 
9
10
  /**
10
11
  * @param {string} s
@@ -12,18 +13,6 @@ import remapping from '@jridgewell/remapping';
12
13
  function last_line_length(s) {
13
14
  return s.length - s.lastIndexOf('\n') - 1;
14
15
  }
15
-
16
- /**
17
- * `array.push(...items)` overflows the stack for large `items`.
18
- * @template T
19
- * @param {T[]} array
20
- * @param {T[]} items
21
- */
22
- function push_array(array, items) {
23
- for (let i = 0; i < items.length; i++) {
24
- array.push(items[i]);
25
- }
26
- }
27
16
  // mutate map in-place
28
17
 
29
18
  /**
package/src/constants.js CHANGED
@@ -55,12 +55,4 @@ export const IGNORABLE_RUNTIME_WARNINGS = /** @type {const} */ ([
55
55
  'ownership_invalid_mutation'
56
56
  ]);
57
57
 
58
- /**
59
- * Whitespace inside one of these elements will not result in
60
- * a whitespace node being created in any circumstances. (This
61
- * list is almost certainly very incomplete)
62
- * TODO this is currently unused
63
- */
64
- export const ELEMENTS_WITHOUT_TEXT = ['audio', 'datalist', 'dl', 'optgroup', 'select', 'video'];
65
-
66
58
  export const ATTACHMENT_KEY = '@attach';
@@ -1,7 +1,7 @@
1
1
  /** @import { ComponentContext, ComponentContextLegacy } from '#client' */
2
2
  /** @import { EventDispatcher } from './index.js' */
3
3
  /** @import { NotFunction } from './internal/types.js' */
4
- import { active_reaction, untrack } from './internal/client/runtime.js';
4
+ import { active_reaction, get_abort_signal, untrack } from './internal/client/runtime.js';
5
5
  import { is_array } from './internal/shared/utils.js';
6
6
  import { user_effect } from './internal/client/index.js';
7
7
  import * as e from './internal/client/errors.js';
@@ -46,7 +46,7 @@ if (DEV) {
46
46
  /**
47
47
  * Returns an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) that aborts when the current [derived](https://svelte.dev/docs/svelte/$derived) or [effect](https://svelte.dev/docs/svelte/$effect) re-runs or is destroyed.
48
48
  *
49
- * Must be called while a derived or effect is running.
49
+ * Must be called while a derived or effect is running, before anything is awaited.
50
50
  *
51
51
  * ```svelte
52
52
  * <script>
@@ -71,7 +71,7 @@ export function getAbortSignal() {
71
71
  e.get_abort_signal_outside_reaction();
72
72
  }
73
73
 
74
- return (active_reaction.ac ??= new AbortController()).signal;
74
+ return get_abort_signal(active_reaction);
75
75
  }
76
76
 
77
77
  /**
@@ -19,7 +19,7 @@ export const BOUNDARY_EFFECT = 1 << 7;
19
19
  * Set on the effect that `pause_effect` was called on, i.e. the root of a paused subtree,
20
20
  * as opposed to its descendants which are merely `INERT`. This allows `resume_effect` on
21
21
  * an ancestor to skip subtrees that were paused for their own reasons (such as a block
22
- * whose condition is still false) rather than resurrecting them
22
+ * whose condition is still false) rather than resurrecting them.
23
23
  */
24
24
  export const PAUSED = 1 << 8;
25
25
  /**
@@ -57,6 +57,13 @@ export const ASYNC = 1 << 22;
57
57
 
58
58
  export const ERROR_VALUE = 1 << 23;
59
59
 
60
+ /** A run of the reaction asked for an abort signal (see `getAbortSignal`) */
61
+ export const HAS_ABORT_CONTROLLER = 1 << 27;
62
+
63
+ // Flags exclusive to deriveds
64
+ /** The global state of the derived is a value that was assigned to it, rather than a run of it */
65
+ export const ASSIGNED = 1 << 26;
66
+
60
67
  export const STATE_SYMBOL = Symbol('$state');
61
68
  /** Marks component export objects, so that `proxy(...)` leaves them untouched */
62
69
  export const COMPONENT_SYMBOL = Symbol('component');
@@ -113,11 +113,11 @@ export function log_effect_tree(effect, highlighted = [], depth = 0, is_reachabl
113
113
  console.groupEnd();
114
114
  }
115
115
 
116
- if (effect.deps !== null) {
116
+ if (effect.edges !== null) {
117
117
  // eslint-disable-next-line no-console
118
118
  console.groupCollapsed('%cdeps', 'font-weight: normal');
119
119
 
120
- for (const dep of effect.deps) {
120
+ for (const dep of effect.edges) {
121
121
  log_dep(dep);
122
122
  }
123
123
 
@@ -163,8 +163,8 @@ function log_dep(dep) {
163
163
  untrack(() => snapshot(derived.v))
164
164
  );
165
165
 
166
- if (derived.deps) {
167
- for (const d of derived.deps) {
166
+ if (derived.edges) {
167
+ for (const d of derived.edges) {
168
168
  log_dep(d);
169
169
  }
170
170
  }
@@ -459,10 +459,10 @@ export function log_inconsistent_branches(effect) {
459
459
  console.log('%cfn:', 'font-weight: bold', branch.effect.fn);
460
460
  }
461
461
 
462
- if (branch.effect.deps !== null) {
462
+ if (branch.effect.edges !== null) {
463
463
  // eslint-disable-next-line no-console
464
464
  console.groupCollapsed('%cdeps', 'font-weight: normal');
465
- for (const dep of branch.effect.deps) {
465
+ for (const dep of branch.effect.edges) {
466
466
  log_dep(dep);
467
467
  }
468
468
  // eslint-disable-next-line no-console
@@ -2,23 +2,37 @@
2
2
  import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE } from '#client/constants';
3
3
  import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
4
4
  import { hydrating } from '../dom/hydration.js';
5
- import { dev_stack } from '../context.js';
5
+ import { component_context, dev_stack } from '../context.js';
6
6
 
7
7
  /**
8
- * @param {any} fn
8
+ * Assign locations immediately after cloning, before blocks or child components insert elements.
9
+ * During hydration, the compiler calls the factory's `hydrate_assign_locations` method after the
10
+ * template's children have their own locations and can be skipped.
11
+ * @param {() => DocumentFragment | Element} fn
9
12
  * @param {string} filename
10
13
  * @param {SourceLocation[]} locations
11
- * @returns {any}
12
14
  */
13
15
  export function add_locations(fn, filename, locations) {
14
- return (/** @type {any[]} */ ...args) => {
15
- const dom = fn(...args);
16
+ var create = () => {
17
+ var dom = fn();
16
18
 
17
- var node = hydrating ? dom : dom.nodeType === DOCUMENT_FRAGMENT_NODE ? dom.firstChild : dom;
18
- assign_locations(node, filename, locations);
19
+ if (!hydrating) {
20
+ assign_locations(
21
+ dom.nodeType === DOCUMENT_FRAGMENT_NODE ? dom.firstChild : dom,
22
+ filename,
23
+ locations
24
+ );
25
+ }
19
26
 
20
27
  return dom;
21
28
  };
29
+
30
+ create.hydrate_assign_locations = (/** @type {Node} */ node) => {
31
+ // Pass the node explicitly because the same factory can be called repeatedly (e.g. in each blocks).
32
+ if (hydrating) assign_locations(node, filename, locations);
33
+ };
34
+
35
+ return create;
22
36
  }
23
37
 
24
38
  /**
@@ -30,7 +44,8 @@ function assign_location(element, filename, location) {
30
44
  // @ts-expect-error
31
45
  element.__svelte_meta = {
32
46
  parent: dev_stack,
33
- loc: { file: filename, line: location[0], column: location[1] }
47
+ loc: { file: filename, line: location[0], column: location[1] },
48
+ component: component_context
34
49
  };
35
50
 
36
51
  if (location[2]) {
@@ -54,7 +69,8 @@ function assign_locations(node, filename, locations) {
54
69
  else if (comment.data[0] === HYDRATION_END) depth -= 1;
55
70
  }
56
71
 
57
- if (depth === 0 && node.nodeType === ELEMENT_NODE) {
72
+ // @ts-expect-error
73
+ if (depth === 0 && node.nodeType === ELEMENT_NODE && !node.__svelte_meta) {
58
74
  assign_location(/** @type {Element} */ (node), filename, locations[i++]);
59
75
  }
60
76
 
@@ -1,9 +1,11 @@
1
- /** @import { Derived, Reaction, Value } from '#client' */
1
+ /** @import { BatchRecord, BatchValue, Derived, DerivedRecord, Reaction, Value } from '#client' */
2
2
  import { UNINITIALIZED } from '../../../constants.js';
3
3
  import { snapshot } from '../../shared/clone.js';
4
4
  import { DERIVED, ASYNC, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
5
5
  import { effect_tracking } from '../reactivity/effects.js';
6
- import { active_reaction, untrack } from '../runtime.js';
6
+ import { batch_values, view_batch } from '../reactivity/batch.js';
7
+ import { get_view } from '../reactivity/deriveds.js';
8
+ import { active_reaction, run_of, untrack, version_of } from '../runtime.js';
7
9
 
8
10
  /**
9
11
  * @typedef {{
@@ -15,19 +17,67 @@ import { active_reaction, untrack } from '../runtime.js';
15
17
  export let tracing_expressions = null;
16
18
 
17
19
  /**
20
+ * The value of `signal`, with its version, as the active view of the world sees it (see `get`): for a
21
+ * derived, the run of it that applies — a record in the batch, or the derived itself — or an assigned value
18
22
  * @param {Value} signal
19
- * @param {TraceEntry} [entry]
23
+ * @returns {Value | DerivedRecord | BatchValue}
20
24
  */
21
- function log_entry(signal, entry) {
22
- const value = signal.v;
25
+ function view_of(signal) {
26
+ if (batch_values === null) return signal;
27
+ if ((signal.f & DERIVED) !== 0) return get_view(/** @type {Derived} */ (signal));
28
+ return batch_values.get(signal) ?? signal;
29
+ }
30
+
31
+ /**
32
+ * The run of `reaction` that applies in the active view of the world (see `run_of`), which for a derived
33
+ * in the view of a batch is its record in the batch if it has one (see `get_view`). While the reaction
34
+ * runs, that's its previous run
35
+ * @param {Reaction} reaction
36
+ * @returns {Reaction | BatchRecord}
37
+ */
38
+ function run_in_view(reaction) {
39
+ if ((reaction.f & DERIVED) !== 0 && batch_values !== null) {
40
+ return view_batch?.get_record(reaction) ?? reaction;
41
+ }
42
+
43
+ return run_of(reaction);
44
+ }
45
+
46
+ /**
47
+ * Whether `signal` changed in the active view of the world since the traced reaction last ran. If its previous
48
+ * run read it, that's whether the version it saw is no longer current. Otherwise (it's a dependency of a derived
49
+ * the reaction read, or the reaction didn't read it before), whether it was written since `since`: the one place
50
+ * where versions are compared by order, which is only a heuristic — a view can undo writes, which gives values
51
+ * older versions again
52
+ * @param {Value} signal
53
+ * @param {Reaction | BatchRecord} run the previous run of the traced reaction (see `run_in_view`)
54
+ * @param {number} since the version of the reaction's previous run (`0` if there's none)
55
+ */
56
+ function is_changed(signal, run, since) {
57
+ var i = run.deps?.indexOf(signal) ?? -1;
58
+ var seen = i === -1 ? undefined : run.dv?.[i];
59
+
60
+ if (seen !== undefined) return version_of(signal) !== seen;
61
+
62
+ return since === 0 || version_of(signal) > since;
63
+ }
64
+
65
+ /**
66
+ * @param {Value} signal
67
+ * @param {TraceEntry | undefined} entry
68
+ * @param {Reaction | BatchRecord} run the previous run of the traced reaction (see `run_in_view`)
69
+ * @param {number} since the version of the reaction's previous run (`0` if there's none)
70
+ */
71
+ function log_entry(signal, entry, run, since) {
72
+ const view = view_of(signal);
73
+ const value = view.v;
23
74
 
24
75
  if (value === UNINITIALIZED) {
25
76
  return;
26
77
  }
27
78
 
28
79
  const type = get_type(signal);
29
- const current_reaction = /** @type {Reaction} */ (active_reaction);
30
- const dirty = signal.wv > current_reaction.wv || current_reaction.wv === 0;
80
+ const dirty = is_changed(signal, run, since);
31
81
  const style = dirty
32
82
  ? 'color: CornflowerBlue; font-weight: bold'
33
83
  : 'color: grey; font-weight: normal';
@@ -42,10 +92,13 @@ function log_entry(signal, entry) {
42
92
  : value
43
93
  );
44
94
 
45
- if (type === '$derived') {
46
- const deps = new Set(/** @type {Derived} */ (signal).deps);
95
+ if ((signal.f & DERIVED) !== 0) {
96
+ const deps = new Set(
97
+ ('deps' in view ? view : run_in_view(/** @type {Derived} */ (signal))).deps
98
+ );
99
+
47
100
  for (const dep of deps) {
48
- log_entry(dep);
101
+ log_entry(dep, undefined, run, since);
49
102
  }
50
103
  }
51
104
 
@@ -111,10 +164,13 @@ export function trace(label, fn) {
111
164
  console.group(`${prefix} %c(${time}ms)`, 'color: grey');
112
165
 
113
166
  var entries = tracing_expressions.entries;
167
+ var reaction = /** @type {Reaction} */ (active_reaction);
168
+ var run = run_in_view(reaction);
169
+ var since = run.wv ?? reaction.wv;
114
170
 
115
171
  untrack(() => {
116
172
  for (const [signal, traces] of entries) {
117
- log_entry(signal, traces);
173
+ log_entry(signal, traces, run, since);
118
174
  }
119
175
  });
120
176
 
@@ -1,4 +1,5 @@
1
- /** @import { Blocker, TemplateNode, Value } from '#client' */
1
+ /** @import { TemplateNode, Value } from '#client' */
2
+ /** @import { Blocker } from '../../reactivity/async.js' */
2
3
  import { flatten } from '../../reactivity/async.js';
3
4
  import { get } from '../../runtime.js';
4
5
  import {