@sigil-dev/grimoire 0.7.6 → 0.7.7

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 (41) hide show
  1. package/index.ts +35 -34
  2. package/package.json +8 -6
  3. package/preload.js +3 -2
  4. package/server.ts +13 -13
  5. package/src/client/head.ts +29 -29
  6. package/src/client/router.ts +290 -224
  7. package/src/dev/compile-module.ts +173 -0
  8. package/src/dev/effect-registry.ts +23 -0
  9. package/src/dev/graph.ts +114 -0
  10. package/src/dev/hmr-client.ts +158 -0
  11. package/src/dev/hmr-server.ts +187 -0
  12. package/src/dev/loader.ts +47 -0
  13. package/src/dev/paths.ts +14 -0
  14. package/src/dev/runtime-bundle.ts +49 -0
  15. package/src/dev/watcher.ts +44 -0
  16. package/src/integrations/vite.ts +73 -72
  17. package/src/rendering/hydrate.ts +120 -81
  18. package/src/rendering/index.ts +296 -199
  19. package/src/rendering/ssrPlugin.ts +67 -53
  20. package/src/routing/manifest-gen.ts +42 -39
  21. package/src/routing/router.ts +109 -106
  22. package/src/routing/scanner.ts +141 -135
  23. package/src/routing/transform-routes.ts +101 -101
  24. package/src/server/build.ts +239 -147
  25. package/src/server/coordinator.ts +306 -306
  26. package/src/server/index.ts +260 -50
  27. package/src/server/worker.ts +59 -59
  28. package/src/typegen/index.ts +356 -353
  29. package/src/types.ts +270 -269
  30. package/test/context.test.ts +52 -52
  31. package/test/hydration.test.ts +119 -119
  32. package/test/middleware.test.ts +223 -223
  33. package/test/rendering.test.ts +579 -425
  34. package/test/routing.test.ts +81 -83
  35. package/test/scanning.test.ts +200 -181
  36. package/test/scope.test.ts +24 -8
  37. package/test/server.test.ts +249 -229
  38. package/test/streaming.test.ts +125 -106
  39. package/test/transform-routes.test.ts +84 -84
  40. package/test/typegen.test.ts +35 -25
  41. package/tsconfig.json +1 -0
