@sigil-dev/compiler 0.9.3 → 0.9.5

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/index.ts +4 -4
  2. package/jsr.json +8 -8
  3. package/package.json +9 -8
  4. package/src/babel/handlers/dom/derived.ts +62 -62
  5. package/src/babel/handlers/dom/effect.ts +15 -15
  6. package/src/babel/handlers/dom/globals.ts +105 -105
  7. package/src/babel/handlers/dom/state.ts +107 -107
  8. package/src/babel/handlers/ssr/derived.ts +27 -27
  9. package/src/babel/handlers/ssr/state.ts +11 -11
  10. package/src/babel/index.ts +13 -0
  11. package/src/babel/jsx/anchor-mount.ts +385 -389
  12. package/src/babel/jsx/children.ts +124 -124
  13. package/src/babel/jsx/element.ts +848 -831
  14. package/src/babel/jsx/fragment.ts +282 -282
  15. package/src/babel/jsx/index.ts +2 -2
  16. package/src/babel/jsx/keyed-list.ts +309 -278
  17. package/src/babel/jsx/ssr.ts +351 -319
  18. package/src/babel/jsx/text-node.ts +192 -192
  19. package/src/babel/jsx/utils.ts +179 -179
  20. package/src/babel/util/bind.ts +139 -139
  21. package/src/babel/util/css.ts +151 -151
  22. package/src/babel/util/dead-code.ts +28 -28
  23. package/src/babel/util/helpers.ts +46 -46
  24. package/src/babel/util/magic.ts +11 -11
  25. package/src/bun-plugin.ts +92 -92
  26. package/src/vite/index.ts +58 -58
  27. package/test/async-components.test.ts +59 -59
  28. package/test/components.test.ts +129 -129
  29. package/test/containsSignal.test.ts +288 -288
  30. package/test/fragments.test.ts +90 -90
  31. package/test/helpers/transform.ts +45 -45
  32. package/test/hydration.test.ts +147 -147
  33. package/test/jsx.test.ts +227 -197
  34. package/test/keyed-lists.test.ts +126 -88
  35. package/test/reactivity.test.ts +225 -225
  36. package/test/scoped-css.test.ts +129 -129
  37. package/test/ssr.test.ts +130 -130
  38. package/test/style-object.test.ts +13 -0
  39. package/test/tree-shaking.test.ts +78 -78
  40. package/tsconfig.json +30 -30
