@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
|
@@ -1,133 +1,133 @@
|
|
|
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 {
|
|
6
|
-
analyzeFile,
|
|
7
|
-
type ComponentReport,
|
|
8
|
-
clearAnalysisCache,
|
|
9
|
-
} from "../src/analysis/hydration.ts";
|
|
10
|
-
|
|
11
|
-
const dir = join(tmpdir(), `sigil-analysis-${Date.now()}`);
|
|
12
|
-
|
|
13
|
-
const files: Record<string, string> = {
|
|
14
|
-
"state.ts": `
|
|
15
|
-
export let cart = $state({ n: 0 });
|
|
16
|
-
export const cartLabel = () => "cart " + cart.n;
|
|
17
|
-
export const formatDate = (d) => String(d);
|
|
18
|
-
`,
|
|
19
|
-
"Dynamic.tsx": `
|
|
20
|
-
export const Counter = () => {
|
|
21
|
-
let n = $state(0);
|
|
22
|
-
return <button onClick={() => n++}>{n}</button>;
|
|
23
|
-
};
|
|
24
|
-
`,
|
|
25
|
-
"Pure.tsx": `export const Pure = () => <i>pure</i>;`,
|
|
26
|
-
"Wrap.tsx": `export const Wrap = (props) => <div>{props.children}</div>;`,
|
|
27
|
-
"Page.tsx": `
|
|
28
|
-
import { Counter } from "./Dynamic.tsx";
|
|
29
|
-
import { Pure } from "./Pure.tsx";
|
|
30
|
-
import { Wrap } from "./Wrap.tsx";
|
|
31
|
-
import { cart, cartLabel, formatDate } from "./state.ts";
|
|
32
|
-
import { clsx } from "some-package";
|
|
33
|
-
import { Head } from "@sigil-dev/grimoire";
|
|
34
|
-
|
|
35
|
-
export const Footer = () => <footer><p>© now</p></footer>;
|
|
36
|
-
export const WithPure = () => <section><Pure /></section>;
|
|
37
|
-
export const WithCounter = () => <section><Counter /></section>;
|
|
38
|
-
export const Card = (props) => <article>{props.children}</article>;
|
|
39
|
-
export const Nested = () => <Wrap><b>x</b></Wrap>;
|
|
40
|
-
export const Handler = () => <a onClick={() => {}}>go</a>;
|
|
41
|
-
export const Bound = () => <input bind:value={x} />;
|
|
42
|
-
export const Ref = () => <div ref={el} />;
|
|
43
|
-
export const Spread = (props) => <div {...props} />;
|
|
44
|
-
export const ReadsState = () => <p>{cart.n}</p>;
|
|
45
|
-
export const CallsStateFn = () => <p>{cartLabel()}</p>;
|
|
46
|
-
export const CallsPureFn = () => <p>{formatDate(1)}</p>;
|
|
47
|
-
export const CallsPackage = () => <p className={clsx("a")}>x</p>;
|
|
48
|
-
export const Effect = () => { $effect(() => {}); return <p>x</p>; };
|
|
49
|
-
export const Head1 = () => <Head><title>x</title></Head>;
|
|
50
|
-
export const Win = () => <Window />;
|
|
51
|
-
export const Loop = () => <Loop />;
|
|
52
|
-
export const Async = async () => <p>x</p>;
|
|
53
|
-
export const FromProps = (props) => <props.Icon />;
|
|
54
|
-
`,
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
beforeAll(() => {
|
|
58
|
-
mkdirSync(dir, { recursive: true });
|
|
59
|
-
for (const [name, src] of Object.entries(files))
|
|
60
|
-
writeFileSync(join(dir, name), src);
|
|
61
|
-
clearAnalysisCache();
|
|
62
|
-
});
|
|
63
|
-
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
64
|
-
|
|
65
|
-
const reports = (): Record<string, ComponentReport> =>
|
|
66
|
-
Object.fromEntries(
|
|
67
|
-
analyzeFile(join(dir, "Page.tsx")).map((r) => [r.name, r]),
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
describe("component verdicts", () => {
|
|
71
|
-
test("no state, handlers or dynamic children is static", () => {
|
|
72
|
-
const r = reports();
|
|
73
|
-
expect(r.Footer!.verdict).toBe("static");
|
|
74
|
-
expect(r.WithPure!.verdict).toBe("static");
|
|
75
|
-
expect(r.CallsPureFn!.verdict).toBe("static");
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("one that renders children is static only if its children are", () => {
|
|
79
|
-
const r = reports();
|
|
80
|
-
expect(r.Card!.verdict).toBe("conditional");
|
|
81
|
-
expect(r.Nested!.verdict).toBe("conditional");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test("state, handlers, directives, refs and spreads each make it dynamic, with the line", () => {
|
|
85
|
-
const r = reports();
|
|
86
|
-
for (const [name, kind] of [
|
|
87
|
-
["Handler", "handler"],
|
|
88
|
-
["Bound", "directive"],
|
|
89
|
-
["Ref", "ref"],
|
|
90
|
-
["Spread", "spread"],
|
|
91
|
-
["Effect", "effect"],
|
|
92
|
-
] as const) {
|
|
93
|
-
expect(r[name]!.verdict).toBe("dynamic");
|
|
94
|
-
expect(r[name]!.reasons[0]!.kind).toBe(kind);
|
|
95
|
-
expect(r[name]!.reasons[0]!.line).toBeGreaterThan(0);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("a dynamic child makes its parent dynamic and says which child", () => {
|
|
100
|
-
const r = reports().WithCounter!;
|
|
101
|
-
expect(r.verdict).toBe("dynamic");
|
|
102
|
-
expect(r.reasons[0]!.kind).toBe("child");
|
|
103
|
-
expect(r.reasons[0]!.detail).toContain("<Counter>");
|
|
104
|
-
expect(r.reasons[0]!.detail).toContain("$state");
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
describe("what it cannot see into counts as dynamic", () => {
|
|
109
|
-
test("imported state, and an imported function that reads state", () => {
|
|
110
|
-
const r = reports();
|
|
111
|
-
expect(r.ReadsState!.reasons[0]!.kind).toBe("imported-state");
|
|
112
|
-
expect(r.CallsStateFn!.reasons[0]!.kind).toBe("imported-call");
|
|
113
|
-
expect(r.CallsStateFn!.reasons[0]!.detail).toContain("state.ts");
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
test("a call into a package is unknown", () => {
|
|
117
|
-
const r = reports().CallsPackage!;
|
|
118
|
-
expect(r.verdict).toBe("dynamic");
|
|
119
|
-
expect(r.reasons[0]!.kind).toBe("unknown-import");
|
|
120
|
-
expect(r.reasons[0]!.detail).toContain("some-package");
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test("Head and friends, components from props, async components and recursion", () => {
|
|
124
|
-
const r = reports();
|
|
125
|
-
// grimoire's Head only writes into <head>, which the server already did
|
|
126
|
-
expect(r.Head1!.verdict).toBe("static");
|
|
127
|
-
expect(r.Win!.reasons[0]!.kind).toBe("global");
|
|
128
|
-
expect(r.FromProps!.reasons[0]!.kind).toBe("unknown-child");
|
|
129
|
-
expect(r.Async!.reasons[0]!.kind).toBe("async");
|
|
130
|
-
expect(r.Loop!.verdict).toBe("dynamic");
|
|
131
|
-
expect(r.Loop!.reasons[0]!.detail).toContain("renders itself");
|
|
132
|
-
});
|
|
133
|
-
});
|
|
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 {
|
|
6
|
+
analyzeFile,
|
|
7
|
+
type ComponentReport,
|
|
8
|
+
clearAnalysisCache,
|
|
9
|
+
} from "../src/analysis/hydration.ts";
|
|
10
|
+
|
|
11
|
+
const dir = join(tmpdir(), `sigil-analysis-${Date.now()}`);
|
|
12
|
+
|
|
13
|
+
const files: Record<string, string> = {
|
|
14
|
+
"state.ts": `
|
|
15
|
+
export let cart = $state({ n: 0 });
|
|
16
|
+
export const cartLabel = () => "cart " + cart.n;
|
|
17
|
+
export const formatDate = (d) => String(d);
|
|
18
|
+
`,
|
|
19
|
+
"Dynamic.tsx": `
|
|
20
|
+
export const Counter = () => {
|
|
21
|
+
let n = $state(0);
|
|
22
|
+
return <button onClick={() => n++}>{n}</button>;
|
|
23
|
+
};
|
|
24
|
+
`,
|
|
25
|
+
"Pure.tsx": `export const Pure = () => <i>pure</i>;`,
|
|
26
|
+
"Wrap.tsx": `export const Wrap = (props) => <div>{props.children}</div>;`,
|
|
27
|
+
"Page.tsx": `
|
|
28
|
+
import { Counter } from "./Dynamic.tsx";
|
|
29
|
+
import { Pure } from "./Pure.tsx";
|
|
30
|
+
import { Wrap } from "./Wrap.tsx";
|
|
31
|
+
import { cart, cartLabel, formatDate } from "./state.ts";
|
|
32
|
+
import { clsx } from "some-package";
|
|
33
|
+
import { Head } from "@sigil-dev/grimoire";
|
|
34
|
+
|
|
35
|
+
export const Footer = () => <footer><p>© now</p></footer>;
|
|
36
|
+
export const WithPure = () => <section><Pure /></section>;
|
|
37
|
+
export const WithCounter = () => <section><Counter /></section>;
|
|
38
|
+
export const Card = (props) => <article>{props.children}</article>;
|
|
39
|
+
export const Nested = () => <Wrap><b>x</b></Wrap>;
|
|
40
|
+
export const Handler = () => <a onClick={() => {}}>go</a>;
|
|
41
|
+
export const Bound = () => <input bind:value={x} />;
|
|
42
|
+
export const Ref = () => <div ref={el} />;
|
|
43
|
+
export const Spread = (props) => <div {...props} />;
|
|
44
|
+
export const ReadsState = () => <p>{cart.n}</p>;
|
|
45
|
+
export const CallsStateFn = () => <p>{cartLabel()}</p>;
|
|
46
|
+
export const CallsPureFn = () => <p>{formatDate(1)}</p>;
|
|
47
|
+
export const CallsPackage = () => <p className={clsx("a")}>x</p>;
|
|
48
|
+
export const Effect = () => { $effect(() => {}); return <p>x</p>; };
|
|
49
|
+
export const Head1 = () => <Head><title>x</title></Head>;
|
|
50
|
+
export const Win = () => <Window />;
|
|
51
|
+
export const Loop = () => <Loop />;
|
|
52
|
+
export const Async = async () => <p>x</p>;
|
|
53
|
+
export const FromProps = (props) => <props.Icon />;
|
|
54
|
+
`,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
beforeAll(() => {
|
|
58
|
+
mkdirSync(dir, { recursive: true });
|
|
59
|
+
for (const [name, src] of Object.entries(files))
|
|
60
|
+
writeFileSync(join(dir, name), src);
|
|
61
|
+
clearAnalysisCache();
|
|
62
|
+
});
|
|
63
|
+
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
64
|
+
|
|
65
|
+
const reports = (): Record<string, ComponentReport> =>
|
|
66
|
+
Object.fromEntries(
|
|
67
|
+
analyzeFile(join(dir, "Page.tsx")).map((r) => [r.name, r]),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
describe("component verdicts", () => {
|
|
71
|
+
test("no state, handlers or dynamic children is static", () => {
|
|
72
|
+
const r = reports();
|
|
73
|
+
expect(r.Footer!.verdict).toBe("static");
|
|
74
|
+
expect(r.WithPure!.verdict).toBe("static");
|
|
75
|
+
expect(r.CallsPureFn!.verdict).toBe("static");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("one that renders children is static only if its children are", () => {
|
|
79
|
+
const r = reports();
|
|
80
|
+
expect(r.Card!.verdict).toBe("conditional");
|
|
81
|
+
expect(r.Nested!.verdict).toBe("conditional");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("state, handlers, directives, refs and spreads each make it dynamic, with the line", () => {
|
|
85
|
+
const r = reports();
|
|
86
|
+
for (const [name, kind] of [
|
|
87
|
+
["Handler", "handler"],
|
|
88
|
+
["Bound", "directive"],
|
|
89
|
+
["Ref", "ref"],
|
|
90
|
+
["Spread", "spread"],
|
|
91
|
+
["Effect", "effect"],
|
|
92
|
+
] as const) {
|
|
93
|
+
expect(r[name]!.verdict).toBe("dynamic");
|
|
94
|
+
expect(r[name]!.reasons[0]!.kind).toBe(kind);
|
|
95
|
+
expect(r[name]!.reasons[0]!.line).toBeGreaterThan(0);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a dynamic child makes its parent dynamic and says which child", () => {
|
|
100
|
+
const r = reports().WithCounter!;
|
|
101
|
+
expect(r.verdict).toBe("dynamic");
|
|
102
|
+
expect(r.reasons[0]!.kind).toBe("child");
|
|
103
|
+
expect(r.reasons[0]!.detail).toContain("<Counter>");
|
|
104
|
+
expect(r.reasons[0]!.detail).toContain("$state");
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("what it cannot see into counts as dynamic", () => {
|
|
109
|
+
test("imported state, and an imported function that reads state", () => {
|
|
110
|
+
const r = reports();
|
|
111
|
+
expect(r.ReadsState!.reasons[0]!.kind).toBe("imported-state");
|
|
112
|
+
expect(r.CallsStateFn!.reasons[0]!.kind).toBe("imported-call");
|
|
113
|
+
expect(r.CallsStateFn!.reasons[0]!.detail).toContain("state.ts");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("a call into a package is unknown", () => {
|
|
117
|
+
const r = reports().CallsPackage!;
|
|
118
|
+
expect(r.verdict).toBe("dynamic");
|
|
119
|
+
expect(r.reasons[0]!.kind).toBe("unknown-import");
|
|
120
|
+
expect(r.reasons[0]!.detail).toContain("some-package");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("Head and friends, components from props, async components and recursion", () => {
|
|
124
|
+
const r = reports();
|
|
125
|
+
// grimoire's Head only writes into <head>, which the server already did
|
|
126
|
+
expect(r.Head1!.verdict).toBe("static");
|
|
127
|
+
expect(r.Win!.reasons[0]!.kind).toBe("global");
|
|
128
|
+
expect(r.FromProps!.reasons[0]!.kind).toBe("unknown-child");
|
|
129
|
+
expect(r.Async!.reasons[0]!.kind).toBe("async");
|
|
130
|
+
expect(r.Loop!.verdict).toBe("dynamic");
|
|
131
|
+
expect(r.Loop!.reasons[0]!.detail).toContain("renders itself");
|
|
132
|
+
});
|
|
133
|
+
});
|