@sigil-dev/grimoire 0.9.2 → 0.9.4
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 +37 -35
- package/package.json +9 -12
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +31 -8
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +5 -5
- package/src/client/router.ts +529 -371
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +163 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +114 -114
- package/src/dev/hmr-client.ts +185 -185
- package/src/dev/loader.ts +47 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/watcher.ts +47 -46
- package/src/env/dynamic-private.ts +19 -19
- package/src/env/dynamic-public.ts +22 -22
- package/src/env/index.ts +25 -25
- package/src/env/plugin.ts +19 -19
- package/src/env/private.ts +5 -5
- package/src/env/public.ts +7 -7
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +114 -114
- package/src/integrations/vite.ts +73 -73
- package/src/logger/index.ts +4 -4
- package/src/logger/instance.ts +6 -6
- package/src/logger/logger.ts +113 -113
- package/src/logger/request-middleware.ts +24 -24
- package/src/logger/transports.ts +117 -117
- package/src/logger/types.ts +39 -39
- package/src/rendering/head.ts +47 -47
- package/src/rendering/hydrate.ts +146 -119
- package/src/rendering/index.ts +573 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +55 -42
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +187 -141
- package/src/routing/transform-routes.ts +102 -102
- package/src/sentinels/error.ts +52 -52
- package/src/sentinels/fail.ts +41 -41
- package/src/sentinels/redirect.ts +35 -35
- package/src/server/build.ts +255 -219
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -356
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +677 -658
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -120
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -356
- package/src/types.ts +8 -4
- package/test/client-router.test.ts +295 -35
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +70 -70
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hydration.test.ts +119 -119
- package/test/middleware.test.ts +223 -223
- package/test/preload.ts +5 -5
- package/test/redirect-error.test.ts +112 -112
- package/test/rendering.test.ts +728 -579
- package/test/routing.test.ts +194 -81
- package/test/scanning.test.ts +254 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +354 -249
- package/test/streaming.test.ts +242 -125
- package/test/transform-routes.test.ts +87 -84
- package/test/typegen.test.ts +697 -682
- package/tsconfig.json +10 -10
- package/.grimoire/_routes.dom.js +0 -8
- package/.grimoire/_routes.hydrate.js +0 -8
- package/.grimoire/tsconfig.generated.json +0 -11
- package/.grimoire/types/ambient.d.ts +0 -59
- package/.grimoire/types/api/hello/$types.d.ts +0 -50
- package/.grimoire/types/api/items/$types.d.ts +0 -50
- package/.grimoire/types/echo/$types.d.ts +0 -50
- package/.grimoire/types/env-private.d.ts +0 -5
- package/.grimoire/types/env-public.d.ts +0 -5
- package/.grimoire/types/mixed/$types.d.ts +0 -50
- package/.grimoire/types/params/[docId]/$types.d.ts +0 -52
- package/.grimoire/types/reject/$types.d.ts +0 -50
- package/public/__grimoire__/hydrate.js +0 -585
- package/public/__grimoire__/index.js +0 -490
package/test/streaming.test.ts
CHANGED
|
@@ -1,125 +1,242 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
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
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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 { renderRoute } from "../src/rendering";
|
|
6
|
+
import { matchRoute } from "../src/routing/router";
|
|
7
|
+
import { scanRoutes } from "../src/routing/scanner";
|
|
8
|
+
|
|
9
|
+
function makeMatched(path: string, filePath: string, params = {}) {
|
|
10
|
+
return {
|
|
11
|
+
route: { path, params: {}, filePath },
|
|
12
|
+
params,
|
|
13
|
+
layouts: [],
|
|
14
|
+
layoutServers: [],
|
|
15
|
+
} as any;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("Streaming SSR", () => {
|
|
19
|
+
test("returns a ReadableStream response", async () => {
|
|
20
|
+
const matched = makeMatched(
|
|
21
|
+
"/",
|
|
22
|
+
"data:text/javascript,export default (p) => '<div>hello</div>'",
|
|
23
|
+
);
|
|
24
|
+
const res = await renderRoute(matched, new Request("http://localhost/"));
|
|
25
|
+
expect(res.body).toBeInstanceOf(ReadableStream);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("stream contains full HTML document", async () => {
|
|
29
|
+
const matched = makeMatched(
|
|
30
|
+
"/test",
|
|
31
|
+
"data:text/javascript,export default (p) => '<p>streaming works</p>'",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const res = await renderRoute(
|
|
35
|
+
matched,
|
|
36
|
+
new Request("http://localhost/test"),
|
|
37
|
+
);
|
|
38
|
+
const html = await res.text();
|
|
39
|
+
|
|
40
|
+
expect(html).toContain("<!DOCTYPE html>");
|
|
41
|
+
expect(html).toContain('<meta charset="UTF-8" />');
|
|
42
|
+
expect(html).toContain("streaming works");
|
|
43
|
+
expect(html).toContain("</html>");
|
|
44
|
+
expect(html).toContain("__grimoire_state__");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("stream includes state JSON with route pattern", async () => {
|
|
48
|
+
const matched = makeMatched(
|
|
49
|
+
"/spells/:id",
|
|
50
|
+
"data:text/javascript,export default (p) => '<div>spell</div>'",
|
|
51
|
+
{ id: "fireball" },
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const res = await renderRoute(
|
|
55
|
+
matched,
|
|
56
|
+
new Request("http://localhost/spells/fireball"),
|
|
57
|
+
);
|
|
58
|
+
const html = await res.text();
|
|
59
|
+
|
|
60
|
+
expect(html).toContain('"pattern":"/spells/:id"');
|
|
61
|
+
expect(html).toContain('"id":"fireball"');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("head script tag comes before head content", async () => {
|
|
65
|
+
const matched = makeMatched(
|
|
66
|
+
"/head-test",
|
|
67
|
+
"data:text/javascript,export default (p) => '<div>head</div>'",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const res = await renderRoute(
|
|
71
|
+
matched,
|
|
72
|
+
new Request("http://localhost/head-test"),
|
|
73
|
+
);
|
|
74
|
+
const html = await res.text();
|
|
75
|
+
|
|
76
|
+
const hydrateScript = html.indexOf("hydrate.js");
|
|
77
|
+
const headClosing = html.indexOf("</head>");
|
|
78
|
+
// hydrate.js should be in <head>
|
|
79
|
+
expect(hydrateScript).toBeGreaterThan(-1);
|
|
80
|
+
expect(hydrateScript).toBeLessThan(headClosing);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("stream chunks arrive incrementally", async () => {
|
|
84
|
+
const matched = makeMatched(
|
|
85
|
+
"/chunked",
|
|
86
|
+
"data:text/javascript,export default (p) => '<div>chunked</div>'",
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const res = await renderRoute(
|
|
90
|
+
matched,
|
|
91
|
+
new Request("http://localhost/chunked"),
|
|
92
|
+
);
|
|
93
|
+
const reader = res.body!.getReader();
|
|
94
|
+
const chunks: string[] = [];
|
|
95
|
+
|
|
96
|
+
while (true) {
|
|
97
|
+
const { done, value } = await reader.read();
|
|
98
|
+
if (done) break;
|
|
99
|
+
// Bun ReadableStream returns Uint8Array
|
|
100
|
+
chunks.push(
|
|
101
|
+
typeof value === "string"
|
|
102
|
+
? value
|
|
103
|
+
: new TextDecoder().decode(value.buffer),
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Should have multiple chunks (at least DOCTYPE + body)
|
|
108
|
+
expect(chunks.length).toBeGreaterThan(1);
|
|
109
|
+
// First chunk should start with DOCTYPE
|
|
110
|
+
expect(chunks[0]).toContain("<!DOCTYPE html>");
|
|
111
|
+
// Last chunk should close the document
|
|
112
|
+
expect(chunks[chunks.length - 1]).toContain("</html>");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("navigation request returns JSON not stream", async () => {
|
|
116
|
+
const matched = makeMatched(
|
|
117
|
+
"/nav",
|
|
118
|
+
"data:text/javascript,export default (p) => '<div>nav</div>'",
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const req = new Request("http://localhost/nav", {
|
|
122
|
+
headers: { "x-grimoire-navigate": "1" },
|
|
123
|
+
});
|
|
124
|
+
const res = await renderRoute(matched, req);
|
|
125
|
+
const json = await res.json();
|
|
126
|
+
|
|
127
|
+
expect(json.pattern).toBe("/nav");
|
|
128
|
+
expect(json.data).toBeDefined();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("state JSON escapes </script> in data (XSS)", async () => {
|
|
132
|
+
const evil = "</script><script>alert(1)</script>";
|
|
133
|
+
const src = [
|
|
134
|
+
"export const load = async () => ({ evil })",
|
|
135
|
+
"const evil = " + JSON.stringify(evil),
|
|
136
|
+
'export default () => "<div>ok</div>"',
|
|
137
|
+
].join(";\n");
|
|
138
|
+
const matched = makeMatched(
|
|
139
|
+
"/xss",
|
|
140
|
+
`data:text/javascript,${encodeURIComponent(src)}`,
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const res = await renderRoute(matched, new Request("http://localhost/xss"));
|
|
144
|
+
const html = await res.text();
|
|
145
|
+
|
|
146
|
+
const tagStart = html.indexOf('<script id="__grimoire_state__"');
|
|
147
|
+
expect(tagStart).toBeGreaterThan(-1);
|
|
148
|
+
const payloadStart = html.indexOf(">", tagStart) + 1;
|
|
149
|
+
const payloadEnd = html.indexOf("</script>", payloadStart);
|
|
150
|
+
const payload = html.slice(payloadStart, payloadEnd);
|
|
151
|
+
|
|
152
|
+
// no literal closing tag inside the JSON payload
|
|
153
|
+
expect(payload).not.toContain("</script>");
|
|
154
|
+
// `<` escaped as \u003c — JSON.parse decodes it back
|
|
155
|
+
expect(payload).toContain("\\u003c/script");
|
|
156
|
+
const parsed = JSON.parse(payload);
|
|
157
|
+
expect(parsed.data.evil).toBe(evil);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("streamed promises (deferred data)", () => {
|
|
162
|
+
let root: string;
|
|
163
|
+
|
|
164
|
+
beforeAll(async () => {
|
|
165
|
+
root = join(tmpdir(), `grimoire-defer-${Date.now()}`);
|
|
166
|
+
await mkdir(root, { recursive: true });
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
afterAll(async () => {
|
|
170
|
+
await rm(root, { recursive: true });
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
async function readChunks(res: Response): Promise<string[]> {
|
|
174
|
+
const reader = res.body!.getReader();
|
|
175
|
+
const chunks: string[] = [];
|
|
176
|
+
while (true) {
|
|
177
|
+
const { done, value } = await reader.read();
|
|
178
|
+
if (done) break;
|
|
179
|
+
chunks.push(String(value));
|
|
180
|
+
}
|
|
181
|
+
return chunks;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async function setup(name: string, serverSrc: string): Promise<string[]> {
|
|
185
|
+
// unique dir per test — Bun caches modules by resolved path
|
|
186
|
+
const dir = join(root, name);
|
|
187
|
+
await mkdir(dir, { recursive: true });
|
|
188
|
+
await writeFile(join(dir, "+page.server.ts"), serverSrc);
|
|
189
|
+
await writeFile(
|
|
190
|
+
join(dir, "+page.tsx"),
|
|
191
|
+
'export default function P({ data }) { return <div>{data.fast}{String(data.slow)}{String(data.boom)}</div>; }',
|
|
192
|
+
);
|
|
193
|
+
const tree = await scanRoutes(dir);
|
|
194
|
+
const matched = matchRoute(tree, new URL("http://localhost/"));
|
|
195
|
+
const res = await renderRoute(matched!, new Request("http://localhost/"));
|
|
196
|
+
return readChunks(res);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
test("slow promise flushes after the document shell", async () => {
|
|
200
|
+
const chunks = await setup(
|
|
201
|
+
"slow",
|
|
202
|
+
[
|
|
203
|
+
"export async function load() {",
|
|
204
|
+
" return {",
|
|
205
|
+
' fast: "now",',
|
|
206
|
+
' slow: new Promise((r) => setTimeout(() => r("late"), 30)),',
|
|
207
|
+
" };",
|
|
208
|
+
"}",
|
|
209
|
+
].join("\n"),
|
|
210
|
+
);
|
|
211
|
+
const all = chunks.join("");
|
|
212
|
+
|
|
213
|
+
// state payload marks the field as deferred
|
|
214
|
+
expect(all).toContain('"__deferred":"gd-0"');
|
|
215
|
+
// the shell flushed before the promise settled
|
|
216
|
+
const shell = chunks.find((c) => c.includes("__grimoire_state__"))!;
|
|
217
|
+
expect(shell).not.toContain('"late"');
|
|
218
|
+
// a later chunk carries the self-contained patch script
|
|
219
|
+
const late = chunks.find((c) => c.includes("grimoire:deferred"));
|
|
220
|
+
expect(late).toBeDefined();
|
|
221
|
+
expect(late).toContain('"late"');
|
|
222
|
+
expect(late).toContain("__grimoire_state__");
|
|
223
|
+
expect(late).toContain('data["slow"]');
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("rejected promise emits error detail and null value", async () => {
|
|
227
|
+
const all = (
|
|
228
|
+
await setup(
|
|
229
|
+
"reject",
|
|
230
|
+
[
|
|
231
|
+
"export async function load() {",
|
|
232
|
+
" const boom = Promise.reject(new Error('nope'));",
|
|
233
|
+
" boom.catch(() => {});",
|
|
234
|
+
" return { boom };",
|
|
235
|
+
"}",
|
|
236
|
+
].join("\n"),
|
|
237
|
+
)
|
|
238
|
+
).join("");
|
|
239
|
+
expect(all).toContain("error:true");
|
|
240
|
+
expect(all).toContain('data["boom"]');
|
|
241
|
+
});
|
|
242
|
+
});
|
|
@@ -1,84 +1,87 @@
|
|
|
1
|
-
import { describe, expect, test } from "bun:test";
|
|
2
|
-
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node: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 "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node: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
|
+
optionalParamNames: [],
|
|
84
|
+
restParamNames: [],
|
|
85
|
+
paramMatchers: {},
|
|
86
|
+
};
|
|
87
|
+
}
|