@sigil-dev/compiler 0.9.28 → 0.9.30
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 +6 -4
- package/package.json +1 -1
- package/src/babel/index.ts +763 -750
- package/src/babel/jsx/anchor-mount.ts +338 -333
- package/src/babel/jsx/children.ts +116 -116
- package/src/babel/jsx/element.ts +1040 -1034
- package/src/babel/jsx/fragment.ts +271 -271
- package/src/babel/jsx/keyed-list.ts +339 -339
- package/src/babel/jsx/utils.ts +356 -356
- package/src/babel/util/bind.ts +143 -143
- package/src/babel/util/css.ts +208 -208
- package/src/babel/util/dead-code.ts +94 -93
- package/src/babel/util/links.ts +101 -0
- package/src/babel/util/reactive-fns.ts +110 -110
- package/src/babel/util/runtime-names.ts +88 -0
- package/src/babel/util/state-imports.ts +122 -121
- package/src/babel/util/static-assertion.ts +51 -51
- package/src/babel/util/static-env.ts +84 -0
- package/src/vite/index.ts +61 -61
- package/test/bun-plugin.test.ts +45 -45
- package/test/context.test.ts +110 -102
- package/test/fixtures/stores/index.ts +2 -2
- package/test/hydration-analysis.test.ts +133 -133
- package/test/hydration.test.ts +315 -313
- package/test/jsx.test.ts +254 -254
- package/test/links.test.ts +57 -0
- package/test/reactive-helpers.test.ts +135 -135
- package/test/render-parity.test.ts +909 -907
- package/test/runtime-names.test.ts +60 -0
- package/test/scoped-css.test.ts +166 -166
- package/test/ssr-boolean-attrs.test.ts +56 -56
- package/test/ssr-raw-text.test.ts +68 -68
- package/test/ssr.test.ts +30 -0
- package/test/static-assertion.test.ts +123 -123
- package/test/static-env.test.ts +85 -0
- package/test/static-skip.test.ts +292 -292
- package/test/style-object.test.ts +36 -36
- package/test/tree-shaking.test.ts +122 -120
package/src/babel/util/css.ts
CHANGED
|
@@ -1,208 +1,208 @@
|
|
|
1
|
-
import { createHash } from "node: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 adding the file's hash class to every selector.
|
|
14
|
-
*
|
|
15
|
-
* - Each selector gets the class on its last compound, before any pseudo
|
|
16
|
-
* part: `.list .item:hover` → `.list .item.sX:hover`, so the rule only
|
|
17
|
-
* matches elements this file rendered.
|
|
18
|
-
* - `:global(…)` is left unscoped.
|
|
19
|
-
* - Grouping at-rules (@media, @supports, @container, @layer, @scope) are
|
|
20
|
-
* scoped inside; others with a block (@keyframes, @font-face, …) pass
|
|
21
|
-
* through, as do statements like @import.
|
|
22
|
-
* - Nested rules are scoped too; declarations are never touched.
|
|
23
|
-
*/
|
|
24
|
-
export function scopeCSS(css: string, hash: string): string {
|
|
25
|
-
let out = "";
|
|
26
|
-
let emitted = 0; // css[0..emitted) is already in `out`
|
|
27
|
-
let start = 0; // where the current statement began
|
|
28
|
-
let i = 0;
|
|
29
|
-
while (i < css.length) {
|
|
30
|
-
const c = css[i];
|
|
31
|
-
if (c === "/" && css[i + 1] === "*") {
|
|
32
|
-
const end = css.indexOf("*/", i + 2);
|
|
33
|
-
i = end === -1 ? css.length : end + 2;
|
|
34
|
-
continue;
|
|
35
|
-
}
|
|
36
|
-
if (c === '"' || c === "'") {
|
|
37
|
-
i = skipString(css, i);
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
if (c === ";" || c === "}") {
|
|
41
|
-
i++;
|
|
42
|
-
start = i;
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (c !== "{") {
|
|
46
|
-
i++;
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
const close = matchingBrace(css, i);
|
|
50
|
-
const prelude = css.slice(start, i);
|
|
51
|
-
const lead = /^(?:\s|\/\*[\s\S]*?\*\/)*/.exec(prelude)![0];
|
|
52
|
-
const head = prelude.slice(lead.length).trim();
|
|
53
|
-
const body = css.slice(i + 1, close);
|
|
54
|
-
let block: string;
|
|
55
|
-
if (head.startsWith("@")) {
|
|
56
|
-
block = `${head} {${PASSTHROUGH_AT.test(head) ? body : scopeCSS(body, hash)}}`;
|
|
57
|
-
} else {
|
|
58
|
-
block = `${rewriteSelector(head, hash)} {${scopeCSS(body, hash)}}`;
|
|
59
|
-
}
|
|
60
|
-
out += css.slice(emitted, start) + lead + block;
|
|
61
|
-
i = close + 1;
|
|
62
|
-
emitted = i;
|
|
63
|
-
start = i;
|
|
64
|
-
}
|
|
65
|
-
return out + css.slice(emitted);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const PASSTHROUGH_AT =
|
|
69
|
-
/^@(?:-webkit-)?(?:keyframes|font-face|page|property|counter-style|font-feature-values|font-palette-values|view-transition)\b/i;
|
|
70
|
-
|
|
71
|
-
function skipString(css: string, i: number): number {
|
|
72
|
-
const quote = css[i];
|
|
73
|
-
let j = i + 1;
|
|
74
|
-
while (j < css.length && css[j] !== quote) {
|
|
75
|
-
if (css[j] === "\\") j++;
|
|
76
|
-
j++;
|
|
77
|
-
}
|
|
78
|
-
return j + 1;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Index of the `}` that closes the `{` at `open` (or the end of input). */
|
|
82
|
-
function matchingBrace(css: string, open: number): number {
|
|
83
|
-
let depth = 0;
|
|
84
|
-
for (let j = open; j < css.length; j++) {
|
|
85
|
-
const c = css[j];
|
|
86
|
-
if (c === "/" && css[j + 1] === "*") {
|
|
87
|
-
const end = css.indexOf("*/", j + 2);
|
|
88
|
-
if (end === -1) return css.length;
|
|
89
|
-
j = end + 1;
|
|
90
|
-
} else if (c === '"' || c === "'") {
|
|
91
|
-
j = skipString(css, j) - 1;
|
|
92
|
-
} else if (c === "{") depth++;
|
|
93
|
-
else if (c === "}" && --depth === 0) return j;
|
|
94
|
-
}
|
|
95
|
-
return css.length;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** Split on `sep` characters that sit outside (), [] and strings. */
|
|
99
|
-
function splitTop(text: string, isSep: (c: string) => boolean): string[] {
|
|
100
|
-
const parts: string[] = [];
|
|
101
|
-
let depth = 0;
|
|
102
|
-
let from = 0;
|
|
103
|
-
for (let j = 0; j < text.length; j++) {
|
|
104
|
-
const c = text[j]!;
|
|
105
|
-
if (c === "(" || c === "[") depth++;
|
|
106
|
-
else if (c === ")" || c === "]") depth--;
|
|
107
|
-
else if (c === '"' || c === "'") j = skipString(text, j) - 1;
|
|
108
|
-
else if (depth === 0 && isSep(c)) {
|
|
109
|
-
parts.push(text.slice(from, j));
|
|
110
|
-
from = j + 1;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
parts.push(text.slice(from));
|
|
114
|
-
return parts;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** `.item:hover::after` → `.item.sX:hover::after` */
|
|
118
|
-
function scopeCompound(compound: string, hash: string): string {
|
|
119
|
-
let depth = 0;
|
|
120
|
-
for (let j = 0; j < compound.length; j++) {
|
|
121
|
-
const c = compound[j];
|
|
122
|
-
if (c === "(" || c === "[") depth++;
|
|
123
|
-
else if (c === ")" || c === "]") depth--;
|
|
124
|
-
else if (c === ":" && depth === 0) {
|
|
125
|
-
return `${compound.slice(0, j)}.${hash}${compound.slice(j)}`;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return `${compound}.${hash}`;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Rewrite a selector list: each selector gets the hash on its last compound
|
|
133
|
-
* that isn't `:global(…)`.
|
|
134
|
-
*/
|
|
135
|
-
function rewriteSelector(selector: string, hash: string): string {
|
|
136
|
-
return splitTop(selector, (c) => c === ",")
|
|
137
|
-
.map((part) => {
|
|
138
|
-
const trimmed = part.trim();
|
|
139
|
-
if (!trimmed) return trimmed;
|
|
140
|
-
// compounds and the combinators between them, in order
|
|
141
|
-
const tokens = trimmed.replace(/\s*([>+~])\s*/g, " $1 ").split(/(\s+)/);
|
|
142
|
-
const isCompound = (t: string) => t.trim() !== "" && !/^[>+~]$/.test(t);
|
|
143
|
-
const isGlobal = (t: string) => /^:global\(/.test(t);
|
|
144
|
-
for (let k = tokens.length - 1; k >= 0; k--) {
|
|
145
|
-
const t = tokens[k]!;
|
|
146
|
-
if (isCompound(t) && !isGlobal(t)) {
|
|
147
|
-
tokens[k] = scopeCompound(t, hash);
|
|
148
|
-
break;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
return tokens
|
|
152
|
-
.map((t) => (isGlobal(t) ? t.slice(8, -1) : t))
|
|
153
|
-
.join("")
|
|
154
|
-
.replace(/ ([>+~]) /g, " $1 ");
|
|
155
|
-
})
|
|
156
|
-
.join(", ");
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const STYLE_BLOCK_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Take a component file's (.tsx/.jsx) `<style>` blocks out of its source. Returns the
|
|
163
|
-
* source without them (the JSX parser would read CSS braces as expressions)
|
|
164
|
-
* and the scoped CSS of all blocks, or null when the file has none.
|
|
165
|
-
*/
|
|
166
|
-
export function extractStyles(
|
|
167
|
-
source: string,
|
|
168
|
-
hash: string,
|
|
169
|
-
filename: string,
|
|
170
|
-
): { code: string; css: string | null } {
|
|
171
|
-
// only component files have style blocks; a .ts file that merely
|
|
172
|
-
// mentions a <style> tag in a string must come through untouched
|
|
173
|
-
if (!/\.[jt]sx$/.test(filename)) return { code: source, css: null };
|
|
174
|
-
const blocks: string[] = [];
|
|
175
|
-
const code = source.replace(STYLE_BLOCK_RE, (_, css: string) => {
|
|
176
|
-
blocks.push(css.trim());
|
|
177
|
-
return "";
|
|
178
|
-
});
|
|
179
|
-
return {
|
|
180
|
-
code,
|
|
181
|
-
css: blocks.length ? scopeCSS(blocks.join("\n"), hash) : null,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Browser build: add the file's CSS to <head> once, when the module first
|
|
187
|
-
* loads. A server-rendered page already has it (see serverStyleSnippet), so
|
|
188
|
-
* the id check makes this a no-op there.
|
|
189
|
-
*/
|
|
190
|
-
export function clientStyleSnippet(hash: string, css: string): string {
|
|
191
|
-
return `if (typeof document !== "undefined" && !document.getElementById("sigil-${hash}")) {
|
|
192
|
-
const __sigilStyle = document.createElement("style");
|
|
193
|
-
__sigilStyle.id = "sigil-${hash}";
|
|
194
|
-
__sigilStyle.textContent = ${JSON.stringify(css)};
|
|
195
|
-
document.head.appendChild(__sigilStyle);
|
|
196
|
-
}
|
|
197
|
-
`;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* Server build: record the file's CSS by hash. The renderer puts a
|
|
202
|
-
* <style id="sigil-HASH"> in the page's <head> for every scope class the
|
|
203
|
-
* rendered HTML uses, so pages are styled before any script runs.
|
|
204
|
-
*/
|
|
205
|
-
export function serverStyleSnippet(hash: string, css: string): string {
|
|
206
|
-
return `(globalThis.__sigil_css__ ??= new Map()).set(${JSON.stringify(hash)}, ${JSON.stringify(css)});
|
|
207
|
-
`;
|
|
208
|
-
}
|
|
1
|
+
import { createHash } from "node: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 adding the file's hash class to every selector.
|
|
14
|
+
*
|
|
15
|
+
* - Each selector gets the class on its last compound, before any pseudo
|
|
16
|
+
* part: `.list .item:hover` → `.list .item.sX:hover`, so the rule only
|
|
17
|
+
* matches elements this file rendered.
|
|
18
|
+
* - `:global(…)` is left unscoped.
|
|
19
|
+
* - Grouping at-rules (@media, @supports, @container, @layer, @scope) are
|
|
20
|
+
* scoped inside; others with a block (@keyframes, @font-face, …) pass
|
|
21
|
+
* through, as do statements like @import.
|
|
22
|
+
* - Nested rules are scoped too; declarations are never touched.
|
|
23
|
+
*/
|
|
24
|
+
export function scopeCSS(css: string, hash: string): string {
|
|
25
|
+
let out = "";
|
|
26
|
+
let emitted = 0; // css[0..emitted) is already in `out`
|
|
27
|
+
let start = 0; // where the current statement began
|
|
28
|
+
let i = 0;
|
|
29
|
+
while (i < css.length) {
|
|
30
|
+
const c = css[i];
|
|
31
|
+
if (c === "/" && css[i + 1] === "*") {
|
|
32
|
+
const end = css.indexOf("*/", i + 2);
|
|
33
|
+
i = end === -1 ? css.length : end + 2;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (c === '"' || c === "'") {
|
|
37
|
+
i = skipString(css, i);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (c === ";" || c === "}") {
|
|
41
|
+
i++;
|
|
42
|
+
start = i;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (c !== "{") {
|
|
46
|
+
i++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const close = matchingBrace(css, i);
|
|
50
|
+
const prelude = css.slice(start, i);
|
|
51
|
+
const lead = /^(?:\s|\/\*[\s\S]*?\*\/)*/.exec(prelude)![0];
|
|
52
|
+
const head = prelude.slice(lead.length).trim();
|
|
53
|
+
const body = css.slice(i + 1, close);
|
|
54
|
+
let block: string;
|
|
55
|
+
if (head.startsWith("@")) {
|
|
56
|
+
block = `${head} {${PASSTHROUGH_AT.test(head) ? body : scopeCSS(body, hash)}}`;
|
|
57
|
+
} else {
|
|
58
|
+
block = `${rewriteSelector(head, hash)} {${scopeCSS(body, hash)}}`;
|
|
59
|
+
}
|
|
60
|
+
out += css.slice(emitted, start) + lead + block;
|
|
61
|
+
i = close + 1;
|
|
62
|
+
emitted = i;
|
|
63
|
+
start = i;
|
|
64
|
+
}
|
|
65
|
+
return out + css.slice(emitted);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const PASSTHROUGH_AT =
|
|
69
|
+
/^@(?:-webkit-)?(?:keyframes|font-face|page|property|counter-style|font-feature-values|font-palette-values|view-transition)\b/i;
|
|
70
|
+
|
|
71
|
+
function skipString(css: string, i: number): number {
|
|
72
|
+
const quote = css[i];
|
|
73
|
+
let j = i + 1;
|
|
74
|
+
while (j < css.length && css[j] !== quote) {
|
|
75
|
+
if (css[j] === "\\") j++;
|
|
76
|
+
j++;
|
|
77
|
+
}
|
|
78
|
+
return j + 1;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Index of the `}` that closes the `{` at `open` (or the end of input). */
|
|
82
|
+
function matchingBrace(css: string, open: number): number {
|
|
83
|
+
let depth = 0;
|
|
84
|
+
for (let j = open; j < css.length; j++) {
|
|
85
|
+
const c = css[j];
|
|
86
|
+
if (c === "/" && css[j + 1] === "*") {
|
|
87
|
+
const end = css.indexOf("*/", j + 2);
|
|
88
|
+
if (end === -1) return css.length;
|
|
89
|
+
j = end + 1;
|
|
90
|
+
} else if (c === '"' || c === "'") {
|
|
91
|
+
j = skipString(css, j) - 1;
|
|
92
|
+
} else if (c === "{") depth++;
|
|
93
|
+
else if (c === "}" && --depth === 0) return j;
|
|
94
|
+
}
|
|
95
|
+
return css.length;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Split on `sep` characters that sit outside (), [] and strings. */
|
|
99
|
+
function splitTop(text: string, isSep: (c: string) => boolean): string[] {
|
|
100
|
+
const parts: string[] = [];
|
|
101
|
+
let depth = 0;
|
|
102
|
+
let from = 0;
|
|
103
|
+
for (let j = 0; j < text.length; j++) {
|
|
104
|
+
const c = text[j]!;
|
|
105
|
+
if (c === "(" || c === "[") depth++;
|
|
106
|
+
else if (c === ")" || c === "]") depth--;
|
|
107
|
+
else if (c === '"' || c === "'") j = skipString(text, j) - 1;
|
|
108
|
+
else if (depth === 0 && isSep(c)) {
|
|
109
|
+
parts.push(text.slice(from, j));
|
|
110
|
+
from = j + 1;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
parts.push(text.slice(from));
|
|
114
|
+
return parts;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** `.item:hover::after` → `.item.sX:hover::after` */
|
|
118
|
+
function scopeCompound(compound: string, hash: string): string {
|
|
119
|
+
let depth = 0;
|
|
120
|
+
for (let j = 0; j < compound.length; j++) {
|
|
121
|
+
const c = compound[j];
|
|
122
|
+
if (c === "(" || c === "[") depth++;
|
|
123
|
+
else if (c === ")" || c === "]") depth--;
|
|
124
|
+
else if (c === ":" && depth === 0) {
|
|
125
|
+
return `${compound.slice(0, j)}.${hash}${compound.slice(j)}`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return `${compound}.${hash}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Rewrite a selector list: each selector gets the hash on its last compound
|
|
133
|
+
* that isn't `:global(…)`.
|
|
134
|
+
*/
|
|
135
|
+
function rewriteSelector(selector: string, hash: string): string {
|
|
136
|
+
return splitTop(selector, (c) => c === ",")
|
|
137
|
+
.map((part) => {
|
|
138
|
+
const trimmed = part.trim();
|
|
139
|
+
if (!trimmed) return trimmed;
|
|
140
|
+
// compounds and the combinators between them, in order
|
|
141
|
+
const tokens = trimmed.replace(/\s*([>+~])\s*/g, " $1 ").split(/(\s+)/);
|
|
142
|
+
const isCompound = (t: string) => t.trim() !== "" && !/^[>+~]$/.test(t);
|
|
143
|
+
const isGlobal = (t: string) => /^:global\(/.test(t);
|
|
144
|
+
for (let k = tokens.length - 1; k >= 0; k--) {
|
|
145
|
+
const t = tokens[k]!;
|
|
146
|
+
if (isCompound(t) && !isGlobal(t)) {
|
|
147
|
+
tokens[k] = scopeCompound(t, hash);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return tokens
|
|
152
|
+
.map((t) => (isGlobal(t) ? t.slice(8, -1) : t))
|
|
153
|
+
.join("")
|
|
154
|
+
.replace(/ ([>+~]) /g, " $1 ");
|
|
155
|
+
})
|
|
156
|
+
.join(", ");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const STYLE_BLOCK_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Take a component file's (.tsx/.jsx) `<style>` blocks out of its source. Returns the
|
|
163
|
+
* source without them (the JSX parser would read CSS braces as expressions)
|
|
164
|
+
* and the scoped CSS of all blocks, or null when the file has none.
|
|
165
|
+
*/
|
|
166
|
+
export function extractStyles(
|
|
167
|
+
source: string,
|
|
168
|
+
hash: string,
|
|
169
|
+
filename: string,
|
|
170
|
+
): { code: string; css: string | null } {
|
|
171
|
+
// only component files have style blocks; a .ts file that merely
|
|
172
|
+
// mentions a <style> tag in a string must come through untouched
|
|
173
|
+
if (!/\.[jt]sx$/.test(filename)) return { code: source, css: null };
|
|
174
|
+
const blocks: string[] = [];
|
|
175
|
+
const code = source.replace(STYLE_BLOCK_RE, (_, css: string) => {
|
|
176
|
+
blocks.push(css.trim());
|
|
177
|
+
return "";
|
|
178
|
+
});
|
|
179
|
+
return {
|
|
180
|
+
code,
|
|
181
|
+
css: blocks.length ? scopeCSS(blocks.join("\n"), hash) : null,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Browser build: add the file's CSS to <head> once, when the module first
|
|
187
|
+
* loads. A server-rendered page already has it (see serverStyleSnippet), so
|
|
188
|
+
* the id check makes this a no-op there.
|
|
189
|
+
*/
|
|
190
|
+
export function clientStyleSnippet(hash: string, css: string): string {
|
|
191
|
+
return `if (typeof document !== "undefined" && !document.getElementById("sigil-${hash}")) {
|
|
192
|
+
const __sigilStyle = document.createElement("style");
|
|
193
|
+
__sigilStyle.id = "sigil-${hash}";
|
|
194
|
+
__sigilStyle.textContent = ${JSON.stringify(css)};
|
|
195
|
+
document.head.appendChild(__sigilStyle);
|
|
196
|
+
}
|
|
197
|
+
`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Server build: record the file's CSS by hash. The renderer puts a
|
|
202
|
+
* <style id="sigil-HASH"> in the page's <head> for every scope class the
|
|
203
|
+
* rendered HTML uses, so pages are styled before any script runs.
|
|
204
|
+
*/
|
|
205
|
+
export function serverStyleSnippet(hash: string, css: string): string {
|
|
206
|
+
return `(globalThis.__sigil_css__ ??= new Map()).set(${JSON.stringify(hash)}, ${JSON.stringify(css)});
|
|
207
|
+
`;
|
|
208
|
+
}
|
|
@@ -1,93 +1,94 @@
|
|
|
1
|
-
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
-
import { warn } from "./warn.ts";
|
|
3
|
-
|
|
4
|
-
const FACTORIES = new Set(["createSignal", "createRawSignal", "createMemo"]);
|
|
5
|
-
|
|
6
|
-
/** `x()` inside `x.set(…)`: the update reading its own old value (`x++`). */
|
|
7
|
-
function isSelfRead(call: NodePath<t.CallExpression>, name: string): boolean {
|
|
8
|
-
return !!call.findParent(
|
|
9
|
-
(p) =>
|
|
10
|
-
p.isCallExpression() &&
|
|
11
|
-
t.isMemberExpression(p.node.callee) &&
|
|
12
|
-
t.isIdentifier(p.node.callee.object, { name }) &&
|
|
13
|
-
t.isIdentifier(p.node.callee.property, { name: "set" }),
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function isExported(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
path.
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
)
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
1
|
+
import { type NodePath, types as t } from "@babel/core";
|
|
2
|
+
import { warn } from "./warn.ts";
|
|
3
|
+
|
|
4
|
+
const FACTORIES = new Set(["createSignal", "createRawSignal", "createMemo"]);
|
|
5
|
+
|
|
6
|
+
/** `x()` inside `x.set(…)`: the update reading its own old value (`x++`). */
|
|
7
|
+
function isSelfRead(call: NodePath<t.CallExpression>, name: string): boolean {
|
|
8
|
+
return !!call.findParent(
|
|
9
|
+
(p) =>
|
|
10
|
+
p.isCallExpression() &&
|
|
11
|
+
t.isMemberExpression(p.node.callee) &&
|
|
12
|
+
t.isIdentifier(p.node.callee.object, { name }) &&
|
|
13
|
+
t.isIdentifier(p.node.callee.property, { name: "set" }),
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isExported(
|
|
18
|
+
decl: NodePath<t.VariableDeclarator>,
|
|
19
|
+
name: string,
|
|
20
|
+
): boolean {
|
|
21
|
+
if (decl.parentPath.parentPath?.isExportNamedDeclaration()) return true;
|
|
22
|
+
const program = decl.findParent((p) => p.isProgram()) as NodePath<t.Program>;
|
|
23
|
+
return program.node.body.some(
|
|
24
|
+
(n) =>
|
|
25
|
+
t.isExportNamedDeclaration(n) &&
|
|
26
|
+
!n.source &&
|
|
27
|
+
n.specifiers.some(
|
|
28
|
+
(s) => t.isExportSpecifier(s) && t.isIdentifier(s.local, { name }),
|
|
29
|
+
),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Warn about state nothing reacts to: declared and never read, or only ever
|
|
35
|
+
* written (`clicks++` with no `{clicks}` anywhere). Runs on the compiled
|
|
36
|
+
* program, where reads are calls (`x()`) and writes are `x.set(…)`.
|
|
37
|
+
* Exported state is read by other modules, so it is never reported.
|
|
38
|
+
*/
|
|
39
|
+
export function warnDeadReactivity(
|
|
40
|
+
path: NodePath<t.Program>,
|
|
41
|
+
exempt: Set<string>,
|
|
42
|
+
): void {
|
|
43
|
+
// The compiled code was built after Babel last counted references, so
|
|
44
|
+
// count them again, once: each signal then reads its own references
|
|
45
|
+
// instead of walking its whole scope, which made this quadratic.
|
|
46
|
+
path.scope.crawl();
|
|
47
|
+
path.traverse({
|
|
48
|
+
VariableDeclarator(decl) {
|
|
49
|
+
const { id, init } = decl.node;
|
|
50
|
+
if (!t.isIdentifier(id) || !t.isCallExpression(init)) return;
|
|
51
|
+
if (!t.isIdentifier(init.callee) || !FACTORIES.has(init.callee.name))
|
|
52
|
+
return;
|
|
53
|
+
const name = id.name;
|
|
54
|
+
if (exempt.has(name) || isExported(decl, name)) return;
|
|
55
|
+
|
|
56
|
+
const binding = decl.scope.getBinding(name);
|
|
57
|
+
if (!binding) return;
|
|
58
|
+
let reads = 0;
|
|
59
|
+
let writes = 0;
|
|
60
|
+
let passed = false;
|
|
61
|
+
for (const ref of binding.referencePaths) {
|
|
62
|
+
const parent = ref.parentPath;
|
|
63
|
+
if (!parent) continue;
|
|
64
|
+
const around: t.Node = parent.node;
|
|
65
|
+
if (parent.isCallExpression({ callee: ref.node })) {
|
|
66
|
+
if (!isSelfRead(parent, name)) reads++;
|
|
67
|
+
} else if (
|
|
68
|
+
parent.isMemberExpression({ object: ref.node }) &&
|
|
69
|
+
t.isIdentifier(parent.node.property, { name: "set" })
|
|
70
|
+
) {
|
|
71
|
+
if (parent.parentPath?.isCallExpression({ callee: parent.node }))
|
|
72
|
+
writes++;
|
|
73
|
+
} else if (
|
|
74
|
+
// handed on as the signal itself (a helper), not to the HMR registry
|
|
75
|
+
!parent.isMemberExpression({ object: ref.node }) &&
|
|
76
|
+
!(
|
|
77
|
+
t.isCallExpression(around) &&
|
|
78
|
+
t.isIdentifier(around.callee, { name: "__hmrRegister__" })
|
|
79
|
+
)
|
|
80
|
+
) {
|
|
81
|
+
passed = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (reads > 0 || passed) return;
|
|
85
|
+
const kind = init.callee.name === "createMemo" ? "$derived" : "$state";
|
|
86
|
+
warn(
|
|
87
|
+
decl.node,
|
|
88
|
+
writes > 0
|
|
89
|
+
? `"${name}" (${kind}) is written but never read: nothing reacts to it. Use a plain variable, or read it somewhere.`
|
|
90
|
+
: `"${name}" (${kind}) is never read.`,
|
|
91
|
+
);
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|