@sigil-dev/compiler 0.9.29 → 0.9.31

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,135 +1,135 @@
1
- import { describe, expect, test } from "bun:test";
2
- import * as runtime from "../../runtime/index.ts";
3
- import { transformDOM } from "./helpers/transform.ts";
4
-
5
- /**
6
- * `{label()}` where `label` reads a signal in its body used to compile to a
7
- * one-time insert: the read is real, but nothing in the expression names a
8
- * signal, so the compiler saw nothing to track and the text never updated.
9
- */
10
-
11
- function run(src: string, exports: string[]) {
12
- const stripped = transformDOM(src).replace(
13
- /import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g,
14
- "",
15
- );
16
- const factory = new Function(
17
- "document",
18
- ...Object.keys(runtime),
19
- `${stripped};\nreturn { ${exports.join(", ")} };`,
20
- );
21
- return factory(
22
- (globalThis as { document: unknown }).document,
23
- ...Object.values(runtime),
24
- ) as Record<string, any>;
25
- }
26
-
27
- const effects = (src: string) =>
28
- (transformDOM(src).match(/createEffect\(/g) ?? []).length;
29
-
30
- describe("helpers that read signals", () => {
31
- test("a same-file helper called in JSX updates", async () => {
32
- const { el, count } = run(
33
- `let count = $state(0);
34
- const label = () => "n=" + count;
35
- const el = <p>{label()}</p>;`,
36
- ["el", "count"],
37
- );
38
- expect(el.textContent).toBe("n=0");
39
- count.set(5);
40
- await runtime.tick();
41
- expect(el.textContent).toBe("n=5");
42
- });
43
-
44
- test("through another helper, declared after the one that calls it", async () => {
45
- const { el, count } = run(
46
- `let count = $state(1);
47
- const outer = () => inner() + "!";
48
- const inner = () => "n=" + count;
49
- const el = <p>{outer()}</p>;`,
50
- ["el", "count"],
51
- );
52
- expect(el.textContent).toBe("n=1!");
53
- count.set(2);
54
- await runtime.tick();
55
- expect(el.textContent).toBe("n=2!");
56
- });
57
-
58
- test("a function declaration, and a derived read inside it", async () => {
59
- const { el, count } = run(
60
- `let count = $state(1);
61
- let double = $derived(count * 2);
62
- function show() { return "d=" + double; }
63
- const el = <p>{show()}</p>;`,
64
- ["el", "count"],
65
- );
66
- count.set(4);
67
- await runtime.tick();
68
- expect(el.textContent).toBe("d=8");
69
- });
70
-
71
- test("in an attribute", async () => {
72
- const { el, count } = run(
73
- `let count = $state(0);
74
- const cls = () => (count > 1 ? "big" : "small");
75
- const el = <p className={cls()}>x</p>;`,
76
- ["el", "count"],
77
- );
78
- expect(el.className).toBe("small");
79
- count.set(2);
80
- await runtime.tick();
81
- expect(el.className).toBe("big");
82
- });
83
-
84
- test("props read through a local helper", async () => {
85
- const out = transformDOM(
86
- `export const A = (props) => {
87
- const title = () => props.title.toUpperCase();
88
- return <h1>{title()}</h1>;
89
- };`,
90
- );
91
- expect(out).toContain("createEffect(");
92
- });
93
- });
94
-
95
- describe("what stays static", () => {
96
- test("a pure helper gets no effect", () => {
97
- expect(
98
- effects(
99
- `let count = $state(0);
100
- const fmt = (n) => "n=" + n;
101
- export const A = () => <p>{fmt(1)}</p>;`,
102
- ),
103
- ).toBe(0);
104
- });
105
-
106
- test("a helper that only writes is not a read", () => {
107
- expect(
108
- effects(
109
- `let count = $state(0);
110
- const reset = () => { count = 0; };
111
- export const A = () => <p>{reset()}</p>;`,
112
- ),
113
- ).toBe(0);
114
- });
115
-
116
- test("a helper that builds JSX is inserted, not stringified", () => {
117
- const out = transformDOM(
118
- `let count = $state(0);
119
- const badge = () => <b>{count}</b>;
120
- export const A = () => <p>{badge()}</p>;`,
121
- );
122
- expect(out).toContain("insert(_el0, badge())");
123
- expect(out).not.toContain("String(badge())");
124
- });
125
-
126
- test("a call inside an event handler is left alone", () => {
127
- const out = transformDOM(
128
- `let count = $state(0);
129
- const inc = () => count++;
130
- export const A = () => <button onClick={() => inc()}>x</button>;`,
131
- );
132
- expect(out).toContain('addEventListener("click", () => inc())');
133
- expect(out).not.toContain("createEffect(");
134
- });
135
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import * as runtime from "../../runtime/index.ts";
3
+ import { transformDOM } from "./helpers/transform.ts";
4
+
5
+ /**
6
+ * `{label()}` where `label` reads a signal in its body used to compile to a
7
+ * one-time insert: the read is real, but nothing in the expression names a
8
+ * signal, so the compiler saw nothing to track and the text never updated.
9
+ */
10
+
11
+ function run(src: string, exports: string[]) {
12
+ const stripped = transformDOM(src).replace(
13
+ /import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g,
14
+ "",
15
+ );
16
+ const factory = new Function(
17
+ "document",
18
+ ...Object.keys(runtime),
19
+ `${stripped};\nreturn { ${exports.join(", ")} };`,
20
+ );
21
+ return factory(
22
+ (globalThis as { document: unknown }).document,
23
+ ...Object.values(runtime),
24
+ ) as Record<string, any>;
25
+ }
26
+
27
+ const effects = (src: string) =>
28
+ (transformDOM(src).match(/createEffect\(/g) ?? []).length;
29
+
30
+ describe("helpers that read signals", () => {
31
+ test("a same-file helper called in JSX updates", async () => {
32
+ const { el, count } = run(
33
+ `let count = $state(0);
34
+ const label = () => "n=" + count;
35
+ const el = <p>{label()}</p>;`,
36
+ ["el", "count"],
37
+ );
38
+ expect(el.textContent).toBe("n=0");
39
+ count.set(5);
40
+ await runtime.tick();
41
+ expect(el.textContent).toBe("n=5");
42
+ });
43
+
44
+ test("through another helper, declared after the one that calls it", async () => {
45
+ const { el, count } = run(
46
+ `let count = $state(1);
47
+ const outer = () => inner() + "!";
48
+ const inner = () => "n=" + count;
49
+ const el = <p>{outer()}</p>;`,
50
+ ["el", "count"],
51
+ );
52
+ expect(el.textContent).toBe("n=1!");
53
+ count.set(2);
54
+ await runtime.tick();
55
+ expect(el.textContent).toBe("n=2!");
56
+ });
57
+
58
+ test("a function declaration, and a derived read inside it", async () => {
59
+ const { el, count } = run(
60
+ `let count = $state(1);
61
+ let double = $derived(count * 2);
62
+ function show() { return "d=" + double; }
63
+ const el = <p>{show()}</p>;`,
64
+ ["el", "count"],
65
+ );
66
+ count.set(4);
67
+ await runtime.tick();
68
+ expect(el.textContent).toBe("d=8");
69
+ });
70
+
71
+ test("in an attribute", async () => {
72
+ const { el, count } = run(
73
+ `let count = $state(0);
74
+ const cls = () => (count > 1 ? "big" : "small");
75
+ const el = <p className={cls()}>x</p>;`,
76
+ ["el", "count"],
77
+ );
78
+ expect(el.className).toBe("small");
79
+ count.set(2);
80
+ await runtime.tick();
81
+ expect(el.className).toBe("big");
82
+ });
83
+
84
+ test("props read through a local helper", async () => {
85
+ const out = transformDOM(
86
+ `export const A = (props) => {
87
+ const title = () => props.title.toUpperCase();
88
+ return <h1>{title()}</h1>;
89
+ };`,
90
+ );
91
+ expect(out).toContain("createEffect(");
92
+ });
93
+ });
94
+
95
+ describe("what stays static", () => {
96
+ test("a pure helper gets no effect", () => {
97
+ expect(
98
+ effects(
99
+ `let count = $state(0);
100
+ const fmt = (n) => "n=" + n;
101
+ export const A = () => <p>{fmt(1)}</p>;`,
102
+ ),
103
+ ).toBe(0);
104
+ });
105
+
106
+ test("a helper that only writes is not a read", () => {
107
+ expect(
108
+ effects(
109
+ `let count = $state(0);
110
+ const reset = () => { count = 0; };
111
+ export const A = () => <p>{reset()}</p>;`,
112
+ ),
113
+ ).toBe(0);
114
+ });
115
+
116
+ test("a helper that builds JSX is inserted, not stringified", () => {
117
+ const out = transformDOM(
118
+ `let count = $state(0);
119
+ const badge = () => <b>{count}</b>;
120
+ export const A = () => <p>{badge()}</p>;`,
121
+ );
122
+ expect(out).toContain("insert(_el0, badge())");
123
+ expect(out).not.toContain("String(badge())");
124
+ });
125
+
126
+ test("a call inside an event handler is left alone", () => {
127
+ const out = transformDOM(
128
+ `let count = $state(0);
129
+ const inc = () => count++;
130
+ export const A = () => <button onClick={() => inc()}>x</button>;`,
131
+ );
132
+ expect(out).toContain('addEventListener("click", () => inc())');
133
+ expect(out).not.toContain("createEffect(");
134
+ });
135
+ });