@sigil-dev/compiler 0.9.29 → 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.
Files changed (38) hide show
  1. package/index.ts +6 -4
  2. package/package.json +1 -1
  3. package/src/babel/index.ts +763 -750
  4. package/src/babel/jsx/anchor-mount.ts +338 -333
  5. package/src/babel/jsx/children.ts +116 -116
  6. package/src/babel/jsx/element.ts +1040 -1034
  7. package/src/babel/jsx/fragment.ts +271 -271
  8. package/src/babel/jsx/keyed-list.ts +339 -339
  9. package/src/babel/jsx/utils.ts +356 -356
  10. package/src/babel/util/bind.ts +143 -143
  11. package/src/babel/util/css.ts +208 -208
  12. package/src/babel/util/dead-code.ts +94 -93
  13. package/src/babel/util/links.ts +101 -0
  14. package/src/babel/util/reactive-fns.ts +110 -110
  15. package/src/babel/util/runtime-names.ts +88 -0
  16. package/src/babel/util/state-imports.ts +122 -121
  17. package/src/babel/util/static-assertion.ts +51 -51
  18. package/src/babel/util/static-env.ts +84 -0
  19. package/src/vite/index.ts +61 -61
  20. package/test/bun-plugin.test.ts +45 -45
  21. package/test/context.test.ts +110 -102
  22. package/test/fixtures/stores/index.ts +2 -2
  23. package/test/hydration-analysis.test.ts +133 -133
  24. package/test/hydration.test.ts +315 -313
  25. package/test/jsx.test.ts +254 -254
  26. package/test/links.test.ts +57 -0
  27. package/test/reactive-helpers.test.ts +135 -135
  28. package/test/render-parity.test.ts +909 -907
  29. package/test/runtime-names.test.ts +60 -0
  30. package/test/scoped-css.test.ts +166 -166
  31. package/test/ssr-boolean-attrs.test.ts +56 -56
  32. package/test/ssr-raw-text.test.ts +68 -68
  33. package/test/ssr.test.ts +30 -0
  34. package/test/static-assertion.test.ts +123 -123
  35. package/test/static-env.test.ts +85 -0
  36. package/test/static-skip.test.ts +292 -292
  37. package/test/style-object.test.ts +36 -36
  38. package/test/tree-shaking.test.ts +122 -120
package/test/ssr.test.ts CHANGED
@@ -31,6 +31,36 @@ describe("SSR", () => {
31
31
  expect(result).not.toContain("createEffect");
32
32
  expect(result).not.toContain("console.log");
33
33
  });
34
+
35
+ test("$effect.pre is dropped too", () => {
36
+ const result = transformSSR(`$effect.pre(() => { console.log("hi"); });`);
37
+ expect(result).not.toContain("$effect");
38
+ expect(result).not.toContain("console.log");
39
+ });
40
+
41
+ test("$state.snapshot(x) is x on the server", () => {
42
+ const result = transformSSR(
43
+ `let user = $state({ name: "a" }); const copy = $state.snapshot(user);`,
44
+ );
45
+ expect(result).not.toContain("$state");
46
+ expect(result).toContain("const copy = user");
47
+ });
48
+
49
+ test("$effect.tracking() is false on the server", () => {
50
+ const result = transformSSR(`const t = $effect.tracking();`);
51
+ expect(result).not.toContain("$effect");
52
+ expect(result).toContain("const t = false");
53
+ });
54
+ });
55
+
56
+ describe("$inspect", () => {
57
+ test("$inspect(...).with(cb) is dropped", () => {
58
+ const result = transformSSR(
59
+ `let n = $state(0); $inspect(n).with((v) => console.trace(v));`,
60
+ );
61
+ expect(result).not.toContain("$inspect");
62
+ expect(result).not.toContain("console.trace");
63
+ });
34
64
  });
35
65
 
