svelte 5.58.1 → 5.59.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 (86) 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/visitors/RegularElement.js +23 -13
  28. package/src/compiler/phases/3-transform/client/visitors/RenderTag.js +13 -1
  29. package/src/compiler/phases/3-transform/client/visitors/SnippetBlock.js +11 -1
  30. package/src/compiler/phases/3-transform/client/visitors/SvelteBoundary.js +8 -5
  31. package/src/compiler/phases/3-transform/client/visitors/shared/component.js +17 -8
  32. package/src/compiler/phases/3-transform/client/visitors/shared/element.js +4 -3
  33. package/src/compiler/phases/3-transform/client/visitors/shared/fragment.js +7 -1
  34. package/src/compiler/phases/3-transform/server/transform-server.js +14 -1
  35. package/src/compiler/phases/3-transform/server/visitors/RegularElement.js +8 -4
  36. package/src/compiler/state.js +12 -0
  37. package/src/compiler/utils/builders.js +2 -2
  38. package/src/compiler/validate-options.js +47 -1
  39. package/src/internal/client/constants.js +7 -0
  40. package/src/internal/client/custom-renderer/containers.js +213 -0
  41. package/src/internal/client/custom-renderer/cross.js +145 -0
  42. package/src/internal/client/custom-renderer/index.js +28 -0
  43. package/src/internal/client/custom-renderer/state.js +59 -0
  44. package/src/internal/client/dev/css.js +3 -1
  45. package/src/internal/client/dev/elements.js +9 -7
  46. package/src/internal/client/dev/validation.js +2 -1
  47. package/src/internal/client/dom/blocks/await.js +3 -2
  48. package/src/internal/client/dom/blocks/boundary.js +41 -24
  49. package/src/internal/client/dom/blocks/branches.js +83 -57
  50. package/src/internal/client/dom/blocks/css-props.js +31 -3
  51. package/src/internal/client/dom/blocks/each.js +78 -32
  52. package/src/internal/client/dom/blocks/html.js +19 -13
  53. package/src/internal/client/dom/blocks/slot.js +3 -3
  54. package/src/internal/client/dom/blocks/snippet.js +52 -3
  55. package/src/internal/client/dom/blocks/svelte-element.js +17 -6
  56. package/src/internal/client/dom/blocks/svelte-head.js +13 -4
  57. package/src/internal/client/dom/css.js +4 -4
  58. package/src/internal/client/dom/elements/attributes.js +89 -50
  59. package/src/internal/client/dom/elements/bindings/select.js +11 -2
  60. package/src/internal/client/dom/elements/class.js +16 -5
  61. package/src/internal/client/dom/elements/events.js +126 -19
  62. package/src/internal/client/dom/elements/misc.js +3 -2
  63. package/src/internal/client/dom/elements/style.js +17 -6
  64. package/src/internal/client/dom/hydration.js +6 -6
  65. package/src/internal/client/dom/operations.js +433 -11
  66. package/src/internal/client/dom/reconciler.js +2 -2
  67. package/src/internal/client/dom/template.js +94 -66
  68. package/src/internal/client/errors.js +67 -0
  69. package/src/internal/client/index.js +9 -1
  70. package/src/internal/client/reactivity/async.js +10 -0
  71. package/src/internal/client/reactivity/batch.js +22 -1
  72. package/src/internal/client/reactivity/effects.js +44 -27
  73. package/src/internal/client/render.js +53 -88
  74. package/src/internal/client/runtime.js +6 -1
  75. package/src/internal/disclose-version.js +1 -1
  76. package/src/internal/flags/custom-renderer-server.js +10 -0
  77. package/src/internal/flags/custom-renderer.js +3 -0
  78. package/src/internal/flags/index.js +6 -0
  79. package/src/internal/init-operations.js +3 -0
  80. package/src/internal/shared/errors.js +20 -0
  81. package/src/legacy/legacy-client.js +6 -2
  82. package/src/renderer/index-server.js +18 -0
  83. package/src/renderer/index.js +2 -0
  84. package/src/version.js +1 -1
  85. package/types/index.d.ts +245 -25
  86. package/types/index.d.ts.map +10 -1
@@ -6,7 +6,7 @@ import { walk } from 'zimmerframe';
6
6
  import * as b from '#compiler/builders';
7
7
  import { build_getter, get_transform } from './utils.js';
8
8
  import { render_stylesheet } from '../css/index.js';
