@sigil-dev/compiler 0.9.29 → 0.9.30

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 (38) hide show
  1. package/index.ts +6 -4
  2. package/package.json +1 -1
  3. package/src/babel/index.ts +763 -750
  4. package/src/babel/jsx/anchor-mount.ts +338 -333
  5. package/src/babel/jsx/children.ts +116 -116
  6. package/src/babel/jsx/element.ts +1040 -1034
  7. package/src/babel/jsx/fragment.ts +271 -271
  8. package/src/babel/jsx/keyed-list.ts +339 -339
  9. package/src/babel/jsx/utils.ts +356 -356
  10. package/src/babel/util/bind.ts +143 -143
  11. package/src/babel/util/css.ts +208 -208
  12. package/src/babel/util/dead-code.ts +94 -93
  13. package/src/babel/util/links.ts +101 -0
  14. package/src/babel/util/reactive-fns.ts +110 -110
  15. package/src/babel/util/runtime-names.ts +88 -0
  16. package/src/babel/util/state-imports.ts +122 -121
  17. package/src/babel/util/static-assertion.ts +51 -51
  18. package/src/babel/util/static-env.ts +84 -0
  19. package/src/vite/index.ts +61 -61
  20. package/test/bun-plugin.test.ts +45 -45
  21. package/test/context.test.ts +110 -102
  22. package/test/fixtures/stores/index.ts +2 -2
  23. package/test/hydration-analysis.test.ts +133 -133
  24. package/test/hydration.test.ts +315 -313
  25. package/test/jsx.test.ts +254 -254
  26. package/test/links.test.ts +57 -0
  27. package/test/reactive-helpers.test.ts +135 -135
  28. package/test/render-parity.test.ts +909 -907
  29. package/test/runtime-names.test.ts +60 -0
  30. package/test/scoped-css.test.ts +166 -166
  31. package/test/ssr-boolean-attrs.test.ts +56 -56
  32. package/test/ssr-raw-text.test.ts +68 -68
  33. package/test/ssr.test.ts +30 -0
  34. package/test/static-assertion.test.ts +123 -123
  35. package/test/static-env.test.ts +85 -0
  36. package/test/static-skip.test.ts +292 -292
  37. package/test/style-object.test.ts +36 -36
  38. package/test/tree-shaking.test.ts +122 -120
