@sigil-dev/compiler 0.10.2 → 0.10.3
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 +5 -4
- package/package.json +1 -1
- package/src/babel/index.ts +801 -801
- package/src/babel/jsx/conditional-return.ts +71 -71
- package/src/babel/jsx/text-node.ts +193 -193
- package/src/babel/jsx/textarea.ts +58 -58
- package/src/bun-plugin.ts +119 -119
- package/test/bun-plugin.test.ts +60 -60
- package/test/conditional-return.test.ts +82 -82
- package/test/hydration.test.ts +380 -380
- package/test/render-parity.test.ts +5 -5
|
@@ -1,71 +1,71 @@
|
|
|
1
|
-
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* A component that returns a conditional at the top,
|
|
5
|
-
* `return open ? <Panel /> : null`, becomes `return <>{open ? <Panel /> : null}</>`
|
|
6
|
-
* before any mode compiles it. A component body runs once, so a bare
|
|
7
|
-
* conditional was decided once and never again: `open` changing did nothing.
|
|
8
|
-
* Inside a fragment it's a dynamic child like any other, with its anchor and
|
|
9
|
-
* effect. Done in every mode, so the server's markers and hydration agree.
|
|
10
|
-
*
|
|
11
|
-
* Only components (a capitalized name, or the default export), only their own
|
|
12
|
-
* top-level returns (not a callback's inside them), and only a conditional
|
|
13
|
-
* (`?:`, `&&`, `||`, `??`) with JSX in it.
|
|
14
|
-
*/
|
|
15
|
-
export function wrapConditionalReturns(program: NodePath<t.Program>): boolean {
|
|
16
|
-
let changed = false;
|
|
17
|
-
program.traverse({
|
|
18
|
-
Function(fn) {
|
|
19
|
-
if (!isComponent(fn)) return;
|
|
20
|
-
const body = fn.get("body");
|
|
21
|
-
if (!Array.isArray(body) && body.isExpression()) {
|
|
22
|
-
if (wrap(body)) changed = true;
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
fn.traverse({
|
|
26
|
-
// a nested function's returns are its own business
|
|
27
|
-
Function(inner) {
|
|
28
|
-
inner.skip();
|
|
29
|
-
},
|
|
30
|
-
ReturnStatement(ret) {
|
|
31
|
-
const arg = ret.get("argument");
|
|
32
|
-
if (arg.node && wrap(arg as NodePath<t.Expression>)) changed = true;
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
return changed;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function wrap(path: NodePath<t.Expression>): boolean {
|
|
41
|
-
let node = path.node;
|
|
42
|
-
while (t.isParenthesizedExpression(node)) node = node.expression;
|
|
43
|
-
if (!(t.isConditionalExpression(node) || t.isLogicalExpression(node))) return false;
|
|
44
|
-
if (!hasJsx(node)) return false;
|
|
45
|
-
path.replaceWith(t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), [t.jsxExpressionContainer(node)]));
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** JSX somewhere in the expression, not counting inside functions it creates. */
|
|
50
|
-
function hasJsx(node: t.Node): boolean {
|
|
51
|
-
if (t.isJSXElement(node) || t.isJSXFragment(node)) return true;
|
|
52
|
-
if (t.isFunction(node)) return false;
|
|
53
|
-
for (const key of t.VISITOR_KEYS[node.type] ?? []) {
|
|
54
|
-
const v = (node as unknown as Record<string, unknown>)[key];
|
|
55
|
-
if (Array.isArray(v)) {
|
|
56
|
-
for (const c of v) if (c && typeof c === "object" && "type" in c && hasJsx(c as t.Node)) return true;
|
|
57
|
-
} else if (v && typeof v === "object" && "type" in v && hasJsx(v as t.Node)) return true;
|
|
58
|
-
}
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const capitalized = (name: string | undefined) => !!name && /^[A-Z]/.test(name);
|
|
63
|
-
|
|
64
|
-
function isComponent(fn: NodePath<t.Function>): boolean {
|
|
65
|
-
const node = fn.node;
|
|
66
|
-
if ((t.isFunctionDeclaration(node) || t.isFunctionExpression(node)) && capitalized(node.id?.name)) return true;
|
|
67
|
-
const parent = fn.parentPath;
|
|
68
|
-
if (parent?.isVariableDeclarator() && t.isIdentifier(parent.node.id)) return capitalized(parent.node.id.name);
|
|
69
|
-
if (parent?.isExportDefaultDeclaration()) return true;
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
1
|
+
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A component that returns a conditional at the top,
|
|
5
|
+
* `return open ? <Panel /> : null`, becomes `return <>{open ? <Panel /> : null}</>`
|
|
6
|
+
* before any mode compiles it. A component body runs once, so a bare
|
|
7
|
+
* conditional was decided once and never again: `open` changing did nothing.
|
|
8
|
+
* Inside a fragment it's a dynamic child like any other, with its anchor and
|
|
9
|
+
* effect. Done in every mode, so the server's markers and hydration agree.
|
|
10
|
+
*
|
|
11
|
+
* Only components (a capitalized name, or the default export), only their own
|
|
12
|
+
* top-level returns (not a callback's inside them), and only a conditional
|
|
13
|
+
* (`?:`, `&&`, `||`, `??`) with JSX in it.
|
|
14
|
+
*/
|
|
15
|
+
export function wrapConditionalReturns(program: NodePath<t.Program>): boolean {
|
|
16
|
+
let changed = false;
|
|
17
|
+
program.traverse({
|
|
18
|
+
Function(fn) {
|
|
19
|
+
if (!isComponent(fn)) return;
|
|
20
|
+
const body = fn.get("body");
|
|
21
|
+
if (!Array.isArray(body) && body.isExpression()) {
|
|
22
|
+
if (wrap(body)) changed = true;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
fn.traverse({
|
|
26
|
+
// a nested function's returns are its own business
|
|
27
|
+
Function(inner) {
|
|
28
|
+
inner.skip();
|
|
29
|
+
},
|
|
30
|
+
ReturnStatement(ret) {
|
|
31
|
+
const arg = ret.get("argument");
|
|
32
|
+
if (arg.node && wrap(arg as NodePath<t.Expression>)) changed = true;
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
return changed;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function wrap(path: NodePath<t.Expression>): boolean {
|
|
41
|
+
let node = path.node;
|
|
42
|
+
while (t.isParenthesizedExpression(node)) node = node.expression;
|
|
43
|
+
if (!(t.isConditionalExpression(node) || t.isLogicalExpression(node))) return false;
|
|
44
|
+
if (!hasJsx(node)) return false;
|
|
45
|
+
path.replaceWith(t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), [t.jsxExpressionContainer(node)]));
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** JSX somewhere in the expression, not counting inside functions it creates. */
|
|
50
|
+
function hasJsx(node: t.Node): boolean {
|
|
51
|
+
if (t.isJSXElement(node) || t.isJSXFragment(node)) return true;
|
|
52
|
+
if (t.isFunction(node)) return false;
|
|
53
|
+
for (const key of t.VISITOR_KEYS[node.type] ?? []) {
|
|
54
|
+
const v = (node as unknown as Record<string, unknown>)[key];
|
|
55
|
+
if (Array.isArray(v)) {
|
|
56
|
+
for (const c of v) if (c && typeof c === "object" && "type" in c && hasJsx(c as t.Node)) return true;
|
|
57
|
+
} else if (v && typeof v === "object" && "type" in v && hasJsx(v as t.Node)) return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const capitalized = (name: string | undefined) => !!name && /^[A-Z]/.test(name);
|
|
63
|
+
|
|
64
|
+
function isComponent(fn: NodePath<t.Function>): boolean {
|
|
65
|
+
const node = fn.node;
|
|
66
|
+
if ((t.isFunctionDeclaration(node) || t.isFunctionExpression(node)) && capitalized(node.id?.name)) return true;
|
|
67
|
+
const parent = fn.parentPath;
|
|
68
|
+
if (parent?.isVariableDeclarator() && t.isIdentifier(parent.node.id)) return capitalized(parent.node.id.name);
|
|
69
|
+
if (parent?.isExportDefaultDeclaration()) return true;
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
@@ -1,193 +1,193 @@
|
|
|
1
|
-
import { types as t } from "@babel/core";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Create a reactive text node: `createEffect(() => node.textContent = String(expr))`
|
|
5
|
-
* In hydrate mode:
|
|
6
|
-
* - Claims <!--g--> anchor via claimComment (fixes adjacent-text bug vs DOM walk)
|
|
7
|
-
* - Consumes <!--/g--> closing delimiter
|
|
8
|
-
* - Gets nextSibling as the text node; if it's not a Text (SPA navigation), creates one
|
|
9
|
-
* - Effect updates that text node's content
|
|
10
|
-
* In dom mode: creates a new text node and appends it.
|
|
11
|
-
*/
|
|
12
|
-
export function buildTextNode(
|
|
13
|
-
varName: string,
|
|
14
|
-
expr: t.Expression,
|
|
15
|
-
statements: t.Statement[],
|
|
16
|
-
genId: () => string,
|
|
17
|
-
hydrate?: boolean,
|
|
18
|
-
nodesVar?: string,
|
|
19
|
-
parentVar?: string,
|
|
20
|
-
): void {
|
|
21
|
-
if (hydrate) {
|
|
22
|
-
const nodesId = t.identifier(nodesVar ?? "__nodes");
|
|
23
|
-
const parentIdent = parentVar ? t.identifier(parentVar) : undefined;
|
|
24
|
-
|
|
25
|
-
// const _anchor = claimComment(nodes, "g", parent) — claim/create <!--g-->
|
|
26
|
-
const anchorVar = genId();
|
|
27
|
-
const claimGArgs: t.Expression[] = [nodesId, t.stringLiteral("g")];
|
|
28
|
-
if (parentIdent) claimGArgs.push(parentIdent);
|
|
29
|
-
statements.push(
|
|
30
|
-
t.variableDeclaration("const", [
|
|
31
|
-
t.variableDeclarator(
|
|
32
|
-
t.identifier(anchorVar),
|
|
33
|
-
t.callExpression(t.identifier("claimComment"), claimGArgs),
|
|
34
|
-
),
|
|
35
|
-
]),
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
// claimComment(nodes, "/g", parent) — consume <!--/g--> from pool
|
|
39
|
-
const claimSlashGArgs: t.Expression[] = [nodesId, t.stringLiteral("/g")];
|
|
40
|
-
if (parentIdent) claimSlashGArgs.push(parentIdent);
|
|
41
|
-
statements.push(
|
|
42
|
-
t.expressionStatement(
|
|
43
|
-
t.callExpression(t.identifier("claimComment"), claimSlashGArgs),
|
|
44
|
-
),
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
// let _t = _anchor?.nextSibling
|
|
48
|
-
// In SSR hydration, this is the text node between the comment delimiters.
|
|
49
|
-
// In SPA navigation, this is the closing <!--/g--> (not a Text), so we create one.
|
|
50
|
-
const textVar = genId();
|
|
51
|
-
statements.push(
|
|
52
|
-
t.variableDeclaration("let", [
|
|
53
|
-
t.variableDeclarator(
|
|
54
|
-
t.identifier(textVar),
|
|
55
|
-
t.optionalMemberExpression(
|
|
56
|
-
t.identifier(anchorVar),
|
|
57
|
-
t.identifier("nextSibling"),
|
|
58
|
-
false,
|
|
59
|
-
true,
|
|
60
|
-
),
|
|
61
|
-
),
|
|
62
|
-
]),
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
// if (!(_t instanceof globalThis.Text)) { const _nt = createTextNode(""); if (_anchor) _anchor.after(_nt); _t = _nt; }
|
|
66
|
-
// globalThis.Text, not Text: the user's module may have its own `Text` (a component) shadowing the DOM's
|
|
67
|
-
const newTextVar = genId();
|
|
68
|
-
statements.push(
|
|
69
|
-
t.ifStatement(
|
|
70
|
-
t.unaryExpression(
|
|
71
|
-
"!",
|
|
72
|
-
t.parenthesizedExpression(
|
|
73
|
-
t.binaryExpression(
|
|
74
|
-
"instanceof",
|
|
75
|
-
t.identifier(textVar),
|
|
76
|
-
t.memberExpression(t.identifier("globalThis"), t.identifier("Text")),
|
|
77
|
-
),
|
|
78
|
-
),
|
|
79
|
-
),
|
|
80
|
-
t.blockStatement([
|
|
81
|
-
t.variableDeclaration("const", [
|
|
82
|
-
t.variableDeclarator(
|
|
83
|
-
t.identifier(newTextVar),
|
|
84
|
-
t.callExpression(
|
|
85
|
-
t.memberExpression(
|
|
86
|
-
t.identifier("document"),
|
|
87
|
-
t.identifier("createTextNode"),
|
|
88
|
-
),
|
|
89
|
-
[t.stringLiteral("")],
|
|
90
|
-
),
|
|
91
|
-
),
|
|
92
|
-
]),
|
|
93
|
-
t.ifStatement(
|
|
94
|
-
t.identifier(anchorVar),
|
|
95
|
-
t.expressionStatement(
|
|
96
|
-
t.callExpression(
|
|
97
|
-
t.memberExpression(
|
|
98
|
-
t.identifier(anchorVar),
|
|
99
|
-
t.identifier("after"),
|
|
100
|
-
),
|
|
101
|
-
[t.identifier(newTextVar)],
|
|
102
|
-
),
|
|
103
|
-
),
|
|
104
|
-
),
|
|
105
|
-
t.expressionStatement(
|
|
106
|
-
t.assignmentExpression(
|
|
107
|
-
"=",
|
|
108
|
-
t.identifier(textVar),
|
|
109
|
-
t.identifier(newTextVar),
|
|
110
|
-
),
|
|
111
|
-
),
|
|
112
|
-
]),
|
|
113
|
-
),
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
// createEffect(() => { if (_t) _t.textContent = String(expr); })
|
|
117
|
-
statements.push(
|
|
118
|
-
t.expressionStatement(
|
|
119
|
-
t.callExpression(t.identifier("createEffect"), [
|
|
120
|
-
t.arrowFunctionExpression(
|
|
121
|
-
[],
|
|
122
|
-
t.blockStatement([
|
|
123
|
-
t.ifStatement(
|
|
124
|
-
t.identifier(textVar),
|
|
125
|
-
t.expressionStatement(
|
|
126
|
-
t.assignmentExpression(
|
|
127
|
-
"=",
|
|
128
|
-
t.memberExpression(
|
|
129
|
-
t.identifier(textVar),
|
|
130
|
-
t.identifier("textContent"),
|
|
131
|
-
),
|
|
132
|
-
t.callExpression(t.identifier("String"), [expr]),
|
|
133
|
-
),
|
|
134
|
-
),
|
|
135
|
-
),
|
|
136
|
-
]),
|
|
137
|
-
),
|
|
138
|
-
]),
|
|
139
|
-
),
|
|
140
|
-
);
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// DOM mode: create text node and append
|
|
145
|
-
const textVar = genId();
|
|
146
|
-
statements.push(
|
|
147
|
-
t.variableDeclaration("const", [
|
|
148
|
-
t.variableDeclarator(
|
|
149
|
-
t.identifier(textVar),
|
|
150
|
-
t.callExpression(
|
|
151
|
-
t.memberExpression(
|
|
152
|
-
t.identifier("document"),
|
|
153
|
-
t.identifier("createTextNode"),
|
|
154
|
-
),
|
|
155
|
-
[t.stringLiteral("")],
|
|
156
|
-
),
|
|
157
|
-
),
|
|
158
|
-
]),
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
// createEffect(() => _t.textContent = String(expr))
|
|
162
|
-
statements.push(
|
|
163
|
-
t.expressionStatement(
|
|
164
|
-
t.callExpression(t.identifier("createEffect"), [
|
|
165
|
-
t.arrowFunctionExpression(
|
|
166
|
-
[],
|
|
167
|
-
t.blockStatement([
|
|
168
|
-
t.expressionStatement(
|
|
169
|
-
t.assignmentExpression(
|
|
170
|
-
"=",
|
|
171
|
-
t.memberExpression(
|
|
172
|
-
t.identifier(textVar),
|
|
173
|
-
t.identifier("textContent"),
|
|
174
|
-
),
|
|
175
|
-
t.callExpression(t.identifier("String"), [expr]),
|
|
176
|
-
),
|
|
177
|
-
),
|
|
178
|
-
]),
|
|
179
|
-
),
|
|
180
|
-
]),
|
|
181
|
-
),
|
|
182
|
-
);
|
|
183
|
-
|
|
184
|
-
// varName.append(_t)
|
|
185
|
-
statements.push(
|
|
186
|
-
t.expressionStatement(
|
|
187
|
-
t.callExpression(
|
|
188
|
-
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
189
|
-
[t.identifier(textVar)],
|
|
190
|
-
),
|
|
191
|
-
),
|
|
192
|
-
);
|
|
193
|
-
}
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create a reactive text node: `createEffect(() => node.textContent = String(expr))`
|
|
5
|
+
* In hydrate mode:
|
|
6
|
+
* - Claims <!--g--> anchor via claimComment (fixes adjacent-text bug vs DOM walk)
|
|
7
|
+
* - Consumes <!--/g--> closing delimiter
|
|
8
|
+
* - Gets nextSibling as the text node; if it's not a Text (SPA navigation), creates one
|
|
9
|
+
* - Effect updates that text node's content
|
|
10
|
+
* In dom mode: creates a new text node and appends it.
|
|
11
|
+
*/
|
|
12
|
+
export function buildTextNode(
|
|
13
|
+
varName: string,
|
|
14
|
+
expr: t.Expression,
|
|
15
|
+
statements: t.Statement[],
|
|
16
|
+
genId: () => string,
|
|
17
|
+
hydrate?: boolean,
|
|
18
|
+
nodesVar?: string,
|
|
19
|
+
parentVar?: string,
|
|
20
|
+
): void {
|
|
21
|
+
if (hydrate) {
|
|
22
|
+
const nodesId = t.identifier(nodesVar ?? "__nodes");
|
|
23
|
+
const parentIdent = parentVar ? t.identifier(parentVar) : undefined;
|
|
24
|
+
|
|
25
|
+
// const _anchor = claimComment(nodes, "g", parent) — claim/create <!--g-->
|
|
26
|
+
const anchorVar = genId();
|
|
27
|
+
const claimGArgs: t.Expression[] = [nodesId, t.stringLiteral("g")];
|
|
28
|
+
if (parentIdent) claimGArgs.push(parentIdent);
|
|
29
|
+
statements.push(
|
|
30
|
+
t.variableDeclaration("const", [
|
|
31
|
+
t.variableDeclarator(
|
|
32
|
+
t.identifier(anchorVar),
|
|
33
|
+
t.callExpression(t.identifier("claimComment"), claimGArgs),
|
|
34
|
+
),
|
|
35
|
+
]),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
// claimComment(nodes, "/g", parent) — consume <!--/g--> from pool
|
|
39
|
+
const claimSlashGArgs: t.Expression[] = [nodesId, t.stringLiteral("/g")];
|
|
40
|
+
if (parentIdent) claimSlashGArgs.push(parentIdent);
|
|
41
|
+
statements.push(
|
|
42
|
+
t.expressionStatement(
|
|
43
|
+
t.callExpression(t.identifier("claimComment"), claimSlashGArgs),
|
|
44
|
+
),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// let _t = _anchor?.nextSibling
|
|
48
|
+
// In SSR hydration, this is the text node between the comment delimiters.
|
|
49
|
+
// In SPA navigation, this is the closing <!--/g--> (not a Text), so we create one.
|
|
50
|
+
const textVar = genId();
|
|
51
|
+
statements.push(
|
|
52
|
+
t.variableDeclaration("let", [
|
|
53
|
+
t.variableDeclarator(
|
|
54
|
+
t.identifier(textVar),
|
|
55
|
+
t.optionalMemberExpression(
|
|
56
|
+
t.identifier(anchorVar),
|
|
57
|
+
t.identifier("nextSibling"),
|
|
58
|
+
false,
|
|
59
|
+
true,
|
|
60
|
+
),
|
|
61
|
+
),
|
|
62
|
+
]),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// if (!(_t instanceof globalThis.Text)) { const _nt = createTextNode(""); if (_anchor) _anchor.after(_nt); _t = _nt; }
|
|
66
|
+
// globalThis.Text, not Text: the user's module may have its own `Text` (a component) shadowing the DOM's
|
|
67
|
+
const newTextVar = genId();
|
|
68
|
+
statements.push(
|
|
69
|
+
t.ifStatement(
|
|
70
|
+
t.unaryExpression(
|
|
71
|
+
"!",
|
|
72
|
+
t.parenthesizedExpression(
|
|
73
|
+
t.binaryExpression(
|
|
74
|
+
"instanceof",
|
|
75
|
+
t.identifier(textVar),
|
|
76
|
+
t.memberExpression(t.identifier("globalThis"), t.identifier("Text")),
|
|
77
|
+
),
|
|
78
|
+
),
|
|
79
|
+
),
|
|
80
|
+
t.blockStatement([
|
|
81
|
+
t.variableDeclaration("const", [
|
|
82
|
+
t.variableDeclarator(
|
|
83
|
+
t.identifier(newTextVar),
|
|
84
|
+
t.callExpression(
|
|
85
|
+
t.memberExpression(
|
|
86
|
+
t.identifier("document"),
|
|
87
|
+
t.identifier("createTextNode"),
|
|
88
|
+
),
|
|
89
|
+
[t.stringLiteral("")],
|
|
90
|
+
),
|
|
91
|
+
),
|
|
92
|
+
]),
|
|
93
|
+
t.ifStatement(
|
|
94
|
+
t.identifier(anchorVar),
|
|
95
|
+
t.expressionStatement(
|
|
96
|
+
t.callExpression(
|
|
97
|
+
t.memberExpression(
|
|
98
|
+
t.identifier(anchorVar),
|
|
99
|
+
t.identifier("after"),
|
|
100
|
+
),
|
|
101
|
+
[t.identifier(newTextVar)],
|
|
102
|
+
),
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
t.expressionStatement(
|
|
106
|
+
t.assignmentExpression(
|
|
107
|
+
"=",
|
|
108
|
+
t.identifier(textVar),
|
|
109
|
+
t.identifier(newTextVar),
|
|
110
|
+
),
|
|
111
|
+
),
|
|
112
|
+
]),
|
|
113
|
+
),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
// createEffect(() => { if (_t) _t.textContent = String(expr); })
|
|
117
|
+
statements.push(
|
|
118
|
+
t.expressionStatement(
|
|
119
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
120
|
+
t.arrowFunctionExpression(
|
|
121
|
+
[],
|
|
122
|
+
t.blockStatement([
|
|
123
|
+
t.ifStatement(
|
|
124
|
+
t.identifier(textVar),
|
|
125
|
+
t.expressionStatement(
|
|
126
|
+
t.assignmentExpression(
|
|
127
|
+
"=",
|
|
128
|
+
t.memberExpression(
|
|
129
|
+
t.identifier(textVar),
|
|
130
|
+
t.identifier("textContent"),
|
|
131
|
+
),
|
|
132
|
+
t.callExpression(t.identifier("String"), [expr]),
|
|
133
|
+
),
|
|
134
|
+
),
|
|
135
|
+
),
|
|
136
|
+
]),
|
|
137
|
+
),
|
|
138
|
+
]),
|
|
139
|
+
),
|
|
140
|
+
);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// DOM mode: create text node and append
|
|
145
|
+
const textVar = genId();
|
|
146
|
+
statements.push(
|
|
147
|
+
t.variableDeclaration("const", [
|
|
148
|
+
t.variableDeclarator(
|
|
149
|
+
t.identifier(textVar),
|
|
150
|
+
t.callExpression(
|
|
151
|
+
t.memberExpression(
|
|
152
|
+
t.identifier("document"),
|
|
153
|
+
t.identifier("createTextNode"),
|
|
154
|
+
),
|
|
155
|
+
[t.stringLiteral("")],
|
|
156
|
+
),
|
|
157
|
+
),
|
|
158
|
+
]),
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
// createEffect(() => _t.textContent = String(expr))
|
|
162
|
+
statements.push(
|
|
163
|
+
t.expressionStatement(
|
|
164
|
+
t.callExpression(t.identifier("createEffect"), [
|
|
165
|
+
t.arrowFunctionExpression(
|
|
166
|
+
[],
|
|
167
|
+
t.blockStatement([
|
|
168
|
+
t.expressionStatement(
|
|
169
|
+
t.assignmentExpression(
|
|
170
|
+
"=",
|
|
171
|
+
t.memberExpression(
|
|
172
|
+
t.identifier(textVar),
|
|
173
|
+
t.identifier("textContent"),
|
|
174
|
+
),
|
|
175
|
+
t.callExpression(t.identifier("String"), [expr]),
|
|
176
|
+
),
|
|
177
|
+
),
|
|
178
|
+
]),
|
|
179
|
+
),
|
|
180
|
+
]),
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// varName.append(_t)
|
|
185
|
+
statements.push(
|
|
186
|
+
t.expressionStatement(
|
|
187
|
+
t.callExpression(
|
|
188
|
+
t.memberExpression(t.identifier(varName), t.identifier("append")),
|
|
189
|
+
[t.identifier(textVar)],
|
|
190
|
+
),
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
}
|
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { types as t } from "@babel/core";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* `<textarea>{text}</textarea>` becomes `<textarea value={text} />`, before
|
|
5
|
-
* any mode compiles it. A textarea's content is raw text to the HTML parser,
|
|
6
|
-
* so the `<!--g-->` markers SSR puts around dynamic children would show up
|
|
7
|
-
* in the box as typed text (and be submitted with the form), and there are
|
|
8
|
-
* no nodes in there for hydration to find. `value` is already handled as the
|
|
9
|
-
* content in SSR and as the property on the client.
|
|
10
|
-
*
|
|
11
|
-
* An explicit `value` wins; the children are then dropped, like React.
|
|
12
|
-
* Text follows JSX whitespace rules; null, undefined and booleans are empty.
|
|
13
|
-
*/
|
|
14
|
-
export function textareaChildrenToValue(node: t.JSXElement | t.JSXFragment): void {
|
|
15
|
-
if (t.isJSXElement(node)) {
|
|
16
|
-
const name = node.openingElement.name;
|
|
17
|
-
if (t.isJSXIdentifier(name) && name.name === "textarea" && node.children.length) {
|
|
18
|
-
const hasValue = node.openingElement.attributes.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === "value");
|
|
19
|
-
if (!hasValue) {
|
|
20
|
-
const value = contentOf(node);
|
|
21
|
-
if (value) node.openingElement.attributes.push(t.jsxAttribute(t.jsxIdentifier("value"), t.jsxExpressionContainer(value)));
|
|
22
|
-
}
|
|
23
|
-
node.children = [];
|
|
24
|
-
node.openingElement.selfClosing = true;
|
|
25
|
-
node.closingElement = null;
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
for (const child of node.children) {
|
|
30
|
-
if (t.isJSXElement(child) || t.isJSXFragment(child)) textareaChildrenToValue(child);
|
|
31
|
-
else if (t.isJSXExpressionContainer(child) && (t.isJSXElement(child.expression) || t.isJSXFragment(child.expression))) textareaChildrenToValue(child.expression);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** The children as one string expression, or null when there is nothing. */
|
|
36
|
-
function contentOf(node: t.JSXElement): t.Expression | null {
|
|
37
|
-
// buildChildren applies JSX's whitespace rules and drops empty expressions
|
|
38
|
-
const parts = t.react.buildChildren(node) as t.Expression[];
|
|
39
|
-
if (!parts.length) return null;
|
|
40
|
-
const text = (e: t.Expression): t.Expression => {
|
|
41
|
-
if (t.isStringLiteral(e)) return e;
|
|
42
|
-
// (v => v == null || typeof v === "boolean" ? "" : String(v))(e)
|
|
43
|
-
const v = t.identifier("__v");
|
|
44
|
-
return t.callExpression(
|
|
45
|
-
t.arrowFunctionExpression(
|
|
46
|
-
[v],
|
|
47
|
-
t.conditionalExpression(
|
|
48
|
-
t.logicalExpression("||", t.binaryExpression("==", v, t.nullLiteral()), t.binaryExpression("===", t.unaryExpression("typeof", v), t.stringLiteral("boolean"))),
|
|
49
|
-
t.stringLiteral(""),
|
|
50
|
-
t.callExpression(t.identifier("String"), [v]),
|
|
51
|
-
),
|
|
52
|
-
),
|
|
53
|
-
[e],
|
|
54
|
-
);
|
|
55
|
-
};
|
|
56
|
-
if (parts.length === 1 && t.isStringLiteral(parts[0])) return parts[0]!;
|
|
57
|
-
return parts.map(text).reduce((a, b) => t.binaryExpression("+", a, b));
|
|
58
|
-
}
|
|
1
|
+
import { types as t } from "@babel/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `<textarea>{text}</textarea>` becomes `<textarea value={text} />`, before
|
|
5
|
+
* any mode compiles it. A textarea's content is raw text to the HTML parser,
|
|
6
|
+
* so the `<!--g-->` markers SSR puts around dynamic children would show up
|
|
7
|
+
* in the box as typed text (and be submitted with the form), and there are
|
|
8
|
+
* no nodes in there for hydration to find. `value` is already handled as the
|
|
9
|
+
* content in SSR and as the property on the client.
|
|
10
|
+
*
|
|
11
|
+
* An explicit `value` wins; the children are then dropped, like React.
|
|
12
|
+
* Text follows JSX whitespace rules; null, undefined and booleans are empty.
|
|
13
|
+
*/
|
|
14
|
+
export function textareaChildrenToValue(node: t.JSXElement | t.JSXFragment): void {
|
|
15
|
+
if (t.isJSXElement(node)) {
|
|
16
|
+
const name = node.openingElement.name;
|
|
17
|
+
if (t.isJSXIdentifier(name) && name.name === "textarea" && node.children.length) {
|
|
18
|
+
const hasValue = node.openingElement.attributes.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name) && a.name.name === "value");
|
|
19
|
+
if (!hasValue) {
|
|
20
|
+
const value = contentOf(node);
|
|
21
|
+
if (value) node.openingElement.attributes.push(t.jsxAttribute(t.jsxIdentifier("value"), t.jsxExpressionContainer(value)));
|
|
22
|
+
}
|
|
23
|
+
node.children = [];
|
|
24
|
+
node.openingElement.selfClosing = true;
|
|
25
|
+
node.closingElement = null;
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (const child of node.children) {
|
|
30
|
+
if (t.isJSXElement(child) || t.isJSXFragment(child)) textareaChildrenToValue(child);
|
|
31
|
+
else if (t.isJSXExpressionContainer(child) && (t.isJSXElement(child.expression) || t.isJSXFragment(child.expression))) textareaChildrenToValue(child.expression);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The children as one string expression, or null when there is nothing. */
|
|
36
|
+
function contentOf(node: t.JSXElement): t.Expression | null {
|
|
37
|
+
// buildChildren applies JSX's whitespace rules and drops empty expressions
|
|
38
|
+
const parts = t.react.buildChildren(node) as t.Expression[];
|
|
39
|
+
if (!parts.length) return null;
|
|
40
|
+
const text = (e: t.Expression): t.Expression => {
|
|
41
|
+
if (t.isStringLiteral(e)) return e;
|
|
42
|
+
// (v => v == null || typeof v === "boolean" ? "" : String(v))(e)
|
|
43
|
+
const v = t.identifier("__v");
|
|
44
|
+
return t.callExpression(
|
|
45
|
+
t.arrowFunctionExpression(
|
|
46
|
+
[v],
|
|
47
|
+
t.conditionalExpression(
|
|
48
|
+
t.logicalExpression("||", t.binaryExpression("==", v, t.nullLiteral()), t.binaryExpression("===", t.unaryExpression("typeof", v), t.stringLiteral("boolean"))),
|
|
49
|
+
t.stringLiteral(""),
|
|
50
|
+
t.callExpression(t.identifier("String"), [v]),
|
|
51
|
+
),
|
|
52
|
+
),
|
|
53
|
+
[e],
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
if (parts.length === 1 && t.isStringLiteral(parts[0])) return parts[0]!;
|
|
57
|
+
return parts.map(text).reduce((a, b) => t.binaryExpression("+", a, b));
|
|
58
|
+
}
|