@sigil-dev/compiler 0.7.6 → 0.7.7

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,151 +1,151 @@
1
- import { createHash } from "crypto";
2
-
3
- /**
4
- * Compute a deterministic hash from a file path.
5
- * Same file always gets same hash. Different files always get different hashes.
6
- * 's' prefix for Sigil.
7
- */
8
- export function computeHash(filePath: string): string {
9
- return "s" + createHash("md5").update(filePath).digest("hex").slice(0, 8);
10
- }
11
-
12
- /**
13
- * Scope CSS by rewriting selectors with hash.
14
- * :global(.selector) escapes scoping (passed through unchanged).
15
- */
16
- export function scopeCSS(css: string, hash: string): string {
17
- let result = "";
18
- let i = 0;
19
-
20
- while (i < css.length) {
21
- // Skip comments
22
- if (css[i] === "/" && css[i + 1] === "*") {
23
- const end = css.indexOf("*/", i + 2);
24
- if (end === -1) {
25
- result += css.slice(i);
26
- break;
27
- }
28
- result += css.slice(i, end + 2);
29
- i = end + 2;
30
- continue;
31
- }
32
-
33
- // Skip string literals
34
- if (css[i] === '"' || css[i] === "'") {
35
- const quote = css[i];
36
- let j = i + 1;
37
- while (j < css.length && css[j] !== quote) {
38
- if (css[j] === "\\") j++;
39
- j++;
40
- }
41
- result += css.slice(i, j + 1);
42
- i = j + 1;
43
- continue;
44
- }
45
-
46
- // Find next { or end of string
47
- if (css[i] === "{") {
48
- // Find the matching }
49
- let depth = 1;
50
- let j = i + 1;
51
- while (j < css.length && depth > 0) {
52
- if (css[j] === "{") depth++;
53
- else if (css[j] === "}") depth--;
54
- j++;
55
- }
56
-
57
- // The block content (inside { }) - recurse into it
58
- const blockContent = css.slice(i + 1, j - 1);
59
- const scopedBlock = scopeCSS(blockContent, hash);
60
-
61
- // Find where the selector starts (look back from {)
62
- let selectorEnd = i;
63
- let k = i - 1;
64
- // Skip whitespace before {
65
- while (k >= 0 && css[k] === " ") k--;
66
- selectorEnd = k + 1;
67
-
68
- // Find start of selector (look back for } or ; or start of string)
69
- let selectorStart = selectorEnd;
70
- k = selectorEnd - 1;
71
- while (k >= 0) {
72
- if (css[k] === "}" || css[k] === ";") {
73
- selectorStart = k + 1;
74
- break;
75
- }
76
- if (k === 0) {
77
- selectorStart = 0;
78
- break;
79
- }
80
- k--;
81
- }
82
-
83
- const selector = css.slice(selectorStart, selectorEnd).trim();
84
-
85
- if (selector) {
86
- // Rewrite selector with hash
87
- result += css.slice(0, selectorStart);
88
- result += rewriteSelector(selector, hash);
89
- result += " {";
90
- result += scopedBlock;
91
- result += "}";
92
- } else {
93
- // No selector found, just output the block
94
- result += css.slice(0, i);
95
- result += " {";
96
- result += scopedBlock;
97
- result += "}";
98
- }
99
-
100
- i = j;
101
- continue;
102
- }
103
-
104
- result += css[i];
105
- i++;
106
- }
107
-
108
- return result;
109
- }
110
-
111
- /**
112
- * Rewrite a selector (or comma-separated list) by appending hash to each part.
113
- * Handles :global() by passing through unchanged.
114
- */
115
- function rewriteSelector(selector: string, hash: string): string {
116
- // First, extract all :global() blocks and replace with placeholders
117
- const globals: string[] = [];
118
- let processed = selector;
119
-
120
- // Find :global(...) with nested parens
121
- while (processed.includes(":global(")) {
122
- const start = processed.indexOf(":global(");
123
- let depth = 1;
124
- let j = start + 7; // Skip ':global('
125
- while (j < processed.length && depth > 0) {
126
- if (processed[j] === "(") depth++;
127
- else if (processed[j] === ")") depth--;
128
- j++;
129
- }
130
- const globalContent = processed.slice(start + 7, j - 1); // Content inside :global()
131
- const placeholder = `__GLOBAL_${globals.length}__`;
132
- globals.push(globalContent);
133
- processed = processed.slice(0, start) + placeholder + processed.slice(j);
134
- }
135
-
136
- // Now split by comma (safe - no commas inside :global())
137
- const parts = processed.split(",").map((part) => {
138
- const trimmed = part.trim();
139
- if (!trimmed) return trimmed;
140
-
141
- // Check for placeholder
142
- const globalMatch = trimmed.match(/^__GLOBAL_(\d+)__$/);
143
- if (globalMatch) {
144
- return globals[parseInt(globalMatch[1]!)];
145
- }
146
-
147
- return trimmed + "." + hash;
148
- });
149
-
150
- return parts.join(", ");
151
- }
1
+ import { createHash } from "crypto";
2
+
3
+ /**
4
+ * Compute a deterministic hash from a file path.
5
+ * Same file always gets same hash. Different files always get different hashes.
6
+ * 's' prefix for Sigil.
7
+ */
8
+ export function computeHash(filePath: string): string {
9
+ return "s" + createHash("md5").update(filePath).digest("hex").slice(0, 8);
10
+ }
11
+
12
+ /**
13
+ * Scope CSS by rewriting selectors with hash.
14
+ * :global(.selector) escapes scoping (passed through unchanged).
15
+ */
16
+ export function scopeCSS(css: string, hash: string): string {
17
+ let result = "";
18
+ let i = 0;
19
+
20
+ while (i < css.length) {
21
+ // Skip comments
22
+ if (css[i] === "/" && css[i + 1] === "*") {
23
+ const end = css.indexOf("*/", i + 2);
24
+ if (end === -1) {
25
+ result += css.slice(i);
26
+ break;
27
+ }
28
+ result += css.slice(i, end + 2);
29
+ i = end + 2;
30
+ continue;
31
+ }
32
+
33
+ // Skip string literals
34
+ if (css[i] === '"' || css[i] === "'") {
35
+ const quote = css[i];
36
+ let j = i + 1;
37
+ while (j < css.length && css[j] !== quote) {
38
+ if (css[j] === "\\") j++;
39
+ j++;
40
+ }
41
+ result += css.slice(i, j + 1);
42
+ i = j + 1;
43
+ continue;
44
+ }
45
+
46
+ // Find next { or end of string
47
+ if (css[i] === "{") {
48
+ // Find the matching }
49
+ let depth = 1;
50
+ let j = i + 1;
51
+ while (j < css.length && depth > 0) {
52
+ if (css[j] === "{") depth++;
53
+ else if (css[j] === "}") depth--;
54
+ j++;
55
+ }
56
+
57
+ // The block content (inside { }) - recurse into it
58
+ const blockContent = css.slice(i + 1, j - 1);
59
+ const scopedBlock = scopeCSS(blockContent, hash);
60
+
61
+ // Find where the selector starts (look back from {)
62
+ let selectorEnd = i;
63
+ let k = i - 1;
64
+ // Skip whitespace before {
65
+ while (k >= 0 && css[k] === " ") k--;
66
+ selectorEnd = k + 1;
67
+
68
+ // Find start of selector (look back for } or ; or start of string)
69
+ let selectorStart = selectorEnd;
70
+ k = selectorEnd - 1;
71
+ while (k >= 0) {
72
+ if (css[k] === "}" || css[k] === ";") {
73
+ selectorStart = k + 1;
74
+ break;
75
+ }
76
+ if (k === 0) {
77
+ selectorStart = 0;
78
+ break;
79
+ }
80
+ k--;
81
+ }
82
+
83
+ const selector = css.slice(selectorStart, selectorEnd).trim();
84
+
85
+ if (selector) {
86
+ // Rewrite selector with hash
87
+ result += css.slice(0, selectorStart);
88
+ result += rewriteSelector(selector, hash);
89
+ result += " {";
90
+ result += scopedBlock;
91
+ result += "}";
92
+ } else {
93
+ // No selector found, just output the block
94
+ result += css.slice(0, i);
95
+ result += " {";
96
+ result += scopedBlock;
97
+ result += "}";
98
+ }
99
+
100
+ i = j;
101
+ continue;
102
+ }
103
+
104
+ result += css[i];
105
+ i++;
106
+ }
107
+
108
+ return result;
109
+ }
110
+
111
+ /**
112
+ * Rewrite a selector (or comma-separated list) by appending hash to each part.
113
+ * Handles :global() by passing through unchanged.
114
+ */
115
+ function rewriteSelector(selector: string, hash: string): string {
116
+ // First, extract all :global() blocks and replace with placeholders
117
+ const globals: string[] = [];
118
+ let processed = selector;
119
+
120
+ // Find :global(...) with nested parens
121
+ while (processed.includes(":global(")) {
122
+ const start = processed.indexOf(":global(");
123
+ let depth = 1;
124
+ let j = start + 7; // Skip ':global('
125
+ while (j < processed.length && depth > 0) {
126
+ if (processed[j] === "(") depth++;
127
+ else if (processed[j] === ")") depth--;
128
+ j++;
129
+ }
130
+ const globalContent = processed.slice(start + 7, j - 1); // Content inside :global()
131
+ const placeholder = `__GLOBAL_${globals.length}__`;
132
+ globals.push(globalContent);
133
+ processed = processed.slice(0, start) + placeholder + processed.slice(j);
134
+ }
135
+
136
+ // Now split by comma (safe - no commas inside :global())
137
+ const parts = processed.split(",").map((part) => {
138
+ const trimmed = part.trim();
139
+ if (!trimmed) return trimmed;
140
+
141
+ // Check for placeholder
142
+ const globalMatch = trimmed.match(/^__GLOBAL_(\d+)__$/);
143
+ if (globalMatch) {
144
+ return globals[parseInt(globalMatch[1]!)];
145
+ }
146
+
147
+ return trimmed + "." + hash;
148
+ });
149
+
150
+ return parts.join(", ");
151
+ }
@@ -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