@@ -1,106 +1,125 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { renderRoute } from "../src/rendering";
3
-
4
- function makeMatched(path: string, filePath: string, params = {}) {
5
- return {
6
- route: { path, params: {}, filePath },
7
- params,
8
- layouts: [],
9
- layoutServers: [],
10
- } as any;
11
- }
12
-
13
- describe("Streaming SSR", () => {
14
- test("returns a ReadableStream response", async () => {
15
- const matched = makeMatched("/", "data:text/javascript,export default (p) => '<div>hello</div>'");
16
- const res = await renderRoute(matched, new Request("http://localhost/"));
17
- expect(res.body).toBeInstanceOf(ReadableStream);
18
- });
19
-
20
- test("stream contains full HTML document", async () => {
21
- const matched = makeMatched("/test", "data:text/javascript,export default (p) => '<p>streaming works</p>'");
22
-
23
- const res = await renderRoute(
24
- matched,
25
- new Request("http://localhost/test"),
26
- );
27
- const html = await res.text();
28
-
29
- expect(html).toContain("<!DOCTYPE html>");
30
- expect(html).toContain('<meta charset="UTF-8" />');
31
- expect(html).toContain("streaming works");
32
- expect(html).toContain("</html>");
33
- expect(html).toContain("__grimoire_state__");
34
- });
35
-
36
- test("stream includes state JSON with route pattern", async () => {
37
- const matched = makeMatched("/spells/:id", "data:text/javascript,export default (p) => '<div>spell</div>'", { id: "fireball" });
38
-
39
- const res = await renderRoute(
40
- matched,
41
- new Request("http://localhost/spells/fireball"),
42
- );
43
- const html = await res.text();
44
-
45
- expect(html).toContain('"pattern":"/spells/:id"');
46
- expect(html).toContain('"id":"fireball"');
47
- });
48
-
49
- test("head script tag comes before head content", async () => {
50
- const matched = makeMatched("/head-test", "data:text/javascript,export default (p) => '<div>head</div>'");
51
-
52
- const res = await renderRoute(
53
- matched,
54
- new Request("http://localhost/head-test"),
55
- );
56
- const html = await res.text();
57
-
58
- const hydrateScript = html.indexOf("hydrate.js");
59
- const headClosing = html.indexOf("</head>");
60
- // hydrate.js should be in <head>
61
- expect(hydrateScript).toBeGreaterThan(-1);
62
- expect(hydrateScript).toBeLessThan(headClosing);
63
- });
64
-
65
- test("stream chunks arrive incrementally", async () => {
66
- const matched = makeMatched("/chunked", "data:text/javascript,export default (p) => '<div>chunked</div>'");
67
-
68
- const res = await renderRoute(
69
- matched,
70
- new Request("http://localhost/chunked"),
71
- );
72
- const reader = res.body!.getReader();
73
- const chunks: string[] = [];
74
-
75
- while (true) {
76
- const { done, value } = await reader.read();
77
- if (done) break;
78
- // Bun ReadableStream returns Uint8Array
79
- chunks.push(
80
- typeof value === "string"
81
- ? value
82
- : new TextDecoder().decode(value.buffer),
83
- );
84
- }
85
-
86
- // Should have multiple chunks (at least DOCTYPE + body)
87
- expect(chunks.length).toBeGreaterThan(1);
88
- // First chunk should start with DOCTYPE
89
- expect(chunks[0]).toContain("<!DOCTYPE html>");
90
- // Last chunk should close the document
91
- expect(chunks[chunks.length - 1]).toContain("</html>");
92
- });
93
-
94
- test("navigation request returns JSON not stream", async () => {
95
- const matched = makeMatched("/nav", "data:text/javascript,export default (p) => '<div>nav</div>'");
96
-
97
- const req = new Request("http://localhost/nav", {
98
- headers: { "x-grimoire-navigate": "1" },
99
- });
100
- const res = await renderRoute(matched, req);
101
- const json = await res.json();
102
-
103
- expect(json.pattern).toBe("/nav");
104
- expect(json.data).toBeDefined();
105
- });
106
- });
1
+ import { describe, expect, test } from "bun:test";
2
+ import { renderRoute } from "../src/rendering";
3
+
4
+ function makeMatched(path: string, filePath: string, params = {}) {
5
+ return {
6
+ route: { path, params: {}, filePath },
7
+ params,
8
+ layouts: [],
9
+ layoutServers: [],
10
+ } as any;
11
+ }
12
+
13
+ describe("Streaming SSR", () => {
14
+ test("returns a ReadableStream response", async () => {
15
+ const matched = makeMatched(
16
+ "/",
17
+ "data:text/javascript,export default (p) => '<div>hello</div>'",
18
+ );
19
+ const res = await renderRoute(matched, new Request("http://localhost/"));
20
+ expect(res.body).toBeInstanceOf(ReadableStream);
21
+ });
22
+
23
+ test("stream contains full HTML document", async () => {
24
+ const matched = makeMatched(
25
+ "/test",
26
+ "data:text/javascript,export default (p) => '<p>streaming works</p>'",
27
+ );
28
+
29
+ const res = await renderRoute(
30
+ matched,
31
+ new Request("http://localhost/test"),
32
+ );
33
+ const html = await res.text();
34
+
35
+ expect(html).toContain("<!DOCTYPE html>");
36
+ expect(html).toContain('<meta charset="UTF-8" />');
37
+ expect(html).toContain("streaming works");
38
+ expect(html).toContain("</html>");
39
+ expect(html).toContain("__grimoire_state__");
40
+ });
41
+
42
+ test("stream includes state JSON with route pattern", async () => {
43
+ const matched = makeMatched(
44
+ "/spells/:id",
45
+ "data:text/javascript,export default (p) => '<div>spell</div>'",
46
+ { id: "fireball" },
47
+ );
48
+
49
+ const res = await renderRoute(
50
+ matched,
51
+ new Request("http://localhost/spells/fireball"),
52
+ );
53
+ const html = await res.text();
54
+
55
+ expect(html).toContain('"pattern":"/spells/:id"');
56
+ expect(html).toContain('"id":"fireball"');
57
+ });
58
+
59
+ test("head script tag comes before head content", async () => {
60
+ const matched = makeMatched(
61
+ "/head-test",
62
+ "data:text/javascript,export default (p) => '<div>head</div>'",
63
+ );
64
+
65
+ const res = await renderRoute(
66
+ matched,
67
+ new Request("http://localhost/head-test"),
68
+ );
69
+ const html = await res.text();
70
+
71
+ const hydrateScript = html.indexOf("hydrate.js");
72
+ const headClosing = html.indexOf("</head>");
73
+ // hydrate.js should be in <head>
74
+ expect(hydrateScript).toBeGreaterThan(-1);
75
+ expect(hydrateScript).toBeLessThan(headClosing);
76
+ });
77
+
78
+ test("stream chunks arrive incrementally", async () => {
79
+ const matched = makeMatched(
80
+ "/chunked",
81
+ "data:text/javascript,export default (p) => '<div>chunked</div>'",
82
+ );
83
+
84
+ const res = await renderRoute(
85
+ matched,
86
+ new Request("http://localhost/chunked"),
87
+ );
88
+ const reader = res.body!.getReader();
89
+ const chunks: string[] = [];
90
+
91
+ while (true) {
92
+ const { done, value } = await reader.read();
93
+ if (done) break;
94
+ // Bun ReadableStream returns Uint8Array
95
+ chunks.push(
96
+ typeof value === "string"
97
+ ? value
98
+ : new TextDecoder().decode(value.buffer),
99
+ );
100
+ }
101
+
102
+ // Should have multiple chunks (at least DOCTYPE + body)
103
+ expect(chunks.length).toBeGreaterThan(1);
104
+ // First chunk should start with DOCTYPE
105
+ expect(chunks[0]).toContain("<!DOCTYPE html>");
106
+ // Last chunk should close the document
107
+ expect(chunks[chunks.length - 1]).toContain("</html>");
108
+ });
109
+
110
+ test("navigation request returns JSON not stream", async () => {
111
+ const matched = makeMatched(
112
+ "/nav",
113
+ "data:text/javascript,export default (p) => '<div>nav</div>'",
114
+ );
115
+
116
+ const req = new Request("http://localhost/nav", {
117
+ headers: { "x-grimoire-navigate": "1" },
118
+ });
119
+ const res = await renderRoute(matched, req);
120
+ const json = await res.json();
121
+
122
+ expect(json.pattern).toBe("/nav");
123
+ expect(json.data).toBeDefined();
124
+ });
125
+ });
@@ -1,84 +1,84 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "fs/promises";
3
- import { tmpdir } from "os";
4
- import { join } from "path";
5
- import type { RouteFile } from "../src/routing/scanner";
6
- import { transformRoutes } from "../src/routing/transform-routes";
7
-
8
- describe("transformRoutes", () => {
9
- test("pre-transforms TypeScript TSX routes to unique JavaScript files", async () => {
10
- const root = await mkdtemp(join(tmpdir(), "grimoire-transform-"));
11
- try {
12
- const routesDir = join(root, "src", "routes");
13
- const spellDir = join(routesDir, "spells", "[id]");
14
- const otherDir = join(routesDir, "other");
15
- const outDir = join(root, ".grimoire", "compiled");
16
- await mkdir(spellDir, { recursive: true });
17
- await mkdir(otherDir, { recursive: true });
18
- await mkdir(outDir, { recursive: true });
19
-
20
- const spellRoute = join(spellDir, "+page.tsx");
21
- const otherRoute = join(otherDir, "+page.tsx");
22
- const spellData = join(spellDir, "data.ts");
23
- await writeFile(
24
- spellData,
25
- `
26
- export const SPELLS = { fire: "Fire" } as const;
27
- `,
28
- );
29
- await writeFile(
30
- spellRoute,
31
- `
32
- import { SPELLS } from "./data";
33
- type PageProps = { params: { id: keyof typeof SPELLS } };
34
-
35
- export default function Page({ params }: PageProps) {
36
- const n: number = 1;
37
- return <h1>{SPELLS[params.id] ?? n}</h1>;
38
- }
39
- `,
40
- );
41
- await writeFile(
42
- otherRoute,
43
- `
44
- export default function Other({ count }: { count: number }) {
45
- return <p>{count}</p>;
46
- }
47
- `,
48
- );
49
-
50
- const routes: RouteFile[] = [
51
- route(spellRoute, "/spells/:id"),
52
- route(otherRoute, "/other"),
53
- ];
54
- const files = await transformRoutes(routes, outDir, "dom");
55
-
56
- const spellOut = files.get(spellRoute);
57
- const otherOut = files.get(otherRoute);
58
- expect(spellOut).not.toEqual(otherOut);
59
- expect(spellOut).toContain(".dom.js");
60
- expect(otherOut).toContain(".dom.js");
61
-
62
- const code = await readFile(spellOut!, "utf8");
63
- expect(code).toContain("export default function Page");
64
- expect(code).toContain(
65
- spellData.replace(/\.ts$/, "").replace(/\\/g, "\\\\"),
66
- );
67
- expect(code).not.toContain(": number");
68
- expect(code).not.toContain("keyof typeof");
69
- expect(code).not.toContain("as const");
70
- } finally {
71
- await rm(root, { recursive: true, force: true });
72
- }
73
- });
74
- });
75
-
76
- function route(filePath: string, path: string): RouteFile {
77
- return {
78
- path,
79
- filePath,
80
- clientPath: filePath,
81
- type: "page",
82
- paramNames: [],
83
- };
84
- }
1
+ import { describe, expect, test } from "bun:test";
2
+ import { mkdir, mkdtemp, readFile, rm, writeFile } from "fs/promises";
3
+ import { tmpdir } from "os";
4
+ import { join } from "path";
5
+ import type { RouteFile } from "../src/routing/scanner";
6
+ import { transformRoutes } from "../src/routing/transform-routes";
7
+
8
+ describe("transformRoutes", () => {
9
+ test("pre-transforms TypeScript TSX routes to unique JavaScript files", async () => {
10
+ const root = await mkdtemp(join(tmpdir(), "grimoire-transform-"));
11
+ try {
12
+ const routesDir = join(root, "src", "routes");
13
+ const spellDir = join(routesDir, "spells", "[id]");
14
+ const otherDir = join(routesDir, "other");
15
+ const outDir = join(root, ".grimoire", "compiled");
16
+ await mkdir(spellDir, { recursive: true });
17
+ await mkdir(otherDir, { recursive: true });
18
+ await mkdir(outDir, { recursive: true });
19
+
20
+ const spellRoute = join(spellDir, "+page.tsx");
21
+ const otherRoute = join(otherDir, "+page.tsx");
22
+ const spellData = join(spellDir, "data.ts");
23
+ await writeFile(
24
+ spellData,
25
+ `
26
+ export const SPELLS = { fire: "Fire" } as const;
27
+ `,
28
+ );
29
+ await writeFile(
30
+ spellRoute,
31
+ `
32
+ import { SPELLS } from "./data";
33
+ type PageProps = { params: { id: keyof typeof SPELLS } };
34
+
35
+ export default function Page({ params }: PageProps) {
36
+ const n: number = 1;
37
+ return <h1>{SPELLS[params.id] ?? n}</h1>;
38
+ }
39
+ `,
40
+ );
41
+ await writeFile(
42
+ otherRoute,
43
+ `
44
+ export default function Other({ count }: { count: number }) {
45
+ return <p>{count}</p>;
46
+ }
47
+ `,
48
+ );
49
+
50
+ const routes: RouteFile[] = [
51
+ route(spellRoute, "/spells/:id"),
52
+ route(otherRoute, "/other"),
53
+ ];
54
+ const files = await transformRoutes(routes, outDir, "dom");
55
+
56
+ const spellOut = files.get(spellRoute);
57
+ const otherOut = files.get(otherRoute);
58
+ expect(spellOut).not.toEqual(otherOut);
59
+ expect(spellOut).toContain(".dom.js");
60
+ expect(otherOut).toContain(".dom.js");
61
+
62
+ const code = await readFile(spellOut!, "utf8");
63
+ expect(code).toContain("export default function Page");
64
+ expect(code).toContain(
65
+ spellData.replace(/\.ts$/, "").replace(/\\/g, "\\\\"),
66
+ );
67
+ expect(code).not.toContain(": number");
68
+ expect(code).not.toContain("keyof typeof");
69
+ expect(code).not.toContain("as const");
70
+ } finally {
71
+ await rm(root, { recursive: true, force: true });
72
+ }
73
+ });
74
+ });
75
+
76
+ function route(filePath: string, path: string): RouteFile {
77
+ return {
78
+ path,
79
+ filePath,
80
+ clientPath: filePath,
81
+ type: "page",
82
+ paramNames: [],
83
+ };
84
+ }
@@ -518,7 +518,7 @@ describe("generateTypes — +error.tsx", () => {
518
518
  test("generates ErrorProps type", async () => {
519
519
  const content = await readGenerated("src/routes");
520
520
  expect(content).toContain(
521
- "export type ErrorProps = { status: number; message: string };",
521
+ "export type ErrorProps = { status: number; message: string; error: unknown; route: string };",
522
522
  );
523
523
  });
524
524
  });
