@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
@@ -0,0 +1,60 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transformDOM, transformSSR } from "./helpers/transform";
3
+
4
+ /**
5
+ * Compiled code calls the runtime by bare name. A user binding with the same
6
+ * name — `insert`, `spread`, `snapshot` — used to capture those calls.
7
+ */
8
+ describe("a user's binding named like a runtime function", () => {
9
+ const src = `
10
+ export default function C() {
11
+ const insert = (x) => x * 2;
12
+ const spread = 3;
13
+ let n = $state(0);
14
+ return <p onClick={() => n++}>{insert(spread)} {n}</p>;
15
+ }`;
16
+
17
+ test("is renamed, so generated calls reach the runtime", () => {
18
+ const out = transformDOM(src);
19
+ expect(out).toContain("const _insert = x => x * 2;");
20
+ expect(out).toMatch(/\binsert\(_el\d+, _insert\(_spread\)\)/);
21
+ expect(out).toMatch(
22
+ /import \{[^}]*\binsert\b[^}]*\} from "@sigil-dev\/runtime"/,
23
+ );
24
+ });
25
+
26
+ test("an exported one keeps its exported name", () => {
27
+ const out = transformDOM(`
28
+ export const snapshot = () => 1;
29
+ export default function C() { let n = $state(0); return <p>{n}</p>; }`);
30
+ expect(out).toMatch(
31
+ /export \{ _snapshot as snapshot \}|export const snapshot/,
32
+ );
33
+ expect(out).not.toMatch(/const snapshot = \(\) => 1[\s\S]*import/);
34
+ });
35
+
36
+ test("an import with that name from another package is aliased", () => {
37
+ const out = transformDOM(`
38
+ import { claim } from "./claims";
39
+ export default function C() { return <p>{claim()}</p>; }`);
40
+ expect(out).toContain('import { claim as _claim } from "./claims";');
41
+ expect(out).toContain("_claim()");
42
+ });
43
+
44
+ test("the runtime's own import is left as it is", () => {
45
+ const out = transformDOM(`
46
+ import { untrack } from "@sigil-dev/runtime";
47
+ export default function C() { let n = $state(0); return <p>{untrack(() => n)}</p>; }`);
48
+ expect(out).not.toContain("_untrack");
49
+ });
50
+
51
+ test("on the server too", () => {
52
+ const out = transformSSR(`
53
+ export default async function C() {
54
+ const getOwner = () => "mine";
55
+ await Promise.resolve();
56
+ return <p>{getOwner()}</p>;
57
+ }`);
58
+ expect(out).toContain("_getOwner");
59
+ });
60
+ });
@@ -1,166 +1,166 @@
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("several rules keep their own selectors (no text carried over)", () => {
32
- const css = `.book {\n\tdisplay: flex;\n}\n.read .title {\n\ttext-decoration: line-through;\n}\n.author { color: #666; }`;
33
- expect(scopeCSS(css, "h1")).toBe(
34
- `.book.h1 {\n\tdisplay: flex;\n}\n.read .title.h1 {\n\ttext-decoration: line-through;\n}\n.author.h1 { color: #666; }`,
35
- );
36
- });
37
-
38
- test("pseudo-classes, combinators and :is() lists", () => {
39
- expect(scopeCSS(".b:hover, .c > .d { x: 1 }", "h1")).toBe(
40
- ".b.h1:hover, .c > .d.h1 { x: 1 }",
41
- );
42
- expect(scopeCSS(":is(.a, .b) .c { x: 1 }", "h1")).toBe(
43
- ":is(.a, .b) .c.h1 { x: 1 }",
44
- );
45
- });
46
-
47
- test("@media is scoped inside; @keyframes and @import pass through", () => {
48
- expect(
49
- scopeCSS(
50
- "@media (max-width: 600px) { .a { x: 1 } } @keyframes spin { from { a: 1 } } @import url(x.css);",
51
- "h1",
52
- ),
53
- ).toBe(
54
- "@media (max-width: 600px) { .a.h1 { x: 1 } } @keyframes spin { from { a: 1 } } @import url(x.css);",
55
- );
56
- });
57
-
58
- test(":global() anywhere in a selector", () => {
59
- expect(scopeCSS(":global(.map) .pin { a: 1 }", "h1")).toBe(
60
- ".map .pin.h1 { a: 1 }",
61
- );
62
- expect(scopeCSS(".x :global(.y) { a: 1 }", "h1")).toBe(
63
- ".x.h1 .y { a: 1 }",
64
- );
65
- });
66
-
67
- test("appends hash to simple selector", () => {
68
- const result = scopeCSS(".card { color: red; }", "abc12345");
69
- expect(result).toContain(".card.abc12345");
70
- });
71
-
72
- test("handles multiple selectors", () => {
73
- const result = scopeCSS(".card, .button { color: red; }", "abc12345");
74
- expect(result).toContain(".card.abc12345");
75
- expect(result).toContain(".button.abc12345");
76
- });
77
-
78
- test(":global() escapes scoping", () => {
79
- const result = scopeCSS(":global(.external) { color: red; }", "abc12345");
80
- expect(result).toContain(".external");
81
- expect(result).not.toContain(".external.abc12345");
82
- });
83
-
84
- test(":global() with multiple selectors", () => {
85
- const result = scopeCSS(":global(.a, .b) { color: red; }", "abc12345");
86
- expect(result).toContain(".a");
87
- expect(result).toContain(".b");
88
- expect(result).not.toContain(".a.abc12345");
89
- expect(result).not.toContain(".b.abc12345");
90
- });
91
-
92
- test("handles nested braces", () => {
93
- const result = scopeCSS(".card { .nested { color: red; } }", "abc12345");
94
- expect(result).toContain(".card.abc12345");
95
- expect(result).toContain(".nested.abc12345");
96
- });
97
-
98
- test("preserves comments", () => {
99
- const result = scopeCSS(
100
- "/* comment */ .card { color: red; }",
101
- "abc12345",
102
- );
103
- expect(result).toContain("/* comment */");
104
- expect(result).toContain(".card.abc12345");
105
- });
106
-
107
- test("handles string literals", () => {
108
- const result = scopeCSS(
109
- '.card::before { content: ".card"; }',
110
- "abc12345",
111
- );
112
- // the class goes before the pseudo-element; after it is invalid CSS
113
- expect(result).toContain(".card.abc12345::before");
114
- expect(result).toContain('".card"'); // String literal preserved
115
- });
116
- });
117
-
118
- describe("Babel plugin integration", () => {
119
- test("classList.add(hash) on native elements", () => {
120
- const result = transform(`<div />`, "abc12345");
121
- expect(result).toContain('classList.add("abc12345")');
122
- });
123
-
124
- test("classList.add on nested elements", () => {
125
- const result = transform(`<div><span /></div>`, "abc12345");
126
- // Both div and span should get the hash
127
- const matches = result.match(/classList\.add\("abc12345"\)/g);
128
- expect(matches?.length).toBe(2);
129
- });
130
-
131
- test("components do NOT get classList.add", () => {
132
- const result = transform(`<Card />`, "abc12345");
133
- expect(result).not.toContain("classList.add");
134
- });
135
-
136
- test("no hash option = no classList.add", () => {
137
- const result = transform(`<div />`);
138
- expect(result).not.toContain("classList.add");
139
- });
140
-
141
- test("classList.add after createElement and attributes", () => {
142
- const result = transform(`<div class="foo" />`, "abc12345");
143
- // setProp(el, "className", "foo") should come before classList.add
144
- const classNameIdx = result.indexOf("setProp(");
145
- const classListIdx = result.indexOf('classList.add("abc12345")');
146
- expect(classNameIdx).toBeGreaterThan(0);
147
- expect(classListIdx).toBeGreaterThan(classNameIdx);
148
- });
149
-
150
- test("dynamic class preserves scope hash after update", () => {
151
- const result = transform(
152
- `
153
- let dynamicClass = $state("foo");
154
- const el = <div class={dynamicClass} />;
155
- `,
156
- "abc12345",
157
- );
158
- // Should use classList.value instead of className
159
- expect(result).toContain("classList.value = dynamicClass()");
160
- // Should re-add hash inside the effect
161
- expect(result).toContain('classList.add("abc12345")');
162
- // Should NOT use className assignment (which would wipe hash)
163
- expect(result).not.toContain("className = dynamicClass()");
164
- });
165
- });
166
- });
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("several rules keep their own selectors (no text carried over)", () => {
32
+ const css = `.book {\n\tdisplay: flex;\n}\n.read .title {\n\ttext-decoration: line-through;\n}\n.author { color: #666; }`;
33
+ expect(scopeCSS(css, "h1")).toBe(
34
+ `.book.h1 {\n\tdisplay: flex;\n}\n.read .title.h1 {\n\ttext-decoration: line-through;\n}\n.author.h1 { color: #666; }`,
35
+ );
36
+ });
37
+
38
+ test("pseudo-classes, combinators and :is() lists", () => {
39
+ expect(scopeCSS(".b:hover, .c > .d { x: 1 }", "h1")).toBe(
40
+ ".b.h1:hover, .c > .d.h1 { x: 1 }",
41
+ );
42
+ expect(scopeCSS(":is(.a, .b) .c { x: 1 }", "h1")).toBe(
43
+ ":is(.a, .b) .c.h1 { x: 1 }",
44
+ );
45
+ });
46
+
47
+ test("@media is scoped inside; @keyframes and @import pass through", () => {
48
+ expect(
49
+ scopeCSS(
50
+ "@media (max-width: 600px) { .a { x: 1 } } @keyframes spin { from { a: 1 } } @import url(x.css);",
51
+ "h1",
52
+ ),
53
+ ).toBe(
54
+ "@media (max-width: 600px) { .a.h1 { x: 1 } } @keyframes spin { from { a: 1 } } @import url(x.css);",
55
+ );
56
+ });
57
+
58
+ test(":global() anywhere in a selector", () => {
59
+ expect(scopeCSS(":global(.map) .pin { a: 1 }", "h1")).toBe(
60
+ ".map .pin.h1 { a: 1 }",
61
+ );
62
+ expect(scopeCSS(".x :global(.y) { a: 1 }", "h1")).toBe(
63
+ ".x.h1 .y { a: 1 }",
64
+ );
65
+ });
66
+
67
+ test("appends hash to simple selector", () => {
68
+ const result = scopeCSS(".card { color: red; }", "abc12345");
69
+ expect(result).toContain(".card.abc12345");
70
+ });
71
+
72
+ test("handles multiple selectors", () => {
73
+ const result = scopeCSS(".card, .button { color: red; }", "abc12345");
74
+ expect(result).toContain(".card.abc12345");
75
+ expect(result).toContain(".button.abc12345");
76
+ });
77
+
78
+ test(":global() escapes scoping", () => {
79
+ const result = scopeCSS(":global(.external) { color: red; }", "abc12345");
80
+ expect(result).toContain(".external");
81
+ expect(result).not.toContain(".external.abc12345");
82
+ });
83
+
84
+ test(":global() with multiple selectors", () => {
85
+ const result = scopeCSS(":global(.a, .b) { color: red; }", "abc12345");
86
+ expect(result).toContain(".a");
87
+ expect(result).toContain(".b");
88
+ expect(result).not.toContain(".a.abc12345");
89
+ expect(result).not.toContain(".b.abc12345");
90
+ });
91
+
92
+ test("handles nested braces", () => {
93
+ const result = scopeCSS(".card { .nested { color: red; } }", "abc12345");
94
+ expect(result).toContain(".card.abc12345");
95
+ expect(result).toContain(".nested.abc12345");
96
+ });
97
+
98
+ test("preserves comments", () => {
99
+ const result = scopeCSS(
100
+ "/* comment */ .card { color: red; }",
101
+ "abc12345",
102
+ );
103
+ expect(result).toContain("/* comment */");
104
+ expect(result).toContain(".card.abc12345");
105
+ });
106
+
107
+ test("handles string literals", () => {
108
+ const result = scopeCSS(
109
+ '.card::before { content: ".card"; }',
110
+ "abc12345",
111
+ );
112
+ // the class goes before the pseudo-element; after it is invalid CSS
113
+ expect(result).toContain(".card.abc12345::before");
114
+ expect(result).toContain('".card"'); // String literal preserved
115
+ });
116
+ });
117
+
118
+ describe("Babel plugin integration", () => {
119
+ test("classList.add(hash) on native elements", () => {
120
+ const result = transform(`<div />`, "abc12345");
121
+ expect(result).toContain('classList.add("abc12345")');
122
+ });
123
+
124
+ test("classList.add on nested elements", () => {
125
+ const result = transform(`<div><span /></div>`, "abc12345");
126
+ // Both div and span should get the hash
127
+ const matches = result.match(/classList\.add\("abc12345"\)/g);
128
+ expect(matches?.length).toBe(2);
129
+ });
130
+
131
+ test("components do NOT get classList.add", () => {
132
+ const result = transform(`<Card />`, "abc12345");
133
+ expect(result).not.toContain("classList.add");
134
+ });
135
+
136
+ test("no hash option = no classList.add", () => {
137
+ const result = transform(`<div />`);
138
+ expect(result).not.toContain("classList.add");
139
+ });
140
+
141
+ test("classList.add after createElement and attributes", () => {
142
+ const result = transform(`<div class="foo" />`, "abc12345");
143
+ // setProp(el, "className", "foo") should come before classList.add
144
+ const classNameIdx = result.indexOf("setProp(");
145
+ const classListIdx = result.indexOf('classList.add("abc12345")');
146
+ expect(classNameIdx).toBeGreaterThan(0);
147
+ expect(classListIdx).toBeGreaterThan(classNameIdx);
148
+ });
149
+
150
+ test("dynamic class preserves scope hash after update", () => {
151
+ const result = transform(
152
+ `
153
+ let dynamicClass = $state("foo");
154
+ const el = <div class={dynamicClass} />;
155
+ `,
156
+ "abc12345",
157
+ );
158
+ // Should use classList.value instead of className
159
+ expect(result).toContain("classList.value = dynamicClass()");
160
+ // Should re-add hash inside the effect
161
+ expect(result).toContain('classList.add("abc12345")');
162
+ // Should NOT use className assignment (which would wipe hash)
163
+ expect(result).not.toContain("className = dynamicClass()");
164
+ });
165
+ });
166
+ });
@@ -1,56 +1,56 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { mkdtempSync, writeFileSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import { transformSSR } from "./helpers/transform.ts";
6
-
7
- // Render through the real SSR output: write the compiled module to disk and
8
- // import it, so the assertions are on the HTML a browser would receive.
9
- const dir = mkdtempSync(join(tmpdir(), "sigil-ssr-bool-"));
10
- let n = 0;
11
- async function render(src: string, ...args: unknown[]): Promise<string> {
12
- const file = join(dir, `m${n++}.js`);
13
- writeFileSync(file, transformSSR(src));
14
- const mod = await import(file);
15
- return String(mod.default(...args));
16
- }
17
-
18
- describe("SSR boolean attributes", () => {
19
- const box = `export default (on) => <input type="checkbox" checked={on} />;`;
20
-
21
- test("true renders the bare attribute", async () => {
22
- expect(await render(box, true)).toBe(`<input type="checkbox" checked>`);
23
- });
24
-
25
- test("false, null and undefined omit it", async () => {
26
- // checked="" would mean checked: every box ticked, every option selected
27
- for (const v of [false, null, undefined]) {
28
- expect(await render(box, v)).toBe(`<input type="checkbox">`);
29
- }
30
- });
31
-
32
- test("selected, disabled and friends follow the same rule", async () => {
33
- const src = `export default (a, b) => <select disabled={a}><option selected={b}>x</option></select>;`;
34
- expect(await render(src, false, true)).toBe(
35
- `<select><option selected>x</option></select>`,
36
- );
37
- expect(await render(src, true, false)).toBe(
38
- `<select disabled><option>x</option></select>`,
39
- );
40
- });
41
-
42
- test("a string value is kept, escaped", async () => {
43
- expect(
44
- await render(`export default (v) => <div hidden={v} />;`, "until-found"),
45
- ).toBe(`<div hidden="until-found"></div>`);
46
- expect(
47
- await render(`export default (v) => <div hidden={v} />;`, `"><x`),
48
- ).toBe(`<div hidden="&quot;&gt;&lt;x"></div>`);
49
- });
50
-
51
- test("literal attributes are untouched", async () => {
52
- expect(
53
- await render(`export default () => <input checked="checked" />;`),
54
- ).toBe(`<input checked="checked">`);
55
- });
56
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { transformSSR } from "./helpers/transform.ts";
6
+
7
+ // Render through the real SSR output: write the compiled module to disk and
8
+ // import it, so the assertions are on the HTML a browser would receive.
9
+ const dir = mkdtempSync(join(tmpdir(), "sigil-ssr-bool-"));
10
+ let n = 0;
11
+ async function render(src: string, ...args: unknown[]): Promise<string> {
12
+ const file = join(dir, `m${n++}.js`);
13
+ writeFileSync(file, transformSSR(src));
14
+ const mod = await import(file);
15
+ return String(mod.default(...args));
16
+ }
17
+
18
+ describe("SSR boolean attributes", () => {
19
+ const box = `export default (on) => <input type="checkbox" checked={on} />;`;
20
+
21
+ test("true renders the bare attribute", async () => {
22
+ expect(await render(box, true)).toBe(`<input type="checkbox" checked>`);
23
+ });
24
+
25
+ test("false, null and undefined omit it", async () => {
26
+ // checked="" would mean checked: every box ticked, every option selected
27
+ for (const v of [false, null, undefined]) {
28
+ expect(await render(box, v)).toBe(`<input type="checkbox">`);
29
+ }
30
+ });
31
+
32
+ test("selected, disabled and friends follow the same rule", async () => {
33
+ const src = `export default (a, b) => <select disabled={a}><option selected={b}>x</option></select>;`;
34
+ expect(await render(src, false, true)).toBe(
35
+ `<select><option selected>x</option></select>`,
36
+ );
37
+ expect(await render(src, true, false)).toBe(
38
+ `<select disabled><option>x</option></select>`,
39
+ );
40
+ });
41
+
42
+ test("a string value is kept, escaped", async () => {
43
+ expect(
44
+ await render(`export default (v) => <div hidden={v} />;`, "until-found"),
45
+ ).toBe(`<div hidden="until-found"></div>`);
46
+ expect(
47
+ await render(`export default (v) => <div hidden={v} />;`, `"><x`),
48
+ ).toBe(`<div hidden="&quot;&gt;&lt;x"></div>`);
49
+ });
50
+
51
+ test("literal attributes are untouched", async () => {
52
+ expect(
53
+ await render(`export default () => <input checked="checked" />;`),
54
+ ).toBe(`<input checked="checked">`);
55
+ });
56
+ });
@@ -1,68 +1,68 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { mkdtempSync, writeFileSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
- import { transformSync } from "@babel/core";
6
- import sigilPlugin from "../src/babel/index.ts";
7
- import { transformSSR } from "./helpers/transform.ts";
8
-
9
- // Real SSR output: compile, write to disk, import, render.
10
- const dir = mkdtempSync(join(tmpdir(), "sigil-ssr-raw-"));
11
- let n = 0;
12
- async function render(src: string, ...args: unknown[]): Promise<string> {
13
- const file = join(dir, `m${n++}.js`);
14
- writeFileSync(file, transformSSR(src));
15
- const mod = await import(file);
16
- return String(mod.default(...args));
17
- }
18
-
19
- const CODE = `if (a > 1 && b < 2) x = "q";`;
20
- // "<!-" + "-": a literal comment opener is a legacy HTML comment in JS source
21
- const G_OPEN = "<!-" + "-g-->";
22
- const G_CLOSE = "<!-" + "-/g-->";
23
-
24
- describe("SSR script text", () => {
25
- test("{code} inside a script is emitted as is", async () => {
26
- expect(
27
- await render("export default (c) => <script>{c}</script>;", CODE),
28
- ).toBe(`<script>${CODE}</script>`);
29
- });
30
-
31
- test("the code can't close its own element", async () => {
32
- expect(
33
- await render(
34
- "export default (c) => <script>{c}</script>;",
35
- 'x = "</SCRIPT>"',
36
- ),
37
- ).toBe('<script>x = "<\\/SCRIPT>"</script>');
38
- });
39
-
40
- test("null, undefined and false render nothing", async () => {
41
- for (const v of [null, undefined, false]) {
42
- expect(
43
- await render("export default (c) => <script>{c}</script>;", v),
44
- ).toBe("<script></script>");
45
- }
46
- });
47
-
48
- test("other elements still escape and mark their text", async () => {
49
- expect(await render("export default (c) => <p>{c}</p>;", "<b>")).toBe(
50
- `<p>${G_OPEN}&lt;b&gt;${G_CLOSE}</p>`,
51
- );
52
- });
53
-
54
- test("hydration leaves script text alone", () => {
55
- const code = transformSync(
56
- "const el = <div><script>{code}</script><p>{text}</p></div>;",
57
- {
58
- plugins: [
59
- "@babel/plugin-syntax-jsx",
60
- [sigilPlugin, { mode: "hydrate" }],
61
- ],
62
- filename: "test.tsx",
63
- },
64
- )!.code!;
65
- expect(code.match(/hydrateSlot\(/g)?.length).toBe(1); // only the <p> slot
66
- expect(code).not.toContain("() => code");
67
- });
68
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { transformSync } from "@babel/core";
6
+ import sigilPlugin from "../src/babel/index.ts";
7
+ import { transformSSR } from "./helpers/transform.ts";
8
+
9
+ // Real SSR output: compile, write to disk, import, render.
10
+ const dir = mkdtempSync(join(tmpdir(), "sigil-ssr-raw-"));
11
+ let n = 0;
12
+ async function render(src: string, ...args: unknown[]): Promise<string> {
13
+ const file = join(dir, `m${n++}.js`);
14
+ writeFileSync(file, transformSSR(src));
15
+ const mod = await import(file);
16
+ return String(mod.default(...args));
17
+ }
18
+
19
+ const CODE = `if (a > 1 && b < 2) x = "q";`;
20
+ // "<!-" + "-": a literal comment opener is a legacy HTML comment in JS source
21
+ const G_OPEN = "<!-" + "-g-->";
22
+ const G_CLOSE = "<!-" + "-/g-->";
23
+
24
+ describe("SSR script text", () => {
25
+ test("{code} inside a script is emitted as is", async () => {
26
+ expect(
27
+ await render("export default (c) => <script>{c}</script>;", CODE),
28
+ ).toBe(`<script>${CODE}</script>`);
29
+ });
30
+
31
+ test("the code can't close its own element", async () => {
32
+ expect(
33
+ await render(
34
+ "export default (c) => <script>{c}</script>;",
35
+ 'x = "</SCRIPT>"',
36
+ ),
37
+ ).toBe('<script>x = "<\\/SCRIPT>"</script>');
38
+ });
39
+
40
+ test("null, undefined and false render nothing", async () => {
41
+ for (const v of [null, undefined, false]) {
42
+ expect(
43
+ await render("export default (c) => <script>{c}</script>;", v),
44
+ ).toBe("<script></script>");
45
+ }
46
+ });
47
+
48
+ test("other elements still escape and mark their text", async () => {
49
+ expect(await render("export default (c) => <p>{c}</p>;", "<b>")).toBe(
50
+ `<p>${G_OPEN}&lt;b&gt;${G_CLOSE}</p>`,
51
+ );
52
+ });
53
+
54
+ test("hydration leaves script text alone", () => {
55
+ const code = transformSync(
56
+ "const el = <div><script>{code}</script><p>{text}</p></div>;",
57
+ {
58
+ plugins: [
59
+ "@babel/plugin-syntax-jsx",
60
+ [sigilPlugin, { mode: "hydrate" }],
61
+ ],
62
+ filename: "test.tsx",
63
+ },
64
+ )!.code!;
65
+ expect(code.match(/hydrateSlot\(/g)?.length).toBe(1); // only the <p> slot
66
+ expect(code).not.toContain("() => code");
67
+ });
68
+ });