@sigil-dev/compiler 0.9.6 → 0.9.10
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.
- package/README.md +162 -162
- package/package.json +1 -1
- package/src/babel/index.ts +618 -608
- package/src/babel/jsx/anchor-mount.ts +408 -408
- package/src/babel/jsx/children.ts +108 -108
- package/src/babel/jsx/element.ts +865 -876
- package/src/babel/jsx/fragment.ts +259 -259
- package/src/babel/jsx/ssr.ts +62 -0
- package/test/hydration.test.ts +276 -276
- package/test/jsx.test.ts +10 -5
- package/test/scoped-css.test.ts +2 -2
- package/test/ssr-boolean-attrs.test.ts +56 -0
- package/test/ssr-raw-text.test.ts +68 -0
- package/test/style-object.test.ts +13 -13
|
@@ -1,259 +1,259 @@
|
|
|
1
|
-
import { types as t } from "@babel/core";
|
|
2
|
-
import { buildAnchorMount } from "./anchor-mount";
|
|
3
|
-
import { processElement } from "./element";
|
|
4
|
-
import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
|
|
5
|
-
import { buildTextNode } from "./text-node";
|
|
6
|
-
import { containsSignal, isPrimitive } from "./utils";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Process a JSX fragment (<>...</>).
|
|
10
|
-
* In DOM mode: creates a DocumentFragment and appends all children to it.
|
|
11
|
-
* In hydrate mode (inside element): children claim from parent's node pool; no fragment created.
|
|
12
|
-
* In hydrate mode (top-level, no parentVar): creates a DocumentFragment so the IIFE returns
|
|
13
|
-
* a valid node for SPA navigation (slot.replaceChildren). SSR hydration ignores the return value.
|
|
14
|
-
*/
|
|
15
|
-
export function processFragment(
|
|
16
|
-
node: t.JSXFragment,
|
|
17
|
-
statements: t.Statement[],
|
|
18
|
-
genId: () => string,
|
|
19
|
-
signals: Set<string>,
|
|
20
|
-
hash?: string,
|
|
21
|
-
hydrate?: boolean,
|
|
22
|
-
nodesVar?: string,
|
|
23
|
-
parentVar?: string,
|
|
24
|
-
): string {
|
|
25
|
-
const varName = genId();
|
|
26
|
-
const currentNodesVar = nodesVar ?? "__nodes";
|
|
27
|
-
|
|
28
|
-
// Where to append children in DOM / SPA mode:
|
|
29
|
-
// DOM mode → varName (the DocumentFragment)
|
|
30
|
-
// hydrate inside elem → parentVar (the real parent element)
|
|
31
|
-
// hydrate top-level → varName (a DocumentFragment we create below)
|
|
32
|
-
const effectiveParent = hydrate ? (parentVar ?? varName) : varName;
|
|
33
|
-
|
|
34
|
-
// Create a DocumentFragment when:
|
|
35
|
-
// - DOM mode: always (it IS the container)
|
|
36
|
-
// - Hydrate top-level (no parentVar): needed so the IIFE can return a real node for SPA nav
|
|
37
|
-
// (SSR hydration ignores the return value, so an empty fragment is harmless)
|
|
38
|
-
if (!hydrate || !parentVar) {
|
|
39
|
-
statements.push(
|
|
40
|
-
t.variableDeclaration("const", [
|
|
41
|
-
t.variableDeclarator(
|
|
42
|
-
t.identifier(varName),
|
|
43
|
-
t.callExpression(
|
|
44
|
-
t.memberExpression(
|
|
45
|
-
t.identifier("document"),
|
|
46
|
-
t.identifier("createDocumentFragment"),
|
|
47
|
-
),
|
|
48
|
-
[],
|
|
49
|
-
),
|
|
50
|
-
),
|
|
51
|
-
]),
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Process each child and append to fragment (or claim in hydrate mode)
|
|
56
|
-
for (const child of node.children) {
|
|
57
|
-
if (t.isJSXElement(child)) {
|
|
58
|
-
const childVar = processElement(
|
|
59
|
-
child,
|
|
60
|
-
statements,
|
|
61
|
-
genId,
|
|
62
|
-
signals,
|
|
63
|
-
hash,
|
|
64
|
-
hydrate,
|
|
65
|
-
currentNodesVar,
|
|
66
|
-
hydrate ? effectiveParent : undefined,
|
|
67
|
-
);
|
|
68
|
-
if (!hydrate) {
|
|
69
|
-
statements.push(
|
|
70
|
-
t.expressionStatement(
|
|
71
|
-
t.callExpression(
|
|
72
|
-
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
73
|
-
[t.identifier(childVar)],
|
|
74
|
-
),
|
|
75
|
-
),
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
} else if (t.isJSXText(child)) {
|
|
79
|
-
const text = child.value.trim();
|
|
80
|
-
if (!text) continue;
|
|
81
|
-
if (hydrate) {
|
|
82
|
-
// SSR hydration: text already in DOM from SSR, do nothing.
|
|
83
|
-
// SPA navigation: pool is empty, create text node and append to effectiveParent.
|
|
84
|
-
const poolLen = t.memberExpression(
|
|
85
|
-
t.identifier(currentNodesVar),
|
|
86
|
-
t.identifier("length"),
|
|
87
|
-
);
|
|
88
|
-
statements.push(
|
|
89
|
-
t.ifStatement(
|
|
90
|
-
t.binaryExpression("===", poolLen, t.numericLiteral(0)),
|
|
91
|
-
t.expressionStatement(
|
|
92
|
-
t.callExpression(
|
|
93
|
-
t.memberExpression(
|
|
94
|
-
t.identifier(effectiveParent),
|
|
95
|
-
t.identifier("append"),
|
|
96
|
-
),
|
|
97
|
-
[
|
|
98
|
-
t.callExpression(
|
|
99
|
-
t.memberExpression(
|
|
100
|
-
t.identifier("document"),
|
|
101
|
-
t.identifier("createTextNode"),
|
|
102
|
-
),
|
|
103
|
-
[t.stringLiteral(text)],
|
|
104
|
-
),
|
|
105
|
-
],
|
|
106
|
-
),
|
|
107
|
-
),
|
|
108
|
-
),
|
|
109
|
-
);
|
|
110
|
-
} else {
|
|
111
|
-
statements.push(
|
|
112
|
-
t.expressionStatement(
|
|
113
|
-
t.callExpression(
|
|
114
|
-
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
115
|
-
[
|
|
116
|
-
t.callExpression(
|
|
117
|
-
t.memberExpression(
|
|
118
|
-
t.identifier("document"),
|
|
119
|
-
t.identifier("createTextNode"),
|
|
120
|
-
),
|
|
121
|
-
[t.stringLiteral(text)],
|
|
122
|
-
),
|
|
123
|
-
],
|
|
124
|
-
),
|
|
125
|
-
),
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
} else if (t.isJSXExpressionContainer(child)) {
|
|
129
|
-
if (t.isJSXEmptyExpression(child.expression)) continue;
|
|
130
|
-
const expr = child.expression as t.Expression;
|
|
131
|
-
if (t.isJSXElement(expr)) {
|
|
132
|
-
const childVar = processElement(
|
|
133
|
-
expr,
|
|
134
|
-
statements,
|
|
135
|
-
genId,
|
|
136
|
-
signals,
|
|
137
|
-
hash,
|
|
138
|
-
hydrate,
|
|
139
|
-
currentNodesVar,
|
|
140
|
-
hydrate ? effectiveParent : undefined,
|
|
141
|
-
);
|
|
142
|
-
if (!hydrate) {
|
|
143
|
-
statements.push(
|
|
144
|
-
t.expressionStatement(
|
|
145
|
-
t.callExpression(
|
|
146
|
-
t.memberExpression(
|
|
147
|
-
t.identifier(varName),
|
|
148
|
-
t.identifier("append"),
|
|
149
|
-
),
|
|
150
|
-
[t.identifier(childVar)],
|
|
151
|
-
),
|
|
152
|
-
),
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
} else if (t.isJSXFragment(expr)) {
|
|
156
|
-
const childVar = processFragment(
|
|
157
|
-
expr,
|
|
158
|
-
statements,
|
|
159
|
-
genId,
|
|
160
|
-
signals,
|
|
161
|
-
hash,
|
|
162
|
-
hydrate,
|
|
163
|
-
currentNodesVar,
|
|
164
|
-
hydrate ? effectiveParent : undefined,
|
|
165
|
-
);
|
|
166
|
-
if (!hydrate) {
|
|
167
|
-
statements.push(
|
|
168
|
-
t.expressionStatement(
|
|
169
|
-
t.callExpression(
|
|
170
|
-
t.memberExpression(
|
|
171
|
-
t.identifier(varName),
|
|
172
|
-
t.identifier("append"),
|
|
173
|
-
),
|
|
174
|
-
[t.identifier(childVar)],
|
|
175
|
-
),
|
|
176
|
-
),
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
} else if (containsSignal(expr, signals)) {
|
|
180
|
-
if (isPrimitive(expr)) {
|
|
181
|
-
buildTextNode(
|
|
182
|
-
varName,
|
|
183
|
-
expr,
|
|
184
|
-
statements,
|
|
185
|
-
genId,
|
|
186
|
-
hydrate,
|
|
187
|
-
currentNodesVar,
|
|
188
|
-
hydrate ? effectiveParent : undefined,
|
|
189
|
-
);
|
|
190
|
-
} else {
|
|
191
|
-
// Check for keyed list pattern
|
|
192
|
-
const keyed = findKeyedMapExpr(expr, signals);
|
|
193
|
-
if (keyed) {
|
|
194
|
-
buildKeyedList(
|
|
195
|
-
varName,
|
|
196
|
-
keyed,
|
|
197
|
-
statements,
|
|
198
|
-
genId,
|
|
199
|
-
signals,
|
|
200
|
-
processElement,
|
|
201
|
-
hash,
|
|
202
|
-
hydrate,
|
|
203
|
-
currentNodesVar,
|
|
204
|
-
hydrate ? effectiveParent : undefined,
|
|
205
|
-
);
|
|
206
|
-
} else {
|
|
207
|
-
buildAnchorMount(
|
|
208
|
-
varName,
|
|
209
|
-
expr,
|
|
210
|
-
statements,
|
|
211
|
-
genId,
|
|
212
|
-
hydrate,
|
|
213
|
-
currentNodesVar,
|
|
214
|
-
hydrate ? effectiveParent : undefined,
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
} else {
|
|
219
|
-
if (hydrate) {
|
|
220
|
-
// Non-reactive expression in hydrate fragment: consume the
|
|
221
|
-
// <!--g-->…<!--/g--> run and hydrate its content. Kept lazy so
|
|
222
|
-
// the value claims from the slot, not the enclosing pool;
|
|
223
|
-
// empty pool (SPA nav) inserts fresh.
|
|
224
|
-
statements.push(
|
|
225
|
-
t.expressionStatement(
|
|
226
|
-
t.callExpression(t.identifier("hydrateSlot"), [
|
|
227
|
-
t.identifier(currentNodesVar),
|
|
228
|
-
t.identifier(effectiveParent),
|
|
229
|
-
t.arrowFunctionExpression([], expr),
|
|
230
|
-
]),
|
|
231
|
-
),
|
|
232
|
-
);
|
|
233
|
-
} else {
|
|
234
|
-
statements.push(
|
|
235
|
-
t.expressionStatement(
|
|
236
|
-
t.callExpression(
|
|
237
|
-
t.memberExpression(
|
|
238
|
-
t.identifier(varName),
|
|
239
|
-
t.identifier("append"),
|
|
240
|
-
),
|
|
241
|
-
[
|
|
242
|
-
t.callExpression(
|
|
243
|
-
t.memberExpression(
|
|
244
|
-
t.identifier("document"),
|
|
245
|
-
t.identifier("createTextNode"),
|
|
246
|
-
),
|
|
247
|
-
[expr],
|
|
248
|
-
),
|
|
249
|
-
],
|
|
250
|
-
),
|
|
251
|
-
),
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return varName;
|
|
259
|
-
}
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
import { buildAnchorMount } from "./anchor-mount";
|
|
3
|
+
import { processElement } from "./element";
|
|
4
|
+
import { buildKeyedList, findKeyedMapExpr } from "./keyed-list";
|
|
5
|
+
import { buildTextNode } from "./text-node";
|
|
6
|
+
import { containsSignal, isPrimitive } from "./utils";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Process a JSX fragment (<>...</>).
|
|
10
|
+
* In DOM mode: creates a DocumentFragment and appends all children to it.
|
|
11
|
+
* In hydrate mode (inside element): children claim from parent's node pool; no fragment created.
|
|
12
|
+
* In hydrate mode (top-level, no parentVar): creates a DocumentFragment so the IIFE returns
|
|
13
|
+
* a valid node for SPA navigation (slot.replaceChildren). SSR hydration ignores the return value.
|
|
14
|
+
*/
|
|
15
|
+
export function processFragment(
|
|
16
|
+
node: t.JSXFragment,
|
|
17
|
+
statements: t.Statement[],
|
|
18
|
+
genId: () => string,
|
|
19
|
+
signals: Set<string>,
|
|
20
|
+
hash?: string,
|
|
21
|
+
hydrate?: boolean,
|
|
22
|
+
nodesVar?: string,
|
|
23
|
+
parentVar?: string,
|
|
24
|
+
): string {
|
|
25
|
+
const varName = genId();
|
|
26
|
+
const currentNodesVar = nodesVar ?? "__nodes";
|
|
27
|
+
|
|
28
|
+
// Where to append children in DOM / SPA mode:
|
|
29
|
+
// DOM mode → varName (the DocumentFragment)
|
|
30
|
+
// hydrate inside elem → parentVar (the real parent element)
|
|
31
|
+
// hydrate top-level → varName (a DocumentFragment we create below)
|
|
32
|
+
const effectiveParent = hydrate ? (parentVar ?? varName) : varName;
|
|
33
|
+
|
|
34
|
+
// Create a DocumentFragment when:
|
|
35
|
+
// - DOM mode: always (it IS the container)
|
|
36
|
+
// - Hydrate top-level (no parentVar): needed so the IIFE can return a real node for SPA nav
|
|
37
|
+
// (SSR hydration ignores the return value, so an empty fragment is harmless)
|
|
38
|
+
if (!hydrate || !parentVar) {
|
|
39
|
+
statements.push(
|
|
40
|
+
t.variableDeclaration("const", [
|
|
41
|
+
t.variableDeclarator(
|
|
42
|
+
t.identifier(varName),
|
|
43
|
+
t.callExpression(
|
|
44
|
+
t.memberExpression(
|
|
45
|
+
t.identifier("document"),
|
|
46
|
+
t.identifier("createDocumentFragment"),
|
|
47
|
+
),
|
|
48
|
+
[],
|
|
49
|
+
),
|
|
50
|
+
),
|
|
51
|
+
]),
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Process each child and append to fragment (or claim in hydrate mode)
|
|
56
|
+
for (const child of node.children) {
|
|
57
|
+
if (t.isJSXElement(child)) {
|
|
58
|
+
const childVar = processElement(
|
|
59
|
+
child,
|
|
60
|
+
statements,
|
|
61
|
+
genId,
|
|
62
|
+
signals,
|
|
63
|
+
hash,
|
|
64
|
+
hydrate,
|
|
65
|
+
currentNodesVar,
|
|
66
|
+
hydrate ? effectiveParent : undefined,
|
|
67
|
+
);
|
|
68
|
+
if (!hydrate) {
|
|
69
|
+
statements.push(
|
|
70
|
+
t.expressionStatement(
|
|
71
|
+
t.callExpression(
|
|
72
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
73
|
+
[t.identifier(childVar)],
|
|
74
|
+
),
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
} else if (t.isJSXText(child)) {
|
|
79
|
+
const text = child.value.trim();
|
|
80
|
+
if (!text) continue;
|
|
81
|
+
if (hydrate) {
|
|
82
|
+
// SSR hydration: text already in DOM from SSR, do nothing.
|
|
83
|
+
// SPA navigation: pool is empty, create text node and append to effectiveParent.
|
|
84
|
+
const poolLen = t.memberExpression(
|
|
85
|
+
t.identifier(currentNodesVar),
|
|
86
|
+
t.identifier("length"),
|
|
87
|
+
);
|
|
88
|
+
statements.push(
|
|
89
|
+
t.ifStatement(
|
|
90
|
+
t.binaryExpression("===", poolLen, t.numericLiteral(0)),
|
|
91
|
+
t.expressionStatement(
|
|
92
|
+
t.callExpression(
|
|
93
|
+
t.memberExpression(
|
|
94
|
+
t.identifier(effectiveParent),
|
|
95
|
+
t.identifier("append"),
|
|
96
|
+
),
|
|
97
|
+
[
|
|
98
|
+
t.callExpression(
|
|
99
|
+
t.memberExpression(
|
|
100
|
+
t.identifier("document"),
|
|
101
|
+
t.identifier("createTextNode"),
|
|
102
|
+
),
|
|
103
|
+
[t.stringLiteral(text)],
|
|
104
|
+
),
|
|
105
|
+
],
|
|
106
|
+
),
|
|
107
|
+
),
|
|
108
|
+
),
|
|
109
|
+
);
|
|
110
|
+
} else {
|
|
111
|
+
statements.push(
|
|
112
|
+
t.expressionStatement(
|
|
113
|
+
t.callExpression(
|
|
114
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
115
|
+
[
|
|
116
|
+
t.callExpression(
|
|
117
|
+
t.memberExpression(
|
|
118
|
+
t.identifier("document"),
|
|
119
|
+
t.identifier("createTextNode"),
|
|
120
|
+
),
|
|
121
|
+
[t.stringLiteral(text)],
|
|
122
|
+
),
|
|
123
|
+
],
|
|
124
|
+
),
|
|
125
|
+
),
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
} else if (t.isJSXExpressionContainer(child)) {
|
|
129
|
+
if (t.isJSXEmptyExpression(child.expression)) continue;
|
|
130
|
+
const expr = child.expression as t.Expression;
|
|
131
|
+
if (t.isJSXElement(expr)) {
|
|
132
|
+
const childVar = processElement(
|
|
133
|
+
expr,
|
|
134
|
+
statements,
|
|
135
|
+
genId,
|
|
136
|
+
signals,
|
|
137
|
+
hash,
|
|
138
|
+
hydrate,
|
|
139
|
+
currentNodesVar,
|
|
140
|
+
hydrate ? effectiveParent : undefined,
|
|
141
|
+
);
|
|
142
|
+
if (!hydrate) {
|
|
143
|
+
statements.push(
|
|
144
|
+
t.expressionStatement(
|
|
145
|
+
t.callExpression(
|
|
146
|
+
t.memberExpression(
|
|
147
|
+
t.identifier(varName),
|
|
148
|
+
t.identifier("append"),
|
|
149
|
+
),
|
|
150
|
+
[t.identifier(childVar)],
|
|
151
|
+
),
|
|
152
|
+
),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
} else if (t.isJSXFragment(expr)) {
|
|
156
|
+
const childVar = processFragment(
|
|
157
|
+
expr,
|
|
158
|
+
statements,
|
|
159
|
+
genId,
|
|
160
|
+
signals,
|
|
161
|
+
hash,
|
|
162
|
+
hydrate,
|
|
163
|
+
currentNodesVar,
|
|
164
|
+
hydrate ? effectiveParent : undefined,
|
|
165
|
+
);
|
|
166
|
+
if (!hydrate) {
|
|
167
|
+
statements.push(
|
|
168
|
+
t.expressionStatement(
|
|
169
|
+
t.callExpression(
|
|
170
|
+
t.memberExpression(
|
|
171
|
+
t.identifier(varName),
|
|
172
|
+
t.identifier("append"),
|
|
173
|
+
),
|
|
174
|
+
[t.identifier(childVar)],
|
|
175
|
+
),
|
|
176
|
+
),
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
} else if (containsSignal(expr, signals)) {
|
|
180
|
+
if (isPrimitive(expr)) {
|
|
181
|
+
buildTextNode(
|
|
182
|
+
varName,
|
|
183
|
+
expr,
|
|
184
|
+
statements,
|
|
185
|
+
genId,
|
|
186
|
+
hydrate,
|
|
187
|
+
currentNodesVar,
|
|
188
|
+
hydrate ? effectiveParent : undefined,
|
|
189
|
+
);
|
|
190
|
+
} else {
|
|
191
|
+
// Check for keyed list pattern
|
|
192
|
+
const keyed = findKeyedMapExpr(expr, signals);
|
|
193
|
+
if (keyed) {
|
|
194
|
+
buildKeyedList(
|
|
195
|
+
varName,
|
|
196
|
+
keyed,
|
|
197
|
+
statements,
|
|
198
|
+
genId,
|
|
199
|
+
signals,
|
|
200
|
+
processElement,
|
|
201
|
+
hash,
|
|
202
|
+
hydrate,
|
|
203
|
+
currentNodesVar,
|
|
204
|
+
hydrate ? effectiveParent : undefined,
|
|
205
|
+
);
|
|
206
|
+
} else {
|
|
207
|
+
buildAnchorMount(
|
|
208
|
+
varName,
|
|
209
|
+
expr,
|
|
210
|
+
statements,
|
|
211
|
+
genId,
|
|
212
|
+
hydrate,
|
|
213
|
+
currentNodesVar,
|
|
214
|
+
hydrate ? effectiveParent : undefined,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
if (hydrate) {
|
|
220
|
+
// Non-reactive expression in hydrate fragment: consume the
|
|
221
|
+
// <!--g-->…<!--/g--> run and hydrate its content. Kept lazy so
|
|
222
|
+
// the value claims from the slot, not the enclosing pool;
|
|
223
|
+
// empty pool (SPA nav) inserts fresh.
|
|
224
|
+
statements.push(
|
|
225
|
+
t.expressionStatement(
|
|
226
|
+
t.callExpression(t.identifier("hydrateSlot"), [
|
|
227
|
+
t.identifier(currentNodesVar),
|
|
228
|
+
t.identifier(effectiveParent),
|
|
229
|
+
t.arrowFunctionExpression([], expr),
|
|
230
|
+
]),
|
|
231
|
+
),
|
|
232
|
+
);
|
|
233
|
+
} else {
|
|
234
|
+
statements.push(
|
|
235
|
+
t.expressionStatement(
|
|
236
|
+
t.callExpression(
|
|
237
|
+
t.memberExpression(
|
|
238
|
+
t.identifier(varName),
|
|
239
|
+
t.identifier("append"),
|
|
240
|
+
),
|
|
241
|
+
[
|
|
242
|
+
t.callExpression(
|
|
243
|
+
t.memberExpression(
|
|
244
|
+
t.identifier("document"),
|
|
245
|
+
t.identifier("createTextNode"),
|
|
246
|
+
),
|
|
247
|
+
[expr],
|
|
248
|
+
),
|
|
249
|
+
],
|
|
250
|
+
),
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return varName;
|
|
259
|
+
}
|
package/src/babel/jsx/ssr.ts
CHANGED
|
@@ -2,6 +2,38 @@ import { types as t } from "@babel/core";
|
|
|
2
2
|
import { isGlobalComponent } from "../handlers/dom/globals";
|
|
3
3
|
import { getTagName } from "../util/helpers";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Attributes that are on by presence alone: checked="" and even
|
|
7
|
+
* checked="false" both mean checked, so these can't go through the generic
|
|
8
|
+
* name="value" path.
|
|
9
|
+
*/
|
|
10
|
+
const BOOLEAN_ATTRS = new Set([
|
|
11
|
+
"allowfullscreen",
|
|
12
|
+
"async",
|
|
13
|
+
"autofocus",
|
|
14
|
+
"autoplay",
|
|
15
|
+
"checked",
|
|
16
|
+
"controls",
|
|
17
|
+
"default",
|
|
18
|
+
"defer",
|
|
19
|
+
"disabled",
|
|
20
|
+
"formnovalidate",
|
|
21
|
+
"hidden",
|
|
22
|
+
"inert",
|
|
23
|
+
"ismap",
|
|
24
|
+
"loop",
|
|
25
|
+
"multiple",
|
|
26
|
+
"muted",
|
|
27
|
+
"nomodule",
|
|
28
|
+
"novalidate",
|
|
29
|
+
"open",
|
|
30
|
+
"playsinline",
|
|
31
|
+
"readonly",
|
|
32
|
+
"required",
|
|
33
|
+
"reversed",
|
|
34
|
+
"selected",
|
|
35
|
+
]);
|
|
36
|
+
|
|
5
37
|
const VOID_ELEMENTS = new Set([
|
|
6
38
|
"area",
|
|
7
39
|
"base",
|
|
@@ -160,6 +192,19 @@ export function processElementSSR(
|
|
|
160
192
|
|
|
161
193
|
if (t.isStringLiteral(attr.value)) {
|
|
162
194
|
parts.push(` ${domAttr}="${attr.value.value}"`);
|
|
195
|
+
} else if (
|
|
196
|
+
t.isJSXExpressionContainer(attr.value) &&
|
|
197
|
+
BOOLEAN_ATTRS.has(domAttr.toLowerCase())
|
|
198
|
+
) {
|
|
199
|
+
// __b: true → bare name, false/null/undefined → omitted, anything
|
|
200
|
+
// else keeps its value (hidden="until-found"). The generic path
|
|
201
|
+
// below rendered checked={false} as checked="", i.e. checked.
|
|
202
|
+
parts.push(
|
|
203
|
+
t.callExpression(t.identifier("__b"), [
|
|
204
|
+
t.stringLiteral(domAttr),
|
|
205
|
+
attr.value.expression as t.Expression,
|
|
206
|
+
]),
|
|
207
|
+
);
|
|
163
208
|
} else if (t.isJSXExpressionContainer(attr.value)) {
|
|
164
209
|
const expr = attr.value.expression as t.Expression;
|
|
165
210
|
if (
|
|
@@ -208,8 +253,25 @@ export function processElementSSR(
|
|
|
208
253
|
return buildTemplate(parts);
|
|
209
254
|
}
|
|
210
255
|
|
|
256
|
+
// <script> holds raw text: HTML escaping would corrupt the code (&& →
|
|
257
|
+
// &&), and <!--g--> markers are just text inside it, not comments,
|
|
258
|
+
// so there is nothing for hydration to find. __r only guards against the
|
|
259
|
+
// code closing its own element. (<style> never gets here: scoped style
|
|
260
|
+
// blocks are stripped from the source before compiling.)
|
|
261
|
+
const rawText = tagName === "script";
|
|
262
|
+
|
|
211
263
|
// children
|
|
212
264
|
for (const child of node.children) {
|
|
265
|
+
if (rawText && t.isJSXExpressionContainer(child)) {
|
|
266
|
+
if (t.isJSXEmptyExpression(child.expression)) continue;
|
|
267
|
+
parts.push(
|
|
268
|
+
t.callExpression(t.identifier("__r"), [
|
|
269
|
+
child.expression as t.Expression,
|
|
270
|
+
t.stringLiteral(tagName),
|
|
271
|
+
]),
|
|
272
|
+
);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
213
275
|
if (t.isJSXText(child)) {
|
|
214
276
|
const text = child.value;
|
|
215
277
|
if (!text.trim()) continue;
|