@sigil-dev/grimoire 0.9.21 → 0.9.23
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/README.md +83 -83
- package/index.ts +50 -48
- package/package.json +22 -10
- package/public/__grimoire__/grimoire-client.js +12 -0
- package/public/__grimoire__/runtime.js +1241 -0
- package/server.ts +22 -20
- package/src/adapters/bun.ts +17 -0
- package/src/adapters/static.ts +41 -0
- package/src/adapters/types.ts +36 -0
- package/src/base.ts +91 -0
- package/src/client/csrf.ts +47 -47
- package/src/client/deferred.ts +179 -179
- package/src/client/enhance.ts +160 -160
- package/src/client/router.ts +871 -833
- package/src/dev/compile-module.ts +211 -211
- package/src/dev/loader.ts +141 -141
- package/src/dev/resolve.ts +70 -70
- package/src/dev/runtime-bundle.ts +51 -51
- package/src/env/modules.ts +62 -62
- package/src/env/required.ts +42 -0
- package/src/env/typegen.ts +61 -61
- package/src/headers.ts +105 -105
- package/src/logger/transports.ts +117 -117
- package/src/rendering/head.ts +49 -49
- package/src/rendering/hydrate.ts +161 -161
- package/src/rendering/images.ts +43 -43
- package/src/rendering/index.ts +755 -749
- package/src/rendering/preload.ts +102 -101
- package/src/rendering/ssrPlugin.ts +1 -1
- package/src/rendering/timing.ts +40 -40
- package/src/routing/router.ts +186 -186
- package/src/routing/scanner.ts +225 -196
- package/src/routing/transform-routes.ts +102 -102
- package/src/server/after-response.ts +206 -206
- package/src/server/asset-cache.ts +57 -57
- package/src/server/build.ts +262 -262
- package/src/server/cached.ts +114 -114
- package/src/server/client-address.ts +82 -82
- package/src/server/client-dir.ts +57 -57
- package/src/server/client-entry.ts +44 -44
- package/src/server/cookie-utils.ts +80 -80
- package/src/server/coordinator.ts +476 -471
- package/src/server/hooks.ts +122 -122
- package/src/server/index.ts +211 -80
- package/src/server/plugins.ts +172 -172
- package/src/server/prerender.ts +444 -0
- package/src/server/reserved.ts +17 -0
- package/src/server/resolver.ts +98 -98
- package/src/server/rewrite.ts +28 -0
- package/src/server/warm.ts +91 -91
- package/src/server/with-headers.ts +27 -27
- package/src/server/worker.ts +74 -72
- package/src/testing/dom.ts +73 -0
- package/src/testing/index.ts +169 -0
- package/src/testing/server.ts +162 -0
- package/src/typegen/index.ts +450 -450
- package/src/types.ts +501 -432
- package/test/after-response.test.ts +132 -132
- package/test/asset-cache-policy.test.ts +96 -96
- package/test/base.test.ts +134 -0
- package/test/cached.test.ts +126 -126
- package/test/client-address.test.ts +78 -78
- package/test/client-csrf.test.ts +61 -61
- package/test/client-dir.test.ts +101 -101
- package/test/client-router.test.ts +789 -734
- package/test/coordinator.test.ts +96 -94
- package/test/deferred.test.ts +256 -256
- package/test/dev-loader.test.ts +30 -30
- package/test/env.test.ts +168 -168
- package/test/force-production.test.ts +44 -0
- package/test/images.test.ts +49 -49
- package/test/locals-header.test.ts +61 -0
- package/test/middleware.test.ts +243 -243
- package/test/parser-plugins.test.ts +1 -1
- package/test/plugin-handle.test.ts +62 -62
- package/test/plugin-services-context.test.ts +97 -97
- package/test/preload.test.ts +48 -48
- package/test/prerender.test.ts +353 -0
- package/test/rendering.test.ts +799 -798
- package/test/required-env.test.ts +32 -0
- package/test/rewrite-html.test.ts +90 -0
- package/test/scanning.test.ts +349 -314
- package/test/server.test.ts +640 -619
- package/test/services.test.ts +90 -90
- package/test/streaming.test.ts +381 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +808 -799
- package/test/warm.test.ts +96 -96
- package/test/worker-context.test.ts +59 -59
- package/test/workspace-files.test.ts +149 -146
- package/test/workspace-versions.test.ts +1 -1
- package/.grimoire/_routes.ts +0 -4
- package/public/__grimoire__/client.js +0 -55
- package/public/__grimoire__/hydrate.js +0 -63
package/src/rendering/preload.ts
CHANGED
|
@@ -1,101 +1,102 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `<link rel="modulepreload">` for the chunks a route needs.
|
|
3
|
-
*
|
|
4
|
-
* The client bundle loads a route's code with a lazy `import()`, which only
|
|
5
|
-
* starts once hydrate.js has downloaded and run: HTML, then hydrate.js, then
|
|
6
|
-
* the route chunk, then whatever that chunk imports. Naming those chunks in
|
|
7
|
-
* the HTML lets the browser fetch them alongside hydrate.js instead of behind
|
|
8
|
-
* it.
|
|
9
|
-
*
|
|
10
|
-
* The chunk graph is read back out of the built hydrate.js (the bundler
|
|
11
|
-
* writes it there for its own preloading), so no build step has to cooperate.
|
|
12
|
-
*/
|
|
13
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
14
|
-
import { join } from "node:path";
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let
|
|
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
|
-
graphOf
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
seen.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
.
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `<link rel="modulepreload">` for the chunks a route needs.
|
|
3
|
+
*
|
|
4
|
+
* The client bundle loads a route's code with a lazy `import()`, which only
|
|
5
|
+
* starts once hydrate.js has downloaded and run: HTML, then hydrate.js, then
|
|
6
|
+
* the route chunk, then whatever that chunk imports. Naming those chunks in
|
|
7
|
+
* the HTML lets the browser fetch them alongside hydrate.js instead of behind
|
|
8
|
+
* it.
|
|
9
|
+
*
|
|
10
|
+
* The chunk graph is read back out of the built hydrate.js (the bundler
|
|
11
|
+
* writes it there for its own preloading), so no build step has to cooperate.
|
|
12
|
+
*/
|
|
13
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { withBase } from "../base";
|
|
16
|
+
import { clientDir } from "../server/client-dir";
|
|
17
|
+
import { hydrateEntry } from "../server/client-entry";
|
|
18
|
+
|
|
19
|
+
interface ChunkGraph {
|
|
20
|
+
/** route pattern → chunk id */
|
|
21
|
+
routes: Map<string, string>;
|
|
22
|
+
/** chunk id → [file, ...ids it imports] */
|
|
23
|
+
nodes: Map<string, { file: string; deps: string[] }>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const DIR = "/__grimoire__/";
|
|
27
|
+
let graph: ChunkGraph | null | undefined;
|
|
28
|
+
|
|
29
|
+
export function parseChunkGraph(source: string): ChunkGraph | null {
|
|
30
|
+
const call = /^__chunks\(import\.meta\.url,(\[.*?\]),(\[\[.*?\]\]),(\d+)\)/m.exec(
|
|
31
|
+
source,
|
|
32
|
+
);
|
|
33
|
+
if (!call) return null;
|
|
34
|
+
let ids: string[];
|
|
35
|
+
let raw: Array<Array<string | number>>;
|
|
36
|
+
try {
|
|
37
|
+
ids = JSON.parse(call[1]);
|
|
38
|
+
raw = JSON.parse(call[2]);
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const nodes: ChunkGraph["nodes"] = new Map();
|
|
43
|
+
ids.forEach((id, i) => {
|
|
44
|
+
const [file, ...deps] = raw[i] ?? [];
|
|
45
|
+
if (typeof file !== "string") return;
|
|
46
|
+
nodes.set(id, { file, deps: deps.map((d) => ids[Number(d)]).filter(Boolean) });
|
|
47
|
+
});
|
|
48
|
+
const routes = new Map<string, string>();
|
|
49
|
+
for (const m of source.matchAll(
|
|
50
|
+
/^\s*("(?:[^"\\]|\\.)*"):\s*\{\s*__lazy:\s*\(\)\s*=>\s*\(__preload\("([^"]+)"\)/gm,
|
|
51
|
+
)) {
|
|
52
|
+
routes.set(JSON.parse(m[1]), m[2]);
|
|
53
|
+
}
|
|
54
|
+
return { routes, nodes };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let graphOf: string | undefined;
|
|
58
|
+
|
|
59
|
+
function load(): ChunkGraph | null {
|
|
60
|
+
// a new entry name means a new build, whose chunks differ
|
|
61
|
+
const entry = hydrateEntry();
|
|
62
|
+
if (graph !== undefined && graphOf === entry) return graph;
|
|
63
|
+
graphOf = entry;
|
|
64
|
+
const file = join(clientDir(), entry);
|
|
65
|
+
graph = existsSync(file) ? parseChunkGraph(readFileSync(file, "utf-8")) : null;
|
|
66
|
+
return graph;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Files (relative to /__grimoire__/) the route's chunk needs, closure included. */
|
|
70
|
+
export function chunksFor(g: ChunkGraph, pattern: string): string[] {
|
|
71
|
+
const start = g.routes.get(pattern);
|
|
72
|
+
if (!start) return [];
|
|
73
|
+
const seen = new Set<string>();
|
|
74
|
+
const stack = [start];
|
|
75
|
+
const files: string[] = [];
|
|
76
|
+
while (stack.length) {
|
|
77
|
+
const id = stack.pop() as string;
|
|
78
|
+
if (seen.has(id)) continue;
|
|
79
|
+
seen.add(id);
|
|
80
|
+
const node = g.nodes.get(id);
|
|
81
|
+
if (!node) continue;
|
|
82
|
+
// hydrate.js itself is already a <script> in the head
|
|
83
|
+
if (!/(^|\/)hydrate\.js$/.test(node.file)) files.push(node.file.replace(/^\.\//, ""));
|
|
84
|
+
stack.push(...node.deps);
|
|
85
|
+
}
|
|
86
|
+
return files;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The preload tags for a route, or "" in dev / when there is no built graph. */
|
|
90
|
+
export function preloadLinks(pattern: string, nonceAttr: string, dev?: boolean): string {
|
|
91
|
+
if (dev) return "";
|
|
92
|
+
const g = load();
|
|
93
|
+
if (!g) return "";
|
|
94
|
+
return chunksFor(g, pattern)
|
|
95
|
+
.map((f) => `\n <link rel="modulepreload" href="${withBase(DIR)}${f}" crossorigin${nonceAttr}>`)
|
|
96
|
+
.join("");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** For tests: forget the cached graph. */
|
|
100
|
+
export function resetChunkGraph(): void {
|
|
101
|
+
graph = undefined;
|
|
102
|
+
}
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
import { log } from "../logger/instance";
|
|
9
9
|
import { rewriteAliases } from "../server/resolver";
|
|
10
10
|
import type { GrimoirePlugin } from "../types";
|
|
11
|
-
import type { ParserPlugin } from "@babel/
|
|
11
|
+
import type { ParserPlugin } from "@babel/parser";
|
|
12
12
|
|
|
13
13
|
const logger = log.scope("scope");
|
|
14
14
|
let registered = false;
|
package/src/rendering/timing.ts
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `Server-Timing` for route renders, so the browser's network panel shows
|
|
3
|
-
* where a slow page spent its time (`load` = layout and page server loads,
|
|
4
|
-
* `render` = components to HTML) without any extra tooling.
|
|
5
|
-
*
|
|
6
|
-
* On in dev; in production only when `serverTiming: true`, since durations are
|
|
7
|
-
* visible to every visitor.
|
|
8
|
-
*/
|
|
9
|
-
let enabled = false;
|
|
10
|
-
|
|
11
|
-
export function setServerTiming(on: boolean): void {
|
|
12
|
-
enabled = on;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface Timer {
|
|
16
|
-
/** Close the phase that has been running since the last mark. */
|
|
17
|
-
mark(name: string): void;
|
|
18
|
-
/** `{ "Server-Timing": ... }`, or nothing when disabled or no marks. */
|
|
19
|
-
headers(): Record<string, string>;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function createTimer(): Timer {
|
|
23
|
-
const marks: Array<[string, number]> = [];
|
|
24
|
-
let last = performance.now();
|
|
25
|
-
return {
|
|
26
|
-
mark(name) {
|
|
27
|
-
const now = performance.now();
|
|
28
|
-
marks.push([name, now - last]);
|
|
29
|
-
last = now;
|
|
30
|
-
},
|
|
31
|
-
headers() {
|
|
32
|
-
if (!enabled || marks.length === 0) return {};
|
|
33
|
-
return {
|
|
34
|
-
"Server-Timing": marks
|
|
35
|
-
.map(([n, d]) => `${n};dur=${d.toFixed(1)}`)
|
|
36
|
-
.join(", "),
|
|
37
|
-
};
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* `Server-Timing` for route renders, so the browser's network panel shows
|
|
3
|
+
* where a slow page spent its time (`load` = layout and page server loads,
|
|
4
|
+
* `render` = components to HTML) without any extra tooling.
|
|
5
|
+
*
|
|
6
|
+
* On in dev; in production only when `serverTiming: true`, since durations are
|
|
7
|
+
* visible to every visitor.
|
|
8
|
+
*/
|
|
9
|
+
let enabled = false;
|
|
10
|
+
|
|
11
|
+
export function setServerTiming(on: boolean): void {
|
|
12
|
+
enabled = on;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Timer {
|
|
16
|
+
/** Close the phase that has been running since the last mark. */
|
|
17
|
+
mark(name: string): void;
|
|
18
|
+
/** `{ "Server-Timing": ... }`, or nothing when disabled or no marks. */
|
|
19
|
+
headers(): Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createTimer(): Timer {
|
|
23
|
+
const marks: Array<[string, number]> = [];
|
|
24
|
+
let last = performance.now();
|
|
25
|
+
return {
|
|
26
|
+
mark(name) {
|
|
27
|
+
const now = performance.now();
|
|
28
|
+
marks.push([name, now - last]);
|
|
29
|
+
last = now;
|
|
30
|
+
},
|
|
31
|
+
headers() {
|
|
32
|
+
if (!enabled || marks.length === 0) return {};
|
|
33
|
+
return {
|
|
34
|
+
"Server-Timing": marks
|
|
35
|
+
.map(([n, d]) => `${n};dur=${d.toFixed(1)}`)
|
|
36
|
+
.join(", "),
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|