@sigil-dev/compiler 0.9.21 → 0.9.23

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.
@@ -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
+ });