@sigil-dev/grimoire 0.9.1 → 0.9.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/index.ts +35 -35
- package/package.json +9 -8
- package/preload.js +3 -3
- package/server.ts +19 -19
- package/src/client/deps.ts +13 -7
- package/src/client/enhance.ts +98 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +445 -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 +98 -79
- package/src/rendering/index.ts +455 -372
- package/src/rendering/ssrPlugin.ts +69 -69
- package/src/routing/manifest-gen.ts +12 -11
- package/src/routing/router.ts +109 -109
- package/src/routing/scanner.ts +141 -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 +243 -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 +655 -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 +356 -356
- package/src/types.ts +4 -4
- package/test/client-router.test.ts +37 -31
- 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 +615 -579
- package/test/routing.test.ts +81 -81
- package/test/scanning.test.ts +200 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +249 -249
- package/test/streaming.test.ts +125 -125
- package/test/transform-routes.test.ts +84 -84
- package/test/typegen.test.ts +682 -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/src/routing/scanner.ts
CHANGED
|
@@ -1,141 +1,141 @@
|
|
|
1
|
-
import { basename, join, relative } from "node:path";
|
|
2
|
-
|
|
3
|
-
export interface RouteFile {
|
|
4
|
-
path: string; // URL (ie /blog/:slug)
|
|
5
|
-
filePath: string; // absolute disk path;
|
|
6
|
-
clientPath: string; // Relative path (/src/routes/spells/+page.tsx)
|
|
7
|
-
type:
|
|
8
|
-
| "page"
|
|
9
|
-
| "pageServer"
|
|
10
|
-
| "layout"
|
|
11
|
-
| "layoutServer"
|
|
12
|
-
| "server"
|
|
13
|
-
| "error"
|
|
14
|
-
| "simple";
|
|
15
|
-
paramNames: string[]; // ["slug"] in /blog/:slug
|
|
16
|
-
restParamNames: string[]; // ["rest"] in /blog/*rest
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface RouteTree {
|
|
20
|
-
routes: RouteFile[];
|
|
21
|
-
layouts: RouteFile[]; // layout + layout.server
|
|
22
|
-
servers: RouteFile[]; // +server.ts API routes
|
|
23
|
-
errors: RouteFile[]; // +error.tsx
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function filePathToRoutePath(
|
|
27
|
-
filePath: string,
|
|
28
|
-
routesDir: string,
|
|
29
|
-
): { pattern: string; params: string[]; restParams: string[] } {
|
|
30
|
-
const rel = relative(routesDir, filePath)
|
|
31
|
-
.replace(/\\/g, "/")
|
|
32
|
-
.replace(/\.(tsx?|jsx?)$/, "")
|
|
33
|
-
.replace(/(?:^|\/)(\([^)]+\))(?=\/|$)/g, "") // strip (groupName) anywhere
|
|
34
|
-
.replace(/\/+/g, "/"); // collapse double slashes
|
|
35
|
-
|
|
36
|
-
const params: string[] = [];
|
|
37
|
-
const restParams: string[] = [];
|
|
38
|
-
const pattern =
|
|
39
|
-
rel
|
|
40
|
-
.replace(/\/?(\+\w+(\.\w+)?|index)$/, "") // index/+page → directory
|
|
41
|
-
.replace(/\[(\.\.\.)?([^\]]+)\]/g, (_, dots, name) => {
|
|
42
|
-
// [...rest] → *rest (captures zero or more remaining path segments)
|
|
43
|
-
if (dots) {
|
|
44
|
-
restParams.push(name);
|
|
45
|
-
return `*${name}`;
|
|
46
|
-
}
|
|
47
|
-
// [param] → :param
|
|
48
|
-
params.push(name);
|
|
49
|
-
return `:${name}`;
|
|
50
|
-
})
|
|
51
|
-
.replace(/^\/?/, "/") || // ensure leading slash
|
|
52
|
-
"/";
|
|
53
|
-
|
|
54
|
-
return { pattern, params, restParams };
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export async function scanRoutes(
|
|
58
|
-
routesDir: string,
|
|
59
|
-
viteRoot: string = routesDir,
|
|
60
|
-
): Promise<RouteTree> {
|
|
61
|
-
const glob = new Bun.Glob("**/*.{tsx,ts,jsx,js}");
|
|
62
|
-
const routes: RouteFile[] = [];
|
|
63
|
-
const layouts: RouteFile[] = [];
|
|
64
|
-
const servers: RouteFile[] = [];
|
|
65
|
-
const errors: RouteFile[] = [];
|
|
66
|
-
|
|
67
|
-
for await (const file of glob.scan(routesDir)) {
|
|
68
|
-
const filePath = join(routesDir, file);
|
|
69
|
-
const name = basename(file).replace(/\.(tsx?|jsx?)$/, "");
|
|
70
|
-
const { pattern, params, restParams } = filePathToRoutePath(
|
|
71
|
-
filePath,
|
|
72
|
-
routesDir,
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
let type: RouteFile["type"];
|
|
76
|
-
if (name === "+page") type = "page";
|
|
77
|
-
else if (name === "+page.server") type = "pageServer";
|
|
78
|
-
else if (name === "+layout") type = "layout";
|
|
79
|
-
else if (name === "+layout.server") type = "layoutServer";
|
|
80
|
-
else if (name === "+server") type = "server";
|
|
81
|
-
else if (name === "+error") type = "error";
|
|
82
|
-
else type = "simple";
|
|
83
|
-
|
|
84
|
-
const clientPath = `/${relative(viteRoot, filePath).replace(/\\/g, "/")}`;
|
|
85
|
-
|
|
86
|
-
const routeFile: RouteFile = {
|
|
87
|
-
path: pattern,
|
|
88
|
-
filePath,
|
|
89
|
-
clientPath, // add this
|
|
90
|
-
type,
|
|
91
|
-
paramNames: params,
|
|
92
|
-
restParamNames: restParams,
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
if (type === "simple" || type === "page" || type === "pageServer")
|
|
96
|
-
routes.push(routeFile);
|
|
97
|
-
if (type === "layout" || type === "layoutServer") layouts.push(routeFile);
|
|
98
|
-
if (type === "server") servers.push(routeFile);
|
|
99
|
-
if (type === "error") errors.push(routeFile);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// sort so static routes match before dynamic ones, rest params last
|
|
103
|
-
const scoreRoute = (r: RouteFile) =>
|
|
104
|
-
r.paramNames.length + r.restParamNames.length * 1000;
|
|
105
|
-
routes.sort((a, b) => scoreRoute(a) - scoreRoute(b));
|
|
106
|
-
servers.sort((a, b) => scoreRoute(a) - scoreRoute(b));
|
|
107
|
-
|
|
108
|
-
assertNoDuplicatePaths(
|
|
109
|
-
routes.filter((r) => r.type === "page" || r.type === "simple"),
|
|
110
|
-
"page",
|
|
111
|
-
);
|
|
112
|
-
assertNoDuplicatePaths(
|
|
113
|
-
routes.filter((r) => r.type === "pageServer"),
|
|
114
|
-
"page server",
|
|
115
|
-
);
|
|
116
|
-
assertNoDuplicatePaths(
|
|
117
|
-
layouts.filter((l) => l.type === "layout"),
|
|
118
|
-
"layout",
|
|
119
|
-
);
|
|
120
|
-
assertNoDuplicatePaths(
|
|
121
|
-
layouts.filter((l) => l.type === "layoutServer"),
|
|
122
|
-
"layout server",
|
|
123
|
-
);
|
|
124
|
-
assertNoDuplicatePaths(servers, "API route");
|
|
125
|
-
assertNoDuplicatePaths(errors, "error page");
|
|
126
|
-
|
|
127
|
-
return { routes, layouts, servers, errors };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function assertNoDuplicatePaths(files: RouteFile[], label: string): void {
|
|
131
|
-
const seen = new Map<string, string>();
|
|
132
|
-
for (const file of files) {
|
|
133
|
-
const existing = seen.get(file.path);
|
|
134
|
-
if (existing) {
|
|
135
|
-
throw new Error(
|
|
136
|
-
`Duplicate ${label} at "${file.path}": "${existing}" and "${file.clientPath}"`,
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
seen.set(file.path, file.clientPath);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
1
|
+
import { basename, join, relative } from "node:path";
|
|
2
|
+
|
|
3
|
+
export interface RouteFile {
|
|
4
|
+
path: string; // URL (ie /blog/:slug)
|
|
5
|
+
filePath: string; // absolute disk path;
|
|
6
|
+
clientPath: string; // Relative path (/src/routes/spells/+page.tsx)
|
|
7
|
+
type:
|
|
8
|
+
| "page"
|
|
9
|
+
| "pageServer"
|
|
10
|
+
| "layout"
|
|
11
|
+
| "layoutServer"
|
|
12
|
+
| "server"
|
|
13
|
+
| "error"
|
|
14
|
+
| "simple";
|
|
15
|
+
paramNames: string[]; // ["slug"] in /blog/:slug
|
|
16
|
+
restParamNames: string[]; // ["rest"] in /blog/*rest
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface RouteTree {
|
|
20
|
+
routes: RouteFile[];
|
|
21
|
+
layouts: RouteFile[]; // layout + layout.server
|
|
22
|
+
servers: RouteFile[]; // +server.ts API routes
|
|
23
|
+
errors: RouteFile[]; // +error.tsx
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function filePathToRoutePath(
|
|
27
|
+
filePath: string,
|
|
28
|
+
routesDir: string,
|
|
29
|
+
): { pattern: string; params: string[]; restParams: string[] } {
|
|
30
|
+
const rel = relative(routesDir, filePath)
|
|
31
|
+
.replace(/\\/g, "/")
|
|
32
|
+
.replace(/\.(tsx?|jsx?)$/, "")
|
|
33
|
+
.replace(/(?:^|\/)(\([^)]+\))(?=\/|$)/g, "") // strip (groupName) anywhere
|
|
34
|
+
.replace(/\/+/g, "/"); // collapse double slashes
|
|
35
|
+
|
|
36
|
+
const params: string[] = [];
|
|
37
|
+
const restParams: string[] = [];
|
|
38
|
+
const pattern =
|
|
39
|
+
rel
|
|
40
|
+
.replace(/\/?(\+\w+(\.\w+)?|index)$/, "") // index/+page → directory
|
|
41
|
+
.replace(/\[(\.\.\.)?([^\]]+)\]/g, (_, dots, name) => {
|
|
42
|
+
// [...rest] → *rest (captures zero or more remaining path segments)
|
|
43
|
+
if (dots) {
|
|
44
|
+
restParams.push(name);
|
|
45
|
+
return `*${name}`;
|
|
46
|
+
}
|
|
47
|
+
// [param] → :param
|
|
48
|
+
params.push(name);
|
|
49
|
+
return `:${name}`;
|
|
50
|
+
})
|
|
51
|
+
.replace(/^\/?/, "/") || // ensure leading slash
|
|
52
|
+
"/";
|
|
53
|
+
|
|
54
|
+
return { pattern, params, restParams };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function scanRoutes(
|
|
58
|
+
routesDir: string,
|
|
59
|
+
viteRoot: string = routesDir,
|
|
60
|
+
): Promise<RouteTree> {
|
|
61
|
+
const glob = new Bun.Glob("**/*.{tsx,ts,jsx,js}");
|
|
62
|
+
const routes: RouteFile[] = [];
|
|
63
|
+
const layouts: RouteFile[] = [];
|
|
64
|
+
const servers: RouteFile[] = [];
|
|
65
|
+
const errors: RouteFile[] = [];
|
|
66
|
+
|
|
67
|
+
for await (const file of glob.scan(routesDir)) {
|
|
68
|
+
const filePath = join(routesDir, file);
|
|
69
|
+
const name = basename(file).replace(/\.(tsx?|jsx?)$/, "");
|
|
70
|
+
const { pattern, params, restParams } = filePathToRoutePath(
|
|
71
|
+
filePath,
|
|
72
|
+
routesDir,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
let type: RouteFile["type"];
|
|
76
|
+
if (name === "+page") type = "page";
|
|
77
|
+
else if (name === "+page.server") type = "pageServer";
|
|
78
|
+
else if (name === "+layout") type = "layout";
|
|
79
|
+
else if (name === "+layout.server") type = "layoutServer";
|
|
80
|
+
else if (name === "+server") type = "server";
|
|
81
|
+
else if (name === "+error") type = "error";
|
|
82
|
+
else type = "simple";
|
|
83
|
+
|
|
84
|
+
const clientPath = `/${relative(viteRoot, filePath).replace(/\\/g, "/")}`;
|
|
85
|
+
|
|
86
|
+
const routeFile: RouteFile = {
|
|
87
|
+
path: pattern,
|
|
88
|
+
filePath,
|
|
89
|
+
clientPath, // add this
|
|
90
|
+
type,
|
|
91
|
+
paramNames: params,
|
|
92
|
+
restParamNames: restParams,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
if (type === "simple" || type === "page" || type === "pageServer")
|
|
96
|
+
routes.push(routeFile);
|
|
97
|
+
if (type === "layout" || type === "layoutServer") layouts.push(routeFile);
|
|
98
|
+
if (type === "server") servers.push(routeFile);
|
|
99
|
+
if (type === "error") errors.push(routeFile);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// sort so static routes match before dynamic ones, rest params last
|
|
103
|
+
const scoreRoute = (r: RouteFile) =>
|
|
104
|
+
r.paramNames.length + r.restParamNames.length * 1000;
|
|
105
|
+
routes.sort((a, b) => scoreRoute(a) - scoreRoute(b));
|
|
106
|
+
servers.sort((a, b) => scoreRoute(a) - scoreRoute(b));
|
|
107
|
+
|
|
108
|
+
assertNoDuplicatePaths(
|
|
109
|
+
routes.filter((r) => r.type === "page" || r.type === "simple"),
|
|
110
|
+
"page",
|
|
111
|
+
);
|
|
112
|
+
assertNoDuplicatePaths(
|
|
113
|
+
routes.filter((r) => r.type === "pageServer"),
|
|
114
|
+
"page server",
|
|
115
|
+
);
|
|
116
|
+
assertNoDuplicatePaths(
|
|
117
|
+
layouts.filter((l) => l.type === "layout"),
|
|
118
|
+
"layout",
|
|
119
|
+
);
|
|
120
|
+
assertNoDuplicatePaths(
|
|
121
|
+
layouts.filter((l) => l.type === "layoutServer"),
|
|
122
|
+
"layout server",
|
|
123
|
+
);
|
|
124
|
+
assertNoDuplicatePaths(servers, "API route");
|
|
125
|
+
assertNoDuplicatePaths(errors, "error page");
|
|
126
|
+
|
|
127
|
+
return { routes, layouts, servers, errors };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function assertNoDuplicatePaths(files: RouteFile[], label: string): void {
|
|
131
|
+
const seen = new Map<string, string>();
|
|
132
|
+
for (const file of files) {
|
|
133
|
+
const existing = seen.get(file.path);
|
|
134
|
+
if (existing) {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`Duplicate ${label} at "${file.path}": "${existing}" and "${file.clientPath}"`,
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
seen.set(file.path, file.clientPath);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
2
|
-
import { transformSync } from "@babel/core";
|
|
3
|
-
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
4
|
-
import type { GrimoirePlugin } from "../types";
|
|
5
|
-
import type { RouteFile } from "./scanner";
|
|
6
|
-
|
|
7
|
-
const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
8
|
-
|
|
9
|
-
// Inline the same hash helper used in bun-plugin.ts to avoid importing
|
|
10
|
-
// unexported internals from @sigil-dev/compiler.
|
|
11
|
-
function computeHash(filePath: string): string {
|
|
12
|
-
let h = 0x811c9dc5;
|
|
13
|
-
for (let i = 0; i < filePath.length; i++) {
|
|
14
|
-
h ^= filePath.charCodeAt(i);
|
|
15
|
-
h = (h * 0x01000193) >>> 0;
|
|
16
|
-
}
|
|
17
|
-
return h.toString(36);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function rewriteRelativeImports(filePath: string) {
|
|
21
|
-
const basedir = dirname(filePath);
|
|
22
|
-
const rewrite = (source?: { value: string }) => {
|
|
23
|
-
if (!source?.value.startsWith(".")) return;
|
|
24
|
-
source.value = resolve(basedir, source.value);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
return {
|
|
28
|
-
name: "sigil-rewrite-relative-route-imports",
|
|
29
|
-
visitor: {
|
|
30
|
-
ImportDeclaration(path: any) {
|
|
31
|
-
rewrite(path.node.source);
|
|
32
|
-
},
|
|
33
|
-
ExportNamedDeclaration(path: any) {
|
|
34
|
-
rewrite(path.node.source);
|
|
35
|
-
},
|
|
36
|
-
ExportAllDeclaration(path: any) {
|
|
37
|
-
rewrite(path.node.source);
|
|
38
|
-
},
|
|
39
|
-
CallExpression(path: any) {
|
|
40
|
-
if (path.node.callee.type !== "Import") return;
|
|
41
|
-
rewrite(path.node.arguments[0]);
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Pre-transforms route .tsx/.jsx files to plain JS using the Babel sigil
|
|
49
|
-
* pipeline, writing the output to `outDir`. Returns a Map from the original
|
|
50
|
-
* filePath to the compiled .js path.
|
|
51
|
-
*
|
|
52
|
-
* Workaround for Bun 1.3.13: Bun.build's native parser runs before onLoad
|
|
53
|
-
* hooks fire, crashing on TypeScript syntax. Pre-transforming to JS means
|
|
54
|
-
* Bun.build only ever sees plain JavaScript.
|
|
55
|
-
*/
|
|
56
|
-
export async function transformRoutes(
|
|
57
|
-
routes: RouteFile[],
|
|
58
|
-
outDir: string,
|
|
59
|
-
mode: "hydrate" | "dom",
|
|
60
|
-
plugins: GrimoirePlugin[] = [],
|
|
61
|
-
): Promise<Map<string, string>> {
|
|
62
|
-
const map = new Map<string, string>();
|
|
63
|
-
// loader: "ts" — Babel already consumed JSX, Bun only strips remaining types
|
|
64
|
-
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
65
|
-
|
|
66
|
-
await Promise.all(
|
|
67
|
-
routes.map(async (route, index) => {
|
|
68
|
-
let code = await Bun.file(route.filePath).text();
|
|
69
|
-
const hash = computeHash(route.filePath);
|
|
70
|
-
code = code.replace(STYLE_RE, "");
|
|
71
|
-
|
|
72
|
-
const res = transformSync(code, {
|
|
73
|
-
configFile: false,
|
|
74
|
-
babelrc: false,
|
|
75
|
-
parserOpts: {
|
|
76
|
-
plugins: ["typescript", "jsx"], // no isTSX
|
|
77
|
-
},
|
|
78
|
-
plugins: [
|
|
79
|
-
[sigilPlugin, { hash, mode }],
|
|
80
|
-
//@ts-expect-error it fills what it needs
|
|
81
|
-
rewriteRelativeImports(route.filePath),
|
|
82
|
-
],
|
|
83
|
-
filename: route.filePath,
|
|
84
|
-
});
|
|
85
|
-
let out = transpiler.transformSync(res?.code ?? "");
|
|
86
|
-
for (const plugin of plugins) {
|
|
87
|
-
if (plugin.transform)
|
|
88
|
-
out = (await plugin.transform(out, route.filePath)) ?? out;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const rel = relative(process.cwd(), route.filePath)
|
|
92
|
-
.replace(/\.tsx?$/, "")
|
|
93
|
-
.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
94
|
-
const nameBase = rel || basename(route.filePath).replace(/\.[jt]sx$/, "");
|
|
95
|
-
const outPath = join(outDir, `${index}-${nameBase}.${mode}.js`);
|
|
96
|
-
|
|
97
|
-
await Bun.write(outPath, out);
|
|
98
|
-
map.set(route.filePath, outPath);
|
|
99
|
-
}),
|
|
100
|
-
);
|
|
101
|
-
return map;
|
|
102
|
-
}
|
|
1
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
2
|
+
import { transformSync } from "@babel/core";
|
|
3
|
+
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
4
|
+
import type { GrimoirePlugin } from "../types";
|
|
5
|
+
import type { RouteFile } from "./scanner";
|
|
6
|
+
|
|
7
|
+
const STYLE_RE = /<style[^>]*>([\s\S]*?)<\/style>/gi;
|
|
8
|
+
|
|
9
|
+
// Inline the same hash helper used in bun-plugin.ts to avoid importing
|
|
10
|
+
// unexported internals from @sigil-dev/compiler.
|
|
11
|
+
function computeHash(filePath: string): string {
|
|
12
|
+
let h = 0x811c9dc5;
|
|
13
|
+
for (let i = 0; i < filePath.length; i++) {
|
|
14
|
+
h ^= filePath.charCodeAt(i);
|
|
15
|
+
h = (h * 0x01000193) >>> 0;
|
|
16
|
+
}
|
|
17
|
+
return h.toString(36);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function rewriteRelativeImports(filePath: string) {
|
|
21
|
+
const basedir = dirname(filePath);
|
|
22
|
+
const rewrite = (source?: { value: string }) => {
|
|
23
|
+
if (!source?.value.startsWith(".")) return;
|
|
24
|
+
source.value = resolve(basedir, source.value);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
name: "sigil-rewrite-relative-route-imports",
|
|
29
|
+
visitor: {
|
|
30
|
+
ImportDeclaration(path: any) {
|
|
31
|
+
rewrite(path.node.source);
|
|
32
|
+
},
|
|
33
|
+
ExportNamedDeclaration(path: any) {
|
|
34
|
+
rewrite(path.node.source);
|
|
35
|
+
},
|
|
36
|
+
ExportAllDeclaration(path: any) {
|
|
37
|
+
rewrite(path.node.source);
|
|
38
|
+
},
|
|
39
|
+
CallExpression(path: any) {
|
|
40
|
+
if (path.node.callee.type !== "Import") return;
|
|
41
|
+
rewrite(path.node.arguments[0]);
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Pre-transforms route .tsx/.jsx files to plain JS using the Babel sigil
|
|
49
|
+
* pipeline, writing the output to `outDir`. Returns a Map from the original
|
|
50
|
+
* filePath to the compiled .js path.
|
|
51
|
+
*
|
|
52
|
+
* Workaround for Bun 1.3.13: Bun.build's native parser runs before onLoad
|
|
53
|
+
* hooks fire, crashing on TypeScript syntax. Pre-transforming to JS means
|
|
54
|
+
* Bun.build only ever sees plain JavaScript.
|
|
55
|
+
*/
|
|
56
|
+
export async function transformRoutes(
|
|
57
|
+
routes: RouteFile[],
|
|
58
|
+
outDir: string,
|
|
59
|
+
mode: "hydrate" | "dom",
|
|
60
|
+
plugins: GrimoirePlugin[] = [],
|
|
61
|
+
): Promise<Map<string, string>> {
|
|
62
|
+
const map = new Map<string, string>();
|
|
63
|
+
// loader: "ts" — Babel already consumed JSX, Bun only strips remaining types
|
|
64
|
+
const transpiler = new Bun.Transpiler({ loader: "ts", target: "browser" });
|
|
65
|
+
|
|
66
|
+
await Promise.all(
|
|
67
|
+
routes.map(async (route, index) => {
|
|
68
|
+
let code = await Bun.file(route.filePath).text();
|
|
69
|
+
const hash = computeHash(route.filePath);
|
|
70
|
+
code = code.replace(STYLE_RE, "");
|
|
71
|
+
|
|
72
|
+
const res = transformSync(code, {
|
|
73
|
+
configFile: false,
|
|
74
|
+
babelrc: false,
|
|
75
|
+
parserOpts: {
|
|
76
|
+
plugins: ["typescript", "jsx"], // no isTSX
|
|
77
|
+
},
|
|
78
|
+
plugins: [
|
|
79
|
+
[sigilPlugin, { hash, mode }],
|
|
80
|
+
//@ts-expect-error it fills what it needs
|
|
81
|
+
rewriteRelativeImports(route.filePath),
|
|
82
|
+
],
|
|
83
|
+
filename: route.filePath,
|
|
84
|
+
});
|
|
85
|
+
let out = transpiler.transformSync(res?.code ?? "");
|
|
86
|
+
for (const plugin of plugins) {
|
|
87
|
+
if (plugin.transform)
|
|
88
|
+
out = (await plugin.transform(out, route.filePath)) ?? out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const rel = relative(process.cwd(), route.filePath)
|
|
92
|
+
.replace(/\.tsx?$/, "")
|
|
93
|
+
.replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
94
|
+
const nameBase = rel || basename(route.filePath).replace(/\.[jt]sx$/, "");
|
|
95
|
+
const outPath = join(outDir, `${index}-${nameBase}.${mode}.js`);
|
|
96
|
+
|
|
97
|
+
await Bun.write(outPath, out);
|
|
98
|
+
map.set(route.filePath, outPath);
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
return map;
|
|
102
|
+
}
|
package/src/sentinels/error.ts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Throw an error from a load function or form action.
|
|
3
|
-
* The server catches it and returns an error response.
|
|
4
|
-
*
|
|
5
|
-
* Usage in +page.server.ts:
|
|
6
|
-
*
|
|
7
|
-
* import { error } from "@sigil-dev/grimoire";
|
|
8
|
-
*
|
|
9
|
-
* export async function load({ params }) {
|
|
10
|
-
* const item = await db.find(params.id);
|
|
11
|
-
* if (!item) throw error(404, "Not found");
|
|
12
|
-
* return { item };
|
|
13
|
-
* }
|
|
14
|
-
*
|
|
15
|
-
* export async function POST({ request }) {
|
|
16
|
-
* const session = await getSession(request);
|
|
17
|
-
* if (!session) throw error(401, "Unauthorized");
|
|
18
|
-
* // ...
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
* The server:
|
|
22
|
-
* - Returns JSON { error: true, status, message } for API requests
|
|
23
|
-
* - Renders +error.tsx page if one exists for the route
|
|
24
|
-
* - Falls back to a plain text error response
|
|
25
|
-
*/
|
|
26
|
-
export interface ErrorResult {
|
|
27
|
-
__error: true;
|
|
28
|
-
status: number;
|
|
29
|
-
message: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function error(status: number, message: string): never;
|
|
33
|
-
export function error(status: number, data: Record<string, unknown>): never;
|
|
34
|
-
export function error(
|
|
35
|
-
status: number,
|
|
36
|
-
messageOrData: string | Record<string, unknown>,
|
|
37
|
-
): never {
|
|
38
|
-
const message =
|
|
39
|
-
typeof messageOrData === "string"
|
|
40
|
-
? messageOrData
|
|
41
|
-
: JSON.stringify(messageOrData);
|
|
42
|
-
|
|
43
|
-
throw { __error: true, status, message } as ErrorResult;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function isErrorResult(value: unknown): value is ErrorResult {
|
|
47
|
-
return (
|
|
48
|
-
typeof value === "object" &&
|
|
49
|
-
value !== null &&
|
|
50
|
-
(value as any).__error === true
|
|
51
|
-
);
|
|
52
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Throw an error from a load function or form action.
|
|
3
|
+
* The server catches it and returns an error response.
|
|
4
|
+
*
|
|
5
|
+
* Usage in +page.server.ts:
|
|
6
|
+
*
|
|
7
|
+
* import { error } from "@sigil-dev/grimoire";
|
|
8
|
+
*
|
|
9
|
+
* export async function load({ params }) {
|
|
10
|
+
* const item = await db.find(params.id);
|
|
11
|
+
* if (!item) throw error(404, "Not found");
|
|
12
|
+
* return { item };
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* export async function POST({ request }) {
|
|
16
|
+
* const session = await getSession(request);
|
|
17
|
+
* if (!session) throw error(401, "Unauthorized");
|
|
18
|
+
* // ...
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* The server:
|
|
22
|
+
* - Returns JSON { error: true, status, message } for API requests
|
|
23
|
+
* - Renders +error.tsx page if one exists for the route
|
|
24
|
+
* - Falls back to a plain text error response
|
|
25
|
+
*/
|
|
26
|
+
export interface ErrorResult {
|
|
27
|
+
__error: true;
|
|
28
|
+
status: number;
|
|
29
|
+
message: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function error(status: number, message: string): never;
|
|
33
|
+
export function error(status: number, data: Record<string, unknown>): never;
|
|
34
|
+
export function error(
|
|
35
|
+
status: number,
|
|
36
|
+
messageOrData: string | Record<string, unknown>,
|
|
37
|
+
): never {
|
|
38
|
+
const message =
|
|
39
|
+
typeof messageOrData === "string"
|
|
40
|
+
? messageOrData
|
|
41
|
+
: JSON.stringify(messageOrData);
|
|
42
|
+
|
|
43
|
+
throw { __error: true, status, message } as ErrorResult;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isErrorResult(value: unknown): value is ErrorResult {
|
|
47
|
+
return (
|
|
48
|
+
typeof value === "object" &&
|
|
49
|
+
value !== null &&
|
|
50
|
+
(value as any).__error === true
|
|
51
|
+
);
|
|
52
|
+
}
|
package/src/sentinels/fail.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Return validation errors from a server form action.
|
|
3
|
-
* Instead of redirecting, the server sends a JSON response with the error data.
|
|
4
|
-
*
|
|
5
|
-
* Usage in +page.server.ts:
|
|
6
|
-
*
|
|
7
|
-
* import { fail } from "@sigil-dev/grimoire";
|
|
8
|
-
*
|
|
9
|
-
* export async function POST({ request }) {
|
|
10
|
-
* const form = await request.formData();
|
|
11
|
-
* const name = form.get("name")?.toString() ?? "";
|
|
12
|
-
*
|
|
13
|
-
* if (!name) {
|
|
14
|
-
* return fail(400, { name, errors: { name: "Name is required" } });
|
|
15
|
-
* }
|
|
16
|
-
*
|
|
17
|
-
* // success — redirect
|
|
18
|
-
* return { redirect: "/dashboard" };
|
|
19
|
-
* }
|
|
20
|
-
*/
|
|
21
|
-
export function fail<T extends Record<string, unknown>>(
|
|
22
|
-
status: number,
|
|
23
|
-
data: T,
|
|
24
|
-
): FailResult<T> {
|
|
25
|
-
return { __fail: true as const, status, data };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface FailResult<T = Record<string, unknown>> {
|
|
29
|
-
__fail: true;
|
|
30
|
-
status: number;
|
|
31
|
-
data: T;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function isFailResult(value: unknown): value is FailResult {
|
|
35
|
-
return (
|
|
36
|
-
typeof value === "object" &&
|
|
37
|
-
value !== null &&
|
|
38
|
-
"__fail" in value &&
|
|
39
|
-
(value as any).__fail === true
|
|
40
|
-
);
|
|
41
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Return validation errors from a server form action.
|
|
3
|
+
* Instead of redirecting, the server sends a JSON response with the error data.
|
|
4
|
+
*
|
|
5
|
+
* Usage in +page.server.ts:
|
|
6
|
+
*
|
|
7
|
+
* import { fail } from "@sigil-dev/grimoire";
|
|
8
|
+
*
|
|
9
|
+
* export async function POST({ request }) {
|
|
10
|
+
* const form = await request.formData();
|
|
11
|
+
* const name = form.get("name")?.toString() ?? "";
|
|
12
|
+
*
|
|
13
|
+
* if (!name) {
|
|
14
|
+
* return fail(400, { name, errors: { name: "Name is required" } });
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // success — redirect
|
|
18
|
+
* return { redirect: "/dashboard" };
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
export function fail<T extends Record<string, unknown>>(
|
|
22
|
+
status: number,
|
|
23
|
+
data: T,
|
|
24
|
+
): FailResult<T> {
|
|
25
|
+
return { __fail: true as const, status, data };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface FailResult<T = Record<string, unknown>> {
|
|
29
|
+
__fail: true;
|
|
30
|
+
status: number;
|
|
31
|
+
data: T;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isFailResult(value: unknown): value is FailResult {
|
|
35
|
+
return (
|
|
36
|
+
typeof value === "object" &&
|
|
37
|
+
value !== null &&
|
|
38
|
+
"__fail" in value &&
|
|
39
|
+
(value as any).__fail === true
|
|
40
|
+
);
|
|
41
|
+
}
|