@sigil-dev/grimoire 0.9.3 → 0.9.5
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 +36 -19
- package/src/client/enhance.ts +102 -98
- package/src/client/head.ts +30 -30
- package/src/client/index.ts +2 -2
- package/src/client/router.ts +174 -68
- package/src/client/scope.ts +22 -22
- package/src/dev/compile-module.ts +215 -163
- package/src/dev/effect-registry.ts +23 -23
- package/src/dev/graph.ts +109 -114
- package/src/dev/hmr-client.ts +202 -185
- package/src/dev/hmr-server.ts +261 -187
- package/src/dev/loader.ts +120 -47
- package/src/dev/paths.ts +14 -14
- package/src/dev/resolve.ts +70 -0
- package/src/dev/runtime-bundle.ts +52 -52
- package/src/dev/scan.ts +78 -0
- package/src/dev/watcher.ts +69 -47
- 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 +52 -47
- package/src/rendering/hydrate.ts +159 -138
- package/src/rendering/index.ts +140 -18
- package/src/rendering/ssrPlugin.ts +78 -69
- package/src/routing/manifest-gen.ts +13 -1
- package/src/routing/router.ts +135 -109
- package/src/routing/scanner.ts +196 -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 +250 -243
- package/src/server/context.ts +10 -10
- package/src/server/cookie-utils.ts +66 -66
- package/src/server/coordinator.ts +364 -364
- package/src/server/hooks.ts +114 -114
- package/src/server/index.ts +711 -655
- package/src/server/interactive.ts +56 -56
- package/src/server/plugins.ts +121 -121
- package/src/server/worker.ts +58 -58
- package/src/typegen/index.ts +389 -356
- package/src/types.ts +286 -282
- package/test/client-router.test.ts +295 -44
- package/test/context.test.ts +52 -52
- package/test/env.test.ts +90 -90
- package/test/exports.test.ts +37 -37
- package/test/fail.test.ts +46 -46
- package/test/headers.test.ts +100 -100
- package/test/hmr.test.ts +426 -0
- 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 +719 -615
- package/test/routing.test.ts +197 -81
- package/test/scanning.test.ts +314 -200
- package/test/scope.test.ts +195 -195
- package/test/server.test.ts +360 -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/src/env/typegen.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
|
|
4
|
-
export async function generateEnvTypes(
|
|
5
|
-
projectRoot: string,
|
|
6
|
-
outDir: string,
|
|
7
|
-
): Promise<void> {
|
|
8
|
-
const envPath = join(projectRoot, ".env");
|
|
9
|
-
let envContent = "";
|
|
10
|
-
try {
|
|
11
|
-
envContent = await readFile(envPath, "utf-8");
|
|
12
|
-
} catch {
|
|
13
|
-
// no .env file — generate empty types
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const publicVars: string[] = [];
|
|
17
|
-
const privateVars: string[] = [];
|
|
18
|
-
|
|
19
|
-
for (const line of envContent.split("\n")) {
|
|
20
|
-
const trimmed = line.trim();
|
|
21
|
-
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
22
|
-
const eqIndex = trimmed.indexOf("=");
|
|
23
|
-
if (eqIndex === -1) continue;
|
|
24
|
-
const key = trimmed.slice(0, eqIndex).trim();
|
|
25
|
-
if (!key) continue;
|
|
26
|
-
if (key.startsWith("PUBLIC_")) {
|
|
27
|
-
publicVars.push(key);
|
|
28
|
-
} else {
|
|
29
|
-
privateVars.push(key);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const publicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
34
|
-
declare module "$env/static/public" {
|
|
35
|
-
${publicVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
36
|
-
${publicVars.length === 0 ? "\texport {};" : ""}
|
|
37
|
-
}
|
|
38
|
-
`;
|
|
39
|
-
|
|
40
|
-
const privateTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
41
|
-
declare module "$env/static/private" {
|
|
42
|
-
${privateVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
43
|
-
${privateVars.length === 0 ? "\texport {};" : ""}
|
|
44
|
-
}
|
|
45
|
-
`;
|
|
46
|
-
|
|
47
|
-
const dynamicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
48
|
-
declare module "$env/dynamic/public" {
|
|
49
|
-
\texport const env: Record<string, string>;
|
|
50
|
-
}
|
|
51
|
-
declare module "$env/dynamic/private" {
|
|
52
|
-
\texport const env: Record<string, string>;
|
|
53
|
-
}
|
|
54
|
-
`;
|
|
55
|
-
|
|
56
|
-
await Promise.all([
|
|
57
|
-
writeFile(join(outDir, "env-public.d.ts"), publicTypeContent, "utf-8"),
|
|
58
|
-
writeFile(join(outDir, "env-private.d.ts"), privateTypeContent, "utf-8"),
|
|
59
|
-
writeFile(join(outDir, "env-dynamic.d.ts"), dynamicTypeContent, "utf-8"),
|
|
60
|
-
]);
|
|
61
|
-
}
|
|
1
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
export async function generateEnvTypes(
|
|
5
|
+
projectRoot: string,
|
|
6
|
+
outDir: string,
|
|
7
|
+
): Promise<void> {
|
|
8
|
+
const envPath = join(projectRoot, ".env");
|
|
9
|
+
let envContent = "";
|
|
10
|
+
try {
|
|
11
|
+
envContent = await readFile(envPath, "utf-8");
|
|
12
|
+
} catch {
|
|
13
|
+
// no .env file — generate empty types
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const publicVars: string[] = [];
|
|
17
|
+
const privateVars: string[] = [];
|
|
18
|
+
|
|
19
|
+
for (const line of envContent.split("\n")) {
|
|
20
|
+
const trimmed = line.trim();
|
|
21
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
22
|
+
const eqIndex = trimmed.indexOf("=");
|
|
23
|
+
if (eqIndex === -1) continue;
|
|
24
|
+
const key = trimmed.slice(0, eqIndex).trim();
|
|
25
|
+
if (!key) continue;
|
|
26
|
+
if (key.startsWith("PUBLIC_")) {
|
|
27
|
+
publicVars.push(key);
|
|
28
|
+
} else {
|
|
29
|
+
privateVars.push(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const publicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
34
|
+
declare module "$env/static/public" {
|
|
35
|
+
${publicVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
36
|
+
${publicVars.length === 0 ? "\texport {};" : ""}
|
|
37
|
+
}
|
|
38
|
+
`;
|
|
39
|
+
|
|
40
|
+
const privateTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
41
|
+
declare module "$env/static/private" {
|
|
42
|
+
${privateVars.map((v) => `\texport const ${v}: string;`).join("\n")}
|
|
43
|
+
${privateVars.length === 0 ? "\texport {};" : ""}
|
|
44
|
+
}
|
|
45
|
+
`;
|
|
46
|
+
|
|
47
|
+
const dynamicTypeContent = `// Auto-generated by Grimoire. Do not edit.
|
|
48
|
+
declare module "$env/dynamic/public" {
|
|
49
|
+
\texport const env: Record<string, string>;
|
|
50
|
+
}
|
|
51
|
+
declare module "$env/dynamic/private" {
|
|
52
|
+
\texport const env: Record<string, string>;
|
|
53
|
+
}
|
|
54
|
+
`;
|
|
55
|
+
|
|
56
|
+
await Promise.all([
|
|
57
|
+
writeFile(join(outDir, "env-public.d.ts"), publicTypeContent, "utf-8"),
|
|
58
|
+
writeFile(join(outDir, "env-private.d.ts"), privateTypeContent, "utf-8"),
|
|
59
|
+
writeFile(join(outDir, "env-dynamic.d.ts"), dynamicTypeContent, "utf-8"),
|
|
60
|
+
]);
|
|
61
|
+
}
|
package/src/headers.ts
CHANGED
|
@@ -1,114 +1,114 @@
|
|
|
1
|
-
import type { GrimoirePlugin } from "./types";
|
|
2
|
-
|
|
3
|
-
export interface SecurityHeadersConfig {
|
|
4
|
-
/** Content-Security-Policy. Set false to omit. */
|
|
5
|
-
contentSecurityPolicy?: string | false;
|
|
6
|
-
/** X-Content-Type-Options. Default: "nosniff" */
|
|
7
|
-
contentTypeOptions?: string | false;
|
|
8
|
-
/** X-Frame-Options. Default: "DENY" */
|
|
9
|
-
frameOptions?: string | false;
|
|
10
|
-
/** X-XSS-Protection. Default: "0" (modern browsers; "1; mode=block" for legacy) */
|
|
11
|
-
xssProtection?: string | false;
|
|
12
|
-
/** Referrer-Policy. Default: "strict-origin-when-cross-origin" */
|
|
13
|
-
referrerPolicy?: string | false;
|
|
14
|
-
/** Strict-Transport-Security. Set false to omit. */
|
|
15
|
-
strictTransportSecurity?: string | false;
|
|
16
|
-
/** Permissions-Policy. Default: "camera=(), microphone=(), geolocation=()" */
|
|
17
|
-
permissionsPolicy?: string | false;
|
|
18
|
-
/** Per-route overrides: path pattern → partial config */
|
|
19
|
-
routes?: Record<string, Partial<SecurityHeadersConfig>>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const DEFAULTS: Required<Omit<SecurityHeadersConfig, "routes">> = {
|
|
23
|
-
contentSecurityPolicy:
|
|
24
|
-
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;",
|
|
25
|
-
contentTypeOptions: "nosniff",
|
|
26
|
-
frameOptions: "DENY",
|
|
27
|
-
xssProtection: "0",
|
|
28
|
-
referrerPolicy: "strict-origin-when-cross-origin",
|
|
29
|
-
strictTransportSecurity: "max-age=31536000; includeSubDomains",
|
|
30
|
-
permissionsPolicy: "camera=(), microphone=(), geolocation=()",
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
function resolveHeaders(
|
|
34
|
-
config: SecurityHeadersConfig,
|
|
35
|
-
pathname: string,
|
|
36
|
-
): Record<string, string> {
|
|
37
|
-
// Start with defaults, overlay user config
|
|
38
|
-
const merged = { ...DEFAULTS, ...config };
|
|
39
|
-
delete (merged as any).routes;
|
|
40
|
-
|
|
41
|
-
// Apply route overrides
|
|
42
|
-
if (config.routes) {
|
|
43
|
-
for (const pattern in config.routes) {
|
|
44
|
-
if (pathname.startsWith(pattern)) {
|
|
45
|
-
Object.assign(merged, config.routes[pattern]);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const headers: Record<string, string> = {};
|
|
51
|
-
|
|
52
|
-
if (merged.contentSecurityPolicy !== false)
|
|
53
|
-
headers["Content-Security-Policy"] = merged.contentSecurityPolicy;
|
|
54
|
-
if (merged.contentTypeOptions !== false)
|
|
55
|
-
headers["X-Content-Type-Options"] = merged.contentTypeOptions;
|
|
56
|
-
if (merged.frameOptions !== false)
|
|
57
|
-
headers["X-Frame-Options"] = merged.frameOptions;
|
|
58
|
-
if (merged.xssProtection !== false)
|
|
59
|
-
headers["X-XSS-Protection"] = merged.xssProtection;
|
|
60
|
-
if (merged.referrerPolicy !== false)
|
|
61
|
-
headers["Referrer-Policy"] = merged.referrerPolicy;
|
|
62
|
-
if (merged.strictTransportSecurity !== false)
|
|
63
|
-
headers["Strict-Transport-Security"] = merged.strictTransportSecurity;
|
|
64
|
-
if (merged.permissionsPolicy !== false)
|
|
65
|
-
headers["Permissions-Policy"] = merged.permissionsPolicy;
|
|
66
|
-
|
|
67
|
-
return headers;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Security headers plugin.
|
|
72
|
-
* Adds standard security headers to all responses.
|
|
73
|
-
*
|
|
74
|
-
* Usage:
|
|
75
|
-
*
|
|
76
|
-
* import { securityHeaders } from "@sigil-dev/grimoire/headers";
|
|
77
|
-
*
|
|
78
|
-
* createServer({
|
|
79
|
-
* plugins: [
|
|
80
|
-
* securityHeaders({
|
|
81
|
-
* // override defaults
|
|
82
|
-
* frameOptions: "SAMEORIGIN",
|
|
83
|
-
* // per-route
|
|
84
|
-
* routes: {
|
|
85
|
-
* "/admin": { contentSecurityPolicy: "default-src 'self'" },
|
|
86
|
-
* },
|
|
87
|
-
* }),
|
|
88
|
-
* ],
|
|
89
|
-
* });
|
|
90
|
-
*/
|
|
91
|
-
export function securityHeaders(
|
|
92
|
-
config: SecurityHeadersConfig = {},
|
|
93
|
-
): GrimoirePlugin {
|
|
94
|
-
return {
|
|
95
|
-
name: "grimoire-security-headers",
|
|
96
|
-
async onRequest(req, next) {
|
|
97
|
-
const res = await next();
|
|
98
|
-
const url = new URL(req.url);
|
|
99
|
-
const headers = resolveHeaders(config, url.pathname);
|
|
100
|
-
|
|
101
|
-
// Clone response with security headers
|
|
102
|
-
const newHeaders = new Headers(res.headers);
|
|
103
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
104
|
-
newHeaders.set(key, value);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return new Response(res.body, {
|
|
108
|
-
status: res.status,
|
|
109
|
-
statusText: res.statusText,
|
|
110
|
-
headers: newHeaders,
|
|
111
|
-
});
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
}
|
|
1
|
+
import type { GrimoirePlugin } from "./types";
|
|
2
|
+
|
|
3
|
+
export interface SecurityHeadersConfig {
|
|
4
|
+
/** Content-Security-Policy. Set false to omit. */
|
|
5
|
+
contentSecurityPolicy?: string | false;
|
|
6
|
+
/** X-Content-Type-Options. Default: "nosniff" */
|
|
7
|
+
contentTypeOptions?: string | false;
|
|
8
|
+
/** X-Frame-Options. Default: "DENY" */
|
|
9
|
+
frameOptions?: string | false;
|
|
10
|
+
/** X-XSS-Protection. Default: "0" (modern browsers; "1; mode=block" for legacy) */
|
|
11
|
+
xssProtection?: string | false;
|
|
12
|
+
/** Referrer-Policy. Default: "strict-origin-when-cross-origin" */
|
|
13
|
+
referrerPolicy?: string | false;
|
|
14
|
+
/** Strict-Transport-Security. Set false to omit. */
|
|
15
|
+
strictTransportSecurity?: string | false;
|
|
16
|
+
/** Permissions-Policy. Default: "camera=(), microphone=(), geolocation=()" */
|
|
17
|
+
permissionsPolicy?: string | false;
|
|
18
|
+
/** Per-route overrides: path pattern → partial config */
|
|
19
|
+
routes?: Record<string, Partial<SecurityHeadersConfig>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const DEFAULTS: Required<Omit<SecurityHeadersConfig, "routes">> = {
|
|
23
|
+
contentSecurityPolicy:
|
|
24
|
+
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;",
|
|
25
|
+
contentTypeOptions: "nosniff",
|
|
26
|
+
frameOptions: "DENY",
|
|
27
|
+
xssProtection: "0",
|
|
28
|
+
referrerPolicy: "strict-origin-when-cross-origin",
|
|
29
|
+
strictTransportSecurity: "max-age=31536000; includeSubDomains",
|
|
30
|
+
permissionsPolicy: "camera=(), microphone=(), geolocation=()",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function resolveHeaders(
|
|
34
|
+
config: SecurityHeadersConfig,
|
|
35
|
+
pathname: string,
|
|
36
|
+
): Record<string, string> {
|
|
37
|
+
// Start with defaults, overlay user config
|
|
38
|
+
const merged = { ...DEFAULTS, ...config };
|
|
39
|
+
delete (merged as any).routes;
|
|
40
|
+
|
|
41
|
+
// Apply route overrides
|
|
42
|
+
if (config.routes) {
|
|
43
|
+
for (const pattern in config.routes) {
|
|
44
|
+
if (pathname.startsWith(pattern)) {
|
|
45
|
+
Object.assign(merged, config.routes[pattern]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const headers: Record<string, string> = {};
|
|
51
|
+
|
|
52
|
+
if (merged.contentSecurityPolicy !== false)
|
|
53
|
+
headers["Content-Security-Policy"] = merged.contentSecurityPolicy;
|
|
54
|
+
if (merged.contentTypeOptions !== false)
|
|
55
|
+
headers["X-Content-Type-Options"] = merged.contentTypeOptions;
|
|
56
|
+
if (merged.frameOptions !== false)
|
|
57
|
+
headers["X-Frame-Options"] = merged.frameOptions;
|
|
58
|
+
if (merged.xssProtection !== false)
|
|
59
|
+
headers["X-XSS-Protection"] = merged.xssProtection;
|
|
60
|
+
if (merged.referrerPolicy !== false)
|
|
61
|
+
headers["Referrer-Policy"] = merged.referrerPolicy;
|
|
62
|
+
if (merged.strictTransportSecurity !== false)
|
|
63
|
+
headers["Strict-Transport-Security"] = merged.strictTransportSecurity;
|
|
64
|
+
if (merged.permissionsPolicy !== false)
|
|
65
|
+
headers["Permissions-Policy"] = merged.permissionsPolicy;
|
|
66
|
+
|
|
67
|
+
return headers;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Security headers plugin.
|
|
72
|
+
* Adds standard security headers to all responses.
|
|
73
|
+
*
|
|
74
|
+
* Usage:
|
|
75
|
+
*
|
|
76
|
+
* import { securityHeaders } from "@sigil-dev/grimoire/headers";
|
|
77
|
+
*
|
|
78
|
+
* createServer({
|
|
79
|
+
* plugins: [
|
|
80
|
+
* securityHeaders({
|
|
81
|
+
* // override defaults
|
|
82
|
+
* frameOptions: "SAMEORIGIN",
|
|
83
|
+
* // per-route
|
|
84
|
+
* routes: {
|
|
85
|
+
* "/admin": { contentSecurityPolicy: "default-src 'self'" },
|
|
86
|
+
* },
|
|
87
|
+
* }),
|
|
88
|
+
* ],
|
|
89
|
+
* });
|
|
90
|
+
*/
|
|
91
|
+
export function securityHeaders(
|
|
92
|
+
config: SecurityHeadersConfig = {},
|
|
93
|
+
): GrimoirePlugin {
|
|
94
|
+
return {
|
|
95
|
+
name: "grimoire-security-headers",
|
|
96
|
+
async onRequest(req, next) {
|
|
97
|
+
const res = await next();
|
|
98
|
+
const url = new URL(req.url);
|
|
99
|
+
const headers = resolveHeaders(config, url.pathname);
|
|
100
|
+
|
|
101
|
+
// Clone response with security headers
|
|
102
|
+
const newHeaders = new Headers(res.headers);
|
|
103
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
104
|
+
newHeaders.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return new Response(res.body, {
|
|
108
|
+
status: res.status,
|
|
109
|
+
statusText: res.statusText,
|
|
110
|
+
headers: newHeaders,
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/integrations/vite.ts
CHANGED
|
@@ -1,73 +1,73 @@
|
|
|
1
|
-
import { isAbsolute, join, resolve } from "node:path";
|
|
2
|
-
import type { Plugin } from "vite";
|
|
3
|
-
import { renderRoute } from "../rendering";
|
|
4
|
-
import { matchRoute } from "../routing/router.ts";
|
|
5
|
-
import { scanRoutes } from "../routing/scanner.ts";
|
|
6
|
-
|
|
7
|
-
const CLIENT_ENTRY = resolve(import.meta.dir, "./index.ts");
|
|
8
|
-
|
|
9
|
-
export function grimoire(options: { routes?: string } = {}): Plugin {
|
|
10
|
-
let isBuild = false;
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
name: "grimoire",
|
|
14
|
-
|
|
15
|
-
configResolved(config) {
|
|
16
|
-
isBuild = config.command === "build";
|
|
17
|
-
},
|
|
18
|
-
|
|
19
|
-
configureServer(vite) {
|
|
20
|
-
const routesDir = isAbsolute(options.routes ?? "src/routes")
|
|
21
|
-
? options.routes!
|
|
22
|
-
: join(process.cwd(), options.routes ?? "src/routes");
|
|
23
|
-
|
|
24
|
-
// client entry
|
|
25
|
-
vite.middlewares.use("/__grimoire__/client.js", async (_req, res) => {
|
|
26
|
-
const result = await vite.transformRequest(CLIENT_ENTRY);
|
|
27
|
-
if (!result) {
|
|
28
|
-
res.statusCode = 404;
|
|
29
|
-
res.end();
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
res.setHeader("Content-Type", "application/javascript");
|
|
33
|
-
res.end(result.code);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// page routes
|
|
37
|
-
vite.middlewares.use(async (req, res, next) => {
|
|
38
|
-
const url = new URL(req.url!, "http://localhost");
|
|
39
|
-
if (url.pathname.startsWith("/__") || url.pathname.includes(".")) {
|
|
40
|
-
return next();
|
|
41
|
-
}
|
|
42
|
-
try {
|
|
43
|
-
const tree = await scanRoutes(routesDir, process.cwd());
|
|
44
|
-
const matched = matchRoute(tree, url);
|
|
45
|
-
if (!matched) return next();
|
|
46
|
-
|
|
47
|
-
const response = await renderRoute(
|
|
48
|
-
matched,
|
|
49
|
-
new Request(`http://localhost${req.url}`),
|
|
50
|
-
[],
|
|
51
|
-
(path) => vite.ssrLoadModule(path), // Vite transforms SSR files correctly
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
const html = await response.text();
|
|
55
|
-
res.setHeader("Content-Type", "text/html");
|
|
56
|
-
res.end(html);
|
|
57
|
-
} catch (e) {
|
|
58
|
-
vite.ssrFixStacktrace(e as Error);
|
|
59
|
-
next(e);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
buildStart() {
|
|
65
|
-
if (!isBuild) return;
|
|
66
|
-
this.emitFile({
|
|
67
|
-
type: "chunk",
|
|
68
|
-
id: CLIENT_ENTRY,
|
|
69
|
-
fileName: "client.js",
|
|
70
|
-
});
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
}
|
|
1
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
2
|
+
import type { Plugin } from "vite";
|
|
3
|
+
import { renderRoute } from "../rendering";
|
|
4
|
+
import { matchRoute } from "../routing/router.ts";
|
|
5
|
+
import { scanRoutes } from "../routing/scanner.ts";
|
|
6
|
+
|
|
7
|
+
const CLIENT_ENTRY = resolve(import.meta.dir, "./index.ts");
|
|
8
|
+
|
|
9
|
+
export function grimoire(options: { routes?: string } = {}): Plugin {
|
|
10
|
+
let isBuild = false;
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
name: "grimoire",
|
|
14
|
+
|
|
15
|
+
configResolved(config) {
|
|
16
|
+
isBuild = config.command === "build";
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
configureServer(vite) {
|
|
20
|
+
const routesDir = isAbsolute(options.routes ?? "src/routes")
|
|
21
|
+
? options.routes!
|
|
22
|
+
: join(process.cwd(), options.routes ?? "src/routes");
|
|
23
|
+
|
|
24
|
+
// client entry
|
|
25
|
+
vite.middlewares.use("/__grimoire__/client.js", async (_req, res) => {
|
|
26
|
+
const result = await vite.transformRequest(CLIENT_ENTRY);
|
|
27
|
+
if (!result) {
|
|
28
|
+
res.statusCode = 404;
|
|
29
|
+
res.end();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
33
|
+
res.end(result.code);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// page routes
|
|
37
|
+
vite.middlewares.use(async (req, res, next) => {
|
|
38
|
+
const url = new URL(req.url!, "http://localhost");
|
|
39
|
+
if (url.pathname.startsWith("/__") || url.pathname.includes(".")) {
|
|
40
|
+
return next();
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const tree = await scanRoutes(routesDir, process.cwd());
|
|
44
|
+
const matched = matchRoute(tree, url);
|
|
45
|
+
if (!matched) return next();
|
|
46
|
+
|
|
47
|
+
const response = await renderRoute(
|
|
48
|
+
matched,
|
|
49
|
+
new Request(`http://localhost${req.url}`),
|
|
50
|
+
[],
|
|
51
|
+
(path) => vite.ssrLoadModule(path), // Vite transforms SSR files correctly
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const html = await response.text();
|
|
55
|
+
res.setHeader("Content-Type", "text/html");
|
|
56
|
+
res.end(html);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
vite.ssrFixStacktrace(e as Error);
|
|
59
|
+
next(e);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
buildStart() {
|
|
65
|
+
if (!isBuild) return;
|
|
66
|
+
this.emitFile({
|
|
67
|
+
type: "chunk",
|
|
68
|
+
id: CLIENT_ENTRY,
|
|
69
|
+
fileName: "client.js",
|
|
70
|
+
});
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
package/src/logger/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from "./logger.ts";
|
|
2
|
-
export * from "./request-middleware.ts";
|
|
3
|
-
export * from "./transports.ts";
|
|
4
|
-
export * from "./types.ts";
|
|
1
|
+
export * from "./logger.ts";
|
|
2
|
+
export * from "./request-middleware.ts";
|
|
3
|
+
export * from "./transports.ts";
|
|
4
|
+
export * from "./types.ts";
|
package/src/logger/instance.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Logger } from "./logger.ts";
|
|
2
|
-
import { ConsoleTransport } from "./transports.ts";
|
|
3
|
-
|
|
4
|
-
export const log = new Logger("grimoire", {
|
|
5
|
-
transports: [new ConsoleTransport()],
|
|
6
|
-
});
|
|
1
|
+
import { Logger } from "./logger.ts";
|
|
2
|
+
import { ConsoleTransport } from "./transports.ts";
|
|
3
|
+
|
|
4
|
+
export const log = new Logger("grimoire", {
|
|
5
|
+
transports: [new ConsoleTransport()],
|
|
6
|
+
});
|