@sigil-dev/compiler 0.7.6 → 0.8.0

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,28 +1,28 @@
1
- import { traverse, types as t, NodePath } from "@babel/core";
2
-
3
- export function warnDeadReactivity(
4
- path: NodePath,
5
- signals: Set<string>,
6
- storeSignals: Set<string>,
7
- filename?: string,
8
- ): void {
9
- const used = new Set<string>();
10
-
11
- path.traverse({
12
- CallExpression(innerPath: any) {
13
- if (t.isIdentifier(innerPath.node.callee)) {
14
- used.add(innerPath.node.callee.name);
15
- }
16
- }
17
- });
18
-
19
- for (const name of signals) {
20
- if (storeSignals.has(name)) continue; // exported, used across files
21
- if (!used.has(name)) {
22
- const tag = filename ? `[sigil:${filename}]` : "[sigil]";
23
- console.warn(
24
- `${tag} unused reactive declaration: "${name}" is declared with $state or $derived but never read.`
25
- );
26
- }
27
- }
28
- }
1
+ import { type NodePath, types as t, traverse } from "@babel/core";
2
+
3
+ export function warnDeadReactivity(
4
+ path: NodePath,
5
+ signals: Set<string>,
6
+ storeSignals: Set<string>,
7
+ filename?: string,
8
+ ): void {
9
+ const used = new Set<string>();
10
+
11
+ path.traverse({
12
+ CallExpression(innerPath: any) {
13
+ if (t.isIdentifier(innerPath.node.callee)) {
14
+ used.add(innerPath.node.callee.name);
15
+ }
16
+ },
17
+ });
18
+
19
+ for (const name of signals) {
20
+ if (storeSignals.has(name)) continue; // exported, used across files
21
+ if (!used.has(name)) {
22
+ const tag = filename ? `[sigil:${filename}]` : "[sigil]";
23
+ console.warn(
24
+ `${tag} unused reactive declaration: "${name}" is declared with $state or $derived but never read.`,
25
+ );
26
+ }
27
+ }
28
+ }
@@ -5,6 +5,7 @@ export const MAGIC = {
5
5
  createEffect: "createEffect",
6
6
  createEffectPre: "createEffectPre",
7
7
  createInspectEffect: "createInspectEffect",
8
+ withEffectScope: "withEffectScope",
8
9
  tracking: "tracking",
9
10
  snapshot: "snapshot",
10
11
  } as const;
