Skip to content

Commit 66e0378

Browse files
authored
chore: remove $.space (#10945)
* chore: remove $.space * simplify space_frag * rename $.space_frag to $.text
1 parent 4fcedb2 commit 66e0378

File tree

4 files changed

+16
-38
lines changed

4 files changed

+16
-38
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,19 +1064,15 @@ function create_block(parent, name, nodes, context) {
10641064
trimmed.every((node) => node.type === 'Text' || node.type === 'ExpressionTag');
10651065

10661066
if (use_space_template) {
1067-
// special case — we can use `$.space_frag` instead of creating a unique template
1067+
// special case — we can use `$.text` instead of creating a unique template
10681068
const id = b.id(context.state.scope.generate('text'));
10691069

10701070
process_children(trimmed, () => id, false, {
10711071
...context,
10721072
state
10731073
});
10741074

1075-
body.push(
1076-
b.var(id, b.call('$.space_frag', b.id('$$anchor'))),
1077-
...state.before_init,
1078-
...state.init
1079-
);
1075+
body.push(b.var(id, b.call('$.text', b.id('$$anchor'))), ...state.before_init, ...state.init);
10801076
close = b.stmt(b.call('$.close', b.id('$$anchor'), id));
10811077
} else {
10821078
/** @type {(is_text: boolean) => import('estree').Expression} */
@@ -1408,7 +1404,7 @@ function process_children(nodes, expression, is_element, { visit, state }) {
14081404

14091405
state.template.push(' ');
14101406

1411-
const text_id = get_node_id(b.call('$.space', expression(true)), state, 'text');
1407+
const text_id = get_node_id(expression(true), state, 'text');
14121408

14131409
const update = b.stmt(
14141410
b.call(

packages/svelte/src/internal/client/dom/template.js

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -115,42 +115,24 @@ export function open_frag(template_element_fn, use_clone_node = true) {
115115
return open_template(true, use_clone_node, template_element_fn);
116116
}
117117

118-
const space_template = template(' ', false);
119118
const comment_template = template('<!>', true);
120119

121-
/**
122-
* @param {Text | Comment | Element | null} anchor
123-
*/
124-
/*#__NO_SIDE_EFFECTS__*/
125-
export function space_frag(anchor) {
126-
/** @type {Node | null} */
127-
var node = /** @type {any} */ (open(space_template));
128-
// if an {expression} is empty during SSR, there might be no
129-
// text node to hydrate (or an anchor comment is falsely detected instead)
130-
// — we must therefore create one
131-
if (hydrating && node?.nodeType !== 3) {
132-
node = empty();
133-
// @ts-ignore in this case the anchor should always be a comment,
134-
// if not something more fundamental is wrong and throwing here is better to bail out early
135-
anchor.before(node);
136-
}
137-
return node;
138-
}
139-
140120
/**
141121
* @param {Text | Comment | Element} anchor
142122
*/
143123
/*#__NO_SIDE_EFFECTS__*/
144-
export function space(anchor) {
145-
// if an {expression} is empty during SSR, there might be no
146-
// text node to hydrate (or an anchor comment is falsely detected instead)
147-
// — we must therefore create one
148-
if (hydrating && anchor.nodeType !== 3) {
149-
const node = empty();
150-
anchor.before(node);
151-
return node;
124+
export function text(anchor) {
125+
if (!hydrating) return empty();
126+
127+
var node = hydrate_nodes[0];
128+
129+
if (!node) {
130+
// if an {expression} is empty during SSR, `hydrate_nodes` will be empty.
131+
// we need to insert an empty text node
132+
anchor.before((node = empty()));
152133
}
153-
return anchor;
134+
135+
return node;
154136
}
155137

156138
/*#__NO_SIDE_EFFECTS__*/

packages/svelte/tests/snapshot/samples/each-string-template/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function Each_string_template($$anchor, $$props) {
1111
var node = $.first_child(fragment);
1212

1313
$.each_indexed(node, 1, () => ['foo', 'bar', 'baz'], ($$anchor, thing, $$index) => {
14-
var text = $.space_frag($$anchor);
14+
var text = $.text($$anchor);
1515

1616
$.render_effect(() => $.set_text(text, `${$.stringify($.unwrap(thing))}, `));
1717
return $.close($$anchor, text);

packages/svelte/tests/snapshot/samples/function-prop-no-getter/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function Function_prop_no_getter($$anchor, $$props) {
2121
onmouseup,
2222
onmouseenter: () => $.set(count, $.proxy(plusOne($.get(count)))),
2323
children: ($$anchor, $$slotProps) => {
24-
var text = $.space_frag($$anchor);
24+
var text = $.text($$anchor);
2525

2626
$.render_effect(() => $.set_text(text, `clicks: ${$.stringify($.get(count))}`));
2727
return $.close($$anchor, text);

0 commit comments

Comments
 (0)