@@ -1,116 +1,116 @@
1
- import { types as t } from "@babel/core";
2
- import { processElement } from "./element";
3
- import { processFragment } from "./fragment";
4
- import { containsSignal, hasJSX, jsxText } from "./utils";
5
-
6
- /**
7
- * Collect children from a JSX element into a children expression.
8
- * Returns null if no meaningful children exist.
9
- *
10
- * DOM mode evaluates each JSX child immediately — the component receives real
11
- * nodes. Hydrate mode emits thunks instead: a component's children live inside
12
- * *its* root element, a subtree the call site cannot see, so they must claim
13
- * from the pool that `hydrateSlot` pushes at the `{children}` site rather than
14
- * from the caller's pool. Evaluating them eagerly here made them steal the
15
- * component's own SSR root and re-render it (double render).
16
- */
17
- export function collectChildren(
18
- node: t.JSXElement,
19
- _statements: t.Statement[],
20
- genId: () => string,
21
- signals: Set<string>,
22
- hash?: string,
23
- hydrate?: boolean,
24
- nodesVar?: string,
25
- ): t.Expression | null {
26
- const childrenElements: t.Expression[] = [];
27
- let hasOnlyText = true;
28
- // Deferred children open their own scope from the hydration stack.
29
- const childNodesVar = hydrate ? "__nodes" : nodesVar;
30
-
31
- for (const child of node.children) {
32
- if (t.isJSXText(child)) {
33
- const text = jsxText(child, node.children);
34
- if (text === null) continue;
35
- childrenElements.push(t.stringLiteral(text));
36
- continue;
37
- }
38
-
39
- let jsx: t.JSXElement | t.JSXFragment | null = null;
40
- if (t.isJSXElement(child) || t.isJSXFragment(child)) {
41
- jsx = child;
42
- } else if (t.isJSXExpressionContainer(child)) {
43
- const expr = child.expression;
44
- if (t.isJSXEmptyExpression(expr)) continue;
45
- if (t.isJSXElement(expr) || t.isJSXFragment(expr)) {
46
- jsx = expr;
47
- } else if (containsSignal(expr, signals) || (hydrate && hasJSX(expr))) {
48
- // <Box>{on ? <A /> : <B />}</Box>: compile it as <>{…}</> so it
49
- // stays reactive (a plain value is read once, when the call is
50
- // built) and, like JSX children, claims inside the component.
51
- jsx = t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), [
52
- child,
53
- ]);
54
- } else {
55
- // Plain expression child — pass the value straight through.
56
- hasOnlyText = false;
57
- childrenElements.push(expr);
58
- continue;
59
- }
60
- } else {
61
- continue;
62
- }
63
-
64
- hasOnlyText = false;
65
- const childStatements: t.Statement[] = [];
66
- const childVar = t.isJSXElement(jsx)
67
- ? processElement(
68
- jsx,
69
- childStatements,
70
- genId,
71
- signals,
72
- hash,
73
- hydrate,
74
- childNodesVar,
75
- )
76
- : processFragment(
77
- jsx,
78
- childStatements,
79
- genId,
80
- signals,
81
- hash,
82
- hydrate,
83
- childNodesVar,
84
- );
85
-
86
- const body: t.Statement[] = [];
87
- if (hydrate) {
88
- // const __nodes = getHydrationNodes() — resolved when the thunk runs
89
- body.push(
90
- t.variableDeclaration("const", [
91
- t.variableDeclarator(
92
- t.identifier("__nodes"),
93
- t.callExpression(t.identifier("getHydrationNodes"), []),
94
- ),
95
- ]),
96
- );
97
- }
98
- body.push(...childStatements, t.returnStatement(t.identifier(childVar)));
99
- const thunk = t.arrowFunctionExpression([], t.blockStatement(body));
100
- childrenElements.push(hydrate ? thunk : t.callExpression(thunk, []));
101
- }
102
-
103
- if (childrenElements.length === 0) return null;
104
- // Build the inner value: string for single text, array otherwise
105
- let inner: t.Expression;
106
- if (
107
- childrenElements.length === 1 &&
108
- hasOnlyText &&
109
- t.isStringLiteral(childrenElements[0])
110
- ) {
111
- inner = childrenElements[0];
112
- } else {
113
- inner = t.arrayExpression(childrenElements);
114
- }
115
- return inner;
116
- }
1
+ import { types as t } from "@babel/core";
2
+ import { processElement } from "./element";
3
+ import { processFragment } from "./fragment";
4
+ import { containsSignal, hasJSX, jsxText } from "./utils";
5
+
6
+ /**
7
+ * Collect children from a JSX element into a children expression.
8
+ * Returns null if no meaningful children exist.
9
+ *
10
+ * DOM mode evaluates each JSX child immediately — the component receives real
11
+ * nodes. Hydrate mode emits thunks instead: a component's children live inside
12
+ * *its* root element, a subtree the call site cannot see, so they must claim
13
+ * from the pool that `hydrateSlot` pushes at the `{children}` site rather than
14
+ * from the caller's pool. Evaluating them eagerly here made them steal the
15
+ * component's own SSR root and re-render it (double render).
16
+ */
17
+ export function collectChildren(
18
+ node: t.JSXElement,
19
+ _statements: t.Statement[],
20
+ genId: () => string,
21
+ signals: Set<string>,
22
+ hash?: string,
23
+ hydrate?: boolean,
24
+ nodesVar?: string,
25
+ ): t.Expression | null {
26
+ const childrenElements: t.Expression[] = [];
27
+ let hasOnlyText = true;
28
+ // Deferred children open their own scope from the hydration stack.
29
+ const childNodesVar = hydrate ? "__nodes" : nodesVar;
30
+
31
+ for (const child of node.children) {
32
+ if (t.isJSXText(child)) {
33
+ const text = jsxText(child, node.children);
34
+ if (text === null) continue;
35
+ childrenElements.push(t.stringLiteral(text));
36
+ continue;
37
+ }
38
+
39
+ let jsx: t.JSXElement | t.JSXFragment | null = null;
40
+ if (t.isJSXElement(child) || t.isJSXFragment(child)) {
41
+ jsx = child;
42
+ } else if (t.isJSXExpressionContainer(child)) {
43
+ const expr = child.expression;
44
+ if (t.isJSXEmptyExpression(expr)) continue;
45
+ if (t.isJSXElement(expr) || t.isJSXFragment(expr)) {
46
+ jsx = expr;
47
+ } else if (containsSignal(expr, signals) || (hydrate && hasJSX(expr))) {
48
+ // <Box>{on ? <A /> : <B />}</Box>: compile it as <>{…}</> so it
49
+ // stays reactive (a plain value is read once, when the call is
50
+ // built) and, like JSX children, claims inside the component.
51
+ jsx = t.jsxFragment(t.jsxOpeningFragment(), t.jsxClosingFragment(), [
52
+ child,
53
+ ]);
54
+ } else {
55
+ // Plain expression child — pass the value straight through.
56
+ hasOnlyText = false;
57
+ childrenElements.push(expr);
58
+ continue;
59
+ }
60
+ } else {
61
+ continue;
62
+ }
63
+
64
+ hasOnlyText = false;
65
+ const childStatements: t.Statement[] = [];
66
+ const childVar = t.isJSXElement(jsx)
67
+ ? processElement(
68
+ jsx,
69
+ childStatements,
70
+ genId,
71
+ signals,
72
+ hash,
73
+ hydrate,
74
+ childNodesVar,
75
+ )
76
+ : processFragment(
77
+ jsx,
78
+ childStatements,
79
+ genId,
80
+ signals,
81
+ hash,
82
+ hydrate,
83
+ childNodesVar,
84
+ );
85
+
86
+ const body: t.Statement[] = [];
87
+ if (hydrate) {
88
+ // const __nodes = getHydrationNodes() — resolved when the thunk runs
89
+ body.push(
90
+ t.variableDeclaration("const", [
91
+ t.variableDeclarator(
92
+ t.identifier("__nodes"),
93
+ t.callExpression(t.identifier("getHydrationNodes"), []),
94
+ ),
95
+ ]),
96
+ );
97
+ }
98
+ body.push(...childStatements, t.returnStatement(t.identifier(childVar)));
99
+ const thunk = t.arrowFunctionExpression([], t.blockStatement(body));
100
+ childrenElements.push(hydrate ? thunk : t.callExpression(thunk, []));
101
+ }
102
+
103
+ if (childrenElements.length === 0) return null;
104
+ // Build the inner value: string for single text, array otherwise
105
+ let inner: t.Expression;
106
+ if (
107
+ childrenElements.length === 1 &&
108
+ hasOnlyText &&
109
+ t.isStringLiteral(childrenElements[0])
110
+ ) {
111
+ inner = childrenElements[0];
112
+ } else {
113
+ inner = t.arrayExpression(childrenElements);
114
+ }
115
+ return inner;
116
+ }