package/src/bun-plugin.ts CHANGED
@@ -1,86 +1,89 @@
1
- import { transformSync } from "@babel/core";
2
- import type { BunPlugin } from "bun";
3
- import sigilPlugin from "./babel/index.ts";
4
- import { computeHash, scopeCSS } from "./babel/util/css.ts";
5
-
6
- const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
7
- const STYLE_TEST_RE = /<style[^>]*>[\s\S]*?<\/style>/i;
8
- const SOURCE_RE = /\.[jt]sx?$/;
9
- const JSX_RE = /\.[jt]sx$/;
10
- const SIGIL_MACRO_RE = /\$(?:state|store|derived|effect)\s*\(/;
11
-
12
- function normalizePath(path: string): string {
13
- return path.replace(/\\/g, "/");
14
- }
15
-
16
- function shouldTransform(path: string): boolean {
17
- const normalized = normalizePath(path);
18
- if (!SOURCE_RE.test(normalized)) return false;
19
- if (normalized.includes("/.grimoire/")) return false;
20
- if (normalized.includes("/node_modules/")) return false;
21
- if (normalized.includes("/packages/runtime/")) return false;
22
- if (normalized.includes("/packages/grimoire/")) return false;
23
- if (normalized.includes("/packages/compiler/")) return false;
24
- return true;
25
- }
26
-
27
- function shouldTransformSource(path: string, code: string): boolean {
28
- const normalized = normalizePath(path);
29
- return JSX_RE.test(normalized) || SIGIL_MACRO_RE.test(code) || STYLE_TEST_RE.test(code);
30
- }
31
-
32
- export function sigil(options?: {
33
- mode?: "dom" | "ssr" | "hydrate";
34
- }): BunPlugin {
35
- return {
36
- name: "sigil",
37
- setup(build) {
38
- const transpiler = new Bun.Transpiler({ loader: "tsx" });
39
-
40
- // When grimoire/compiler are bun-linked from the sigil monorepo,
41
- // @sigil-dev/runtime resolves to the workspace source at
42
- // D:\Projects\sigil\packages\runtime rather than the project's
43
- // node_modules — causing "Unexpected reading file" from Bun.build.
44
- // Force resolution from the project's cwd instead.
45
- build.onResolve({ filter: /^@sigil-dev\/runtime($|\/)/ }, (args) => {
46
- try {
47
- return { path: Bun.resolveSync(args.path, process.cwd()) };
48
- } catch {
49
- return undefined;
50
- }
51
- });
52
-
53
- build.onLoad({ filter: SOURCE_RE }, async ({ path }) => {
54
- if (!shouldTransform(path)) return undefined;
55
- let code = await Bun.file(path).text();
56
- if (!shouldTransformSource(path, code)) return undefined;
57
- // console.log("[bun-plugin] intercepting:", path);
58
- const hash = computeHash(path);
59
- let scopedCSS = "";
60
- code = code.replace(STYLE_RE, (_, css) => {
61
- scopedCSS = scopeCSS(css.trim(), hash);
62
- return "";
63
- });
64
- const res = transformSync(code, {
65
- parserOpts: {
66
- plugins: [["typescript", { isTSX: true }], "jsx"],
67
- },
68
- plugins: [[sigilPlugin, { hash, mode: options?.mode }]],
69
- filename: path,
70
- });
71
- let out = transpiler.transformSync(res?.code ?? "");
72
- if (scopedCSS && options?.mode !== "ssr") {
73
- out =
74
- `
75
- if (typeof document !== 'undefined' && !document.getElementById('sigil-${hash}')) {
76
- const __style = document.createElement('style');
77
- __style.id = 'sigil-${hash}';
78
- __style.textContent = ${JSON.stringify(scopedCSS)};
79
- document.head.appendChild(__style);
80
- }` + out;
81
- }
82
- return { contents: out, loader: "js" };
83
- });
84
- },
85
- };
86
- }
1
+ import { transformSync } from "@babel/core";
2
+ import type { BunPlugin } from "bun";
3
+ import sigilPlugin from "./babel/index.ts";
4
+ import { computeHash, scopeCSS } from "./babel/util/css.ts";
5
+
6
+ const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
7
+ const STYLE_TEST_RE = /<style[^>]*>[\s\S]*?<\/style>/i;
8
+ const SOURCE_RE = /\.[jt]sx?$/;
9
+ const JSX_RE = /\.[jt]sx$/;
10
+ const SIGIL_MACRO_RE = /\$(?:state|store|derived|effect)\s*\(/;
11
+
12
+ function normalizePath(path: string): string {
13
+ return path.replace(/\\/g, "/");
14
+ }
15
+
16
+ function shouldTransform(path: string): boolean {
17
+ const normalized = normalizePath(path);
18
+ if (!SOURCE_RE.test(normalized)) return false;
19
+ if (normalized.includes("/.grimoire/")) return false;
20
+ if (normalized.includes("/node_modules/")) return false;
21
+ if (normalized.includes("/packages/runtime/")) return false;
22
+ if (normalized.includes("/packages/grimoire/")) return false;
23
+ if (normalized.includes("/packages/compiler/")) return false;
24
+ return true;
25
+ }
26
+
27
+ function shouldTransformSource(path: string, code: string): boolean {
28
+ const normalized = normalizePath(path);
29
+ return (
30
+ JSX_RE.test(normalized) ||
31
+ SIGIL_MACRO_RE.test(code) ||
32
+ STYLE_TEST_RE.test(code)
33
+ );
34
+ }
35
+
36
+ export function sigil(options?: {
37
+ mode?: "dom" | "ssr" | "hydrate";
38
+ }): BunPlugin {
39
+ return {
40
+ name: "sigil",
41
+ setup(build) {
42
+ const transpiler = new Bun.Transpiler({ loader: "tsx" });
43
+
44
+ // This is required for both linking errors,
45
+ // and for a bug in the bundler, which was reported in https://github.com/oven-sh/bun/issues/13897 (2 YEARSS AGO)
46
+ // and apparently fixed in https://github.com/oven-sh/bun/pull/15119. It wasnt
47
+ build.onResolve({ filter: /^@sigil-dev\/runtime($|\/)/ }, (args) => {
48
+ try {
49
+ const resolved = Bun.resolveSync(args.path, process.cwd());
50
+ return { path: resolved.replaceAll("\\", "/") };
51
+ } catch {
52
+ return undefined;
53
+ }
54
+ });
55
+
56
+ build.onLoad({ filter: SOURCE_RE }, async ({ path }) => {
57
+ if (!shouldTransform(path)) return undefined;
58
+ let code = await Bun.file(path).text();
59
+ if (!shouldTransformSource(path, code)) return undefined;
60
+ // console.log("[bun-plugin] intercepting:", path);
61
+ const hash = computeHash(path);
62
+ let scopedCSS = "";
63
+ code = code.replace(STYLE_RE, (_, css) => {
64
+ scopedCSS = scopeCSS(css.trim(), hash);
65
+ return "";
66
+ });
67
+ const res = transformSync(code, {
68
+ parserOpts: {
69
+ plugins: [["typescript", { isTSX: true }], "jsx"],
70
+ },
71
+ plugins: [[sigilPlugin, { hash, mode: options?.mode }]],
72
+ filename: path,
73
+ });
74
+ let out = transpiler.transformSync(res?.code ?? "");
75
+ if (scopedCSS && options?.mode !== "ssr") {
76
+ out =
77
+ `
78
+ if (typeof document !== 'undefined' && !document.getElementById('sigil-${hash}')) {
79
+ const __style = document.createElement('style');
80
+ __style.id = 'sigil-${hash}';
81
+ __style.textContent = ${JSON.stringify(scopedCSS)};
82
+ document.head.appendChild(__style);
83
+ }` + out;
84
+ }
85
+ return { contents: out, loader: "js" };
86
+ });
87
+ },
88
+ };
89
+ }
@@ -112,7 +112,7 @@ describe("Hydration", () => {
112
112
  test("imports claim and claimText from @sigil-dev/runtime", () => {
113
113
  const code = transform(`const el = <div></div>`, { mode: "hydrate" });
114
114
  expect(code).toContain(
115
- "createSignal, createEffect, createMemo, reconcile, claim, claimText, claimComment, hydrateKeyedList, insert",
115
+ "createSignal, createRawSignal, createEffect, createMemo, reconcile, claim, claimText, claimComment, hydrateKeyedList, insert, getHydrationNodes, pushHydrationNodes, popHydrationNodes, snapshot, tracking, createEffectPre, createInspectEffect, withEffectScope",
116
116
  );
117
117
  });
118
118
 
package/test/jsx.test.ts CHANGED
@@ -1,195 +1,197 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { transform } from "./helpers/transform.ts";
3
-
4
- describe("JSX", () => {
5
- test("single static element", () => {
6
- const result = transform(`const el = <div />;`);
7
- expect(result).toContain('document.createElement("div")');
8
- });
9
-
10
- test("nested static elements", () => {
11
- const result = transform(`const el = <div><span /></div>;`);
12
- expect(result).toContain('document.createElement("div")');
13
- expect(result).toContain('document.createElement("span")');
14
- expect(result).toContain(".append(");
15
- });
16
-
17
- test("static string attribute", () => {
18
- const result = transform(`const el = <div class="app" />;`);
19
- expect(result).toContain('_el0.className = "app"');
20
- });
21
-
22
- test("attribute without mapping", () => {
23
- const result = transform(`const el = <input type="text" />;`);
24
- expect(result).toContain('_el0.type = "text"');
25
- });
26
-
27
- test("expression attribute", () => {
28
- const result = transform(`const el = <div id={myId} />;`);
29
- expect(result).toContain("_el0.id = myId");
30
- });
31
-
32
- describe("Reactivity", () => {
33
- test("dynamic attribute wraps in createEffect", () => {
34
- const result = transform(`
35
- let count = $state(0);
36
- const el = <div id={count} />;
37
- `);
38
- expect(result).toContain("createEffect(");
39
- expect(result).toContain("_el0.id = count()");
40
- });
41
-
42
- test("static expression attribute does not wrap in createEffect", () => {
43
- const result = transform(`
44
- const myId = "app";
45
- const el = <div id={myId} />;
46
- `);
47
- expect(result).not.toContain("createEffect(");
48
- expect(result).toContain("_el0.id = myId");
49
- });
50
-
51
- test("style object compiles to per-property assignment", () => {
52
- const code = transform(`
53
- const el = <div style={{ color: "red", fontSize: "16px" }} />;
54
- `);
55
- expect(code).toContain('.style.color = "red"');
56
- expect(code).toContain('.style.fontSize = "16px"');
57
- expect(code).not.toContain("[object Object]");
58
- });
59
-
60
- test("reactive style property wraps in createEffect", () => {
61
- const code = transform(`
62
- let size = $state(16);
63
- const el = <div style={{ fontSize: size + "px", color: "red" }} />;
64
- `);
65
- expect(code).toContain("createEffect");
66
- expect(code).toContain(".style.fontSize");
67
- expect(code).toContain('.style.color = "red"');
68
- });
69
- });
70
-
71
- describe("Children", () => {
72
- // text children
73
- test("static text child", () => {
74
- const result = transform(`const el = <div>hello</div>;`);
75
- expect(result).toContain('createTextNode("hello")');
76
- });
77
-
78
- test("dynamic text child", () => {
79
- const result = transform(`
80
- let count = $state(0);
81
- const el = <div>{count}</div>;
82
- `);
83
- expect(result).toContain("createEffect(");
84
- expect(result).toContain("count()");
85
- });
86
-
87
- // event handlers
88
- test("onClick handler", () => {
89
- const result = transform(
90
- `const el = <button onClick={() => {}}>click</button>;`,
91
- );
92
- expect(result).toContain('addEventListener("click"');
93
- });
94
-
95
- // combined
96
- test("counter shape", () => {
97
- const result = transform(`
98
- let count = $state(0);
99
- const el = (
100
- <div>
101
- <span>{count}</span>
102
- <button onClick={() => count++}>+</button>
103
- </div>
104
- );
105
- `);
106
- expect(result).toContain("createSignal(0)");
107
- expect(result).toContain('addEventListener("click"');
108
- expect(result).toContain("createEffect(");
109
- expect(result).toContain("createTextNode");
110
- });
111
- });
112
-
113
- describe("Events", () => {
114
- test("onClick handler", () => {
115
- const result = transform(
116
- `const el = <button onClick={() => {}}>click</button>;`,
117
- );
118
- expect(result).toContain('addEventListener("click"');
119
- });
120
- });
121
-
122
- describe("Integration", () => {
123
- test("counter shape", () => {
124
- const result = transform(`
125
- let count = $state(0);
126
- const el = (
127
- <div>
128
- <span>{count}</span>
129
- <button onClick={() => count++}>+</button>
130
- </div>
131
- );
132
- `);
133
- expect(result).toContain("createSignal(0)");
134
- expect(result).toContain('addEventListener("click"');
135
- expect(result).toContain("createEffect(");
136
- expect(result).toContain("createTextNode");
137
- });
138
- });
139
-
140
- describe("Bind", () => {
141
- test("bindValue compiles to effect + event listener", () => {
142
- const code = transform(`
143
- let text = $state("");
144
- const el = <input bindValue={text} />;
145
- `);
146
- // getter: createEffect(() => el.value = text())
147
- expect(code).toContain("createEffect");
148
- expect(code).toContain(".value = text()");
149
- // setter: addEventListener("input", e => text.set(e.target.value))
150
- expect(code).toContain('"input"');
151
- expect(code).toContain("text.set(e.target.value)");
152
- });
153
-
154
- test("bindChecked compiles to change event", () => {
155
- const code = transform(`
156
- let checked = $state(false);
157
- const el = <input type="checkbox" bindChecked={checked} />;
158
- `);
159
- expect(code).toContain(".checked = checked()");
160
- expect(code).toContain('"change"');
161
- expect(code).toContain("checked.set(e.target.checked)");
162
- });
163
-
164
- test("bindThis assigns element reference", () => {
165
- const code = transform(`
166
- let el = $state(null);
167
- const div = <div bindThis={el} />;
168
- `);
169
- expect(code).toContain("el.set(");
170
- expect(code).not.toContain("addEventListener");
171
- expect(code).not.toContain("createEffect(");
172
- });
173
-
174
- test("bindValue on object property uses proxy assignment", () => {
175
- const code = transform(`
176
- let form = $state({ name: "" });
177
- const el = <input bindValue={form.name} />;
178
- `);
179
- // getter: createEffect(() => el.value = form().name)
180
- expect(code).toContain("form().name");
181
- expect(code).toContain("createEffect(");
182
- // setter: proxy assignment, not .set()
183
- expect(code).toContain("form().name = e.target.value");
184
- expect(code).not.toContain("form.name.set");
185
- });
186
-
187
- test("bindValue does not affect non-signal expressions", () => {
188
- // static string should not compile — but if it does, at least no crash
189
- // this is a misuse case, just verify it does not throw
190
- expect(() => transform(`
191
- const el = <input bindValue={"static"} />;
192
- `)).not.toThrow();
193
- });
194
- });
195
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { transform } from "./helpers/transform.ts";
3
+
4
+ describe("JSX", () => {
5
+ test("single static element", () => {
6
+ const result = transform(`const el = <div />;`);
7
+ expect(result).toContain('document.createElement("div")');
8
+ });
9
+
10
+ test("nested static elements", () => {
11
+ const result = transform(`const el = <div><span /></div>;`);
12
+ expect(result).toContain('document.createElement("div")');
13
+ expect(result).toContain('document.createElement("span")');
14
+ expect(result).toContain(".append(");
15
+ });
16
+
17
+ test("static string attribute", () => {
18
+ const result = transform(`const el = <div class="app" />;`);
19
+ expect(result).toContain('_el0.className = "app"');
20
+ });
21
+
22
+ test("attribute without mapping", () => {
23
+ const result = transform(`const el = <input type="text" />;`);
24
+ expect(result).toContain('_el0.type = "text"');
25
+ });
26
+
27
+ test("expression attribute", () => {
28
+ const result = transform(`const el = <div id={myId} />;`);
29
+ expect(result).toContain("_el0.id = myId");
30
+ });
31
+
32
+ describe("Reactivity", () => {
33
+ test("dynamic attribute wraps in createEffect", () => {
34
+ const result = transform(`
35
+ let count = $state(0);
36
+ const el = <div id={count} />;
37
+ `);
38
+ expect(result).toContain("createEffect(");
39
+ expect(result).toContain("_el0.id = count()");
40
+ });
41
+
42
+ test("static expression attribute does not wrap in createEffect", () => {
43
+ const result = transform(`
44
+ const myId = "app";
45
+ const el = <div id={myId} />;
46
+ `);
47
+ expect(result).not.toContain("createEffect(");
48
+ expect(result).toContain("_el0.id = myId");
49
+ });
50
+
51
+ test("style object compiles to per-property assignment", () => {
52
+ const code = transform(`
53
+ const el = <div style={{ color: "red", fontSize: "16px" }} />;
54
+ `);
55
+ expect(code).toContain('.style.color = "red"');
56
+ expect(code).toContain('.style.fontSize = "16px"');
57
+ expect(code).not.toContain("[object Object]");
58
+ });
59
+
60
+ test("reactive style property wraps in createEffect", () => {
61
+ const code = transform(`
62
+ let size = $state(16);
63
+ const el = <div style={{ fontSize: size + "px", color: "red" }} />;
64
+ `);
65
+ expect(code).toContain("createEffect");
66
+ expect(code).toContain(".style.fontSize");
67
+ expect(code).toContain('.style.color = "red"');
68
+ });
69
+ });
70
+
71
+ describe("Children", () => {
72
+ // text children
73
+ test("static text child", () => {
74
+ const result = transform(`const el = <div>hello</div>;`);
75
+ expect(result).toContain('createTextNode("hello")');
76
+ });
77
+
78
+ test("dynamic text child", () => {
79
+ const result = transform(`
80
+ let count = $state(0);
81
+ const el = <div>{count}</div>;
82
+ `);
83
+ expect(result).toContain("createEffect(");
84
+ expect(result).toContain("count()");
85
+ });
86
+
87
+ // event handlers
88
+ test("onClick handler", () => {
89
+ const result = transform(
90
+ `const el = <button onClick={() => {}}>click</button>;`,
91
+ );
92
+ expect(result).toContain('addEventListener("click"');
93
+ });
94
+
95
+ // combined
96
+ test("counter shape", () => {
97
+ const result = transform(`
98
+ let count = $state(0);
99
+ const el = (
100
+ <div>
101
+ <span>{count}</span>
102
+ <button onClick={() => count++}>+</button>
103
+ </div>
104
+ );
105
+ `);
106
+ expect(result).toContain("createSignal(0)");
107
+ expect(result).toContain('addEventListener("click"');
108
+ expect(result).toContain("createEffect(");
109
+ expect(result).toContain("createTextNode");
110
+ });
111
+ });
112
+
113
+ describe("Events", () => {
114
+ test("onClick handler", () => {
115
+ const result = transform(
116
+ `const el = <button onClick={() => {}}>click</button>;`,
117
+ );
118
+ expect(result).toContain('addEventListener("click"');
119
+ });
120
+ });
121
+
122
+ describe("Integration", () => {
123
+ test("counter shape", () => {
124
+ const result = transform(`
125
+ let count = $state(0);
126
+ const el = (
127
+ <div>
128
+ <span>{count}</span>
129
+ <button onClick={() => count++}>+</button>
130
+ </div>
131
+ );
132
+ `);
133
+ expect(result).toContain("createSignal(0)");
134
+ expect(result).toContain('addEventListener("click"');
135
+ expect(result).toContain("createEffect(");
136
+ expect(result).toContain("createTextNode");
137
+ });
138
+ });
139
+
140
+ describe("Bind", () => {
141
+ test("bindValue compiles to effect + event listener", () => {
142
+ const code = transform(`
143
+ let text = $state("");
144
+ const el = <input bindValue={text} />;
145
+ `);
146
+ // getter: createEffect(() => el.value = text())
147
+ expect(code).toContain("createEffect");
148
+ expect(code).toContain(".value = text()");
149
+ // setter: addEventListener("input", e => text.set(e.target.value))
150
+ expect(code).toContain('"input"');
151
+ expect(code).toContain("text.set(e.target.value)");
152
+ });
153
+
154
+ test("bindChecked compiles to change event", () => {
155
+ const code = transform(`
156
+ let checked = $state(false);
157
+ const el = <input type="checkbox" bindChecked={checked} />;
158
+ `);
159
+ expect(code).toContain(".checked = checked()");
160
+ expect(code).toContain('"change"');
161
+ expect(code).toContain("checked.set(e.target.checked)");
162
+ });
163
+
164
+ test("bindThis assigns element reference", () => {
165
+ const code = transform(`
166
+ let el = $state(null);
167
+ const div = <div bindThis={el} />;
168
+ `);
169
+ expect(code).toContain("el.set(");
170
+ expect(code).not.toContain("addEventListener");
171
+ expect(code).not.toContain("createEffect(");
172
+ });
173
+
174
+ test("bindValue on object property uses proxy assignment", () => {
175
+ const code = transform(`
176
+ let form = $state({ name: "" });
177
+ const el = <input bindValue={form.name} />;
178
+ `);
179
+ // getter: createEffect(() => el.value = form().name)
180
+ expect(code).toContain("form().name");
181
+ expect(code).toContain("createEffect(");
182
+ // setter: proxy assignment, not .set()
183
+ expect(code).toContain("form().name = e.target.value");
184
+ expect(code).not.toContain("form.name.set");
185
+ });
186
+
187
+ test("bindValue does not affect non-signal expressions", () => {
188
+ // static string should not compile — but if it does, at least no crash
189
+ // this is a misuse case, just verify it does not throw
190
+ expect(() =>
191
+ transform(`
192
+ const el = <input bindValue={"static"} />;
193
+ `),
194
+ ).not.toThrow();
195
+ });
196
+ });
197
+ });