Skip to content

Commit 160c57f

Browse files
feat(compiler): emit the spread/ssrElement sources array for multi-source element spreads
Native elements with several spread sources compile to the runtimes' array form instead of a mergeProps() call, in DOM and SSR output, in both the Babel plugin and the native compiler: <div id="x" {...a} {...b}> → spread(el, [{ id: "x" }, a, b], skipChildren) (dom) → ssrElement("div", [{ id: "x" }, a, b], children, id) (ssr) The runtimes read the sources directly — later sources win per key, only the winning source is read — so there is no merge proxy to build and walk once. A reactive spread is a plain thunk called inside the tracking scope: no memo, so no hydration id on either side. That also retires the hydratable SSR `() => mergeProps(...)` wrapper, which existed only to order the merge memo's id after the element's key; a lone dynamic spread left behind once the server drops on*/prop:/children keys now passes through as the props thunk itself. A lone spread still passes straight through (#3105). Universal output is unchanged; its runtime spread has no array form yet. Requires @solidjs/web with the array forms (#3418, #3419). Fixtures regenerated for both compilers; the recorded cross-mode Babel/Oxc divergences changed only in context (mergeProps → array). Co-authored-by: Cursor <[email protected]>
1 parent 7f6332a commit 160c57f

42 files changed

Lines changed: 846 additions & 675 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@solidjs/babel-plugin": patch
3+
"@solidjs/compiler": patch
4+
---
5+
6+
Native elements with several spread sources compile to the runtimes' array form instead of a `mergeProps()` call, in DOM and SSR output: `<div id="x" {...a} {...b}>` becomes `spread(el, [{ id: "x" }, a, b], …)` on the client and `ssrElement("div", [{ id: "x" }, a, b], …)` on the server. The runtimes read the sources directly — later sources win per key, only the winning source is read — with no merge proxy to build and walk, and a reactive spread is a plain thunk called inside the tracking scope, so it mints no memo and consumes no hydration id on either side (the hydratable SSR `() => mergeProps(…)` wrapper is gone for the same reason). A lone spread still passes straight through (#3105). Requires `@solidjs/web` with the `spread`/`ssrElement` array forms (#3418, #3419).

