@sigil-dev/grimoire 0.7.6 → 0.8.0
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 +35 -34
- package/package.json +8 -6
- package/preload.js +3 -2
- package/server.ts +13 -13
- package/src/client/head.ts +29 -29
- package/src/client/router.ts +120 -53
- package/src/dev/compile-module.ts +173 -0
- package/src/dev/effect-registry.ts +23 -0
- package/src/dev/graph.ts +114 -0
- package/src/dev/hmr-client.ts +158 -0
- package/src/dev/hmr-server.ts +187 -0
- package/src/dev/loader.ts +47 -0
- package/src/dev/paths.ts +14 -0
- package/src/dev/runtime-bundle.ts +49 -0
- package/src/dev/watcher.ts +44 -0
- package/src/integrations/vite.ts +73 -72
- package/src/rendering/hydrate.ts +102 -64
- package/src/rendering/index.ts +296 -199
- package/src/rendering/ssrPlugin.ts +67 -53
- package/src/routing/manifest-gen.ts +42 -39
- package/src/routing/router.ts +109 -106
- package/src/routing/scanner.ts +141 -135
- package/src/routing/transform-routes.ts +101 -101
- package/src/server/build.ts +239 -147
- package/src/server/coordinator.ts +306 -306
- package/src/server/index.ts +260 -50
- package/src/server/worker.ts +59 -59
- package/src/typegen/index.ts +356 -353
- package/src/types.ts +270 -269
- package/test/context.test.ts +52 -52
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/rendering.test.ts +579 -425
- package/test/routing.test.ts +81 -83
- package/test/scanning.test.ts +200 -181
- package/test/scope.test.ts +24 -8
- package/test/server.test.ts +249 -229
- package/test/streaming.test.ts +125 -106
- package/test/transform-routes.test.ts +84 -84
- package/test/typegen.test.ts +35 -25
- package/tsconfig.json +1 -0
package/test/streaming.test.ts
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
describe("Streaming SSR", () => {
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
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
|
+
}
|
package/test/typegen.test.ts
CHANGED
|
@@ -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
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
-
|
|
625
|
-
[
|
|
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", () => {
|