9
- import { dev, filename } from '../../../state.js';
9
+ import { dev, filename, custom_renderer } from '../../../state.js';
10
10
  import { AnimateDirective } from './visitors/AnimateDirective.js';
11
11
  import { ArrowFunctionExpression } from './visitors/ArrowFunctionExpression.js';
12
12
  import { AssignmentExpression } from './visitors/AssignmentExpression.js';
@@ -555,6 +555,19 @@ export function client_component(analysis, options) {
555
555
  if (options.discloseVersion) {
556
556
  body.unshift(b.imports([], 'svelte/internal/disclose-version'));
557
557
  }
558
+ // this needs to be !== undefined because we want to enable the flag even if the custom renderer is null (which is a valid value meaning "no renderer")
559
+ if (custom_renderer !== undefined) {
560
+ body.unshift(b.imports([], 'svelte/internal/flags/custom-renderer'));
561
+ }
562
+
563
+ if (custom_renderer) {
564
+ body.unshift(b.imports([['$renderer', '$renderer', true]], custom_renderer));
565
+ } else {
566
+ // DOM components set up the DOM operations (prototype caches etc) by importing this module, rather
567
+ // than `mount`/`hydrate` doing it, so that apps that only use custom renderers don't bundle it
568
+ // (or touch `window` etc, which might not exist). It only runs once however many components import it
569
+ body.unshift(b.imports([], 'svelte/internal/init-operations'));
570
+ }
558
571
 
559
572
  if (options.compatibility.componentApi === 4) {
560
573
  body.unshift(b.imports([['createClassComponent', '$$_createClassComponent']], 'svelte/legacy'));
@@ -579,6 +592,60 @@ export function client_component(analysis, options) {
579
592
  component_block.body.unshift(b.const(analysis.props_id, b.call('$.props_id')));
580
593
  }
581
594
 
595
+ if (custom_renderer !== undefined) {
596
+ // when the custom renderer feature is enabled every component pushes a renderer: components
597
+ // with a renderer module push `$renderer`, DOM components push `null`
598
+ const renderer = custom_renderer ? b.id('$renderer') : b.literal(null);
599
+ const params = should_inject_props ? [b.id('$$anchor'), b.id('$$props')] : [b.id('$$anchor')];
600
+
601
+ /** @type {ESTree.Expression} */
602
+ let is_crossing = b.call('$.is_crossing', renderer);
603
+
604
+ if (options.compatibility.componentApi === 4) {
605
+ // `new Component(options)` is handled by the legacy wrapper inside the body
606
+ is_crossing = b.logical('&&', b.unary('!', b.id('new.target')), is_crossing);
607
+ }
608
+
609
+ /** @type {ESTree.Expression} */
610
+ let component = b.id(analysis.name);
611
+
612
+ if (options.hmr) {
613
+ // with HMR, the component's binding is reassigned to the HMR wrapper, which is what
614
+ // called us. Call the unwrapped component, so the guest isn't wrapped a second time
615
+ component = b.logical(
616
+ '??',
617
+ /** @type {ESTree.Expression} */ ({
618
+ type: 'ChainExpression',
619
+ expression: b.member(b.member(component, b.member_id('$.HMR'), true), 'fn', false, true)
620
+ }),
621
+ component
622
+ );
623
+ }
624
+
625
+ component_block.body = [
626
+ // if the component is rendered inside content that belongs to a different renderer,
627
+ // render it across the boundary (which calls the component again, with its own renderer)
628
+ b.if(
629
+ is_crossing,
630
+ b.return(
631
+ b.call(
632
+ '$.cross',
633
+ renderer,
634
+ b.id('$$anchor'),
635
+ b.arrow([b.id('$$anchor')], b.call(component, ...params))
636
+ )
637
+ )
638
+ ),
639
+ b.var('$$previous_renderer', b.call('$.push_renderer', renderer)),
640
+ {
641
+ type: 'TryStatement',
642
+ block: b.block(component_block.body),
643
+ handler: null,
644
+ finalizer: b.block([b.stmt(b.call('$.pop_renderer', b.id('$$previous_renderer')))])
645
+ }
646
+ ];
647
+ }
648
+
582
649
  if (state.events.size > 0) {
583
650
  body.push(
584
651
  b.stmt(b.call('$.delegate', b.array(Array.from(state.events).map((name) => b.literal(name)))))
@@ -3,7 +3,7 @@
3
3
  /** @import { ComponentClientTransformState } from '../types.js' */
4
4
  /** @import { Node } from './types.js' */
5
5
  import { TEMPLATE_USE_MATHML, TEMPLATE_USE_SVG } from '../../../../../constants.js';
6
- import { dev, locator } from '../../../../state.js';
6
+ import { dev, locator, custom_renderer } from '../../../../state.js';
7
7
  import * as b from '../../../../utils/builders.js';
8
8
 
9
9
  /**
@@ -52,7 +52,8 @@ export function build_hydration_locations_statement(state, template_name, node)
52
52
  */
53
53
  export function transform_template(state, name, flags = 0) {
54
54
  const namespace = state.metadata.namespace;
55
- const tree = state.options.fragments === 'tree';
55
+ // custom renderers needs a tree to work because there's no template element we can use
56
+ const tree = state.options.fragments === 'tree' || custom_renderer;
56
57
 
57
58
  const { nodes } = state.template;
58
59
  const is_lone_anchor = nodes.length === 1 && nodes[0].type === 'comment';
@@ -87,7 +88,7 @@ export function transform_template(state, name, flags = 0) {
87
88
  flags ? b.literal(flags) : undefined
88
89
  );
89
90
 
90
- if (state.template.contains_script_tag) {
91
+ if (state.template.contains_script_tag && !custom_renderer) {
91
92
  call = b.call(`$.with_script`, call);
92
93
  }
93
94
 
@@ -137,7 +137,7 @@ function objectify(item) {
137
137
  attributes.properties.push(
138
138
  b.prop(
139
139
  'init',
140
- b.key(fix_attribute_casing(key)),
140
+ b.key(item.is_html ? fix_attribute_casing(key) : key),
141
141
  value === undefined ? b.void0 : b.literal(value)
142
142
  )
143
143
  );
@@ -7,7 +7,7 @@ import {
7
7
  is_dom_property,
8
8
  is_load_error_element
9
9
  } from '../../../../../utils.js';
10
- import { is_ignored } from '../../../../state.js';
10
+ import { is_ignored, custom_renderer } from '../../../../state.js';
11
11
  import { is_event_attribute, is_text_attribute } from '../../../../utils/ast.js';
12
12
  import * as b from '#compiler/builders';
13
13
  import {
@@ -39,7 +39,8 @@ import { TEMPLATE_FRAGMENT } from '../../../../../constants.js';
39
39
  * @param {ComponentContext} context
40
40
  */
41
41
  export function RegularElement(node, context) {
42
- const is_html = context.state.metadata.namespace === 'html' && node.name !== 'svg';
42
+ const is_html =
43
+ context.state.metadata.namespace === 'html' && node.name !== 'svg' && !custom_renderer;
43
44
  const name = is_html ? node.name.toLowerCase() : node.name;
44
45
  context.state.template.push_element(name, node.start, is_html);
45
46
 
@@ -48,7 +49,8 @@ export function RegularElement(node, context) {
48
49
  return;
49
50
  }
50
51
 
51
- const is_custom_element = is_custom_element_node(node);
52
+ // we never treat elements as custom element in custom renderers, since we don't want to apply special handling to them (e.g. class merging)
53
+ const is_custom_element = is_custom_element_node(node) && !custom_renderer;
52
54
 
53
55
  // cloneNode is faster, but it does not instantiate the underlying class of the
54
56
  // custom element until the template is connected to the dom, which would
@@ -209,7 +211,8 @@ export function RegularElement(node, context) {
209
211
 
210
212
  /** If true, needs `__value` for inputs */
211
213
  const needs_special_value_handling =
212
- name === 'option' || name === 'select' || bindings.has('group') || bindings.has('checked');
214
+ !custom_renderer &&
215
+ (name === 'option' || name === 'select' || bindings.has('group') || bindings.has('checked'));
213
216
 
214
217
  if (has_spread) {
215
218
  build_attribute_effect(
@@ -234,7 +237,11 @@ export function RegularElement(node, context) {
234
237
 
235
238
  // `<select defaultValue>` needs the options to exist before it can mark one
236
239
  // as selected, so it is handled after the children, alongside `value`
237
- if (node.name === 'select' && get_attribute_name(node, attribute) === 'defaultValue') {
240
+ if (
241
+ !custom_renderer &&
242
+ node.name === 'select' &&
243
+ get_attribute_name(node, attribute) === 'defaultValue'
244
+ ) {
238
245
  continue;
239
246
  }
240
247
 
@@ -261,7 +268,7 @@ export function RegularElement(node, context) {
261
268
  if (name !== 'class' || value) {
262
269
  context.state.template.set_prop(attribute.name, value === true ? '' : value);
263
270
  }
264
- } else if (name === 'autofocus') {
271
+ } else if (name === 'autofocus' && !custom_renderer) {
265
272
  let { value } = build_attribute_value(attribute.value, context);
266
273
  context.state.init.push(b.stmt(b.call('$.autofocus', node_id, value)));
267
274
  } else if (name === 'class') {
@@ -351,7 +358,10 @@ export function RegularElement(node, context) {
351
358
  // to descend into it if the text is non-reactive
352
359
  // in the rare case that we have static text that can't be inlined
353
360
  // (e.g. `<span>{location}</span>`), set `textContent` programmatically
361
+ // (custom renderers have no `textContent` — the regular path below creates a text node
362
+ // and updates it via the renderer instead)
354
363
  const use_text_content =
364
+ !custom_renderer &&
355
365
  trimmed.every((node) => node.type === 'Text' || node.type === 'ExpressionTag') &&
356
366
  trimmed.every(
357
367
  (node) =>
@@ -371,7 +381,7 @@ export function RegularElement(node, context) {
371
381
  b.stmt(b.assignment('=', b.member(context.state.node, 'textContent'), value))
372
382
  );
373
383
  }
374
- } else if (is_customizable_select_element(node)) {
384
+ } else if (is_customizable_select_element(node) && !custom_renderer) {
375
385
  // For <option>, <optgroup>, or <select> elements with rich content, we need to branch based on browser support.
376
386
  // Modern browsers preserve rich HTML in options, older browsers strip it to text only.
377
387
  // We create a separate template for the rich content and append it to the element.
@@ -437,7 +447,7 @@ export function RegularElement(node, context) {
437
447
 
438
448
  // The same applies if it's a `<template>` element, since we need to
439
449
  // set the value of `hydrate_node` to `node.content`
440
- if (name === 'template') {
450
+ if (name === 'template' && !custom_renderer) {
441
451
  needs_reset = true;
442
452
  child_state.init.push(b.stmt(b.call('$.hydrate_template', arg)));
443
453
  arg = b.member(arg, 'content');
@@ -484,7 +494,7 @@ export function RegularElement(node, context) {
484
494
  context.state.after_update.push(...element_state.after_update);
485
495
  }
486
496
 
487
- if (name === 'selectedcontent') {
497
+ if (name === 'selectedcontent' && !custom_renderer) {
488
498
  context.state.init.push(
489
499
  b.stmt(
490
500
  b.call(
@@ -529,7 +539,7 @@ export function RegularElement(node, context) {
529
539
 
530
540
  // deferred from the attribute loop above, so that the options it selects from
531
541
  // have been created and had their values assigned
532
- if (!has_spread && name === 'select') {
542
+ if (!custom_renderer && !has_spread && name === 'select') {
533
543
  const default_value = /** @type {AST.Attribute[]} */ (attributes).find(
534
544
  (attribute) => get_attribute_name(node, attribute) === 'defaultValue'
535
545
  );
@@ -633,7 +643,7 @@ export function build_style_directives_object(
633
643
  * @param {Array<AST.Attribute | AST.SpreadAttribute>} attributes
634
644
  */
635
645
  function build_element_attribute_update(element, node_id, name, value, attributes) {
636
- if (name === 'muted') {
646
+ if (name === 'muted' && !custom_renderer) {
637
647
  // Special case for Firefox who needs it set as a property in order to work
638
648
  return b.assignment('=', b.member(node_id, b.id('muted')), value);
639
649
  }
@@ -674,12 +684,12 @@ function build_element_attribute_update(element, node_id, name, value, attribute
674
684
  return b.call('$.set_default_checked', node_id, value);
675
685
  }
676
686
 
677
- if (is_dom_property(name)) {
687
+ if (is_dom_property(name) && !custom_renderer) {
678
688
  return b.assignment('=', b.member(node_id, name), value);
679
689
  }
680
690
 
681
691
  return b.call(
682
- name.startsWith('xlink') ? '$.set_xlink_attribute' : '$.set_attribute',
692
+ name.startsWith('xlink') && !custom_renderer ? '$.set_xlink_attribute' : '$.set_attribute',
683
693
  node_id,
684
694
  b.literal(name),
685
695
  value,
@@ -2,9 +2,9 @@
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext } from '../types' */
4
4
  import { unwrap_optional } from '../../../../utils/ast.js';
5
+ import { custom_renderer, source } from '../../../../state.js';
5
6
  import * as b from '#compiler/builders';
6
7
  import { add_svelte_meta, build_expression, Memoizer } from './shared/utils.js';
7
- import { source } from '../../../../state.js';
8
8
 
9
9
  /**
10
10
  * @param {AST.RenderTag} node
@@ -50,6 +50,12 @@ export function RenderTag(node, context) {
50
50
  };
51
51
 
52
52
  if (node.metadata.dynamic) {
53
+ // In custom renderer components, validate that the snippet is compatible
54
+ // with the current renderer before rendering it
55
+ if (custom_renderer) {
56
+ snippet_function = b.call('$.validate_snippet_renderer', snippet_function);
57
+ }
58
+
53
59
  // If we have a chain expression then ensure a nullish snippet function gets turned into an empty one
54
60
  if (node.expression.type === 'ChainExpression') {
55
61
  snippet_function = b.logical('??', snippet_function, b.id('$.noop'));
@@ -64,6 +70,12 @@ export function RenderTag(node, context) {
64
70
  )
65
71
  );
66
72
  } else {
73
+ // In custom renderer components, validate that the snippet is compatible
74
+ // with the current renderer before rendering it
75
+ if (custom_renderer) {
76
+ snippet_function = b.call('$.validate_snippet_renderer', snippet_function);
77
+ }
78
+
67
79
  statements.push(
68
80
  add_svelte_meta(
69
81
  (node.expression.type === 'CallExpression' ? b.call : b.maybe_call)(
@@ -1,7 +1,7 @@
1
1
  /** @import { AssignmentPattern, BlockStatement, Expression, Identifier, Statement } from 'estree' */
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext } from '../types' */
4
- import { dev } from '../../../../state.js';
4
+ import { dev, custom_renderer } from '../../../../state.js';
5
5
  import { extract_paths } from '../../../../utils/ast.js';
6
6
  import * as b from '#compiler/builders';
7
7
  import { get_value } from './shared/declarations.js';
@@ -79,6 +79,16 @@ export function SnippetBlock(node, context) {
79
79
  ? b.call('$.wrap_snippet', b.id(context.state.analysis.name), b.function(null, args, body))
80
80
  : b.arrow(args, body);
81
81
 
82
+ // when custom renderers are enabled, snippets remember which renderer they were
83
+ // compiled for (`null` for DOM) so that they can be rendered across renderers
84
+ if (custom_renderer !== undefined) {
85
+ snippet = b.call(
86
+ '$.renderer_snippet',
87
+ custom_renderer ? b.id('$renderer') : b.literal(null),
88
+ snippet
89
+ );
90
+ }
91
+
82
92
  const declaration = b.const(node.expression, snippet);
83
93
 
84
94
  // Top-level snippets are hoisted so they can be referenced in the `<script>`
@@ -1,7 +1,6 @@
1
1
  /** @import { BlockStatement, Statement, Expression, VariableDeclaration } from 'estree' */
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext } from '../types' */
4
- import { dev } from '../../../../state.js';
5
4
  import * as b from '#compiler/builders';
6
5
 
7
6
  /**
@@ -86,10 +85,14 @@ export function SvelteBoundary(node, context) {
86
85
 
87
86
  const snippet = /** @type {VariableDeclaration} */ (statements[0]);
88
87
 
89
- const snippet_fn = dev
90
- ? // @ts-expect-error we know this shape is correct
91
- snippet.declarations[0].init.arguments[1]
92
- : snippet.declarations[0].init;
88
+ /** @type {any} */
89
+ let snippet_fn = snippet.declarations[0].init;
90
+
91
+ // unwrap `$.wrap_snippet(component, fn)` (dev) and `$.renderer_snippet(renderer, fn)`
92
+ // (custom renderers), which both take the snippet function as their last argument
93
+ while (snippet_fn.type === 'CallExpression') {
94
+ snippet_fn = snippet_fn.arguments.at(-1);
95
+ }
93
96
 
94
97
  if (!context.state.options.experimental.async) {
95
98
  snippet_fn.body.body.unshift(
@@ -1,7 +1,7 @@
1
1
  /** @import { BlockStatement, Expression, ExpressionStatement, Identifier, MemberExpression, Pattern, Property, SequenceExpression, SourceLocation, Statement } from 'estree' */
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext } from '../../types.js' */
4
- import { dev, is_ignored } from '../../../../../state.js';
4
+ import { custom_renderer, dev, is_ignored } from '../../../../../state.js';
5
5
  import { get_attribute_chunks, object } from '../../../../../utils/ast.js';
6
6
  import * as b from '#compiler/builders';
7
7
  import { add_svelte_meta, build_bind_this, Memoizer, validate_binding } from '../shared/utils.js';
@@ -391,13 +391,20 @@ export function build_component(node, component_name, loc, context) {
391
391
  !node.attributes.some((attr) => attr.type === 'LetDirective')
392
392
  )
393
393
  ) {
394
+ let children_fn = dev
395
+ ? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn)
396
+ : slot_fn;
397
+
398
+ if (custom_renderer !== undefined) {
399
+ children_fn = b.call(
400
+ '$.renderer_snippet',
401
+ custom_renderer ? b.id('$renderer') : b.literal(null),
402
+ children_fn
403
+ );
404
+ }
405
+
394
406
  // create `children` prop...
395
- push_prop(
396
- b.init(
397
- 'children',
398
- dev ? b.call('$.wrap_snippet', b.id(context.state.analysis.name), slot_fn) : slot_fn
399
- )
400
- );
407
+ push_prop(b.init('children', children_fn));
401
408
 
402
409
  // and `$$slots.default: true` so that `<slot>` on the child works
403
410
  serialized_slots.push(b.init(slot_name, b.true));
@@ -500,7 +507,9 @@ export function build_component(node, component_name, loc, context) {
500
507
 
501
508
  statements.push(
502
509
  b.stmt(b.call('$.css_props', anchor, b.thunk(b.object(custom_css_props)))),
503
- b.stmt(fn(b.member(anchor, 'lastChild'))),
510
+ b.stmt(
511
+ fn(custom_renderer ? b.call('$.get_last_child', anchor) : b.member(anchor, 'lastChild'))
512
+ ),
504
513
  b.stmt(b.call('$.reset', anchor))
505
514
  );
506
515
  } else {
@@ -4,7 +4,7 @@
4
4
  /** @import { ExpressionMetadata } from '../../../../nodes.js' */
5
5
  import { escape_html } from '../../../../../../escaping.js';
6
6
  import { normalize_attribute } from '../../../../../../utils.js';
7
- import { is_ignored } from '../../../../../state.js';
7
+ import { is_ignored, custom_renderer } from '../../../../../state.js';
8
8
  import { is_event_attribute } from '../../../../../utils/ast.js';
9
9
  import * as b from '#compiler/builders';
10
10
  import { build_class_directives_object, build_style_directives_object } from '../RegularElement.js';
@@ -30,7 +30,8 @@ export function build_attribute_effect(
30
30
  ) {
31
31
  /** @type {ObjectExpression['properties']} */
32
32
  const values = [];
33
- const is_select = element.type === 'RegularElement' && element.name === 'select';
33
+ const is_select =
34
+ !custom_renderer && element.type === 'RegularElement' && element.name === 'select';
34
35
 
35
36
  const memoizer = new Memoizer();
36
37
 
@@ -139,7 +140,7 @@ export function build_attribute_value(value, context, memoize = (value) => value
139
140
  * @param {AST.Attribute} attribute
140
141
  */
141
142
  export function get_attribute_name(element, attribute) {
142
- if (!element.metadata.svg && !element.metadata.mathml) {
143
+ if (!custom_renderer && !element.metadata.svg && !element.metadata.mathml) {
143
144
  return normalize_attribute(attribute.name);
144
145
  }
145
146
 
@@ -2,6 +2,7 @@
2
2
  /** @import { AST } from '#compiler' */
3
3
  /** @import { ComponentContext } from '../../types' */
4
4
  import { cannot_be_set_statically } from '../../../../../../utils.js';
5
+ import { custom_renderer } from '../../../../../state.js';
5
6
  import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
6
7
  import * as b from '#compiler/builders';
7
8
  import { is_custom_element_node } from '../../../../nodes.js';
@@ -83,7 +84,12 @@ export function process_children(nodes, initial, is_element, context) {
83
84
  if (has_state && !within_bound_contenteditable) {
84
85
  context.state.update.push(update);
85
86
  } else {
86
- context.state.init.push(b.stmt(b.assignment('=', b.member(id, 'nodeValue'), value)));
87
+ if (custom_renderer) {
88
+ // custom renderers need to use the method to invoke the renderer
89
+ context.state.init.push(update);
90
+ } else {
91
+ context.state.init.push(b.stmt(b.assignment('=', b.member(id, 'nodeValue'), value)));
92
+ }
87
93
  }
88
94
  }
89
95
 
@@ -6,7 +6,7 @@ import { walk } from 'zimmerframe';
6
6
  import { set_scope } from '../../scope.js';
7
7
  import { extract_identifiers } from '../../../utils/ast.js';
8
8
  import * as b from '#compiler/builders';
9
- import { component_name, dev, filename } from '../../../state.js';
9
+ import { component_name, custom_renderer, dev, filename } from '../../../state.js';
10
10
  import { render_stylesheet } from '../css/index.js';
11
11
  import { AssignmentExpression } from './visitors/AssignmentExpression.js';
12
12
  import { AwaitBlock } from './visitors/AwaitBlock.js';
@@ -92,6 +92,19 @@ const template_visitors = {
92
92
  * @returns {ESTree.Program}
93
93
  */
94
94
  export function server_component(analysis, options) {
95
+ // Components compiled with a custom renderer have no meaningful server
96
+ // representation — they only render on the client through the custom
97
+ // renderer — so we compile them down to a no-op.
98
+ if (custom_renderer) {
99
+ const component_function = b.function_declaration(b.id(analysis.name), [], b.block([]));
100
+
101
+ return {
102
+ type: 'Program',
103
+ sourceType: 'module',
104
+ body: [b.export_default(component_function)]
105
+ };
106
+ }
107
+
95
108
  /** @type {ComponentServerTransformState} */
96
109
  const state = {
97
110
  analysis,
@@ -3,7 +3,7 @@
3
3
  /** @import { ComponentContext, ComponentServerTransformState } from '../types.js' */
4
4
  /** @import { Scope } from '../../../scope.js' */
5
5
  import { is_void } from '../../../../../utils.js';
6
- import { dev, locator } from '../../../../state.js';
6
+ import { dev, locator, custom_renderer } from '../../../../state.js';
7
7
  import * as b from '#compiler/builders';
8
8
  import { clean_nodes, determine_namespace_for_children } from '../../utils.js';
9
9
  import { build_element_attributes, prepare_element_spread_object } from './shared/element.js';
@@ -117,7 +117,7 @@ export function RegularElement(node, context) {
117
117
 
118
118
  const [attributes, ...rest] = prepare_element_spread_object(node, context, optimiser.transform);
119
119
 
120
- if (is_customizable_select_element(node)) {
120
+ if (is_customizable_select_element(node) && !custom_renderer) {
121
121
  rest.push(b.true);
122
122
  }
123
123
 
@@ -164,7 +164,7 @@ export function RegularElement(node, context) {
164
164
 
165
165
  const [attributes, ...rest] = prepare_element_spread_object(node, context, optimiser.transform);
166
166
 
167
- if (is_customizable_select_element(node)) {
167
+ if (is_customizable_select_element(node) && !custom_renderer) {
168
168
  rest.push(b.true);
169
169
  }
170
170
 
@@ -199,7 +199,11 @@ export function RegularElement(node, context) {
199
199
  } else {
200
200
  // For optgroup or select with rich content, add hydration marker at the start
201
201
  process_children(trimmed, { ...context, state });
202
- if ((name === 'optgroup' || name === 'select') && is_customizable_select_element(node)) {
202
+ if (
203
+ (name === 'optgroup' || name === 'select') &&
204
+ is_customizable_select_element(node) &&
205
+ !custom_renderer
206
+ ) {
203
207
  state.template.push(b.literal('<!>'));
204
208
  }
205
209
  }
@@ -50,6 +50,15 @@ export let dev;
50
50
 
51
51
  export let runes = false;
52
52
 
53
+ /**
54
+ * The custom renderer for the component currently being compiled:
55
+ * - `string`: the renderer module path (the component uses a custom renderer)
56
+ * - `null`: the custom renderer feature is enabled but this component renders to the DOM
57
+ * - `undefined`: the custom renderer feature is off
58
+ * @type {string | null | undefined}
59
+ */
60
+ export let custom_renderer = undefined;
61
+
53
62
  /** @type {(index: number) => Location} */
54
63
  export let locator;
55
64
 
@@ -143,6 +152,7 @@ export function is_ignored(node, code) {
143
152
  export function reset(state) {
144
153
  dev = false;
145
154
  runes = false;
155
+ custom_renderer = undefined;
146
156
  component_name = UNKNOWN_FILENAME;
147
157
  source = '';
148
158
  source_lines = undefined;
@@ -159,6 +169,7 @@ export function reset(state) {
159
169
  * component_name?: string;
160
170
  * rootDir?: string;
161
171
  * runes: boolean;
172
+ * custom_renderer?: string | null | undefined;
162
173
  * }} state
163
174
  */
164
175
  export function adjust(state) {
@@ -166,6 +177,7 @@ export function adjust(state) {
166
177
 
167
178
  dev = state.dev;
168
179
  runes = state.runes;
180
+ custom_renderer = state.custom_renderer;
169
181
  component_name = state.component_name ?? UNKNOWN_FILENAME;
170
182
 
171
183
  if (typeof root_dir === 'string' && filename.startsWith(root_dir)) {
@@ -636,7 +636,7 @@ export function import_all(as, source) {
636
636
  }
637
637
 
638
638
  /**
639
- * @param {Array<[string, string]>} parts
639
+ * @param {Array<[string, string] | [string, string, boolean]>} parts
640
640
  * @param {string} source
641
641
  * @returns {ESTree.ImportDeclaration}
642
642
  */
@@ -646,7 +646,7 @@ export function imports(parts, source) {
646
646
  attributes: [],
647
647
  source: literal(source),
648
648
  specifiers: parts.map((p) => ({
649
- type: 'ImportSpecifier',
649
+ type: p[2] ? 'ImportDefaultSpecifier' : 'ImportSpecifier',
650
650
  imported: id(p[0]),
651
651
  local: id(p[1])
652
652
  }))
@@ -44,7 +44,53 @@ const common_options = {
44
44
  warningFilter: fun(() => true),
45
45
 
46
46
  experimental: object({
47
- async: boolean(false)
47
+ async: boolean(false),
48
+ // `customRenderer` can be:
49
+ // - `undefined`: the feature is off, components compile to plain DOM
50
+ // - `true`: the feature is on, every component defaults to DOM (opt in via `<svelte:options>`)
51
+ // - a string: the feature is on, every component defaults to that module (opt out via `<svelte:options>`)
52
+ // - a function: the feature is on, the module is resolved lazily per file (`null` means DOM)
53
+ // The normalized value is always a function returning `string | null | undefined` where
54
+ // `string` is the renderer module, `null` is "DOM but feature enabled" (push `null`) and
55
+ // `undefined` is "feature off" (no renderer pushed at all). A function can't return
56
+ // `undefined` for individual files: those components wouldn't push `null`, so they'd break
57
+ // when rendered inside a component that uses a custom renderer. We throw rather than
58
+ // silently treating `undefined` as `null`, so that a forgotten `return` doesn't go unnoticed.
59
+ customRenderer: validator(
60
+ /** @type {(options: { filename: string }) => string | null | undefined} */ (() => undefined),
61
+ (input, keypath) => {
62
+ if (input === true) {
63
+ return () => null;
64
+ }
65
+
66
+ if (typeof input === 'string') {
67
+ // the module is imported by every component, so a relative path would resolve
68
+ // relative to each component rather than to wherever the option is set (which
69
+ // the compiler can't know)
70
+ if (/^\.\.?([/\\]|$)/.test(input)) {
71
+ throw_error(
72
+ `${keypath} cannot be a relative path, because it would be resolved relative to each component. Use a package name, an alias or an absolute path instead (received "${input}")`
73
+ );
74
+ }
75
+
76
+ return () => input;
77
+ }
78
+
79
+ if (typeof input === 'function') {
80
+ return (/** @type {{ filename: string }} */ options) => {
81
+ const result = input(options);
82
+
83
+ if (result !== null && typeof result !== 'string') {
84
+ throw_error(`${keypath} function must return a string or null`);
85
+ }
86
+
87
+ return result;
88
+ };
89
+ }
90
+
91
+ throw_error(`${keypath} should be true, a string or a function, if specified`);
92
+ }
93
+ )
48
94
  })
49
95
  };
50
96
 
@@ -92,3 +92,10 @@ export const ELEMENT_NODE = 1;
92
92
  export const TEXT_NODE = 3;
93
93
  export const COMMENT_NODE = 8;
94
94
  export const DOCUMENT_FRAGMENT_NODE = 11;
95
+
96
+ export const CUSTOM_RENDERER_NODE_TYPE_MAP = /** @type {const} */ ({
97
+ fragment: DOCUMENT_FRAGMENT_NODE,
98
+ element: ELEMENT_NODE,
99
+ text: TEXT_NODE,
100
+ comment: COMMENT_NODE
101
+ });