@sigil-dev/compiler 0.10.1 → 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.
@@ -166,7 +166,7 @@ export function buildAnchorMount(
166
166
  t.binaryExpression(
167
167
  "instanceof",
168
168
  t.identifier("_item"),
169
- t.identifier("Node"),
169
+ t.memberExpression(t.identifier("globalThis"), t.identifier("Node")),
170
170
  ),
171
171
  t.identifier("_item"),
172
172
  t.callExpression(
@@ -202,7 +202,7 @@ export function buildAnchorMount(
202
202
  t.binaryExpression(
203
203
  "instanceof",
204
204
  t.identifier("_v"),
205
- t.identifier("Node"),
205
+ t.memberExpression(t.identifier("globalThis"), t.identifier("Node")),
206
206
  ),
207
207
  t.blockStatement([
208
208
  keepNodes("_v"),
@@ -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,192 +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 Text)) { const _nt = createTextNode(""); if (_anchor) _anchor.after(_nt); _t = _nt; }
66
- const newTextVar = genId();
67
- statements.push(
68
- t.ifStatement(
69
- t.unaryExpression(
70
- "!",
71
- t.parenthesizedExpression(
72
- t.binaryExpression(
73
- "instanceof",
74
- t.identifier(textVar),
75
- t.identifier("Text"),
76
- ),
77
- ),
78
- ),
79
- t.blockStatement([
80
- t.variableDeclaration("const", [
81
- t.variableDeclarator(
82
- t.identifier(newTextVar),
83
- t.callExpression(
84
- t.memberExpression(
85
- t.identifier("document"),
86
- t.identifier("createTextNode"),
87
- ),
88
- [t.stringLiteral("")],
89
- ),
90
- ),
91
- ]),
92
- t.ifStatement(
93
- t.identifier(anchorVar),
94
- t.expressionStatement(
95
- t.callExpression(
96
- t.memberExpression(
97
- t.identifier(anchorVar),
98
- t.identifier("after"),
99
- ),
100
- [t.identifier(newTextVar)],
101
- ),
102
- ),
103
- ),
104
- t.expressionStatement(
105
- t.assignmentExpression(
106
- "=",
107
- t.identifier(textVar),
108
- t.identifier(newTextVar),
109
- ),
110
- ),
111
- ]),
112
- ),
113
- );
114
-
115
- // createEffect(() => { if (_t) _t.textContent = String(expr); })
116
- statements.push(
117
- t.expressionStatement(
118
- t.callExpression(t.identifier("createEffect"), [
119
- t.arrowFunctionExpression(
120
- [],
121
- t.blockStatement([
122
- t.ifStatement(
123
- t.identifier(textVar),
124
- t.expressionStatement(
125
- t.assignmentExpression(
126
- "=",
127
- t.memberExpression(
128
- t.identifier(textVar),
129
- t.identifier("textContent"),
130
- ),
131
- t.callExpression(t.identifier("String"), [expr]),
132
- ),
133
- ),
134
- ),
135
- ]),
136
- ),
137
- ]),
138
- ),
139
- );
140
- return;
141
- }
142
-
143
- // DOM mode: create text node and append
144
- const textVar = genId();
145
- statements.push(
146
- t.variableDeclaration("const", [
147
- t.variableDeclarator(
148
- t.identifier(textVar),
149
- t.callExpression(
150
- t.memberExpression(
151
- t.identifier("document"),
152
- t.identifier("createTextNode"),
153
- ),
154
- [t.stringLiteral("")],
155
- ),
156
- ),
157
- ]),
158
- );
159
-
160
- // createEffect(() => _t.textContent = String(expr))
161
- statements.push(
162
- t.expressionStatement(
163
- t.callExpression(t.identifier("createEffect"), [
164
- t.arrowFunctionExpression(
165
- [],
166
- t.blockStatement([
167
- t.expressionStatement(
168
- t.assignmentExpression(
169
- "=",
170
- t.memberExpression(
171
- t.identifier(textVar),
172
- t.identifier("textContent"),
173
- ),
174
- t.callExpression(t.identifier("String"), [expr]),
175
- ),
176
- ),
177
- ]),
178
- ),
179
- ]),
180
- ),
181
- );
182
-
183
- // varName.append(_t)
184
- statements.push(
185
- t.expressionStatement(
186
- t.callExpression(
187
- t.memberExpression(t.identifier(varName), t.identifier("append")),
188
- [t.identifier(textVar)],
189
- ),
190
- ),
191
- );
192
- }
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
+ }