@sigil-dev/compiler 0.9.2 → 0.9.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.
Files changed (40) hide show
  1. package/README.md +162 -162
  2. package/index.ts +4 -4
  3. package/jsr.json +8 -8
  4. package/package.json +1 -1
  5. package/src/babel/handlers/dom/derived.ts +62 -62
  6. package/src/babel/handlers/dom/effect.ts +15 -15
  7. package/src/babel/handlers/dom/globals.ts +105 -105
  8. package/src/babel/handlers/dom/state.ts +107 -104
  9. package/src/babel/handlers/ssr/derived.ts +27 -27
  10. package/src/babel/handlers/ssr/state.ts +11 -11
  11. package/src/babel/index.ts +594 -589
  12. package/src/babel/jsx/anchor-mount.ts +389 -389
  13. package/src/babel/jsx/children.ts +124 -124
  14. package/src/babel/jsx/element.ts +831 -831
  15. package/src/babel/jsx/fragment.ts +282 -282
  16. package/src/babel/jsx/index.ts +2 -2
  17. package/src/babel/jsx/keyed-list.ts +278 -278
  18. package/src/babel/jsx/ssr.ts +319 -319
  19. package/src/babel/jsx/text-node.ts +192 -192
  20. package/src/babel/jsx/utils.ts +179 -179
  21. package/src/babel/util/bind.ts +139 -139
  22. package/src/babel/util/css.ts +151 -151
  23. package/src/babel/util/dead-code.ts +28 -28
  24. package/src/babel/util/helpers.ts +46 -46
  25. package/src/babel/util/magic.ts +11 -11
  26. package/src/bun-plugin.ts +92 -90
  27. package/src/vite/index.ts +58 -58
  28. package/test/async-components.test.ts +24 -24
  29. package/test/components.test.ts +129 -129
  30. package/test/containsSignal.test.ts +288 -288
  31. package/test/fragments.test.ts +90 -90
  32. package/test/helpers/transform.ts +45 -48
  33. package/test/hydration.test.ts +147 -147
  34. package/test/jsx.test.ts +197 -197
  35. package/test/keyed-lists.test.ts +88 -88
  36. package/test/reactivity.test.ts +225 -225
  37. package/test/scoped-css.test.ts +129 -129
  38. package/test/ssr.test.ts +130 -130
  39. package/test/tree-shaking.test.ts +78 -78
  40. package/tsconfig.json +30 -30
@@ -1,192 +1,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 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 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
+ }