@sigil-dev/grimoire 0.9.25 → 0.9.27
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/package.json +3 -3
- package/src/client/router.ts +915 -901
- package/src/dev/compile-module.ts +193 -211
- package/src/dev/graph.ts +6 -5
- package/src/dev/hmr-client.ts +229 -202
- package/src/dev/hmr-server.ts +7 -2
- package/src/dev/loader.ts +144 -141
- package/src/dev/resolve.ts +85 -70
- package/src/rendering/hydrate.ts +162 -161
- package/src/rendering/index.ts +779 -779
- package/src/rendering/ssrPlugin.ts +4 -5
- package/src/server/build.ts +262 -262
- package/src/server/client-dir.ts +1 -1
- package/src/server/prerender.ts +18 -1
- package/src/server/resolver.ts +123 -102
- package/src/typegen/index.ts +61 -9
- package/test/hmr.test.ts +24 -0
- package/test/prerender.test.ts +19 -0
- package/test/typegen.test.ts +109 -0
package/src/server/resolver.ts
CHANGED
|
@@ -1,102 +1,123 @@
|
|
|
1
|
-
import { isAbsolute, join } from "node:path";
|
|
2
|
-
import type { BunPlugin } from "bun";
|
|
3
|
-
import { resolveFile } from "../dev/resolve";
|
|
4
|
-
import { ENV_MODULE, envModuleSource } from "../env/modules";
|
|
5
|
-
import type { GrimoireConfig } from "../types";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Import prefixes → absolute directories: `$lib` for `src/lib` unless the
|
|
9
|
-
* config says otherwise, plus `GrimoireConfig.alias`.
|
|
10
|
-
*/
|
|
11
|
-
export function projectAliases(
|
|
12
|
-
config: GrimoireConfig,
|
|
13
|
-
root = process.cwd(),
|
|
14
|
-
): Record<string, string> {
|
|
15
|
-
const out: Record<string, string> = { $lib: join(root, "src/lib") };
|
|
16
|
-
for (const [prefix, target] of Object.entries(config.alias ?? {})) {
|
|
17
|
-
out[prefix] = isAbsolute(target) ? target : join(root, target);
|
|
18
|
-
}
|
|
19
|
-
return out;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Resolves `$env/*` and aliased imports, for the browser builds (Bun.build)
|
|
26
|
-
* and for the server (Bun.plugin). Bun has no alias option of its own.
|
|
27
|
-
*/
|
|
28
|
-
export function resolverPlugin(
|
|
29
|
-
config: GrimoireConfig,
|
|
30
|
-
target: "server" | "browser",
|
|
31
|
-
): BunPlugin {
|
|
32
|
-
const aliases = projectAliases(config);
|
|
33
|
-
return {
|
|
34
|
-
name: `grimoire-resolve-${target}`,
|
|
35
|
-
setup(build) {
|
|
36
|
-
if (target === "server") {
|
|
37
|
-
// Bun's runtime plugins don't see static imports in onResolve;
|
|
38
|
-
// virtual modules have to be registered by name. Aliases on the
|
|
39
|
-
// server are rewritten by the SSR plugin (rewriteAliases).
|
|
40
|
-
for (const specifier of ENV_SPECIFIERS) {
|
|
41
|
-
build.module(specifier, () => ({
|
|
42
|
-
contents: envModuleSource(specifier, "server"),
|
|
43
|
-
loader: "js",
|
|
44
|
-
}));
|
|
45
|
-
}
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
build.onResolve({ filter: ENV_MODULE }, (args) => ({
|
|
49
|
-
path: args.path,
|
|
50
|
-
namespace: "grimoire-env",
|
|
51
|
-
}));
|
|
52
|
-
build.onLoad({ filter: /.*/, namespace: "grimoire-env" }, (args) => ({
|
|
53
|
-
contents: envModuleSource(args.path, target),
|
|
54
|
-
loader: "js",
|
|
55
|
-
}));
|
|
56
|
-
for (const [prefix, dir] of Object.entries(aliases)) {
|
|
57
|
-
build.onResolve(
|
|
58
|
-
{ filter: new RegExp(`^${escapeRegExp(prefix)}(/|$)`) },
|
|
59
|
-
(args) => {
|
|
60
|
-
const file = resolveFile(join(dir, args.path.slice(prefix.length)));
|
|
61
|
-
return file ? { path: file } : undefined;
|
|
62
|
-
},
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const ENV_SPECIFIERS = [
|
|
70
|
-
"$env/static/public",
|
|
71
|
-
"$env/static/private",
|
|
72
|
-
"$env/dynamic/public",
|
|
73
|
-
"$env/dynamic/private",
|
|
74
|
-
];
|
|
75
|
-
|
|
76
|
-
const SPEC_RE = /(from\s+|import\s+|import\s*\(\s*)(["'])([^"']+)\2/g;
|
|
77
|
-
|
|
78
|
-
/** Point aliased import specifiers in compiled code at their files. */
|
|
79
|
-
export function rewriteAliases(
|
|
80
|
-
code: string,
|
|
81
|
-
aliases: Record<string, string>,
|
|
82
|
-
): string {
|
|
83
|
-
const prefixes = Object.keys(aliases);
|
|
84
|
-
if (prefixes.length === 0) return code;
|
|
85
|
-
return code.replace(
|
|
86
|
-
SPEC_RE,
|
|
87
|
-
(match, keyword: string, quote: string, specifier: string) => {
|
|
88
|
-
const prefix = prefixes.find(
|
|
89
|
-
(p) => specifier === p || specifier.startsWith(`${p}/`),
|
|
90
|
-
);
|
|
91
|
-
if (!prefix) return match;
|
|
92
|
-
const file = resolveFile(
|
|
93
|
-
join(aliases[prefix]!, specifier.slice(prefix.length)),
|
|
94
|
-
);
|
|
95
|
-
// Forward slashes: this lands inside a JS string literal, where a
|
|
96
|
-
// Windows path's backslashes would be read as escapes (\P → P).
|
|
97
|
-
return file
|
|
98
|
-
? `${keyword}${quote}${file.replaceAll("\\", "/")}${quote}`
|
|
99
|
-
: match;
|
|
100
|
-
},
|
|
101
|
-
);
|
|
102
|
-
}
|
|
1
|
+
import { isAbsolute, join } from "node:path";
|
|
2
|
+
import type { BunPlugin } from "bun";
|
|
3
|
+
import { resolveFile } from "../dev/resolve";
|
|
4
|
+
import { ENV_MODULE, envModuleSource } from "../env/modules";
|
|
5
|
+
import type { GrimoireConfig } from "../types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Import prefixes → absolute directories: `$lib` for `src/lib` unless the
|
|
9
|
+
* config says otherwise, plus `GrimoireConfig.alias`.
|
|
10
|
+
*/
|
|
11
|
+
export function projectAliases(
|
|
12
|
+
config: GrimoireConfig,
|
|
13
|
+
root = process.cwd(),
|
|
14
|
+
): Record<string, string> {
|
|
15
|
+
const out: Record<string, string> = { $lib: join(root, "src/lib") };
|
|
16
|
+
for (const [prefix, target] of Object.entries(config.alias ?? {})) {
|
|
17
|
+
out[prefix] = isAbsolute(target) ? target : join(root, target);
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Resolves `$env/*` and aliased imports, for the browser builds (Bun.build)
|
|
26
|
+
* and for the server (Bun.plugin). Bun has no alias option of its own.
|
|
27
|
+
*/
|
|
28
|
+
export function resolverPlugin(
|
|
29
|
+
config: GrimoireConfig,
|
|
30
|
+
target: "server" | "browser",
|
|
31
|
+
): BunPlugin {
|
|
32
|
+
const aliases = projectAliases(config);
|
|
33
|
+
return {
|
|
34
|
+
name: `grimoire-resolve-${target}`,
|
|
35
|
+
setup(build) {
|
|
36
|
+
if (target === "server") {
|
|
37
|
+
// Bun's runtime plugins don't see static imports in onResolve;
|
|
38
|
+
// virtual modules have to be registered by name. Aliases on the
|
|
39
|
+
// server are rewritten by the SSR plugin (rewriteAliases).
|
|
40
|
+
for (const specifier of ENV_SPECIFIERS) {
|
|
41
|
+
build.module(specifier, () => ({
|
|
42
|
+
contents: envModuleSource(specifier, "server"),
|
|
43
|
+
loader: "js",
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
build.onResolve({ filter: ENV_MODULE }, (args) => ({
|
|
49
|
+
path: args.path,
|
|
50
|
+
namespace: "grimoire-env",
|
|
51
|
+
}));
|
|
52
|
+
build.onLoad({ filter: /.*/, namespace: "grimoire-env" }, (args) => ({
|
|
53
|
+
contents: envModuleSource(args.path, target),
|
|
54
|
+
loader: "js",
|
|
55
|
+
}));
|
|
56
|
+
for (const [prefix, dir] of Object.entries(aliases)) {
|
|
57
|
+
build.onResolve(
|
|
58
|
+
{ filter: new RegExp(`^${escapeRegExp(prefix)}(/|$)`) },
|
|
59
|
+
(args) => {
|
|
60
|
+
const file = resolveFile(join(dir, args.path.slice(prefix.length)));
|
|
61
|
+
return file ? { path: file } : undefined;
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const ENV_SPECIFIERS = [
|
|
70
|
+
"$env/static/public",
|
|
71
|
+
"$env/static/private",
|
|
72
|
+
"$env/dynamic/public",
|
|
73
|
+
"$env/dynamic/private",
|
|
74
|
+
];
|
|
75
|
+
|
|
76
|
+
const SPEC_RE = /(from\s+|import\s+|import\s*\(\s*)(["'])([^"']+)\2/g;
|
|
77
|
+
|
|
78
|
+
/** Point aliased import specifiers in compiled code at their files. */
|
|
79
|
+
export function rewriteAliases(
|
|
80
|
+
code: string,
|
|
81
|
+
aliases: Record<string, string>,
|
|
82
|
+
): string {
|
|
83
|
+
const prefixes = Object.keys(aliases);
|
|
84
|
+
if (prefixes.length === 0) return code;
|
|
85
|
+
return code.replace(
|
|
86
|
+
SPEC_RE,
|
|
87
|
+
(match, keyword: string, quote: string, specifier: string) => {
|
|
88
|
+
const prefix = prefixes.find(
|
|
89
|
+
(p) => specifier === p || specifier.startsWith(`${p}/`),
|
|
90
|
+
);
|
|
91
|
+
if (!prefix) return match;
|
|
92
|
+
const file = resolveFile(
|
|
93
|
+
join(aliases[prefix]!, specifier.slice(prefix.length)),
|
|
94
|
+
);
|
|
95
|
+
// Forward slashes: this lands inside a JS string literal, where a
|
|
96
|
+
// Windows path's backslashes would be read as escapes (\P → P).
|
|
97
|
+
return file
|
|
98
|
+
? `${keyword}${quote}${file.replaceAll("\\", "/")}${quote}`
|
|
99
|
+
: match;
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let runtimeEntry: string | null = null;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Point server-compiled code's `@sigil-dev/runtime` imports at grimoire's own
|
|
108
|
+
* copy of the runtime. The server's context root and the components' owners
|
|
109
|
+
* live in that instance, so page code must share it; and a page file outside
|
|
110
|
+
* the project's node_modules tree must still find it. Bun's runtime plugins
|
|
111
|
+
* don't see static imports in onResolve, so this rewrites the specifier.
|
|
112
|
+
*/
|
|
113
|
+
export function pinRuntime(code: string): string {
|
|
114
|
+
if (!code.includes("@sigil-dev/runtime")) return code;
|
|
115
|
+
runtimeEntry ??= Bun.resolveSync("@sigil-dev/runtime", import.meta.dir).replace(
|
|
116
|
+
/\\/g,
|
|
117
|
+
"/",
|
|
118
|
+
);
|
|
119
|
+
return code.replace(
|
|
120
|
+
/(\bfrom\s*|\bimport\s*\(\s*|\bimport\s+)(["'])@sigil-dev\/runtime\2/g,
|
|
121
|
+
(_m, lead: string) => `${lead}${JSON.stringify(runtimeEntry)}`,
|
|
122
|
+
);
|
|
123
|
+
}
|
package/src/typegen/index.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
mkdirSync,
|
|
3
|
+
readdirSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
rmSync,
|
|
6
|
+
writeFileSync,
|
|
7
|
+
} from "node:fs";
|
|
2
8
|
import { mkdir } from "node:fs/promises";
|
|
3
9
|
import { dirname, isAbsolute, join, relative } from "node:path";
|
|
4
10
|
import type { RouteFile, RouteTree } from "../routing/scanner.ts";
|
|
@@ -112,6 +118,22 @@ export function buildParams(
|
|
|
112
118
|
return `{\n${all.join("\n")}\n}`;
|
|
113
119
|
}
|
|
114
120
|
|
|
121
|
+
/**
|
|
122
|
+
* Whether a route module exports `load` itself. The runtime merges that
|
|
123
|
+
* universal load with the server one (server wins on conflict), so the types
|
|
124
|
+
* have to read both. Unreadable files count as "no".
|
|
125
|
+
*/
|
|
126
|
+
function exportsLoad(file: RouteFile | undefined): boolean {
|
|
127
|
+
if (!file) return false;
|
|
128
|
+
try {
|
|
129
|
+
return /export\s+(?:async\s+)?function\s+load(?![\w$])|export\s+(?:const|let|var)\s+load(?![\w$])|export\s*\{[^}]*(?<![\w$])load(?![\w$])[^}]*\}/.test(
|
|
130
|
+
readFileSync(file.filePath, "utf8"),
|
|
131
|
+
);
|
|
132
|
+
} catch {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
115
137
|
function generateTypesForGroup(group: RouteGroup, outFileDir: string): string {
|
|
116
138
|
const lines: string[] = [
|
|
117
139
|
"// Auto-generated by @sigil-dev/grimoire — do not edit",
|
|
@@ -127,6 +149,9 @@ function generateTypesForGroup(group: RouteGroup, outFileDir: string): string {
|
|
|
127
149
|
// --- Params ---
|
|
128
150
|
lines.push(`export type Params = ${paramsType};`, "");
|
|
129
151
|
|
|
152
|
+
const universalPage = exportsLoad(group.page);
|
|
153
|
+
const universalLayout = exportsLoad(group.layout);
|
|
154
|
+
|
|
130
155
|
// --- PageData from +page.server.ts ---
|
|
131
156
|
if (group.pageServer) {
|
|
132
157
|
const imp = toImportPath(outFileDir, group.pageServer.filePath);
|
|
@@ -136,9 +161,18 @@ function generateTypesForGroup(group: RouteGroup, outFileDir: string): string {
|
|
|
136
161
|
`type _PS = typeof import("${imp}");`,
|
|
137
162
|
`/** A top-level promise streams: undefined until it lands, null if it rejected. */`,
|
|
138
163
|
`type _Streamed<T> = { [K in keyof T]: T[K] extends PromiseLike<infer V> ? V | null | undefined : T[K] };`,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
164
|
+
...(universalPage
|
|
165
|
+
? [
|
|
166
|
+
`type _PG = typeof import("${toImportPath(outFileDir, group.page!.filePath)}");`,
|
|
167
|
+
`type _LoadOf<M> = M extends { load: (...args: any[]) => infer R } ? Awaited<R> : {};`,
|
|
168
|
+
`/** The page's own load() merged under the server load, which wins on conflict. */`,
|
|
169
|
+
`export type PageData = Omit<_LoadOf<_PG>, keyof _LoadOf<_PS>> & _Streamed<_LoadOf<_PS>>;`,
|
|
170
|
+
]
|
|
171
|
+
: [
|
|
172
|
+
`export type PageData = _PS extends { load: (...args: any[]) => infer R }`,
|
|
173
|
+
` ? _Streamed<Awaited<R>>`,
|
|
174
|
+
` : Record<string, never>;`,
|
|
175
|
+
]),
|
|
142
176
|
"",
|
|
143
177
|
);
|
|
144
178
|
// ActionData: what the route's actions pass to fail(), which comes back
|
|
@@ -201,7 +235,12 @@ function generateTypesForGroup(group: RouteGroup, outFileDir: string): string {
|
|
|
201
235
|
);
|
|
202
236
|
} else {
|
|
203
237
|
lines.push(
|
|
204
|
-
|
|
238
|
+
...(universalPage
|
|
239
|
+
? [
|
|
240
|
+
`type _PG = typeof import("${toImportPath(outFileDir, group.page!.filePath)}");`,
|
|
241
|
+
`export type PageData = _PG extends { load: (...args: any[]) => infer R } ? Awaited<R> : Record<string, never>;`,
|
|
242
|
+
]
|
|
243
|
+
: [`export type PageData = Record<string, never>;`]),
|
|
205
244
|
`export type ActionData = never;`,
|
|
206
245
|
`export type PageProps = { data: PageData; params: Params; form?: ActionData };`,
|
|
207
246
|
`export type PageServerLoad = never;`,
|
|
@@ -215,17 +254,30 @@ function generateTypesForGroup(group: RouteGroup, outFileDir: string): string {
|
|
|
215
254
|
const imp = toImportPath(outFileDir, group.layoutServer.filePath);
|
|
216
255
|
lines.push(
|
|
217
256
|
`type _LS = typeof import("${imp}");`,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
257
|
+
...(universalLayout
|
|
258
|
+
? [
|
|
259
|
+
`type _LG = typeof import("${toImportPath(outFileDir, group.layout!.filePath)}");`,
|
|
260
|
+
`type _LayoutLoadOf<M> = M extends { load: (...args: any[]) => infer R } ? Awaited<R> : {};`,
|
|
261
|
+
`export type LayoutData = Omit<_LayoutLoadOf<_LG>, keyof _LayoutLoadOf<_LS>> & _LayoutLoadOf<_LS>;`,
|
|
262
|
+
]
|
|
263
|
+
: [
|
|
264
|
+
`export type LayoutData = _LS extends { load: (...args: any[]) => infer R }`,
|
|
265
|
+
` ? Awaited<R>`,
|
|
266
|
+
` : Record<string, never>;`,
|
|
267
|
+
]),
|
|
221
268
|
"",
|
|
222
269
|
);
|
|
270
|
+
} else if (universalLayout) {
|
|
271
|
+
lines.push(
|
|
272
|
+
`type _LG = typeof import("${toImportPath(outFileDir, group.layout!.filePath)}");`,
|
|
273
|
+
`export type LayoutData = _LG extends { load: (...args: any[]) => infer R } ? Awaited<R> : Record<string, never>;`,
|
|
274
|
+
);
|
|
223
275
|
} else {
|
|
224
276
|
lines.push(`export type LayoutData = Record<string, never>;`);
|
|
225
277
|
}
|
|
226
278
|
|
|
227
279
|
lines.push(
|
|
228
|
-
`export type LayoutProps = { data: LayoutData; params: Params; children?: unknown };`,
|
|
280
|
+
`export type LayoutProps = { data: LayoutData; params: Params; path: string; children?: unknown };`,
|
|
229
281
|
`/** What a layout's load() receives. */`,
|
|
230
282
|
`export type LayoutServerLoadEvent = {`,
|
|
231
283
|
` params: Params;`,
|
package/test/hmr.test.ts
CHANGED
|
@@ -196,6 +196,30 @@ describe("DevGraph", () => {
|
|
|
196
196
|
}
|
|
197
197
|
});
|
|
198
198
|
|
|
199
|
+
test("a page that imports another page is affected by edits to it", async () => {
|
|
200
|
+
const f = await fixture();
|
|
201
|
+
try {
|
|
202
|
+
// /other re-exports the home page's module
|
|
203
|
+
await writeFile(
|
|
204
|
+
f.files.other,
|
|
205
|
+
`import Home from "./../+page.tsx";
|
|
206
|
+
export default Home;`,
|
|
207
|
+
);
|
|
208
|
+
await crawlFrom([f.files.other], {
|
|
209
|
+
graph: f.graph,
|
|
210
|
+
projectRoot: f.root,
|
|
211
|
+
aliases: {},
|
|
212
|
+
});
|
|
213
|
+
const patterns = f.graph
|
|
214
|
+
.affectedRoutes(f.files.page)
|
|
215
|
+
.map((r) => `${r.type} ${r.path}`)
|
|
216
|
+
.sort();
|
|
217
|
+
expect(patterns).toEqual(["page /", "page /other"]);
|
|
218
|
+
} finally {
|
|
219
|
+
await f.cleanup();
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
199
223
|
test("setImports drops back-links for removed imports", async () => {
|
|
200
224
|
const f = await fixture();
|
|
201
225
|
try {
|
package/test/prerender.test.ts
CHANGED
|
@@ -202,6 +202,25 @@ describe("prerenderSite", () => {
|
|
|
202
202
|
expect(await exists(join(out, "404.html"))).toBe(true);
|
|
203
203
|
});
|
|
204
204
|
|
|
205
|
+
test("copies the client bundle to /__grimoire__/, without dev-only files", async () => {
|
|
206
|
+
const t = await setup({
|
|
207
|
+
routes: [route("/", "page")],
|
|
208
|
+
pages: { "/": () => html("home") },
|
|
209
|
+
});
|
|
210
|
+
const client = join(t.root, ".grimoire", "client");
|
|
211
|
+
await mkdir(client, { recursive: true });
|
|
212
|
+
await writeFile(join(client, "hydrate-abcd1234.js"), "hydrate");
|
|
213
|
+
await writeFile(join(client, "chunk-efgh5678.js"), "chunk");
|
|
214
|
+
await writeFile(join(client, "runtime.js"), "dev only");
|
|
215
|
+
await t.run();
|
|
216
|
+
const out = join(t.root, "build", "__grimoire__");
|
|
217
|
+
expect(await readFile(join(out, "hydrate-abcd1234.js"), "utf8")).toBe(
|
|
218
|
+
"hydrate",
|
|
219
|
+
);
|
|
220
|
+
expect(await readFile(join(out, "chunk-efgh5678.js"), "utf8")).toBe("chunk");
|
|
221
|
+
expect(await exists(join(out, "runtime.js"))).toBe(false);
|
|
222
|
+
});
|
|
223
|
+
|
|
205
224
|
test("writes a hashed copy of each public stylesheet beside the original", async () => {
|
|
206
225
|
const t = await setup({
|
|
207
226
|
routes: [route("/", "page")],
|
package/test/typegen.test.ts
CHANGED
|
@@ -806,3 +806,112 @@ describe("tsconfig paths for aliases", () => {
|
|
|
806
806
|
}
|
|
807
807
|
});
|
|
808
808
|
});
|
|
809
|
+
|
|
810
|
+
describe("universal load in +page.tsx / +layout.tsx", () => {
|
|
811
|
+
test("PageData and LayoutData merge it under the server load, and tsc agrees", async () => {
|
|
812
|
+
const root = join(tmpdir(), `grimoire-universal-${Date.now()}`);
|
|
813
|
+
const routeDir = join(root, "src", "routes", "post");
|
|
814
|
+
await mkdir(routeDir, { recursive: true });
|
|
815
|
+
try {
|
|
816
|
+
await writeFile(
|
|
817
|
+
join(routeDir, "+page.tsx"),
|
|
818
|
+
`export async function load() { return { a: "universal", shared: 1, loadUsers: true }; }
|
|
819
|
+
export default () => null;
|
|
820
|
+
`,
|
|
821
|
+
);
|
|
822
|
+
await writeFile(
|
|
823
|
+
join(routeDir, "+page.server.ts"),
|
|
824
|
+
`export async function load() { return { b: 2, shared: "server" }; }`,
|
|
825
|
+
);
|
|
826
|
+
await writeFile(
|
|
827
|
+
join(routeDir, "+layout.tsx"),
|
|
828
|
+
`export async function load() { return { theme: "dark" }; }
|
|
829
|
+
export default (p: { children?: unknown }) => p.children;
|
|
830
|
+
`,
|
|
831
|
+
);
|
|
832
|
+
await writeFile(
|
|
833
|
+
join(routeDir, "check.ts"),
|
|
834
|
+
`import type { LayoutProps, PageProps } from "./$types";
|
|
835
|
+
declare const page: PageProps;
|
|
836
|
+
declare const layout: LayoutProps;
|
|
837
|
+
const _a: string = page.data.a;
|
|
838
|
+
const _b: number = page.data.b;
|
|
839
|
+
// the server load wins on a clashing key
|
|
840
|
+
const _shared: string = page.data.shared;
|
|
841
|
+
const _theme: string = layout.data.theme;
|
|
842
|
+
const _path: string = layout.path;
|
|
843
|
+
// @ts-expect-error not exported by either load
|
|
844
|
+
page.data.nope;
|
|
845
|
+
export {};`,
|
|
846
|
+
);
|
|
847
|
+
await writeFile(
|
|
848
|
+
join(root, "tsconfig.json"),
|
|
849
|
+
JSON.stringify({
|
|
850
|
+
extends: "./.grimoire/tsconfig.generated.json",
|
|
851
|
+
compilerOptions: {
|
|
852
|
+
target: "ESNext",
|
|
853
|
+
module: "ESNext",
|
|
854
|
+
moduleResolution: "bundler",
|
|
855
|
+
jsx: "preserve",
|
|
856
|
+
strict: true,
|
|
857
|
+
skipLibCheck: true,
|
|
858
|
+
noEmit: true,
|
|
859
|
+
},
|
|
860
|
+
include: ["src", ".grimoire/types"],
|
|
861
|
+
}),
|
|
862
|
+
);
|
|
863
|
+
const routesDir = join(root, "src", "routes");
|
|
864
|
+
const tree = await scanRoutes(routesDir, root);
|
|
865
|
+
await generateTypes(tree, {
|
|
866
|
+
projectRoot: root,
|
|
867
|
+
routesDir,
|
|
868
|
+
outDir: join(root, ".grimoire", "types"),
|
|
869
|
+
});
|
|
870
|
+
const proc = Bun.spawnSync(
|
|
871
|
+
[
|
|
872
|
+
process.execPath,
|
|
873
|
+
"x",
|
|
874
|
+
"--bun",
|
|
875
|
+
"@typescript/native-preview",
|
|
876
|
+
"--noEmit",
|
|
877
|
+
"--project",
|
|
878
|
+
"tsconfig.json",
|
|
879
|
+
],
|
|
880
|
+
{ cwd: root },
|
|
881
|
+
);
|
|
882
|
+
expect(proc.stdout.toString() + proc.stderr.toString()).not.toContain(
|
|
883
|
+
"error TS",
|
|
884
|
+
);
|
|
885
|
+
expect(proc.exitCode).toBe(0);
|
|
886
|
+
} finally {
|
|
887
|
+
await rm(root, { recursive: true, force: true });
|
|
888
|
+
}
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
test("a load-less page keeps the plain Record<string, never>", async () => {
|
|
892
|
+
const root = join(tmpdir(), `grimoire-noload-${Date.now()}`);
|
|
893
|
+
const routeDir = join(root, "src", "routes", "x");
|
|
894
|
+
await mkdir(routeDir, { recursive: true });
|
|
895
|
+
try {
|
|
896
|
+
// `loadUsers` is not `load`
|
|
897
|
+
await writeFile(
|
|
898
|
+
join(routeDir, "+page.tsx"),
|
|
899
|
+
`export function loadUsers() { return {}; }\nexport default () => null;\n`,
|
|
900
|
+
);
|
|
901
|
+
const routesDir = join(root, "src", "routes");
|
|
902
|
+
const tree = await scanRoutes(routesDir, root);
|
|
903
|
+
await generateTypes(tree, {
|
|
904
|
+
projectRoot: root,
|
|
905
|
+
routesDir,
|
|
906
|
+
outDir: join(root, ".grimoire", "types"),
|
|
907
|
+
});
|
|
908
|
+
const out = await readFile(
|
|
909
|
+
join(root, ".grimoire", "types", "src", "routes", "x", "$types.d.ts"),
|
|
910
|
+
"utf-8",
|
|
911
|
+
);
|
|
912
|
+
expect(out).toContain("export type PageData = Record<string, never>;");
|
|
913
|
+
} finally {
|
|
914
|
+
await rm(root, { recursive: true, force: true });
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
});
|