@sigil-dev/compiler 0.11.1 → 0.11.2
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/package.json +1 -1
- package/src/analysis/hydration.ts +1112 -1012
- package/src/babel/index.ts +24 -9
- package/src/babel/util/css.ts +217 -208
- package/src/babel/util/dead-code.ts +15 -0
- package/src/babel/util/static-env.ts +7 -6
- package/test/compiles-to-itself.test.ts +72 -0
- package/test/static-env.test.ts +8 -5
package/src/babel/index.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
type NodePath,
|
|
3
|
+
type PluginObject,
|
|
4
|
+
types as t,
|
|
5
|
+
template,
|
|
6
|
+
} from "@babel/core";
|
|
4
7
|
import { createResolver, markStaticUses } from "../analysis/hydration.ts";
|
|
5
8
|
import { handleDerived } from "./handlers/dom/derived.ts";
|
|
6
9
|
import { handleEffect } from "./handlers/dom/effect.ts";
|
|
7
10
|
import { handleState } from "./handlers/dom/state.ts";
|
|
8
11
|
import { handleDerivedSSR } from "./handlers/ssr/derived.ts";
|
|
9
12
|
import { handleStateSSR } from "./handlers/ssr/state.ts";
|
|
13
|
+
import { wrapConditionalReturns } from "./jsx/conditional-return.ts";
|
|
10
14
|
import { processElement, processFragment } from "./jsx/index.ts";
|
|
11
15
|
import { processElementSSR, processFragmentSSR } from "./jsx/ssr.ts";
|
|
16
|
+
import { textareaChildrenToValue } from "./jsx/textarea.ts";
|
|
12
17
|
import { SignalScope } from "./jsx/utils.ts";
|
|
13
18
|
import { compileState } from "./util/compile-state.ts";
|
|
14
|
-
import { warnDeadReactivity } from "./util/dead-code.ts";
|
|
19
|
+
import { importsFactory, warnDeadReactivity } from "./util/dead-code.ts";
|
|
15
20
|
import { getMacro } from "./util/helpers.ts";
|
|
16
21
|
import { checkLinks } from "./util/links";
|
|
17
22
|
import { MAGIC } from "./util/magic.ts";
|
|
@@ -231,10 +236,16 @@ export default function sigilPlugin(): PluginObject {
|
|
|
231
236
|
collectImportRoots(path);
|
|
232
237
|
},
|
|
233
238
|
exit(path, state) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
239
|
+
// a signal is made by one of the runtime's factories: for a rune
|
|
240
|
+
// (which puts its name in `signals`), or called by the file
|
|
241
|
+
// itself (which imports it). A file with neither has no signal
|
|
242
|
+
// to warn about, and counting its references again was most of
|
|
243
|
+
// a plain module's compile
|
|
244
|
+
if (signals.size > 0 || importsFactory(path))
|
|
245
|
+
warnDeadReactivity(
|
|
246
|
+
path,
|
|
247
|
+
new Set([...storeSignals, ...propObjects, ...importedSignals]),
|
|
248
|
+
);
|
|
238
249
|
|
|
239
250
|
if (asyncDefaultPath && !isSSR) {
|
|
240
251
|
let fnNode: t.FunctionDeclaration;
|
|
@@ -793,7 +804,11 @@ function collectJsxFunctions(program: NodePath<t.Program>): Set<string> {
|
|
|
793
804
|
},
|
|
794
805
|
VariableDeclarator(p) {
|
|
795
806
|
const init = p.get("init");
|
|
796
|
-
if (
|
|
807
|
+
if (
|
|
808
|
+
!t.isIdentifier(p.node.id) ||
|
|
809
|
+
!(init.isArrowFunctionExpression() || init.isFunctionExpression())
|
|
810
|
+
)
|
|
811
|
+
return;
|
|
797
812
|
if (builds(init as NodePath)) names.add(p.node.id.name);
|
|
798
813
|
},
|
|
799
814
|
});
|
package/src/babel/util/css.ts
CHANGED
|
@@ -1,208 +1,217 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (c === "
|
|
41
|
-
i
|
|
42
|
-
|
|
43
|
-
continue;
|
|
44
|
-
}
|
|
45
|
-
if (c
|
|
46
|
-
i
|
|
47
|
-
continue;
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isAbsolute, relative } from "node:path";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Compute a deterministic hash from a file path.
|
|
6
|
+
* Same file always gets same hash. Different files always get different hashes.
|
|
7
|
+
* 's' prefix for Sigil.
|
|
8
|
+
*
|
|
9
|
+
* An absolute path is hashed relative to the working directory (the app's
|
|
10
|
+
* root), with `/`: the class must not depend on where the app sits on disk
|
|
11
|
+
* or on the OS, or a build made in one directory and run in another
|
|
12
|
+
* scopes its server HTML differently from the browser bundle it ships.
|
|
13
|
+
*/
|
|
14
|
+
export function computeHash(filePath: string): string {
|
|
15
|
+
const key = isAbsolute(filePath)
|
|
16
|
+
? relative(process.cwd(), filePath).replaceAll("\\", "/")
|
|
17
|
+
: filePath.replaceAll("\\", "/");
|
|
18
|
+
return `s${createHash("md5").update(key).digest("hex").slice(0, 8)}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Scope CSS by adding the file's hash class to every selector.
|
|
23
|
+
*
|
|
24
|
+
* - Each selector gets the class on its last compound, before any pseudo
|
|
25
|
+
* part: `.list .item:hover` → `.list .item.sX:hover`, so the rule only
|
|
26
|
+
* matches elements this file rendered.
|
|
27
|
+
* - `:global(…)` is left unscoped.
|
|
28
|
+
* - Grouping at-rules (@media, @supports, @container, @layer, @scope) are
|
|
29
|
+
* scoped inside; others with a block (@keyframes, @font-face, …) pass
|
|
30
|
+
* through, as do statements like @import.
|
|
31
|
+
* - Nested rules are scoped too; declarations are never touched.
|
|
32
|
+
*/
|
|
33
|
+
export function scopeCSS(css: string, hash: string): string {
|
|
34
|
+
let out = "";
|
|
35
|
+
let emitted = 0; // css[0..emitted) is already in `out`
|
|
36
|
+
let start = 0; // where the current statement began
|
|
37
|
+
let i = 0;
|
|
38
|
+
while (i < css.length) {
|
|
39
|
+
const c = css[i];
|
|
40
|
+
if (c === "/" && css[i + 1] === "*") {
|
|
41
|
+
const end = css.indexOf("*/", i + 2);
|
|
42
|
+
i = end === -1 ? css.length : end + 2;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (c === '"' || c === "'") {
|
|
46
|
+
i = skipString(css, i);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (c === ";" || c === "}") {
|
|
50
|
+
i++;
|
|
51
|
+
start = i;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (c !== "{") {
|
|
55
|
+
i++;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const close = matchingBrace(css, i);
|
|
59
|
+
const prelude = css.slice(start, i);
|
|
60
|
+
const lead = /^(?:\s|\/\*[\s\S]*?\*\/)*/.exec(prelude)![0];
|
|
61
|
+
const head = prelude.slice(lead.length).trim();
|
|
62
|
+
const body = css.slice(i + 1, close);
|
|
63
|
+
let block: string;
|
|
64
|
+
if (head.startsWith("@")) {
|
|
65
|
+
block = `${head} {${PASSTHROUGH_AT.test(head) ? body : scopeCSS(body, hash)}}`;
|
|
66
|
+
} else {
|
|
67
|
+
block = `${rewriteSelector(head, hash)} {${scopeCSS(body, hash)}}`;
|
|
68
|
+
}
|
|
69
|
+
out += css.slice(emitted, start) + lead + block;
|
|
70
|
+
i = close + 1;
|
|
71
|
+
emitted = i;
|
|
72
|
+
start = i;
|
|
73
|
+
}
|
|
74
|
+
return out + css.slice(emitted);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const PASSTHROUGH_AT =
|
|
78
|
+
/^@(?:-webkit-)?(?:keyframes|font-face|page|property|counter-style|font-feature-values|font-palette-values|view-transition)\b/i;
|
|
79
|
+
|
|
80
|
+
function skipString(css: string, i: number): number {
|
|
81
|
+
const quote = css[i];
|
|
82
|
+
let j = i + 1;
|
|
83
|
+
while (j < css.length && css[j] !== quote) {
|
|
84
|
+
if (css[j] === "\\") j++;
|
|
85
|
+
j++;
|
|
86
|
+
}
|
|
87
|
+
return j + 1;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Index of the `}` that closes the `{` at `open` (or the end of input). */
|
|
91
|
+
function matchingBrace(css: string, open: number): number {
|
|
92
|
+
let depth = 0;
|
|
93
|
+
for (let j = open; j < css.length; j++) {
|
|
94
|
+
const c = css[j];
|
|
95
|
+
if (c === "/" && css[j + 1] === "*") {
|
|
96
|
+
const end = css.indexOf("*/", j + 2);
|
|
97
|
+
if (end === -1) return css.length;
|
|
98
|
+
j = end + 1;
|
|
99
|
+
} else if (c === '"' || c === "'") {
|
|
100
|
+
j = skipString(css, j) - 1;
|
|
101
|
+
} else if (c === "{") depth++;
|
|
102
|
+
else if (c === "}" && --depth === 0) return j;
|
|
103
|
+
}
|
|
104
|
+
return css.length;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Split on `sep` characters that sit outside (), [] and strings. */
|
|
108
|
+
function splitTop(text: string, isSep: (c: string) => boolean): string[] {
|
|
109
|
+
const parts: string[] = [];
|
|
110
|
+
let depth = 0;
|
|
111
|
+
let from = 0;
|
|
112
|
+
for (let j = 0; j < text.length; j++) {
|
|
113
|
+
const c = text[j]!;
|
|
114
|
+
if (c === "(" || c === "[") depth++;
|
|
115
|
+
else if (c === ")" || c === "]") depth--;
|
|
116
|
+
else if (c === '"' || c === "'") j = skipString(text, j) - 1;
|
|
117
|
+
else if (depth === 0 && isSep(c)) {
|
|
118
|
+
parts.push(text.slice(from, j));
|
|
119
|
+
from = j + 1;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
parts.push(text.slice(from));
|
|
123
|
+
return parts;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** `.item:hover::after` → `.item.sX:hover::after` */
|
|
127
|
+
function scopeCompound(compound: string, hash: string): string {
|
|
128
|
+
let depth = 0;
|
|
129
|
+
for (let j = 0; j < compound.length; j++) {
|
|
130
|
+
const c = compound[j];
|
|
131
|
+
if (c === "(" || c === "[") depth++;
|
|
132
|
+
else if (c === ")" || c === "]") depth--;
|
|
133
|
+
else if (c === ":" && depth === 0) {
|
|
134
|
+
return `${compound.slice(0, j)}.${hash}${compound.slice(j)}`;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return `${compound}.${hash}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Rewrite a selector list: each selector gets the hash on its last compound
|
|
142
|
+
* that isn't `:global(…)`.
|
|
143
|
+
*/
|
|
144
|
+
function rewriteSelector(selector: string, hash: string): string {
|
|
145
|
+
return splitTop(selector, (c) => c === ",")
|
|
146
|
+
.map((part) => {
|
|
147
|
+
const trimmed = part.trim();
|
|
148
|
+
if (!trimmed) return trimmed;
|
|
149
|
+
// compounds and the combinators between them, in order
|
|
150
|
+
const tokens = trimmed.replace(/\s*([>+~])\s*/g, " $1 ").split(/(\s+)/);
|
|
151
|
+
const isCompound = (t: string) => t.trim() !== "" && !/^[>+~]$/.test(t);
|
|
152
|
+
const isGlobal = (t: string) => /^:global\(/.test(t);
|
|
153
|
+
for (let k = tokens.length - 1; k >= 0; k--) {
|
|
154
|
+
const t = tokens[k]!;
|
|
155
|
+
if (isCompound(t) && !isGlobal(t)) {
|
|
156
|
+
tokens[k] = scopeCompound(t, hash);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return tokens
|
|
161
|
+
.map((t) => (isGlobal(t) ? t.slice(8, -1) : t))
|
|
162
|
+
.join("")
|
|
163
|
+
.replace(/ ([>+~]) /g, " $1 ");
|
|
164
|
+
})
|
|
165
|
+
.join(", ");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const STYLE_BLOCK_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Take a component file's (.tsx/.jsx) `<style>` blocks out of its source. Returns the
|
|
172
|
+
* source without them (the JSX parser would read CSS braces as expressions)
|
|
173
|
+
* and the scoped CSS of all blocks, or null when the file has none.
|
|
174
|
+
*/
|
|
175
|
+
export function extractStyles(
|
|
176
|
+
source: string,
|
|
177
|
+
hash: string,
|
|
178
|
+
filename: string,
|
|
179
|
+
): { code: string; css: string | null } {
|
|
180
|
+
// only component files have style blocks; a .ts file that merely
|
|
181
|
+
// mentions a <style> tag in a string must come through untouched
|
|
182
|
+
if (!/\.[jt]sx$/.test(filename)) return { code: source, css: null };
|
|
183
|
+
const blocks: string[] = [];
|
|
184
|
+
const code = source.replace(STYLE_BLOCK_RE, (_, css: string) => {
|
|
185
|
+
blocks.push(css.trim());
|
|
186
|
+
return "";
|
|
187
|
+
});
|
|
188
|
+
return {
|
|
189
|
+
code,
|
|
190
|
+
css: blocks.length ? scopeCSS(blocks.join("\n"), hash) : null,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Browser build: add the file's CSS to <head> once, when the module first
|
|
196
|
+
* loads. A server-rendered page already has it (see serverStyleSnippet), so
|
|
197
|
+
* the id check makes this a no-op there.
|
|
198
|
+
*/
|
|
199
|
+
export function clientStyleSnippet(hash: string, css: string): string {
|
|
200
|
+
return `if (typeof document !== "undefined" && !document.getElementById("sigil-${hash}")) {
|
|
201
|
+
const __sigilStyle = document.createElement("style");
|
|
202
|
+
__sigilStyle.id = "sigil-${hash}";
|
|
203
|
+
__sigilStyle.textContent = ${JSON.stringify(css)};
|
|
204
|
+
document.head.appendChild(__sigilStyle);
|
|
205
|
+
}
|
|
206
|
+
`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Server build: record the file's CSS by hash. The renderer puts a
|
|
211
|
+
* <style id="sigil-HASH"> in the page's <head> for every scope class the
|
|
212
|
+
* rendered HTML uses, so pages are styled before any script runs.
|
|
213
|
+
*/
|
|
214
|
+
export function serverStyleSnippet(hash: string, css: string): string {
|
|
215
|
+
return `(globalThis.__sigil_css__ ??= new Map()).set(${JSON.stringify(hash)}, ${JSON.stringify(css)});
|
|
216
|
+
`;
|
|
217
|
+
}
|
|
@@ -3,6 +3,21 @@ import { warn } from "./warn.ts";
|
|
|
3
3
|
|
|
4
4
|
const FACTORIES = new Set(["createSignal", "createRawSignal", "createMemo"]);
|
|
5
5
|
|
|
6
|
+
/** Whether the program imports a signal factory, under any local name. */
|
|
7
|
+
export function importsFactory(program: NodePath<t.Program>): boolean {
|
|
8
|
+
return program.node.body.some(
|
|
9
|
+
(n) =>
|
|
10
|
+
t.isImportDeclaration(n) &&
|
|
11
|
+
n.specifiers.some(
|
|
12
|
+
(s) =>
|
|
13
|
+
t.isImportSpecifier(s) &&
|
|
14
|
+
FACTORIES.has(
|
|
15
|
+
t.isIdentifier(s.imported) ? s.imported.name : s.imported.value,
|
|
16
|
+
),
|
|
17
|
+
),
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
6
21
|
/** `x()` inside `x.set(…)`: the update reading its own old value (`x++`). */
|
|
7
22
|
function isSelfRead(call: NodePath<t.CallExpression>, name: string): boolean {
|
|
8
23
|
return !!call.findParent(
|
|
@@ -22,8 +22,11 @@ export function setStaticEnv(
|
|
|
22
22
|
* `$env/static` is read when the app is built, so a value cannot change
|
|
23
23
|
* afterwards, and written in place it lets the bundler drop the branches it
|
|
24
24
|
* decides: `if (PUBLIC_BETA === "on")` with BETA off leaves nothing of the
|
|
25
|
-
* feature in the browser bundle.
|
|
26
|
-
*
|
|
25
|
+
* feature in the browser bundle. The browser only: server code reads the
|
|
26
|
+
* environment of the process running it (grimoire's .grimoire/env modules),
|
|
27
|
+
* so a server build holds no values and doesn't depend on where it was
|
|
28
|
+
* made. A browser import of `$env/static/private` stays, and fails the
|
|
29
|
+
* build as before.
|
|
27
30
|
* Missing variables read as the module reads them: `env.MISSING` is
|
|
28
31
|
* undefined, and a named import of one is left for the module to refuse.
|
|
29
32
|
*/
|
|
@@ -32,16 +35,14 @@ export function inlineStaticEnv(
|
|
|
32
35
|
mode: string,
|
|
33
36
|
): void {
|
|
34
37
|
const env = values;
|
|
35
|
-
if (!env) return;
|
|
38
|
+
if (!env || mode === "ssr") return;
|
|
36
39
|
for (const decl of program.get("body")) {
|
|
37
40
|
if (!decl.isImportDeclaration()) continue;
|
|
38
41
|
const source = decl.node.source.value;
|
|
39
42
|
const table =
|
|
40
43
|
source === "$env/static/public"
|
|
41
44
|
? env.public
|
|
42
|
-
:
|
|
43
|
-
? env.private
|
|
44
|
-
: undefined;
|
|
45
|
+
: undefined;
|
|
45
46
|
if (!table) continue;
|
|
46
47
|
for (const spec of decl.get("specifiers")) {
|
|
47
48
|
if (!spec.isImportSpecifier()) continue;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { compilesToItself, createResolver } from "../src/analysis/hydration";
|
|
6
|
+
|
|
7
|
+
// A server build skips Babel for modules the compiler would leave as they
|
|
8
|
+
// are; anything a rune could reach still compiles.
|
|
9
|
+
describe("compilesToItself", () => {
|
|
10
|
+
const dir = join(tmpdir(), `sigil-cti-${Date.now()}`);
|
|
11
|
+
const resolve = createResolver({ $lib: join(dir, "lib") });
|
|
12
|
+
const files: Record<string, string> = {
|
|
13
|
+
"lib/plain.ts": "export const add = (a: number, b: number) => a + b;\n",
|
|
14
|
+
"lib/count.ts":
|
|
15
|
+
"export let count = $state(0);\nexport const read = () => count;\n",
|
|
16
|
+
"lib/barrel.ts":
|
|
17
|
+
'export { add } from "./plain.ts";\nexport * from "./count.ts";\n',
|
|
18
|
+
"lib/plain-barrel.ts": 'export { add } from "./plain.ts";\n',
|
|
19
|
+
"lib/local.ts": 'import { count } from "./count.ts";\nexport { count };\n',
|
|
20
|
+
};
|
|
21
|
+
const check = (name: string, source: string) =>
|
|
22
|
+
compilesToItself(join(dir, name), source, resolve);
|
|
23
|
+
|
|
24
|
+
beforeAll(() => {
|
|
25
|
+
mkdirSync(join(dir, "lib"), { recursive: true });
|
|
26
|
+
for (const [f, c] of Object.entries(files)) writeFileSync(join(dir, f), c);
|
|
27
|
+
});
|
|
28
|
+
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
29
|
+
|
|
30
|
+
test("plain code importing plain code", () => {
|
|
31
|
+
expect(
|
|
32
|
+
check(
|
|
33
|
+
"a.ts",
|
|
34
|
+
'import { add } from "$lib/plain.ts";\nexport const x = add(1, 2);\n',
|
|
35
|
+
),
|
|
36
|
+
).toBe(true);
|
|
37
|
+
expect(check("a.ts", 'import { add } from "$lib/plain-barrel.ts";\n')).toBe(
|
|
38
|
+
true,
|
|
39
|
+
);
|
|
40
|
+
// what it can't resolve (a package) isn't state the compiler knows of either
|
|
41
|
+
expect(check("a.ts", 'import { z } from "zod";\n')).toBe(true);
|
|
42
|
+
expect(check("a.ts", 'import type { Count } from "$lib/count.ts";\n')).toBe(
|
|
43
|
+
true,
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("not with a rune, a style, or JSX's extension", () => {
|
|
48
|
+
expect(check("a.ts", "let n = $state(0);\n")).toBe(false);
|
|
49
|
+
expect(check("a.ts", "$effect(() => {});\n")).toBe(false);
|
|
50
|
+
expect(check("a.ts", "export {};\n<style>p{}</style>\n")).toBe(false);
|
|
51
|
+
expect(check("a.tsx", "export const x = 1;\n")).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("not when it imports state, derived values or functions reading them", () => {
|
|
55
|
+
expect(check("a.ts", 'import { count } from "$lib/count.ts";\n')).toBe(
|
|
56
|
+
false,
|
|
57
|
+
);
|
|
58
|
+
expect(check("a.ts", 'import { read } from "$lib/count.ts";\n')).toBe(
|
|
59
|
+
false,
|
|
60
|
+
);
|
|
61
|
+
expect(check("a.ts", 'import { count } from "$lib/barrel.ts";\n')).toBe(
|
|
62
|
+
false,
|
|
63
|
+
);
|
|
64
|
+
expect(check("a.ts", 'import { count } from "$lib/local.ts";\n')).toBe(
|
|
65
|
+
false,
|
|
66
|
+
);
|
|
67
|
+
expect(check("a.ts", 'import * as c from "$lib/count.ts";\n')).toBe(false);
|
|
68
|
+
expect(check("a.ts", 'export * from "$lib/count.ts";\n')).toBe(false);
|
|
69
|
+
// but the plain name from that same barrel is fine
|
|
70
|
+
expect(check("a.ts", 'import { add } from "$lib/barrel.ts";\n')).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
});
|
package/test/static-env.test.ts
CHANGED
|
@@ -46,11 +46,14 @@ describe("$env/static inlined", () => {
|
|
|
46
46
|
expect(out).toContain('from "$env/static/public"');
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
test("
|
|
50
|
-
setStaticEnv({}, { SECRET: "s3cret" });
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
test("the server inlines nothing: it reads the environment it runs in", () => {
|
|
50
|
+
setStaticEnv({ PUBLIC_A: "a" }, { SECRET: "s3cret" });
|
|
51
|
+
const priv = `import { SECRET } from "$env/static/private"; export const s = SECRET;`;
|
|
52
|
+
const server = transformSSR(priv);
|
|
53
|
+
expect(server).not.toContain("s3cret");
|
|
54
|
+
expect(server).toContain("$env/static/private");
|
|
55
|
+
expect(transformSSR(`import { PUBLIC_A } from "$env/static/public"; export const a = PUBLIC_A;`)).toContain("$env/static/public");
|
|
56
|
+
const browser = transformDOM(priv);
|
|
54
57
|
expect(browser).not.toContain("s3cret");
|
|
55
58
|
expect(browser).toContain("$env/static/private");
|
|
56
59
|
});
|