@sigil-dev/compiler 0.10.2 → 0.10.3
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/README.md +5 -4
- package/package.json +1 -1
- package/src/babel/index.ts +801 -801
- package/src/babel/jsx/conditional-return.ts +71 -71
- package/src/babel/jsx/text-node.ts +193 -193
- package/src/babel/jsx/textarea.ts +58 -58
- package/src/bun-plugin.ts +119 -119
- package/test/bun-plugin.test.ts +60 -60
- package/test/conditional-return.test.ts +82 -82
- package/test/hydration.test.ts +380 -380
- package/test/render-parity.test.ts +5 -5
package/src/bun-plugin.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { transformSync } from "@babel/core";
|
|
2
|
-
import type { BunPlugin } from "bun";
|
|
3
|
-
import sigilPlugin from "./babel/index.ts";
|
|
4
|
-
import {
|
|
5
|
-
clientStyleSnippet,
|
|
6
|
-
computeHash,
|
|
7
|
-
extractStyles,
|
|
8
|
-
serverStyleSnippet,
|
|
9
|
-
} from "./babel/util/css.ts";
|
|
10
|
-
|
|
11
|
-
const STYLE_TEST_RE = /<style[^>]*>[\s\S]*?<\/style>/i;
|
|
12
|
-
const SOURCE_RE = /\.[jt]sx?$/;
|
|
13
|
-
// What onLoad is asked about: source files, minus everything shouldTransform
|
|
14
|
-
// turns away. Bun's runtime plugins (Bun.plugin, which SSR and `bun test`
|
|
15
|
-
// use) reject an onLoad that returns nothing, so a file this plugin won't
|
|
16
|
-
// compile must not reach it at all; dependencies can be CommonJS, which it
|
|
17
|
-
// couldn't hand back as they are either.
|
|
18
|
-
export const LOAD_FILTER =
|
|
19
|
-
/^(?!.*[\\/](?:node_modules|\.grimoire)[\\/])(?!.*[\\/]packages[\\/](?:runtime|grimoire|compiler)[\\/]).*\.[jt]sx?$/;
|
|
20
|
-
const JSX_RE = /\.[jt]sx$/;
|
|
21
|
-
// any macro, including $state.raw(…), $effect.pre(…) and $store<T>(…)
|
|
22
|
-
const SIGIL_MACRO_RE = /\$(?:state|store|derived|effect|inspect|debug)\b/;
|
|
23
|
-
// a project import: reads of an imported store need the compiler too
|
|
24
|
-
const PROJECT_IMPORT_RE = /\bfrom\s*["'](?:\.{1,2}\/|\$|~\/|@\/)/;
|
|
25
|
-
|
|
26
|
-
function normalizePath(path: string): string {
|
|
27
|
-
return path.replace(/\\/g, "/");
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function shouldTransform(path: string): boolean {
|
|
31
|
-
const normalized = normalizePath(path);
|
|
32
|
-
if (!SOURCE_RE.test(normalized)) return false;
|
|
33
|
-
if (normalized.includes("/.grimoire/")) return false;
|
|
34
|
-
if (normalized.includes("/node_modules/")) return false;
|
|
35
|
-
if (normalized.includes("/packages/runtime/")) return false;
|
|
36
|
-
if (normalized.includes("/packages/grimoire/")) return false;
|
|
37
|
-
if (normalized.includes("/packages/compiler/")) return false;
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function shouldTransformSource(path: string, code: string): boolean {
|
|
42
|
-
const normalized = normalizePath(path);
|
|
43
|
-
return (
|
|
44
|
-
JSX_RE.test(normalized) ||
|
|
45
|
-
SIGIL_MACRO_RE.test(code) ||
|
|
46
|
-
PROJECT_IMPORT_RE.test(code) ||
|
|
47
|
-
STYLE_TEST_RE.test(code)
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function sigil(options?: {
|
|
52
|
-
mode?: "dom" | "ssr" | "hydrate";
|
|
53
|
-
dev?: boolean;
|
|
54
|
-
/** Import aliases, prefix to absolute directory, so imported state resolves. */
|
|
55
|
-
aliases?: Record<string, string>;
|
|
56
|
-
}): BunPlugin {
|
|
57
|
-
return {
|
|
58
|
-
name: "sigil",
|
|
59
|
-
setup(build) {
|
|
60
|
-
const transpiler = new Bun.Transpiler({ loader: "tsx" });
|
|
61
|
-
|
|
62
|
-
// This is required for both linking errors,
|
|
63
|
-
// and for a bug in the bundler, which was reported in https://github.com/oven-sh/bun/issues/13897
|
|
64
|
-
// 2 YEARS AGO and apparently fixed in https://github.com/oven-sh/bun/pull/15119. It wasnt
|
|
65
|
-
build.onResolve({ filter: /^@sigil-dev\/runtime($|\/)/ }, (args) => {
|
|
66
|
-
try {
|
|
67
|
-
const resolved = Bun.resolveSync(args.path, process.cwd());
|
|
68
|
-
return { path: resolved.replaceAll("\\", "/") };
|
|
69
|
-
} catch {
|
|
70
|
-
return undefined;
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// a project file that needs no compiling goes back as it is (see LOAD_FILTER)
|
|
75
|
-
const asIs = (path: string, code: string) => ({
|
|
76
|
-
contents: code,
|
|
77
|
-
loader: (path.match(/\.([jt]sx?)$/)?.[1] ?? "js") as "js" | "jsx" | "ts" | "tsx",
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
build.onLoad({ filter: LOAD_FILTER }, async ({ path }) => {
|
|
81
|
-
let code = await Bun.file(path).text();
|
|
82
|
-
if (!shouldTransform(path)) return asIs(path, code);
|
|
83
|
-
if (!shouldTransformSource(path, code)) return asIs(path, code);
|
|
84
|
-
// console.log("[bun-plugin] intercepting:", path);
|
|
85
|
-
const hash = computeHash(path);
|
|
86
|
-
const styles = extractStyles(code, hash, path);
|
|
87
|
-
code = styles.code;
|
|
88
|
-
const scopedCSS = styles.css;
|
|
89
|
-
const res = transformSync(code, {
|
|
90
|
-
parserOpts: {
|
|
91
|
-
//@ts-expect-error holy shut up
|
|
92
|
-
plugins: [["typescript", { isTSX: true }], "jsx"],
|
|
93
|
-
},
|
|
94
|
-
plugins: [
|
|
95
|
-
[
|
|
96
|
-
sigilPlugin,
|
|
97
|
-
// scope classes only where there is scoped CSS to match
|
|
98
|
-
{
|
|
99
|
-
hash: scopedCSS ? hash : undefined,
|
|
100
|
-
mode: options?.mode,
|
|
101
|
-
dev: options?.dev,
|
|
102
|
-
aliases: options?.aliases,
|
|
103
|
-
},
|
|
104
|
-
],
|
|
105
|
-
],
|
|
106
|
-
filename: path,
|
|
107
|
-
});
|
|
108
|
-
let out = transpiler.transformSync(res?.code ?? "");
|
|
109
|
-
if (scopedCSS) {
|
|
110
|
-
out =
|
|
111
|
-
(options?.mode === "ssr"
|
|
112
|
-
? serverStyleSnippet(hash, scopedCSS)
|
|
113
|
-
: clientStyleSnippet(hash, scopedCSS)) + out;
|
|
114
|
-
}
|
|
115
|
-
return { contents: out, loader: "js" };
|
|
116
|
-
});
|
|
117
|
-
},
|
|
118
|
-
};
|
|
119
|
-
}
|
|
1
|
+
import { transformSync } from "@babel/core";
|
|
2
|
+
import type { BunPlugin } from "bun";
|
|
3
|
+
import sigilPlugin from "./babel/index.ts";
|
|
4
|
+
import {
|
|
5
|
+
clientStyleSnippet,
|
|
6
|
+
computeHash,
|
|
7
|
+
extractStyles,
|
|
8
|
+
serverStyleSnippet,
|
|
9
|
+
} from "./babel/util/css.ts";
|
|
10
|
+
|
|
11
|
+
const STYLE_TEST_RE = /<style[^>]*>[\s\S]*?<\/style>/i;
|
|
12
|
+
const SOURCE_RE = /\.[jt]sx?$/;
|
|
13
|
+
// What onLoad is asked about: source files, minus everything shouldTransform
|
|
14
|
+
// turns away. Bun's runtime plugins (Bun.plugin, which SSR and `bun test`
|
|
15
|
+
// use) reject an onLoad that returns nothing, so a file this plugin won't
|
|
16
|
+
// compile must not reach it at all; dependencies can be CommonJS, which it
|
|
17
|
+
// couldn't hand back as they are either.
|
|
18
|
+
export const LOAD_FILTER =
|
|
19
|
+
/^(?!.*[\\/](?:node_modules|\.grimoire)[\\/])(?!.*[\\/]packages[\\/](?:runtime|grimoire|compiler)[\\/]).*\.[jt]sx?$/;
|
|
20
|
+
const JSX_RE = /\.[jt]sx$/;
|
|
21
|
+
// any macro, including $state.raw(…), $effect.pre(…) and $store<T>(…)
|
|
22
|
+
const SIGIL_MACRO_RE = /\$(?:state|store|derived|effect|inspect|debug)\b/;
|
|
23
|
+
// a project import: reads of an imported store need the compiler too
|
|
24
|
+
const PROJECT_IMPORT_RE = /\bfrom\s*["'](?:\.{1,2}\/|\$|~\/|@\/)/;
|
|
25
|
+
|
|
26
|
+
function normalizePath(path: string): string {
|
|
27
|
+
return path.replace(/\\/g, "/");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function shouldTransform(path: string): boolean {
|
|
31
|
+
const normalized = normalizePath(path);
|
|
32
|
+
if (!SOURCE_RE.test(normalized)) return false;
|
|
33
|
+
if (normalized.includes("/.grimoire/")) return false;
|
|
34
|
+
if (normalized.includes("/node_modules/")) return false;
|
|
35
|
+
if (normalized.includes("/packages/runtime/")) return false;
|
|
36
|
+
if (normalized.includes("/packages/grimoire/")) return false;
|
|
37
|
+
if (normalized.includes("/packages/compiler/")) return false;
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function shouldTransformSource(path: string, code: string): boolean {
|
|
42
|
+
const normalized = normalizePath(path);
|
|
43
|
+
return (
|
|
44
|
+
JSX_RE.test(normalized) ||
|
|
45
|
+
SIGIL_MACRO_RE.test(code) ||
|
|
46
|
+
PROJECT_IMPORT_RE.test(code) ||
|
|
47
|
+
STYLE_TEST_RE.test(code)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function sigil(options?: {
|
|
52
|
+
mode?: "dom" | "ssr" | "hydrate";
|
|
53
|
+
dev?: boolean;
|
|
54
|
+
/** Import aliases, prefix to absolute directory, so imported state resolves. */
|
|
55
|
+
aliases?: Record<string, string>;
|
|
56
|
+
}): BunPlugin {
|
|
57
|
+
return {
|
|
58
|
+
name: "sigil",
|
|
59
|
+
setup(build) {
|
|
60
|
+
const transpiler = new Bun.Transpiler({ loader: "tsx" });
|
|
61
|
+
|
|
62
|
+
// This is required for both linking errors,
|
|
63
|
+
// and for a bug in the bundler, which was reported in https://github.com/oven-sh/bun/issues/13897
|
|
64
|
+
// 2 YEARS AGO and apparently fixed in https://github.com/oven-sh/bun/pull/15119. It wasnt
|
|
65
|
+
build.onResolve({ filter: /^@sigil-dev\/runtime($|\/)/ }, (args) => {
|
|
66
|
+
try {
|
|
67
|
+
const resolved = Bun.resolveSync(args.path, process.cwd());
|
|
68
|
+
return { path: resolved.replaceAll("\\", "/") };
|
|
69
|
+
} catch {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// a project file that needs no compiling goes back as it is (see LOAD_FILTER)
|
|
75
|
+
const asIs = (path: string, code: string) => ({
|
|
76
|
+
contents: code,
|
|
77
|
+
loader: (path.match(/\.([jt]sx?)$/)?.[1] ?? "js") as "js" | "jsx" | "ts" | "tsx",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
build.onLoad({ filter: LOAD_FILTER }, async ({ path }) => {
|
|
81
|
+
let code = await Bun.file(path).text();
|
|
82
|
+
if (!shouldTransform(path)) return asIs(path, code);
|
|
83
|
+
if (!shouldTransformSource(path, code)) return asIs(path, code);
|
|
84
|
+
// console.log("[bun-plugin] intercepting:", path);
|
|
85
|
+
const hash = computeHash(path);
|
|
86
|
+
const styles = extractStyles(code, hash, path);
|
|
87
|
+
code = styles.code;
|
|
88
|
+
const scopedCSS = styles.css;
|
|
89
|
+
const res = transformSync(code, {
|
|
90
|
+
parserOpts: {
|
|
91
|
+
//@ts-expect-error holy shut up
|
|
92
|
+
plugins: [["typescript", { isTSX: true }], "jsx"],
|
|
93
|
+
},
|
|
94
|
+
plugins: [
|
|
95
|
+
[
|
|
96
|
+
sigilPlugin,
|
|
97
|
+
// scope classes only where there is scoped CSS to match
|
|
98
|
+
{
|
|
99
|
+
hash: scopedCSS ? hash : undefined,
|
|
100
|
+
mode: options?.mode,
|
|
101
|
+
dev: options?.dev,
|
|
102
|
+
aliases: options?.aliases,
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
],
|
|
106
|
+
filename: path,
|
|
107
|
+
});
|
|
108
|
+
let out = transpiler.transformSync(res?.code ?? "");
|
|
109
|
+
if (scopedCSS) {
|
|
110
|
+
out =
|
|
111
|
+
(options?.mode === "ssr"
|
|
112
|
+
? serverStyleSnippet(hash, scopedCSS)
|
|
113
|
+
: clientStyleSnippet(hash, scopedCSS)) + out;
|
|
114
|
+
}
|
|
115
|
+
return { contents: out, loader: "js" };
|
|
116
|
+
});
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
package/test/bun-plugin.test.ts
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
import { sigil } from "../src/bun-plugin.ts";
|
|
6
|
-
|
|
7
|
-
const dir = join(tmpdir(), `sigil-bun-plugin-${Date.now()}`);
|
|
8
|
-
|
|
9
|
-
beforeAll(async () => {
|
|
10
|
-
await mkdir(dir, { recursive: true });
|
|
11
|
-
await writeFile(
|
|
12
|
-
join(dir, "theme.ts"),
|
|
13
|
-
'export let theme = $store<"light" | "dark">("light");\nexport function toggle() { theme = theme === "light" ? "dark" : "light"; }',
|
|
14
|
-
);
|
|
15
|
-
await writeFile(
|
|
16
|
-
join(dir, "raw.ts"),
|
|
17
|
-
"export let rows = $state.raw([1, 2]);\nexport const first = () => rows[0];",
|
|
18
|
-
);
|
|
19
|
-
await writeFile(
|
|
20
|
-
join(dir, "label.ts"),
|
|
21
|
-
'import { theme } from "./theme";\nexport const label = () => "theme: " + theme;',
|
|
22
|
-
);
|
|
23
|
-
await writeFile(
|
|
24
|
-
join(dir, "entry.ts"),
|
|
25
|
-
'import { toggle } from "./theme";\nimport { first } from "./raw";\nimport { label } from "./label";\nconsole.log(toggle, first, label);',
|
|
26
|
-
);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
30
|
-
|
|
31
|
-
test("plain .ts modules with any macro or a store import are compiled", async () => {
|
|
32
|
-
const result = await Bun.build({
|
|
33
|
-
entrypoints: [join(dir, "entry.ts")],
|
|
34
|
-
plugins: [sigil({ mode: "dom" })],
|
|
35
|
-
external: ["@sigil-dev/runtime"],
|
|
36
|
-
throw: false,
|
|
37
|
-
});
|
|
38
|
-
expect(result.success).toBe(true);
|
|
39
|
-
const code = await result.outputs[0]!.text();
|
|
40
|
-
// $store<T>(…) with a type argument, and $state.raw(…)
|
|
41
|
-
expect(code).not.toMatch(/\$store|\$state/);
|
|
42
|
-
expect(code).toContain("createRawSignal");
|
|
43
|
-
// label.ts has no macro, but reads an imported store
|
|
44
|
-
// compiled statically: the import is known to be state
|
|
45
|
-
expect(code).toContain(`"theme: " + theme()`);
|
|
46
|
-
expect(code).not.toContain("readStore");
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe("what onLoad is asked about", () => {
|
|
50
|
-
// Bun's runtime plugins reject an onLoad that returns nothing, so files the
|
|
51
|
-
// plugin won't compile mustn't reach it (and dependencies may be CommonJS)
|
|
52
|
-
test("project sources, on either OS's paths", async () => {
|
|
53
|
-
const { LOAD_FILTER } = await import("../src/bun-plugin.ts");
|
|
54
|
-
for (const p of ["/app/src/routes/+page.tsx", "C:\\app\\src\\lib\\x.ts", "/app/src/page.js", "/r/packages/tome/src/a.ts"]) expect(LOAD_FILTER.test(p)).toBe(true);
|
|
55
|
-
});
|
|
56
|
-
test("not dependencies, build output or Sigil's own runtime packages", async () => {
|
|
57
|
-
const { LOAD_FILTER } = await import("../src/bun-plugin.ts");
|
|
58
|
-
for (const p of ["/app/node_modules/x/i.js", "C:\\app\\node_modules\\x\\i.js", "/app/.grimoire/client/a.js", "/r/packages/runtime/index.ts", "/r/packages/grimoire/src/a.ts", "/r/packages/compiler/src/a.ts", "/app/style.css"]) expect(LOAD_FILTER.test(p)).toBe(false);
|
|
59
|
-
});
|
|
60
|
-
});
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
2
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { sigil } from "../src/bun-plugin.ts";
|
|
6
|
+
|
|
7
|
+
const dir = join(tmpdir(), `sigil-bun-plugin-${Date.now()}`);
|
|
8
|
+
|
|
9
|
+
beforeAll(async () => {
|
|
10
|
+
await mkdir(dir, { recursive: true });
|
|
11
|
+
await writeFile(
|
|
12
|
+
join(dir, "theme.ts"),
|
|
13
|
+
'export let theme = $store<"light" | "dark">("light");\nexport function toggle() { theme = theme === "light" ? "dark" : "light"; }',
|
|
14
|
+
);
|
|
15
|
+
await writeFile(
|
|
16
|
+
join(dir, "raw.ts"),
|
|
17
|
+
"export let rows = $state.raw([1, 2]);\nexport const first = () => rows[0];",
|
|
18
|
+
);
|
|
19
|
+
await writeFile(
|
|
20
|
+
join(dir, "label.ts"),
|
|
21
|
+
'import { theme } from "./theme";\nexport const label = () => "theme: " + theme;',
|
|
22
|
+
);
|
|
23
|
+
await writeFile(
|
|
24
|
+
join(dir, "entry.ts"),
|
|
25
|
+
'import { toggle } from "./theme";\nimport { first } from "./raw";\nimport { label } from "./label";\nconsole.log(toggle, first, label);',
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterAll(() => rm(dir, { recursive: true, force: true }));
|
|
30
|
+
|
|
31
|
+
test("plain .ts modules with any macro or a store import are compiled", async () => {
|
|
32
|
+
const result = await Bun.build({
|
|
33
|
+
entrypoints: [join(dir, "entry.ts")],
|
|
34
|
+
plugins: [sigil({ mode: "dom" })],
|
|
35
|
+
external: ["@sigil-dev/runtime"],
|
|
36
|
+
throw: false,
|
|
37
|
+
});
|
|
38
|
+
expect(result.success).toBe(true);
|
|
39
|
+
const code = await result.outputs[0]!.text();
|
|
40
|
+
// $store<T>(…) with a type argument, and $state.raw(…)
|
|
41
|
+
expect(code).not.toMatch(/\$store|\$state/);
|
|
42
|
+
expect(code).toContain("createRawSignal");
|
|
43
|
+
// label.ts has no macro, but reads an imported store
|
|
44
|
+
// compiled statically: the import is known to be state
|
|
45
|
+
expect(code).toContain(`"theme: " + theme()`);
|
|
46
|
+
expect(code).not.toContain("readStore");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("what onLoad is asked about", () => {
|
|
50
|
+
// Bun's runtime plugins reject an onLoad that returns nothing, so files the
|
|
51
|
+
// plugin won't compile mustn't reach it (and dependencies may be CommonJS)
|
|
52
|
+
test("project sources, on either OS's paths", async () => {
|
|
53
|
+
const { LOAD_FILTER } = await import("../src/bun-plugin.ts");
|
|
54
|
+
for (const p of ["/app/src/routes/+page.tsx", "C:\\app\\src\\lib\\x.ts", "/app/src/page.js", "/r/packages/tome/src/a.ts"]) expect(LOAD_FILTER.test(p)).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
test("not dependencies, build output or Sigil's own runtime packages", async () => {
|
|
57
|
+
const { LOAD_FILTER } = await import("../src/bun-plugin.ts");
|
|
58
|
+
for (const p of ["/app/node_modules/x/i.js", "C:\\app\\node_modules\\x\\i.js", "/app/.grimoire/client/a.js", "/r/packages/runtime/index.ts", "/r/packages/grimoire/src/a.ts", "/r/packages/compiler/src/a.ts", "/app/style.css"]) expect(LOAD_FILTER.test(p)).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -1,82 +1,82 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { transformSync } from "@babel/core";
|
|
3
|
-
import * as runtime from "../../runtime/index.ts";
|
|
4
|
-
import sigilPlugin from "../src/babel/index.ts";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A component whose body returns a conditional at the top must follow it:
|
|
8
|
-
* the body runs once, so before 0.10 `return open ? <A /> : null` was decided
|
|
9
|
-
* once and never updated.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
function compile(source: string, mode: "dom" | "ssr" | "hydrate") {
|
|
13
|
-
return transformSync(source, { plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode }]], filename: "test.tsx" })!.code!;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function evalApp(code: string): (props?: unknown) => unknown {
|
|
17
|
-
const body = code.replace(/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g, "").replace(/^export\s+/gm, "");
|
|
18
|
-
return new Function("document", ...Object.keys(runtime), `${body};\nreturn App;`)(globalThis.document, ...Object.values(runtime));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const flush = () => new Promise((r) => setTimeout(r, 0));
|
|
22
|
-
const g = globalThis as { __flip?: () => void };
|
|
23
|
-
|
|
24
|
-
const STATE = `
|
|
25
|
-
export const App = () => {
|
|
26
|
-
let open = $state(false);
|
|
27
|
-
globalThis.__flip = () => (open = !open);
|
|
28
|
-
return open ? <p class="panel">hi</p> : null;
|
|
29
|
-
};
|
|
30
|
-
`;
|
|
31
|
-
|
|
32
|
-
const PROPS = `
|
|
33
|
-
const Panel = ({ show }) => show && <p class="panel">hi</p>;
|
|
34
|
-
export const App = () => {
|
|
35
|
-
let show = $state(false);
|
|
36
|
-
globalThis.__flip = () => (show = !show);
|
|
37
|
-
return <div><Panel show={show} /></div>;
|
|
38
|
-
};
|
|
39
|
-
`;
|
|
40
|
-
|
|
41
|
-
describe("a component returning a conditional", () => {
|
|
42
|
-
for (const [what, src] of [["its own state", STATE], ["a prop", PROPS]] as const) {
|
|
43
|
-
test(`follows ${what} after mounting`, async () => {
|
|
44
|
-
const host = document.createElement("div");
|
|
45
|
-
host.append(evalApp(compile(src, "dom"))() as Node);
|
|
46
|
-
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
47
|
-
g.__flip!();
|
|
48
|
-
await flush();
|
|
49
|
-
expect(host.querySelectorAll("p.panel").length).toBe(1);
|
|
50
|
-
g.__flip!();
|
|
51
|
-
await flush();
|
|
52
|
-
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test(`hydrates what the server sent, then follows ${what}`, async () => {
|
|
56
|
-
const html = String(evalApp(compile(src, "ssr"))());
|
|
57
|
-
const host = document.createElement("div");
|
|
58
|
-
host.innerHTML = html;
|
|
59
|
-
runtime.pushHydrationNodes(Array.from(host.childNodes));
|
|
60
|
-
evalApp(compile(src, "hydrate"))();
|
|
61
|
-
runtime.popHydrationNodes();
|
|
62
|
-
// claimed, not drawn again (an empty slot's closing marker is used up, as anywhere)
|
|
63
|
-
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
64
|
-
expect(host.innerHTML.replace("<!--/g-->", "")).toBe(html.replace("<!--/g-->", ""));
|
|
65
|
-
g.__flip!();
|
|
66
|
-
await flush();
|
|
67
|
-
expect(host.querySelectorAll("p.panel").length).toBe(1);
|
|
68
|
-
g.__flip!();
|
|
69
|
-
await flush();
|
|
70
|
-
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
test("a callback's own conditional return is left alone", () => {
|
|
75
|
-
const code = compile(`const List = () => <ul>{[1].map((i) => (i ? <li>{i}</li> : null))}</ul>;`, "dom");
|
|
76
|
-
expect(code).not.toContain("createDocumentFragment");
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
test("a lowercase helper is not a component", () => {
|
|
80
|
-
expect(compile(`const helper = (w) => (w ? <b>1</b> : null);`, "dom")).not.toContain("createDocumentFragment");
|
|
81
|
-
});
|
|
82
|
-
});
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { transformSync } from "@babel/core";
|
|
3
|
+
import * as runtime from "../../runtime/index.ts";
|
|
4
|
+
import sigilPlugin from "../src/babel/index.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A component whose body returns a conditional at the top must follow it:
|
|
8
|
+
* the body runs once, so before 0.10 `return open ? <A /> : null` was decided
|
|
9
|
+
* once and never updated.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
function compile(source: string, mode: "dom" | "ssr" | "hydrate") {
|
|
13
|
+
return transformSync(source, { plugins: ["@babel/plugin-syntax-jsx", [sigilPlugin, { mode }]], filename: "test.tsx" })!.code!;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function evalApp(code: string): (props?: unknown) => unknown {
|
|
17
|
+
const body = code.replace(/import\s*\{[^}]*\}\s*from\s*["']@sigil-dev\/runtime["'];?/g, "").replace(/^export\s+/gm, "");
|
|
18
|
+
return new Function("document", ...Object.keys(runtime), `${body};\nreturn App;`)(globalThis.document, ...Object.values(runtime));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const flush = () => new Promise((r) => setTimeout(r, 0));
|
|
22
|
+
const g = globalThis as { __flip?: () => void };
|
|
23
|
+
|
|
24
|
+
const STATE = `
|
|
25
|
+
export const App = () => {
|
|
26
|
+
let open = $state(false);
|
|
27
|
+
globalThis.__flip = () => (open = !open);
|
|
28
|
+
return open ? <p class="panel">hi</p> : null;
|
|
29
|
+
};
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
const PROPS = `
|
|
33
|
+
const Panel = ({ show }) => show && <p class="panel">hi</p>;
|
|
34
|
+
export const App = () => {
|
|
35
|
+
let show = $state(false);
|
|
36
|
+
globalThis.__flip = () => (show = !show);
|
|
37
|
+
return <div><Panel show={show} /></div>;
|
|
38
|
+
};
|
|
39
|
+
`;
|
|
40
|
+
|
|
41
|
+
describe("a component returning a conditional", () => {
|
|
42
|
+
for (const [what, src] of [["its own state", STATE], ["a prop", PROPS]] as const) {
|
|
43
|
+
test(`follows ${what} after mounting`, async () => {
|
|
44
|
+
const host = document.createElement("div");
|
|
45
|
+
host.append(evalApp(compile(src, "dom"))() as Node);
|
|
46
|
+
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
47
|
+
g.__flip!();
|
|
48
|
+
await flush();
|
|
49
|
+
expect(host.querySelectorAll("p.panel").length).toBe(1);
|
|
50
|
+
g.__flip!();
|
|
51
|
+
await flush();
|
|
52
|
+
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test(`hydrates what the server sent, then follows ${what}`, async () => {
|
|
56
|
+
const html = String(evalApp(compile(src, "ssr"))());
|
|
57
|
+
const host = document.createElement("div");
|
|
58
|
+
host.innerHTML = html;
|
|
59
|
+
runtime.pushHydrationNodes(Array.from(host.childNodes));
|
|
60
|
+
evalApp(compile(src, "hydrate"))();
|
|
61
|
+
runtime.popHydrationNodes();
|
|
62
|
+
// claimed, not drawn again (an empty slot's closing marker is used up, as anywhere)
|
|
63
|
+
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
64
|
+
expect(host.innerHTML.replace("<!--/g-->", "")).toBe(html.replace("<!--/g-->", ""));
|
|
65
|
+
g.__flip!();
|
|
66
|
+
await flush();
|
|
67
|
+
expect(host.querySelectorAll("p.panel").length).toBe(1);
|
|
68
|
+
g.__flip!();
|
|
69
|
+
await flush();
|
|
70
|
+
expect(host.querySelectorAll("p.panel").length).toBe(0);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
test("a callback's own conditional return is left alone", () => {
|
|
75
|
+
const code = compile(`const List = () => <ul>{[1].map((i) => (i ? <li>{i}</li> : null))}</ul>;`, "dom");
|
|
76
|
+
expect(code).not.toContain("createDocumentFragment");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("a lowercase helper is not a component", () => {
|
|
80
|
+
expect(compile(`const helper = (w) => (w ? <b>1</b> : null);`, "dom")).not.toContain("createDocumentFragment");
|
|
81
|
+
});
|
|
82
|
+
});
|