‎packages/babel-plugin/src/dom/element.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,11 +1691,12 @@ function processSpreads(
16911691
// A lone spread — reactive included — passes straight through: spread()
16921692
// resolves a function source inside its own tracking scopes, and merging
16931693
// one source would mint a memo that consumes a hydration id the SSR fast
1694-
// path never allocates (#3105).
1695-
const props =
1696-
spreadArgs.length === 1
1697-
? spreadArgs[0]
1698-
: t.callExpression(registerImportMethod(path, "mergeProps"), spreadArgs);
1694+
// path never allocates (#3105). Several sources go as an ARRAY, not a
1695+
// mergeProps() call: spread() reads the sources directly (later wins per
1696+
// key, only the winner read) with no merge proxy to build and walk, and a
1697+
// reactive source is called inside the tracking scope with no memo — so
1698+
// no hydration id here either, matching the ssrElement array form.
1699+
const props = spreadArgs.length === 1 ? spreadArgs[0] : t.arrayExpression(spreadArgs);
16991700

17001701
return [
17011702
filteredAttributes,

‎packages/babel-plugin/src/ssr/element.ts‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,6 @@ function createElement(
991991
} else {
992992
props = [];
993993
let runningObject: Array<babelTypes.ObjectProperty | babelTypes.ObjectMethod> = [],
994-
dynamicSpread = false,
995994
hasChildren = path.node.children.length > 0;
996995

997996
attributes.forEach(attribute => {
@@ -1001,10 +1000,16 @@ function createElement(
10011000
props.push(t.objectExpression(runningObject));
10021001
runningObject = [];
10031002
}
1003+
// A dynamic spread defers behind a thunk: evaluated in argument
1004+
// position it would run before `ssrElement` allocates the element's
1005+
// own hydration id, while the client claims the element
1006+
// (getNextElement) before applying the spread — shifting the
1007+
// element's id by one and leaving it unclaimed. `ssrElement` calls
1008+
// function sources after allocating the key, matching that order.
10041009
props.push(
10051010
isDynamic(attribute.get("argument"), {
10061011
checkMember: true
1007-
}) && (dynamicSpread = true)
1012+
})
10081013
? inlineCallExpression(node.argument)
10091014
: node.argument
10101015
);
@@ -1043,21 +1048,13 @@ function createElement(
10431048

10441049
if (runningObject.length || !props.length) props.push(t.objectExpression(runningObject));
10451050

1046-
if (props.length > 1 || dynamicSpread) {
1047-
let merged: babelTypes.Expression = t.callExpression(
1048-
registerImportMethod(path, "mergeProps"),
1049-
props
1050-
);
1051-
// Defer the merge behind a thunk when hydratable: `mergeProps` with a
1052-
// function source creates a memo, which consumes a hydration child id.
1053-
// Evaluated in argument position it would run before `ssrElement`
1054-
// allocates the element's own id, while the client claims the element
1055-
// (getNextElement) before applying the spread — shifting the element's
1056-
// id by one and leaving it unclaimed. `ssrElement` resolves function
1057-
// props after allocating the hydration key, matching the client order.
1058-
if (hydratable) merged = t.arrowFunctionExpression([], merged);
1059-
props = [merged];
1060-
}
1051+
// Several sources go as an ARRAY, not a mergeProps() call: ssrElement
1052+
// serializes straight from the sources (later wins per key, only the
1053+
// winner read) with no merged object to build and walk once, and a
1054+
// function source is a plain thunk called after the key — no memo, so no
1055+
// hydration id, matching the client spread() array form. A single
1056+
// source, thunk included, passes through as the props argument itself.
1057+
if (props.length > 1) props = [t.arrayExpression(props)];
10611058
}
10621059

10631060
const exprs = [

‎packages/babel-plugin/test/__dom_compatible_fixtures__/attributeExpressions/output.js‎

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { className as _$className } from "r-dom";
1414
import { ref as _$ref } from "r-dom";
1515
import { claimElement as _$claimElement } from "r-dom";
1616
import { spread as _$spread } from "r-dom";
17-
import { mergeProps as _$mergeProps } from "r-dom";
1817
var _tmpl$ = /*#__PURE__*/ _$template(`<div><h1><a href="/">Welcome</a></h1></div>`),
1918
_tmpl$2 = /*#__PURE__*/ _$template(`<div><div></div><div> </div><div></div></div>`),
2019
_tmpl$3 = /*#__PURE__*/ _$template(`<div foo></div>`),
@@ -92,7 +91,7 @@ var _el$ = _tmpl$(),
9291
_el$3 = _el$2.firstChild;
9392
_$spread(
9493
_el$,
95-
_$mergeProps(
94+
[
9695
{
9796
id: "main"
9897
},
@@ -105,12 +104,12 @@ _$spread(
105104
color
106105
}
107106
}
108-
),
107+
],
109108
true
110109
);
111110
_$spread(
112111
_el$2,
113-
_$mergeProps(
112+
[
114113
{
115114
id: "my-h1"
116115
},
@@ -137,7 +136,7 @@ _$spread(
137136
];
138137
}
139138
}
140-
),
139+
],
141140
true
142141
);
143142
var _ref$ = link;
@@ -307,9 +306,12 @@ var _el$29 = _tmpl$14();
307306
_$claimElement(_el$29);
308307
_$spread(
309308
_el$29,
310-
_$mergeProps(props, {
311-
something: true
312-
}),
309+
[
310+
props,
311+
{
312+
something: true
313+
}
314+
],
313315
false
314316
);
315317
const template24 = _el$29;
@@ -319,29 +321,32 @@ _$insert(_el$30, () => props.children, _el$31);
319321
_$claimElement(_el$31);
320322
_$spread(
321323
_el$31,
322-
_$mergeProps(props, {
323-
something: true
324-
}),
324+
[
325+
props,
326+
{
327+
something: true
328+
}
329+
],
325330
false
326331
);
327332
const template25 = _el$30;
328333
var _el$32 = _tmpl$16();
329334
_$spread(
330335
_el$32,
331-
_$mergeProps(
336+
[
332337
{
333338
start: "Hi",
334339
middle: middle
335340
},
336341
spread
337-
),
342+
],
338343
true
339344
);
340345
const template26 = _el$32;
341346
var _el$33 = _tmpl$16();
342347
_$spread(
343348
_el$33,
344-
_$mergeProps(
349+
[
345350
{
346351
start: "Hi"
347352
},
@@ -350,7 +355,7 @@ _$spread(
350355
middle: middle
351356
},
352357
second
353-
),
358+
],
354359
true
355360
);
356361
const template27 = _el$33;
@@ -595,19 +600,22 @@ const template81 = _el$96;
595600
var _el$97 = _tmpl$4();
596601
_$spread(
597602
_el$97,
598-
_$mergeProps(propsSpread, {
599-
get ["data-dynamic"]() {
600-
return color();
601-
},
602-
"data-static": /* @static */ color()
603-
}),
603+
[
604+
propsSpread,
605+
{
606+
get ["data-dynamic"]() {
607+
return color();
608+
},
609+
"data-static": /* @static */ color()
610+
}
611+
],
604612
false
605613
);
606614
const template82 = _el$97;
607615
var _el$98 = _tmpl$4();
608616
_$spread(
609617
_el$98,
610-
_$mergeProps(
618+
[
611619
{
612620
...propsSpread
613621
},
@@ -617,14 +625,14 @@ _$spread(
617625
},
618626
"data-static": /* @static */ color()
619627
}
620-
),
628+
],
621629
false
622630
);
623631
const template83 = _el$98;
624632
var _el$99 = _tmpl$4();
625633
_$spread(
626634
_el$99,
627-
_$mergeProps(
635+
[
628636
{
629637
...propsSpread1
630638
},
@@ -638,7 +646,7 @@ _$spread(
638646
},
639647
"data-static": /* @static */ color()
640648
}
641-
),
649+
],
642650
false
643651
);
644652
const template84 = _el$99;
@@ -815,7 +823,7 @@ function MyVideo() {
815823
var _el$134 = _tmpl$53();
816824
_$spread(
817825
_el$134,
818-
_$mergeProps(
826+
[
819827
{
820828
get value() {
821829
return get();
@@ -824,7 +832,7 @@ _$spread(
824832
{
825833
style: "color:red"
826834
}
827-
),
835+
],
828836
false
829837
);
830838
const template97 = _el$134;

0 commit comments

Comments
 (0)