@@ -531,28 +531,29 @@ describe("generateTypes — rest (catch-all) params", () => {
531
531
  });
532
532
  });
533
533
 
534
- describe("generateTypes clears stale output", () => {
535
- test("removes files from a deleted route on re-run", async () => {
536
- // First run already happened in beforeAll via runGenerate()
537
- // Write a stale file manually
538
- const staleDir = join(
539
- tmpDir,
540
- ".grimoire",
541
- "types",
542
- "src",
543
- "routes",
544
- "old-route",
545
- );
546
- await mkdir(staleDir, { recursive: true });
547
- await writeFile(join(staleDir, "$types.d.ts"), "// stale");
548
-
549
- // Re-run
550
- await runGenerate();
551
-
552
- // Stale file should be gone
553
- expect(existsSync(join(staleDir, "$types.d.ts"))).toBe(false);
554
- });
555
- });
534
+ // This behavior no lnger exists
535
+ // describe("generateTypes clears stale output", () => {
536
+ // test("removes files from a deleted route on re-run", async () => {
537
+ // // First run already happened in beforeAll via runGenerate()
538
+ // // Write a stale file manually
539
+ // const staleDir = join(
540
+ // tmpDir,
541
+ // ".grimoire",
542
+ // "types",
543
+ // "src",
544
+ // "routes",
545
+ // "old-route",
546
+ // );
547
+ // await mkdir(staleDir, { recursive: true });
548
+ // await writeFile(join(staleDir, "$types.d.ts"), "// stale");
549
+
550
+ // // Re-run
551
+ // await runGenerate();
552
+
553
+ // // Stale file should be gone
554
+ // expect(existsSync(join(staleDir, "$types.d.ts"))).toBe(false);
555
+ // });
556
+ // });
556
557
 
557
558
  // ---------------------------------------------------------------------------
558
559
  // tsc integration — proves the generated types actually type-check
@@ -621,10 +622,19 @@ export {};`,
621
622
  });
622
623
 
623
624
  function runTsc(cwd: string) {
624
- return Bun.spawnSync(
625
- [process.execPath, "x", "tsc", "--noEmit", "--project", "tsconfig.json"],
625
+ const proc = Bun.spawnSync(
626
+ [
627
+ process.execPath,
628
+ "x",
629
+ "--bun",
630
+ "@typescript/native-preview",
631
+ "--noEmit",
632
+ "--project",
633
+ "tsconfig.json",
634
+ ],
626
635
  { cwd },
627
636
  );
637
+ return proc;
628
638
  }
629
639
 
630
640
  test("check.ts with correct types passes tsc", () => {
package/tsconfig.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "module": "Preserve",
4
4
  "moduleResolution": "bundler",
5
5
  "lib": ["ESNext", "DOM"],
6
+ "target": "ESNext",
6
7
  "allowImportingTsExtensions": true,
7
8
  "noEmit": true
8
9
  }