@sigil-dev/compiler 0.9.4 → 0.9.6
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.
- package/index.ts +4 -4
- package/jsr.json +8 -8
- package/package.json +2 -1
- package/src/babel/handlers/dom/derived.ts +62 -62
- package/src/babel/handlers/dom/effect.ts +15 -15
- package/src/babel/handlers/dom/globals.ts +105 -105
- package/src/babel/handlers/dom/state.ts +107 -107
- package/src/babel/handlers/ssr/derived.ts +27 -27
- package/src/babel/handlers/ssr/state.ts +11 -11
- package/src/babel/index.ts +9 -0
- package/src/babel/jsx/anchor-mount.ts +24 -1
- package/src/babel/jsx/children.ts +53 -69
- package/src/babel/jsx/element.ts +76 -50
- package/src/babel/jsx/fragment.ts +8 -31
- package/src/babel/jsx/index.ts +2 -2
- package/src/babel/jsx/keyed-list.ts +309 -309
- package/src/babel/jsx/ssr.ts +351 -324
- package/src/babel/jsx/text-node.ts +192 -192
- package/src/babel/jsx/utils.ts +179 -179
- package/src/babel/util/bind.ts +139 -139
- package/src/babel/util/css.ts +151 -151
- package/src/babel/util/dead-code.ts +28 -28
- package/src/babel/util/helpers.ts +46 -46
- package/src/babel/util/magic.ts +11 -11
- package/src/bun-plugin.ts +92 -92
- package/src/vite/index.ts +58 -58
- package/test/async-components.test.ts +59 -59
- package/test/components.test.ts +129 -129
- package/test/containsSignal.test.ts +288 -288
- package/test/fragments.test.ts +90 -90
- package/test/helpers/transform.ts +45 -45
- package/test/hydration.test.ts +130 -1
- package/test/jsx.test.ts +227 -225
- package/test/keyed-lists.test.ts +126 -126
- package/test/reactivity.test.ts +225 -225
- package/test/scoped-css.test.ts +129 -129
- package/test/ssr.test.ts +130 -130
- package/test/style-object.test.ts +13 -0
- package/test/tree-shaking.test.ts +78 -78
- package/tsconfig.json +30 -30
package/test/keyed-lists.test.ts
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import * as runtime from "../../runtime/index.ts";
|
|
3
|
-
import { transform } from "./helpers/transform.ts";
|
|
4
|
-
|
|
5
|
-
describe("Keyed Lists", () => {
|
|
6
|
-
test("detects .map() with key prop", () => {
|
|
7
|
-
const result = transform(`
|
|
8
|
-
let items = $state([{ id: 1 }]);
|
|
9
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
10
|
-
`);
|
|
11
|
-
expect(result).toContain("reconcile(");
|
|
12
|
-
expect(result).toContain("new Map()");
|
|
13
|
-
expect(result).not.toContain("_mountFn");
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("key prop is stripped from DOM output", () => {
|
|
17
|
-
const result = transform(`
|
|
18
|
-
let items = $state([{ id: 1 }]);
|
|
19
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
20
|
-
`);
|
|
21
|
-
expect(result).not.toContain(".key =");
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test("key extractor is arrow function of key prop", () => {
|
|
25
|
-
const result = transform(`
|
|
26
|
-
let items = $state([{ id: 1 }]);
|
|
27
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
28
|
-
`);
|
|
29
|
-
// The key extractor should be: item => item.id
|
|
30
|
-
expect(result).toMatch(/i => i\.id/);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("createNode factory contains JSX element creation", () => {
|
|
34
|
-
const result = transform(`
|
|
35
|
-
let items = $state([{ id: 1, text: "a" }]);
|
|
36
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
37
|
-
`);
|
|
38
|
-
expect(result).toContain('document.createElement("li")');
|
|
39
|
-
expect(result).toContain("i.text");
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("signal expression is passed to reconcile", () => {
|
|
43
|
-
const result = transform(`
|
|
44
|
-
let items = $state([1, 2, 3]);
|
|
45
|
-
const el = <ul>{items.map(i => <li key={i}>{i}</li>)}</ul>;
|
|
46
|
-
`);
|
|
47
|
-
// The signal accessor should be passed as the items argument
|
|
48
|
-
expect(result).toMatch(/reconcile\([^,]+, [^,]+, items\(\)/);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test("anchor and keyMap are created", () => {
|
|
52
|
-
const result = transform(`
|
|
53
|
-
let items = $state([{ id: 1 }]);
|
|
54
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
55
|
-
`);
|
|
56
|
-
expect(result).toContain("document.createComment");
|
|
57
|
-
expect(result).toContain("new Map()");
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("effect wraps reconcile call", () => {
|
|
61
|
-
const result = transform(`
|
|
62
|
-
let items = $state([{ id: 1 }]);
|
|
63
|
-
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
64
|
-
`);
|
|
65
|
-
expect(result).toContain("createEffect(");
|
|
66
|
-
expect(result).toContain("reconcile(");
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("unkeyed .map() reconciles with index keys", () => {
|
|
70
|
-
const result = transform(`
|
|
71
|
-
let items = $state([1, 2, 3]);
|
|
72
|
-
const el = <ul>{items.map(i => <li>{i}</li>)}</ul>;
|
|
73
|
-
`);
|
|
74
|
-
// No key prop → still reconcile, but keyed by position
|
|
75
|
-
expect(result).toContain("reconcile(");
|
|
76
|
-
expect(result).toContain("(_item, _i) => _i");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("unkeyed map preserves DOM nodes on shift (behavioral)", async () => {
|
|
80
|
-
// minimal structural view of the DOM the compiled code touches — this
|
|
81
|
-
// package's compiler options have no DOM lib
|
|
82
|
-
type TextLike = { textContent: string };
|
|
83
|
-
type ListEl = {
|
|
84
|
-
querySelector(selectors: string): { firstChild: TextLike | null } | null;
|
|
85
|
-
querySelectorAll(selectors: string): { length: number };
|
|
86
|
-
};
|
|
87
|
-
const src = `
|
|
88
|
-
let items = $state(["a", "b", "c"]);
|
|
89
|
-
const el = <ul>{items.map(i => <li>{i}</li>)}</ul>;
|
|
90
|
-
`;
|
|
91
|
-
const code = transform(src);
|
|
92
|
-
const stripped = code.replace(
|
|
93
|
-
/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g,
|
|
94
|
-
"",
|
|
95
|
-
);
|
|
96
|
-
const factory = new Function(
|
|
97
|
-
"document",
|
|
98
|
-
...Object.keys(runtime),
|
|
99
|
-
`${stripped};\nreturn { el, items };`,
|
|
100
|
-
);
|
|
101
|
-
const doc = (globalThis as { document: unknown }).document;
|
|
102
|
-
const result = factory(doc, ...Object.values(runtime)) as {
|
|
103
|
-
el: ListEl;
|
|
104
|
-
items: { set: (v: string[]) => void };
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
const firstText = result.el.querySelector("li")!.firstChild!;
|
|
108
|
-
expect(firstText.textContent).toBe("a");
|
|
109
|
-
|
|
110
|
-
result.items.set(["a", "c"]); // remove middle item
|
|
111
|
-
await runtime.tick();
|
|
112
|
-
|
|
113
|
-
expect(result.el.querySelectorAll("li").length).toBe(2);
|
|
114
|
-
// first item's Text node survived — no full remount
|
|
115
|
-
expect(result.el.querySelector("li")!.firstChild).toBe(firstText);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test("complex key expression", () => {
|
|
119
|
-
const result = transform(`
|
|
120
|
-
let items = $state([{ id: 1, meta: { version: 2 } }]);
|
|
121
|
-
const el = <ul>{items.map(i => <li key={i.meta.version}>{i.id}</li>)}</ul>
|
|
122
|
-
`);
|
|
123
|
-
expect(result).toContain("reconcile(");
|
|
124
|
-
expect(result).toMatch(/i => i\.meta\.version/);
|
|
125
|
-
});
|
|
126
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import * as runtime from "../../runtime/index.ts";
|
|
3
|
+
import { transform } from "./helpers/transform.ts";
|
|
4
|
+
|
|
5
|
+
describe("Keyed Lists", () => {
|
|
6
|
+
test("detects .map() with key prop", () => {
|
|
7
|
+
const result = transform(`
|
|
8
|
+
let items = $state([{ id: 1 }]);
|
|
9
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
10
|
+
`);
|
|
11
|
+
expect(result).toContain("reconcile(");
|
|
12
|
+
expect(result).toContain("new Map()");
|
|
13
|
+
expect(result).not.toContain("_mountFn");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test("key prop is stripped from DOM output", () => {
|
|
17
|
+
const result = transform(`
|
|
18
|
+
let items = $state([{ id: 1 }]);
|
|
19
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
20
|
+
`);
|
|
21
|
+
expect(result).not.toContain(".key =");
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("key extractor is arrow function of key prop", () => {
|
|
25
|
+
const result = transform(`
|
|
26
|
+
let items = $state([{ id: 1 }]);
|
|
27
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
28
|
+
`);
|
|
29
|
+
// The key extractor should be: item => item.id
|
|
30
|
+
expect(result).toMatch(/i => i\.id/);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("createNode factory contains JSX element creation", () => {
|
|
34
|
+
const result = transform(`
|
|
35
|
+
let items = $state([{ id: 1, text: "a" }]);
|
|
36
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
37
|
+
`);
|
|
38
|
+
expect(result).toContain('document.createElement("li")');
|
|
39
|
+
expect(result).toContain("i.text");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("signal expression is passed to reconcile", () => {
|
|
43
|
+
const result = transform(`
|
|
44
|
+
let items = $state([1, 2, 3]);
|
|
45
|
+
const el = <ul>{items.map(i => <li key={i}>{i}</li>)}</ul>;
|
|
46
|
+
`);
|
|
47
|
+
// The signal accessor should be passed as the items argument
|
|
48
|
+
expect(result).toMatch(/reconcile\([^,]+, [^,]+, items\(\)/);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("anchor and keyMap are created", () => {
|
|
52
|
+
const result = transform(`
|
|
53
|
+
let items = $state([{ id: 1 }]);
|
|
54
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
55
|
+
`);
|
|
56
|
+
expect(result).toContain("document.createComment");
|
|
57
|
+
expect(result).toContain("new Map()");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test("effect wraps reconcile call", () => {
|
|
61
|
+
const result = transform(`
|
|
62
|
+
let items = $state([{ id: 1 }]);
|
|
63
|
+
const el = <ul>{items.map(i => <li key={i.id}>{i.text}</li>)}</ul>;
|
|
64
|
+
`);
|
|
65
|
+
expect(result).toContain("createEffect(");
|
|
66
|
+
expect(result).toContain("reconcile(");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("unkeyed .map() reconciles with index keys", () => {
|
|
70
|
+
const result = transform(`
|
|
71
|
+
let items = $state([1, 2, 3]);
|
|
72
|
+
const el = <ul>{items.map(i => <li>{i}</li>)}</ul>;
|
|
73
|
+
`);
|
|
74
|
+
// No key prop → still reconcile, but keyed by position
|
|
75
|
+
expect(result).toContain("reconcile(");
|
|
76
|
+
expect(result).toContain("(_item, _i) => _i");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("unkeyed map preserves DOM nodes on shift (behavioral)", async () => {
|
|
80
|
+
// minimal structural view of the DOM the compiled code touches — this
|
|
81
|
+
// package's compiler options have no DOM lib
|
|
82
|
+
type TextLike = { textContent: string };
|
|
83
|
+
type ListEl = {
|
|
84
|
+
querySelector(selectors: string): { firstChild: TextLike | null } | null;
|
|
85
|
+
querySelectorAll(selectors: string): { length: number };
|
|
86
|
+
};
|
|
87
|
+
const src = `
|
|
88
|
+
let items = $state(["a", "b", "c"]);
|
|
89
|
+
const el = <ul>{items.map(i => <li>{i}</li>)}</ul>;
|
|
90
|
+
`;
|
|
91
|
+
const code = transform(src);
|
|
92
|
+
const stripped = code.replace(
|
|
93
|
+
/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g,
|
|
94
|
+
"",
|
|
95
|
+
);
|
|
96
|
+
const factory = new Function(
|
|
97
|
+
"document",
|
|
98
|
+
...Object.keys(runtime),
|
|
99
|
+
`${stripped};\nreturn { el, items };`,
|
|
100
|
+
);
|
|
101
|
+
const doc = (globalThis as { document: unknown }).document;
|
|
102
|
+
const result = factory(doc, ...Object.values(runtime)) as {
|
|
103
|
+
el: ListEl;
|
|
104
|
+
items: { set: (v: string[]) => void };
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const firstText = result.el.querySelector("li")!.firstChild!;
|
|
108
|
+
expect(firstText.textContent).toBe("a");
|
|
109
|
+
|
|
110
|
+
result.items.set(["a", "c"]); // remove middle item
|
|
111
|
+
await runtime.tick();
|
|
112
|
+
|
|
113
|
+
expect(result.el.querySelectorAll("li").length).toBe(2);
|
|
114
|
+
// first item's Text node survived — no full remount
|
|
115
|
+
expect(result.el.querySelector("li")!.firstChild).toBe(firstText);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("complex key expression", () => {
|
|
119
|
+
const result = transform(`
|
|
120
|
+
let items = $state([{ id: 1, meta: { version: 2 } }]);
|
|
121
|
+
const el = <ul>{items.map(i => <li key={i.meta.version}>{i.id}</li>)}</ul>
|
|
122
|
+
`);
|
|
123
|
+
expect(result).toContain("reconcile(");
|
|
124
|
+
expect(result).toMatch(/i => i\.meta\.version/);
|
|
125
|
+
});
|
|
126
|
+
});
|