@sigil-dev/grimoire 0.9.21 → 0.9.22
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 +16 -5
- 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 +870 -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 +101 -101
- package/src/rendering/ssrPlugin.ts +108 -108
- 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 +1138 -1018
- 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 +92 -92
- 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 +798 -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 +378 -378
- package/test/testing-helpers.test.ts +166 -0
- package/test/typegen.test.ts +805 -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 +131 -131
- 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,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 { clientDir } from "../server/client-dir";
|
|
16
|
-
import { hydrateEntry } from "../server/client-entry";
|
|
17
|
-
|
|
18
|
-
interface ChunkGraph {
|
|
19
|
-
/** route pattern → chunk id */
|
|
20
|
-
routes: Map<string, string>;
|
|
21
|
-
/** chunk id → [file, ...ids it imports] */
|
|
22
|
-
nodes: Map<string, { file: string; deps: string[] }>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
let graph: ChunkGraph | null | undefined;
|
|
27
|
-
|
|
28
|
-
export function parseChunkGraph(source: string): ChunkGraph | null {
|
|
29
|
-
const call = /^__chunks\(import\.meta\.url,(\[.*?\]),(\[\[.*?\]\]),(\d+)\)/m.exec(
|
|
30
|
-
source,
|
|
31
|
-
);
|
|
32
|
-
if (!call) return null;
|
|
33
|
-
let ids: string[];
|
|
34
|
-
let raw: Array<Array<string | number>>;
|
|
35
|
-
try {
|
|
36
|
-
ids = JSON.parse(call[1]);
|
|
37
|
-
raw = JSON.parse(call[2]);
|
|
38
|
-
} catch {
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
const nodes: ChunkGraph["nodes"] = new Map();
|
|
42
|
-
ids.forEach((id, i) => {
|
|
43
|
-
const [file, ...deps] = raw[i] ?? [];
|
|
44
|
-
if (typeof file !== "string") return;
|
|
45
|
-
nodes.set(id, { file, deps: deps.map((d) => ids[Number(d)]).filter(Boolean) });
|
|
46
|
-
});
|
|
47
|
-
const routes = new Map<string, string>();
|
|
48
|
-
for (const m of source.matchAll(
|
|
49
|
-
/^\s*("(?:[^"\\]|\\.)*"):\s*\{\s*__lazy:\s*\(\)\s*=>\s*\(__preload\("([^"]+)"\)/gm,
|
|
50
|
-
)) {
|
|
51
|
-
routes.set(JSON.parse(m[1]), m[2]);
|
|
52
|
-
}
|
|
53
|
-
return { routes, nodes };
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let graphOf: string | undefined;
|
|
57
|
-
|
|
58
|
-
function load(): ChunkGraph | null {
|
|
59
|
-
// a new entry name means a new build, whose chunks differ
|
|
60
|
-
const entry = hydrateEntry();
|
|
61
|
-
if (graph !== undefined && graphOf === entry) return graph;
|
|
62
|
-
graphOf = entry;
|
|
63
|
-
const file = join(clientDir(), entry);
|
|
64
|
-
graph = existsSync(file) ? parseChunkGraph(readFileSync(file, "utf-8")) : null;
|
|
65
|
-
return graph;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/** Files (relative to /__grimoire__/) the route's chunk needs, closure included. */
|
|
69
|
-
export function chunksFor(g: ChunkGraph, pattern: string): string[] {
|
|
70
|
-
const start = g.routes.get(pattern);
|
|
71
|
-
if (!start) return [];
|
|
72
|
-
const seen = new Set<string>();
|
|
73
|
-
const stack = [start];
|
|
74
|
-
const files: string[] = [];
|
|
75
|
-
while (stack.length) {
|
|
76
|
-
const id = stack.pop() as string;
|
|
77
|
-
if (seen.has(id)) continue;
|
|
78
|
-
seen.add(id);
|
|
79
|
-
const node = g.nodes.get(id);
|
|
80
|
-
if (!node) continue;
|
|
81
|
-
// hydrate.js itself is already a <script> in the head
|
|
82
|
-
if (!/(^|\/)hydrate\.js$/.test(node.file)) files.push(node.file.replace(/^\.\//, ""));
|
|
83
|
-
stack.push(...node.deps);
|
|
84
|
-
}
|
|
85
|
-
return files;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/** The preload tags for a route, or "" in dev / when there is no built graph. */
|
|
89
|
-
export function preloadLinks(pattern: string, nonceAttr: string, dev?: boolean): string {
|
|
90
|
-
if (dev) return "";
|
|
91
|
-
const g = load();
|
|
92
|
-
if (!g) return "";
|
|
93
|
-
return chunksFor(g, pattern)
|
|
94
|
-
.map((f) => `\n <link rel="modulepreload" href="${
|
|
95
|
-
.join("");
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** For tests: forget the cached graph. */
|
|
99
|
-
export function resetChunkGraph(): void {
|
|
100
|
-
graph = undefined;
|
|
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 { clientDir } from "../server/client-dir";
|
|
16
|
+
import { hydrateEntry } from "../server/client-entry";
|
|
17
|
+
|
|
18
|
+
interface ChunkGraph {
|
|
19
|
+
/** route pattern → chunk id */
|
|
20
|
+
routes: Map<string, string>;
|
|
21
|
+
/** chunk id → [file, ...ids it imports] */
|
|
22
|
+
nodes: Map<string, { file: string; deps: string[] }>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const DIR = "/__grimoire__/";
|
|
26
|
+
let graph: ChunkGraph | null | undefined;
|
|
27
|
+
|
|
28
|
+
export function parseChunkGraph(source: string): ChunkGraph | null {
|
|
29
|
+
const call = /^__chunks\(import\.meta\.url,(\[.*?\]),(\[\[.*?\]\]),(\d+)\)/m.exec(
|
|
30
|
+
source,
|
|
31
|
+
);
|
|
32
|
+
if (!call) return null;
|
|
33
|
+
let ids: string[];
|
|
34
|
+
let raw: Array<Array<string | number>>;
|
|
35
|
+
try {
|
|
36
|
+
ids = JSON.parse(call[1]);
|
|
37
|
+
raw = JSON.parse(call[2]);
|
|
38
|
+
} catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
const nodes: ChunkGraph["nodes"] = new Map();
|
|
42
|
+
ids.forEach((id, i) => {
|
|
43
|
+
const [file, ...deps] = raw[i] ?? [];
|
|
44
|
+
if (typeof file !== "string") return;
|
|
45
|
+
nodes.set(id, { file, deps: deps.map((d) => ids[Number(d)]).filter(Boolean) });
|
|
46
|
+
});
|
|
47
|
+
const routes = new Map<string, string>();
|
|
48
|
+
for (const m of source.matchAll(
|
|
49
|
+
/^\s*("(?:[^"\\]|\\.)*"):\s*\{\s*__lazy:\s*\(\)\s*=>\s*\(__preload\("([^"]+)"\)/gm,
|
|
50
|
+
)) {
|
|
51
|
+
routes.set(JSON.parse(m[1]), m[2]);
|
|
52
|
+
}
|
|
53
|
+
return { routes, nodes };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let graphOf: string | undefined;
|
|
57
|
+
|
|
58
|
+
function load(): ChunkGraph | null {
|
|
59
|
+
// a new entry name means a new build, whose chunks differ
|
|
60
|
+
const entry = hydrateEntry();
|
|
61
|
+
if (graph !== undefined && graphOf === entry) return graph;
|
|
62
|
+
graphOf = entry;
|
|
63
|
+
const file = join(clientDir(), entry);
|
|
64
|
+
graph = existsSync(file) ? parseChunkGraph(readFileSync(file, "utf-8")) : null;
|
|
65
|
+
return graph;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Files (relative to /__grimoire__/) the route's chunk needs, closure included. */
|
|
69
|
+
export function chunksFor(g: ChunkGraph, pattern: string): string[] {
|
|
70
|
+
const start = g.routes.get(pattern);
|
|
71
|
+
if (!start) return [];
|
|
72
|
+
const seen = new Set<string>();
|
|
73
|
+
const stack = [start];
|
|
74
|
+
const files: string[] = [];
|
|
75
|
+
while (stack.length) {
|
|
76
|
+
const id = stack.pop() as string;
|
|
77
|
+
if (seen.has(id)) continue;
|
|
78
|
+
seen.add(id);
|
|
79
|
+
const node = g.nodes.get(id);
|
|
80
|
+
if (!node) continue;
|
|
81
|
+
// hydrate.js itself is already a <script> in the head
|
|
82
|
+
if (!/(^|\/)hydrate\.js$/.test(node.file)) files.push(node.file.replace(/^\.\//, ""));
|
|
83
|
+
stack.push(...node.deps);
|
|
84
|
+
}
|
|
85
|
+
return files;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The preload tags for a route, or "" in dev / when there is no built graph. */
|
|
89
|
+
export function preloadLinks(pattern: string, nonceAttr: string, dev?: boolean): string {
|
|
90
|
+
if (dev) return "";
|
|
91
|
+
const g = load();
|
|
92
|
+
if (!g) return "";
|
|
93
|
+
return chunksFor(g, pattern)
|
|
94
|
+
.map((f) => `\n <link rel="modulepreload" href="${withBase(DIR)}${f}" crossorigin${nonceAttr}>`)
|
|
95
|
+
.join("");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** For tests: forget the cached graph. */
|
|
99
|
+
export function resetChunkGraph(): void {
|
|
100
|
+
graph = undefined;
|
|
101
|
+
}
|
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
import { transformSync } from "@babel/core";
|
|
2
|
-
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
3
|
-
import {
|
|
4
|
-
computeHash,
|
|
5
|
-
extractStyles,
|
|
6
|
-
serverStyleSnippet,
|
|
7
|
-
} from "@sigil-dev/compiler/css";
|
|
8
|
-
import { log } from "../logger/instance";
|
|
9
|
-
import { rewriteAliases } from "../server/resolver";
|
|
10
|
-
import type { GrimoirePlugin } from "../types";
|
|
11
|
-
import type { ParserPlugin } from "@babel/core";
|
|
12
|
-
|
|
13
|
-
const logger = log.scope("scope");
|
|
14
|
-
let registered = false;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* `aliases` (prefix → absolute directory, see projectAliases) are rewritten
|
|
18
|
-
* to file paths in every compiled module: Bun's runtime plugins can't
|
|
19
|
-
* resolve them for static imports.
|
|
20
|
-
*/
|
|
21
|
-
export function registerSSRPlugin(
|
|
22
|
-
plugins: GrimoirePlugin[] = [],
|
|
23
|
-
aliases: Record<string, string> = {},
|
|
24
|
-
) {
|
|
25
|
-
if (registered) return;
|
|
26
|
-
registered = true;
|
|
27
|
-
|
|
28
|
-
Bun.plugin({
|
|
29
|
-
name: "sigil-ssr",
|
|
30
|
-
|
|
31
|
-
setup(build) {
|
|
32
|
-
// loader: "ts" not "tsx" — Babel already consumed the JSX,
|
|
33
|
-
// Bun only needs to strip remaining TypeScript types
|
|
34
|
-
const transpiler = new Bun.Transpiler({ loader: "ts", target: "bun" });
|
|
35
|
-
build.onResolve({ filter: /^@sigil-dev\/runtime($|\/)/ }, (args) => {
|
|
36
|
-
try {
|
|
37
|
-
const resolved = Bun.resolveSync(args.path, process.cwd());
|
|
38
|
-
return { path: resolved.replaceAll("\\", "/") };
|
|
39
|
-
} catch {
|
|
40
|
-
return undefined;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
build.onLoad({ filter: /\.tsx?$/ }, async ({ path }) => {
|
|
45
|
-
if (path.includes("index.tsx"))
|
|
46
|
-
logger.debug("[ssr-plugin] onLoad:", path);
|
|
47
|
-
const source = await Bun.file(path).text();
|
|
48
|
-
|
|
49
|
-
// node_modules and .grimoire files are plain TypeScript (no sigil JSX).
|
|
50
|
-
// Still must return { contents, loader } — never return undefined from onLoad.
|
|
51
|
-
if (path.includes("node_modules") || path.includes(".grimoire")) {
|
|
52
|
-
return {
|
|
53
|
-
contents: transpiler.transformSync(source),
|
|
54
|
-
loader: "js" as const,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (process.env.SIGIL_VERBOSE) {
|
|
59
|
-
logger.debug("[sigil-ssr] XFORM:", path);
|
|
60
|
-
}
|
|
61
|
-
// must match the client build, or scoped classes differ per side;
|
|
62
|
-
// files without a <style> block get no scope class at all. The
|
|
63
|
-
// CSS is registered for the renderer, which puts it in <head>.
|
|
64
|
-
const styles = extractStyles(source, computeHash(path), path);
|
|
65
|
-
const hash = styles.css ? computeHash(path) : undefined;
|
|
66
|
-
const sourceNoStyle = styles.code;
|
|
67
|
-
const result = transformSync(sourceNoStyle, {
|
|
68
|
-
configFile: false,
|
|
69
|
-
babelrc: false,
|
|
70
|
-
parserOpts: {
|
|
71
|
-
// The jsx plugin goes on .tsx files only.
|
|
72
|
-
//
|
|
73
|
-
// With it enabled everywhere, Babel parses `<R>(x: R) => x`
|
|
74
|
-
// as the opening of a JSX element and fails with
|
|
75
|
-
// "Unexpected token `>`". So a generic arrow function in a
|
|
76
|
-
// plain .ts file — completely ordinary TypeScript — could
|
|
77
|
-
// not be compiled. Found while writing the Drizzle adapter
|
|
78
|
-
// recipe, where `transaction: <R>(db, fn) => ...` is the
|
|
79
|
-
// form that removes an `as any`.
|
|
80
|
-
//
|
|
81
|
-
// A .tsx file is genuinely ambiguous (`<R>(x) => x` is
|
|
82
|
-
// JSX there), so .tsx keeps both and users write `<R,>`.
|
|
83
|
-
plugins: path.endsWith(".tsx")
|
|
84
|
-
? (["typescript", "jsx"] as ParserPlugin[])
|
|
85
|
-
: (["typescript"] as ParserPlugin[]),
|
|
86
|
-
},
|
|
87
|
-
plugins: [[sigilPlugin, { mode: "ssr", hash }]],
|
|
88
|
-
filename: path,
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
let contents = rewriteAliases(
|
|
92
|
-
transpiler.transformSync(result?.code ?? ""),
|
|
93
|
-
aliases,
|
|
94
|
-
);
|
|
95
|
-
if (styles.css && hash) {
|
|
96
|
-
contents = serverStyleSnippet(hash, styles.css) + contents;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
for (const plugin of plugins) {
|
|
100
|
-
if (plugin.transform)
|
|
101
|
-
contents = (await plugin.transform(contents, path)) ?? contents;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return { contents, loader: "js" as const };
|
|
105
|
-
});
|
|
106
|
-
},
|
|
107
|
-
});
|
|
108
|
-
}
|
|
1
|
+
import { transformSync } from "@babel/core";
|
|
2
|
+
import sigilPlugin from "@sigil-dev/compiler/babel";
|
|
3
|
+
import {
|
|
4
|
+
computeHash,
|
|
5
|
+
extractStyles,
|
|
6
|
+
serverStyleSnippet,
|
|
7
|
+
} from "@sigil-dev/compiler/css";
|
|
8
|
+
import { log } from "../logger/instance";
|
|
9
|
+
import { rewriteAliases } from "../server/resolver";
|
|
10
|
+
import type { GrimoirePlugin } from "../types";
|
|
11
|
+
import type { ParserPlugin } from "@babel/core";
|
|
12
|
+
|
|
13
|
+
const logger = log.scope("scope");
|
|
14
|
+
let registered = false;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `aliases` (prefix → absolute directory, see projectAliases) are rewritten
|
|
18
|
+
* to file paths in every compiled module: Bun's runtime plugins can't
|
|
19
|
+
* resolve them for static imports.
|
|
20
|
+
*/
|
|
21
|
+
export function registerSSRPlugin(
|
|
22
|
+
plugins: GrimoirePlugin[] = [],
|
|
23
|
+
aliases: Record<string, string> = {},
|
|
24
|
+
) {
|
|
25
|
+
if (registered) return;
|
|
26
|
+
registered = true;
|
|
27
|
+
|
|
28
|
+
Bun.plugin({
|
|
29
|
+
name: "sigil-ssr",
|
|
30
|
+
|
|
31
|
+
setup(build) {
|
|
32
|
+
// loader: "ts" not "tsx" — Babel already consumed the JSX,
|
|
33
|
+
// Bun only needs to strip remaining TypeScript types
|
|
34
|
+
const transpiler = new Bun.Transpiler({ loader: "ts", target: "bun" });
|
|
35
|
+
build.onResolve({ filter: /^@sigil-dev\/runtime($|\/)/ }, (args) => {
|
|
36
|
+
try {
|
|
37
|
+
const resolved = Bun.resolveSync(args.path, process.cwd());
|
|
38
|
+
return { path: resolved.replaceAll("\\", "/") };
|
|
39
|
+
} catch {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
build.onLoad({ filter: /\.tsx?$/ }, async ({ path }) => {
|
|
45
|
+
if (path.includes("index.tsx"))
|
|
46
|
+
logger.debug("[ssr-plugin] onLoad:", path);
|
|
47
|
+
const source = await Bun.file(path).text();
|
|
48
|
+
|
|
49
|
+
// node_modules and .grimoire files are plain TypeScript (no sigil JSX).
|
|
50
|
+
// Still must return { contents, loader } — never return undefined from onLoad.
|
|
51
|
+
if (path.includes("node_modules") || path.includes(".grimoire")) {
|
|
52
|
+
return {
|
|
53
|
+
contents: transpiler.transformSync(source),
|
|
54
|
+
loader: "js" as const,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (process.env.SIGIL_VERBOSE) {
|
|
59
|
+
logger.debug("[sigil-ssr] XFORM:", path);
|
|
60
|
+
}
|
|
61
|
+
// must match the client build, or scoped classes differ per side;
|
|
62
|
+
// files without a <style> block get no scope class at all. The
|
|
63
|
+
// CSS is registered for the renderer, which puts it in <head>.
|
|
64
|
+
const styles = extractStyles(source, computeHash(path), path);
|
|
65
|
+
const hash = styles.css ? computeHash(path) : undefined;
|
|
66
|
+
const sourceNoStyle = styles.code;
|
|
67
|
+
const result = transformSync(sourceNoStyle, {
|
|
68
|
+
configFile: false,
|
|
69
|
+
babelrc: false,
|
|
70
|
+
parserOpts: {
|
|
71
|
+
// The jsx plugin goes on .tsx files only.
|
|
72
|
+
//
|
|
73
|
+
// With it enabled everywhere, Babel parses `<R>(x: R) => x`
|
|
74
|
+
// as the opening of a JSX element and fails with
|
|
75
|
+
// "Unexpected token `>`". So a generic arrow function in a
|
|
76
|
+
// plain .ts file — completely ordinary TypeScript — could
|
|
77
|
+
// not be compiled. Found while writing the Drizzle adapter
|
|
78
|
+
// recipe, where `transaction: <R>(db, fn) => ...` is the
|
|
79
|
+
// form that removes an `as any`.
|
|
80
|
+
//
|
|
81
|
+
// A .tsx file is genuinely ambiguous (`<R>(x) => x` is
|
|
82
|
+
// JSX there), so .tsx keeps both and users write `<R,>`.
|
|
83
|
+
plugins: path.endsWith(".tsx")
|
|
84
|
+
? (["typescript", "jsx"] as ParserPlugin[])
|
|
85
|
+
: (["typescript"] as ParserPlugin[]),
|
|
86
|
+
},
|
|
87
|
+
plugins: [[sigilPlugin, { mode: "ssr", hash }]],
|
|
88
|
+
filename: path,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
let contents = rewriteAliases(
|
|
92
|
+
transpiler.transformSync(result?.code ?? ""),
|
|
93
|
+
aliases,
|
|
94
|
+
);
|
|
95
|
+
if (styles.css && hash) {
|
|
96
|
+
contents = serverStyleSnippet(hash, styles.css) + contents;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
for (const plugin of plugins) {
|
|
100
|
+
if (plugin.transform)
|
|
101
|
+
contents = (await plugin.transform(contents, path)) ?? contents;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { contents, loader: "js" as const };
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
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
|
+
}
|