36
66
  describe("JSX", () => {
@@ -1,123 +1,123 @@
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 { transformSync } from "@babel/core";
6
- import { clearAnalysisCache } from "../src/analysis/hydration.ts";
7
- import sigilPlugin from "../src/babel/index.ts";
8
-
9
- const dir = join(tmpdir(), `sigil-static-${Date.now()}`);
10
-
11
- beforeAll(() => {
12
- mkdirSync(dir, { recursive: true });
13
- writeFileSync(
14
- join(dir, "Counter.tsx"),
15
- `export const Counter = () => { let n = $state(0); return <button onClick={() => n++}>{n}</button>; };`,
16
- );
17
- writeFileSync(
18
- join(dir, "Pure.tsx"),
19
- `export const Pure = (props) => <i>{props.label}</i>;`,
20
- );
21
- writeFileSync(
22
- join(dir, "Wrap.tsx"),
23
- `export const Wrap = (props) => <div>{props.children}</div>;`,
24
- );
25
- clearAnalysisCache();
26
- });
27
- afterAll(() => rmSync(dir, { recursive: true, force: true }));
28
-
29
- function compile(body: string, mode?: "ssr" | "dom") {
30
- const header = `
31
- import { Counter } from "./Counter.tsx";
32
- import { Pure } from "./Pure.tsx";
33
- import { Wrap } from "./Wrap.tsx";
34
- const Footer = () => <footer>© now</footer>;
35
- `;
36
- return (
37
- transformSync(header + body, {
38
- filename: join(dir, "Page.tsx"),
39
- babelrc: false,
40
- configFile: false,
41
- parserOpts: {
42
- // @ts-expect-error babel's option type does not list the tuple form
43
- plugins: [["typescript", { isTSX: true }], "jsx"],
44
- },
45
- plugins: [[sigilPlugin, { mode }]],
46
- })?.code ?? ""
47
- );
48
- }
49
-
50
- describe("<X static />", () => {
51
- test("holds for a static component, and the attribute never reaches it", () => {
52
- const out = compile(
53
- `export const A = () => <main><Footer static /></main>;`,
54
- );
55
- expect(out).not.toContain("static");
56
- });
57
-
58
- test("holds for an imported static component given a constant prop", () => {
59
- expect(() =>
60
- compile(`export const A = () => <Pure label="hi" static />;`),
61
- ).not.toThrow();
62
- });
63
-
64
- test("fails for a component that hydrates, naming the reason and the line", () => {
65
- const run = () => compile(`export const A = () => <Counter static />;`);
66
- expect(run).toThrow(/<Counter static> is not static/);
67
- expect(run).toThrow(/\$state/);
68
- expect(run).toThrow(/line \d+/);
69
- });
70
-
71
- test("fails when this use passes state, even to a static component", () => {
72
- expect(() =>
73
- compile(
74
- `let n = $state(0); export const A = () => <Pure label={n} static />;`,
75
- ),
76
- ).toThrow(/passes `label` from state `n`/);
77
- });
78
-
79
- test("fails when the enclosing component's own props flow in", () => {
80
- expect(() =>
81
- compile(
82
- `export const A = (props) => <Pure label={props.title} static />;`,
83
- ),
84
- ).toThrow(/prop object `props`/);
85
- });
86
-
87
- test("a static wrapper cannot hide a dynamic child", () => {
88
- expect(() =>
89
- compile(`export const A = () => <Wrap static><Counter /></Wrap>;`),
90
- ).toThrow(/<Counter> hydrates/);
91
- expect(() =>
92
- compile(`export const A = () => <Wrap static><p>text</p></Wrap>;`),
93
- ).not.toThrow();
94
- });
95
-
96
- test("a handler written inside the use makes it dynamic", () => {
97
- expect(() =>
98
- compile(
99
- `export const A = () => <Wrap static><button onClick={() => {}}>x</button></Wrap>;`,
100
- ),
101
- ).toThrow(/onClick handler/);
102
- });
103
-
104
- test("is for components, not HTML elements", () => {
105
- expect(() => compile(`export const A = () => <div static />;`)).toThrow(
106
- /applies to components/,
107
- );
108
- });
109
-
110
- test("static={false} switches it off", () => {
111
- const out = compile(`export const A = () => <Counter static={false} />;`);
112
- expect(out).not.toContain("static");
113
- });
114
-
115
- test("the server build reaches the same verdict", () => {
116
- expect(() =>
117
- compile(`export const A = () => <Counter static />;`, "ssr"),
118
- ).toThrow(/not static/);
119
- expect(() =>
120
- compile(`export const A = () => <Footer static />;`, "ssr"),
121
- ).not.toThrow();
122
- });
123
- });
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 { transformSync } from "@babel/core";
6
+ import { clearAnalysisCache } from "../src/analysis/hydration.ts";
7
+ import sigilPlugin from "../src/babel/index.ts";
8
+
9
+ const dir = join(tmpdir(), `sigil-static-${Date.now()}`);
10
+
11
+ beforeAll(() => {
12
+ mkdirSync(dir, { recursive: true });
13
+ writeFileSync(
14
+ join(dir, "Counter.tsx"),
15
+ `export const Counter = () => { let n = $state(0); return <button onClick={() => n++}>{n}</button>; };`,
16
+ );
17
+ writeFileSync(
18
+ join(dir, "Pure.tsx"),
19
+ `export const Pure = (props) => <i>{props.label}</i>;`,
20
+ );
21
+ writeFileSync(
22
+ join(dir, "Wrap.tsx"),
23
+ `export const Wrap = (props) => <div>{props.children}</div>;`,
24
+ );
25
+ clearAnalysisCache();
26
+ });
27
+ afterAll(() => rmSync(dir, { recursive: true, force: true }));
28
+
29
+ function compile(body: string, mode?: "ssr" | "dom") {
30
+ const header = `
31
+ import { Counter } from "./Counter.tsx";
32
+ import { Pure } from "./Pure.tsx";
33
+ import { Wrap } from "./Wrap.tsx";
34
+ const Footer = () => <footer>© now</footer>;
35
+ `;
36
+ return (
37
+ transformSync(header + body, {
38
+ filename: join(dir, "Page.tsx"),
39
+ babelrc: false,
40
+ configFile: false,
41
+ parserOpts: {
42
+ // @ts-expect-error babel's option type does not list the tuple form
43
+ plugins: [["typescript", { isTSX: true }], "jsx"],
44
+ },
45
+ plugins: [[sigilPlugin, { mode }]],
46
+ })?.code ?? ""
47
+ );
48
+ }
49
+
50
+ describe("<X static />", () => {
51
+ test("holds for a static component, and the attribute never reaches it", () => {
52
+ const out = compile(
53
+ `export const A = () => <main><Footer static /></main>;`,
54
+ );
55
+ expect(out).not.toContain("static");
56
+ });
57
+
58
+ test("holds for an imported static component given a constant prop", () => {
59
+ expect(() =>
60
+ compile(`export const A = () => <Pure label="hi" static />;`),
61
+ ).not.toThrow();
62
+ });
63
+
64
+ test("fails for a component that hydrates, naming the reason and the line", () => {
65
+ const run = () => compile(`export const A = () => <Counter static />;`);
66
+ expect(run).toThrow(/<Counter static> is not static/);
67
+ expect(run).toThrow(/\$state/);
68
+ expect(run).toThrow(/line \d+/);
69
+ });
70
+
71
+ test("fails when this use passes state, even to a static component", () => {
72
+ expect(() =>
73
+ compile(
74
+ `let n = $state(0); export const A = () => <Pure label={n} static />;`,
75
+ ),
76
+ ).toThrow(/passes `label` from state `n`/);
77
+ });
78
+
79
+ test("fails when the enclosing component's own props flow in", () => {
80
+ expect(() =>
81
+ compile(
82
+ `export const A = (props) => <Pure label={props.title} static />;`,
83
+ ),
84
+ ).toThrow(/prop object `props`/);
85
+ });
86
+
87
+ test("a static wrapper cannot hide a dynamic child", () => {
88
+ expect(() =>
89
+ compile(`export const A = () => <Wrap static><Counter /></Wrap>;`),
90
+ ).toThrow(/<Counter> hydrates/);
91
+ expect(() =>
92
+ compile(`export const A = () => <Wrap static><p>text</p></Wrap>;`),
93
+ ).not.toThrow();
94
+ });
95
+
96
+ test("a handler written inside the use makes it dynamic", () => {
97
+ expect(() =>
98
+ compile(
99
+ `export const A = () => <Wrap static><button onClick={() => {}}>x</button></Wrap>;`,
100
+ ),
101
+ ).toThrow(/onClick handler/);
102
+ });
103
+
104
+ test("is for components, not HTML elements", () => {
105
+ expect(() => compile(`export const A = () => <div static />;`)).toThrow(
106
+ /applies to components/,
107
+ );
108
+ });
109
+
110
+ test("static={false} switches it off", () => {
111
+ const out = compile(`export const A = () => <Counter static={false} />;`);
112
+ expect(out).not.toContain("static");
113
+ });
114
+
115
+ test("the server build reaches the same verdict", () => {
116
+ expect(() =>
117
+ compile(`export const A = () => <Counter static />;`, "ssr"),
118
+ ).toThrow(/not static/);
119
+ expect(() =>
120
+ compile(`export const A = () => <Footer static />;`, "ssr"),
121
+ ).not.toThrow();
122
+ });
123
+ });
@@ -0,0 +1,85 @@
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
+ import { mkdtempSync, rmSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { setStaticEnv } from "../src/babel/util/static-env";
6
+ import { transformDOM, transformSSR } from "./helpers/transform";
7
+
8
+ afterEach(() => setStaticEnv(undefined));
9
+
10
+ describe("$env/static inlined", () => {
11
+ test("named imports become literals, and the import goes", () => {
12
+ setStaticEnv({ PUBLIC_BETA: "off", PUBLIC_NAME: "Sigil" });
13
+ const out = transformDOM(`
14
+ import { PUBLIC_BETA, PUBLIC_NAME } from "$env/static/public";
15
+ export const beta = PUBLIC_BETA === "on";
16
+ export const name = PUBLIC_NAME;`);
17
+ expect(out).not.toContain("$env/static/public");
18
+ expect(out).toContain('"off" === "on"');
19
+ expect(out).toContain('name = "Sigil"');
20
+ });
21
+
22
+ test("env.X reads too; a missing one is undefined, as the module reads it", () => {
23
+ setStaticEnv({ PUBLIC_A: "a" });
24
+ const out = transformDOM(`
25
+ import { env } from "$env/static/public";
26
+ export const a = env.PUBLIC_A;
27
+ export const b = env["PUBLIC_B"];`);
28
+ expect(out).toContain('a = "a"');
29
+ expect(out).toContain("b = undefined");
30
+ expect(out).not.toContain("$env/static/public");
31
+ });
32
+
33
+ test("a named import of a missing variable is left for the module to refuse", () => {
34
+ setStaticEnv({});
35
+ const out = transformDOM(
36
+ 'import { PUBLIC_NOPE } from "$env/static/public"; export const x = PUBLIC_NOPE;',
37
+ );
38
+ expect(out).toContain('import { PUBLIC_NOPE } from "$env/static/public"');
39
+ });
40
+
41
+ test("a whole env object passed on keeps the import", () => {
42
+ setStaticEnv({ PUBLIC_A: "a" });
43
+ const out = transformDOM(`
44
+ import { env } from "$env/static/public";
45
+ export const all = env;`);
46
+ expect(out).toContain('from "$env/static/public"');
47
+ });
48
+
49
+ test("private values are inlined on the server only", () => {
50
+ setStaticEnv({}, { SECRET: "s3cret" });
51
+ const src = `import { SECRET } from "$env/static/private"; export const s = SECRET;`;
52
+ expect(transformSSR(src)).toContain('s = "s3cret"');
53
+ const browser = transformDOM(src);
54
+ expect(browser).not.toContain("s3cret");
55
+ expect(browser).toContain("$env/static/private");
56
+ });
57
+
58
+ test("nothing changes outside an app", () => {
59
+ const out = transformDOM(
60
+ `import { PUBLIC_A } from "$env/static/public"; export const a = PUBLIC_A;`,
61
+ );
62
+ expect(out).toContain('from "$env/static/public"');
63
+ });
64
+
65
+ test("the bundler drops the branch the value decides", async () => {
66
+ setStaticEnv({ PUBLIC_BETA: "off" });
67
+ const code = transformDOM(`
68
+ import { PUBLIC_BETA } from "$env/static/public";
69
+ if (PUBLIC_BETA === "on") console.log("BETA FEATURE");
70
+ console.log("always");`);
71
+ const dir = mkdtempSync(join(tmpdir(), "static-env-"));
72
+ await Bun.write(`${dir}/entry.js`, code);
73
+ try {
74
+ const out = await Bun.build({
75
+ entrypoints: [`${dir}/entry.js`],
76
+ target: "browser",
77
+ });
78
+ const text = await out.outputs[0]!.text();
79
+ expect(text).not.toContain("BETA FEATURE");
80
+ expect(text).toContain("always");
81
+ } finally {
82
+ rmSync(dir, { recursive: true, force: true });
83
+ }
84
+ });
85
+ });