@@ -1,129 +1,129 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { computeHash, scopeCSS } from "../src/babel/util/css.ts";
3
- import { transform } from "./helpers/transform.ts";
4
-
5
- describe("Scoped CSS", () => {
6
- describe("computeHash", () => {
7
- test("deterministic from file path", () => {
8
- const hash1 = computeHash("/src/components/App.tsx");
9
- const hash2 = computeHash("/src/components/App.tsx");
10
- expect(hash1).toBe(hash2);
11
- });
12
-
13
- test("different files get different hashes", () => {
14
- const hash1 = computeHash("/src/components/App.tsx");
15
- const hash2 = computeHash("/src/components/Button.tsx");
16
- expect(hash1).not.toBe(hash2);
17
- });
18
-
19
- test("starts with 's' prefix", () => {
20
- const hash = computeHash("/src/App.tsx");
21
- expect(hash.startsWith("s")).toBe(true);
22
- });
23
-
24
- test("8 characters after prefix", () => {
25
- const hash = computeHash("/src/App.tsx");
26
- expect(hash.length).toBe(9); // 's' + 8 hex chars
27
- });
28
- });
29
-
30
- describe("scopeCSS", () => {
31
- test("appends hash to simple selector", () => {
32
- const result = scopeCSS(".card { color: red; }", "abc12345");
33
- expect(result).toContain(".card.abc12345");
34
- });
35
-
36
- test("handles multiple selectors", () => {
37
- const result = scopeCSS(".card, .button { color: red; }", "abc12345");
38
- expect(result).toContain(".card.abc12345");
39
- expect(result).toContain(".button.abc12345");
40
- });
41
-
42
- test(":global() escapes scoping", () => {
43
- const result = scopeCSS(":global(.external) { color: red; }", "abc12345");
44
- expect(result).toContain(".external");
45
- expect(result).not.toContain(".external.abc12345");
46
- });
47
-
48
- test(":global() with multiple selectors", () => {
49
- const result = scopeCSS(":global(.a, .b) { color: red; }", "abc12345");
50
- expect(result).toContain(".a");
51
- expect(result).toContain(".b");
52
- expect(result).not.toContain(".a.abc12345");
53
- expect(result).not.toContain(".b.abc12345");
54
- });
55
-
56
- test("handles nested braces", () => {
57
- const result = scopeCSS(".card { .nested { color: red; } }", "abc12345");
58
- expect(result).toContain(".card.abc12345");
59
- expect(result).toContain(".nested.abc12345");
60
- });
61
-
62
- test("preserves comments", () => {
63
- const result = scopeCSS(
64
- "/* comment */ .card { color: red; }",
65
- "abc12345",
66
- );
67
- expect(result).toContain("/* comment */");
68
- expect(result).toContain(".card.abc12345");
69
- });
70
-
71
- test("handles string literals", () => {
72
- const result = scopeCSS(
73
- '.card::before { content: ".card"; }',
74
- "abc12345",
75
- );
76
- expect(result).toContain(".card::before.abc12345");
77
- expect(result).toContain('".card"'); // String literal preserved
78
- });
79
- });
80
-
81
- describe("Babel plugin integration", () => {
82
- test("classList.add(hash) on native elements", () => {
83
- const result = transform(`<div />`, "abc12345");
84
- expect(result).toContain('classList.add("abc12345")');
85
- });
86
-
87
- test("classList.add on nested elements", () => {
88
- const result = transform(`<div><span /></div>`, "abc12345");
89
- // Both div and span should get the hash
90
- const matches = result.match(/classList\.add\("abc12345"\)/g);
91
- expect(matches?.length).toBe(2);
92
- });
93
-
94
- test("components do NOT get classList.add", () => {
95
- const result = transform(`<Card />`, "abc12345");
96
- expect(result).not.toContain("classList.add");
97
- });
98
-
99
- test("no hash option = no classList.add", () => {
100
- const result = transform(`<div />`);
101
- expect(result).not.toContain("classList.add");
102
- });
103
-
104
- test("classList.add after createElement and attributes", () => {
105
- const result = transform(`<div class="foo" />`, "abc12345");
106
- // className = "foo" should come before classList.add
107
- const classNameIdx = result.indexOf('.className = "foo"');
108
- const classListIdx = result.indexOf('classList.add("abc12345")');
109
- expect(classNameIdx).toBeGreaterThan(0);
110
- expect(classListIdx).toBeGreaterThan(classNameIdx);
111
- });
112
-
113
- test("dynamic class preserves scope hash after update", () => {
114
- const result = transform(
115
- `
116
- let dynamicClass = $state("foo");
117
- const el = <div class={dynamicClass} />;
118
- `,
119
- "abc12345",
120
- );
121
- // Should use classList.value instead of className
122
- expect(result).toContain("classList.value = dynamicClass()");
123
- // Should re-add hash inside the effect
124
- expect(result).toContain('classList.add("abc12345")');
125
- // Should NOT use className assignment (which would wipe hash)
126
- expect(result).not.toContain("className = dynamicClass()");
127
- });
128
- });
129
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { computeHash, scopeCSS } from "../src/babel/util/css.ts";
3
+ import { transform } from "./helpers/transform.ts";
4
+
5
+ describe("Scoped CSS", () => {
6
+ describe("computeHash", () => {
7
+ test("deterministic from file path", () => {
8
+ const hash1 = computeHash("/src/components/App.tsx");
9
+ const hash2 = computeHash("/src/components/App.tsx");
10
+ expect(hash1).toBe(hash2);
11
+ });
12
+
13
+ test("different files get different hashes", () => {
14
+ const hash1 = computeHash("/src/components/App.tsx");
15
+ const hash2 = computeHash("/src/components/Button.tsx");
16
+ expect(hash1).not.toBe(hash2);
17
+ });
18
+
19
+ test("starts with 's' prefix", () => {
20
+ const hash = computeHash("/src/App.tsx");
21
+ expect(hash.startsWith("s")).toBe(true);
22
+ });
23
+
24
+ test("8 characters after prefix", () => {
25
+ const hash = computeHash("/src/App.tsx");
26
+ expect(hash.length).toBe(9); // 's' + 8 hex chars
27
+ });
28
+ });
29
+
30
+ describe("scopeCSS", () => {
31
+ test("appends hash to simple selector", () => {
32
+ const result = scopeCSS(".card { color: red; }", "abc12345");
33
+ expect(result).toContain(".card.abc12345");
34
+ });
35
+
36
+ test("handles multiple selectors", () => {
37
+ const result = scopeCSS(".card, .button { color: red; }", "abc12345");
38
+ expect(result).toContain(".card.abc12345");
39
+ expect(result).toContain(".button.abc12345");
40
+ });
41
+
42
+ test(":global() escapes scoping", () => {
43
+ const result = scopeCSS(":global(.external) { color: red; }", "abc12345");
44
+ expect(result).toContain(".external");
45
+ expect(result).not.toContain(".external.abc12345");
46
+ });
47
+
48
+ test(":global() with multiple selectors", () => {
49
+ const result = scopeCSS(":global(.a, .b) { color: red; }", "abc12345");
50
+ expect(result).toContain(".a");
51
+ expect(result).toContain(".b");
52
+ expect(result).not.toContain(".a.abc12345");
53
+ expect(result).not.toContain(".b.abc12345");
54
+ });
55
+
56
+ test("handles nested braces", () => {
57
+ const result = scopeCSS(".card { .nested { color: red; } }", "abc12345");
58
+ expect(result).toContain(".card.abc12345");
59
+ expect(result).toContain(".nested.abc12345");
60
+ });
61
+
62
+ test("preserves comments", () => {
63
+ const result = scopeCSS(
64
+ "/* comment */ .card { color: red; }",
65
+ "abc12345",
66
+ );
67
+ expect(result).toContain("/* comment */");
68
+ expect(result).toContain(".card.abc12345");
69
+ });
70
+
71
+ test("handles string literals", () => {
72
+ const result = scopeCSS(
73
+ '.card::before { content: ".card"; }',
74
+ "abc12345",
75
+ );
76
+ expect(result).toContain(".card::before.abc12345");
77
+ expect(result).toContain('".card"'); // String literal preserved
78
+ });
79
+ });
80
+
81
+ describe("Babel plugin integration", () => {
82
+ test("classList.add(hash) on native elements", () => {
83
+ const result = transform(`<div />`, "abc12345");
84
+ expect(result).toContain('classList.add("abc12345")');
85
+ });
86
+
87
+ test("classList.add on nested elements", () => {
88
+ const result = transform(`<div><span /></div>`, "abc12345");
89
+ // Both div and span should get the hash
90
+ const matches = result.match(/classList\.add\("abc12345"\)/g);
91
+ expect(matches?.length).toBe(2);
92
+ });
93
+
94
+ test("components do NOT get classList.add", () => {
95
+ const result = transform(`<Card />`, "abc12345");
96
+ expect(result).not.toContain("classList.add");
97
+ });
98
+
99
+ test("no hash option = no classList.add", () => {
100
+ const result = transform(`<div />`);
101
+ expect(result).not.toContain("classList.add");
102
+ });
103
+
104
+ test("classList.add after createElement and attributes", () => {
105
+ const result = transform(`<div class="foo" />`, "abc12345");
106
+ // className = "foo" should come before classList.add
107
+ const classNameIdx = result.indexOf('.className = "foo"');
108
+ const classListIdx = result.indexOf('classList.add("abc12345")');
109
+ expect(classNameIdx).toBeGreaterThan(0);
110
+ expect(classListIdx).toBeGreaterThan(classNameIdx);
111
+ });
112
+
113
+ test("dynamic class preserves scope hash after update", () => {
114
+ const result = transform(
115
+ `
116
+ let dynamicClass = $state("foo");
117
+ const el = <div class={dynamicClass} />;
118
+ `,
119
+ "abc12345",
120
+ );
121
+ // Should use classList.value instead of className
122
+ expect(result).toContain("classList.value = dynamicClass()");
123
+ // Should re-add hash inside the effect
124
+ expect(result).toContain('classList.add("abc12345")');
125
+ // Should NOT use className assignment (which would wipe hash)
126
+ expect(result).not.toContain("className = dynamicClass()");
127
+ });
128
+ });
129
+ });
package/test/ssr.test.ts CHANGED
@@ -1,130 +1,130 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { transformSSR } from "./helpers/transform.ts";
3
-
4
- describe("SSR", () => {
5
- describe("$state", () => {
6
- test("becomes plain let", () => {
7
- const result = transformSSR(`let count = $state(0);`);
8
- expect(result).toContain("let count = 0");
9
- expect(result).not.toContain("createSignal");
10
- });
11
-
12
- test("mutations stay as mutations", () => {
13
- const result = transformSSR(`let count = $state(0); count++;`);
14
- expect(result).toContain("let count = 0");
15
- expect(result).toContain("count++");
16
- });
17
- });
18
-
19
- describe("$derived", () => {
20
- test("becomes arrow function", () => {
21
- const result = transformSSR(
22
- `let count = $state(0); let doubled = $derived(count * 2);`,
23
- );
24
- expect(result).toContain("const doubled = () => count * 2");
25
- });
26
- });
27
-
28
- describe("$effect", () => {
29
- test("is dropped entirely", () => {
30
- const result = transformSSR(`$effect(() => { console.log("hi"); });`);
31
- expect(result).not.toContain("createEffect");
32
- expect(result).not.toContain("console.log");
33
- });
34
- });
35
-
36
- describe("JSX", () => {
37
- test("static element", () => {
38
- const result = transformSSR(`const el = <div />;`);
39
- expect(result).toContain("<div>");
40
- expect(result).toContain("</div>");
41
- });
42
-
43
- test("static element with class", () => {
44
- const result = transformSSR(`const el = <div class="app" />;`);
45
- expect(result).toContain('class="app"');
46
- expect(result).not.toContain("className");
47
- });
48
-
49
- test("void element self closes", () => {
50
- const result = transformSSR(`const el = <input type="text" />;`);
51
- expect(result).toContain("<input");
52
- expect(result).not.toContain("</input>");
53
- });
54
-
55
- test("static text child", () => {
56
- const result = transformSSR(`const el = <div>hello</div>;`);
57
- expect(result).toContain("<div>");
58
- expect(result).toContain("hello");
59
- expect(result).toContain("</div>");
60
- });
61
-
62
- test("dynamic child is escaped", () => {
63
- const result = transformSSR(`
64
- let count = $state(0);
65
- const el = <div>{count}</div>;
66
- `);
67
- expect(result).toContain("__e(");
68
- expect(result).toContain("</div>");
69
- });
70
-
71
- test("event handlers dropped", () => {
72
- const result = transformSSR(
73
- `const el = <button onClick={() => {}}>click</button>;`,
74
- );
75
- expect(result).not.toContain("addEventListener");
76
- expect(result).not.toContain("onClick");
77
- });
78
-
79
- test("nested elements", () => {
80
- const result = transformSSR(`const el = <div><span>hi</span></div>;`);
81
- expect(result).toContain("<div>");
82
- expect(result).toContain("<span>");
83
- expect(result).toContain("hi");
84
- expect(result).toContain("</span>");
85
- expect(result).toContain("</div>");
86
- });
87
-
88
- test("component called as function returning string", () => {
89
- const result = transformSSR(`const el = <Card title="hi" />;`);
90
- expect(result).toContain("Card(");
91
- expect(result).toContain('title: "hi"');
92
- expect(result).not.toContain("createElement");
93
- });
94
-
95
- test("component children passed as string", () => {
96
- const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
97
- expect(result).toContain("Card(");
98
- expect(result).toContain("children:");
99
- expect(result).toContain("<p>");
100
- });
101
-
102
- test("runtime import is escape only", () => {
103
- const result = transformSSR(
104
- `const name = $state("John"); const el = <div>{name}</div>;`,
105
- );
106
- expect(result).toContain("__e(");
107
- expect(result).not.toContain("createSignal");
108
- expect(result).not.toContain("createEffect");
109
- });
110
-
111
- test("innerHTML passes through unsanitized", () => {
112
- const result = transformSSR(
113
- `const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
114
- );
115
- expect(result).toContain("html");
116
- // innerHTML wraps in __h() for SafeHtml, not __e() for escaping
117
- expect(result).toContain("__h(");
118
- expect(result).not.toMatch(/__e\([^)]*html/);
119
- });
120
-
121
- test("keyed list converts key to data-key", () => {
122
- const result = transformSSR(
123
- `let items = $state([{id: 1, text: "A"}]);
124
- const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
125
- );
126
- expect(result).toContain("data-key");
127
- expect(result).not.toContain('"key"');
128
- });
129
- });
130
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformSSR } from "./helpers/transform.ts";
3
+
4
+ describe("SSR", () => {
5
+ describe("$state", () => {
6
+ test("becomes plain let", () => {
7
+ const result = transformSSR(`let count = $state(0);`);
8
+ expect(result).toContain("let count = 0");
9
+ expect(result).not.toContain("createSignal");
10
+ });
11
+
12
+ test("mutations stay as mutations", () => {
13
+ const result = transformSSR(`let count = $state(0); count++;`);
14
+ expect(result).toContain("let count = 0");
15
+ expect(result).toContain("count++");
16
+ });
17
+ });
18
+
19
+ describe("$derived", () => {
20
+ test("becomes arrow function", () => {
21
+ const result = transformSSR(
22
+ `let count = $state(0); let doubled = $derived(count * 2);`,
23
+ );
24
+ expect(result).toContain("const doubled = () => count * 2");
25
+ });
26
+ });
27
+
28
+ describe("$effect", () => {
29
+ test("is dropped entirely", () => {
30
+ const result = transformSSR(`$effect(() => { console.log("hi"); });`);
31
+ expect(result).not.toContain("createEffect");
32
+ expect(result).not.toContain("console.log");
33
+ });
34
+ });
35
+
36
+ describe("JSX", () => {
37
+ test("static element", () => {
38
+ const result = transformSSR(`const el = <div />;`);
39
+ expect(result).toContain("<div>");
40
+ expect(result).toContain("</div>");
41
+ });
42
+
43
+ test("static element with class", () => {
44
+ const result = transformSSR(`const el = <div class="app" />;`);
45
+ expect(result).toContain('class="app"');
46
+ expect(result).not.toContain("className");
47
+ });
48
+
49
+ test("void element self closes", () => {
50
+ const result = transformSSR(`const el = <input type="text" />;`);
51
+ expect(result).toContain("<input");
52
+ expect(result).not.toContain("</input>");
53
+ });
54
+
55
+ test("static text child", () => {
56
+ const result = transformSSR(`const el = <div>hello</div>;`);
57
+ expect(result).toContain("<div>");
58
+ expect(result).toContain("hello");
59
+ expect(result).toContain("</div>");
60
+ });
61
+
62
+ test("dynamic child is escaped", () => {
63
+ const result = transformSSR(`
64
+ let count = $state(0);
65
+ const el = <div>{count}</div>;
66
+ `);
67
+ expect(result).toContain("__e(");
68
+ expect(result).toContain("</div>");
69
+ });
70
+
71
+ test("event handlers dropped", () => {
72
+ const result = transformSSR(
73
+ `const el = <button onClick={() => {}}>click</button>;`,
74
+ );
75
+ expect(result).not.toContain("addEventListener");
76
+ expect(result).not.toContain("onClick");
77
+ });
78
+
79
+ test("nested elements", () => {
80
+ const result = transformSSR(`const el = <div><span>hi</span></div>;`);
81
+ expect(result).toContain("<div>");
82
+ expect(result).toContain("<span>");
83
+ expect(result).toContain("hi");
84
+ expect(result).toContain("</span>");
85
+ expect(result).toContain("</div>");
86
+ });
87
+
88
+ test("component called as function returning string", () => {
89
+ const result = transformSSR(`const el = <Card title="hi" />;`);
90
+ expect(result).toContain("Card(");
91
+ expect(result).toContain('title: "hi"');
92
+ expect(result).not.toContain("createElement");
93
+ });
94
+
95
+ test("component children passed as string", () => {
96
+ const result = transformSSR(`const el = <Card><p>body</p></Card>;`);
97
+ expect(result).toContain("Card(");
98
+ expect(result).toContain("children:");
99
+ expect(result).toContain("<p>");
100
+ });
101
+
102
+ test("runtime import is escape only", () => {
103
+ const result = transformSSR(
104
+ `const name = $state("John"); const el = <div>{name}</div>;`,
105
+ );
106
+ expect(result).toContain("__e(");
107
+ expect(result).not.toContain("createSignal");
108
+ expect(result).not.toContain("createEffect");
109
+ });
110
+
111
+ test("innerHTML passes through unsanitized", () => {
112
+ const result = transformSSR(
113
+ `const html = "<b>bold</b>"; const el = <div innerHTML={html}></div>;`,
114
+ );
115
+ expect(result).toContain("html");
116
+ // innerHTML wraps in __h() for SafeHtml, not __e() for escaping
117
+ expect(result).toContain("__h(");
118
+ expect(result).not.toMatch(/__e\([^)]*html/);
119
+ });
120
+
121
+ test("keyed list converts key to data-key", () => {
122
+ const result = transformSSR(
123
+ `let items = $state([{id: 1, text: "A"}]);
124
+ const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;`,
125
+ );
126
+ expect(result).toContain("data-key");
127
+ expect(result).not.toContain('"key"');
128
+ });
129
+ });
130
+ });
@@ -0,0 +1,13 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformSSR } from "./helpers/transform.ts";
3
+
4
+ describe("SSR style objects", () => {
5
+ test("style={{...}} serializes to CSS string, not [object Object]", () => {
6
+ const out = transformSSR(
7
+ `export function Card() { return <div style={{ display: "grid", marginTop: 12 }}>x</div>; }`,
8
+ );
9
+ // the __e helper must carry the object branch
10
+ expect(out).toContain("constructor === Object");
11
+ expect(out).toContain("UNITLESS");
12
+ });
13
+ });