@sigil-dev/grimoire 0.9.24 → 0.9.26

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.
@@ -11,12 +11,13 @@
11
11
  * for tests. `prerenderSite` is the orchestration.
12
12
  */
13
13
 
14
- import { cp, mkdir, rm, writeFile } from "node:fs/promises";
14
+ import { copyFile, cp, mkdir, rm, writeFile } from "node:fs/promises";
15
15
  import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
16
16
  import type { AdapterContext } from "../adapters/types";
17
17
  import { normalizeBase, stripBase, withBase } from "../base";
18
18
  import { matchRoute } from "../routing/router";
19
19
  import type { RouteFile, RouteTree } from "../routing/scanner";
20
+ import { publicCssMap } from "./public-assets";
20
21
  import { RESERVED_EXPORTS } from "./reserved";
21
22
 
22
23
  // ---------------------------------------------------------------------------
@@ -357,7 +358,8 @@ export async function prerenderSite(
357
358
  files.set(dataFileFor(item.path), await nav.text());
358
359
  }
359
360
  if (crawl) {
360
- for (const link of extractLinks(html, origin, base)) follow(link, item.path);
361
+ for (const link of extractLinks(html, origin, base))
362
+ follow(link, item.path);
361
363
  }
362
364
  };
363
365
 
@@ -382,7 +384,9 @@ export async function prerenderSite(
382
384
 
383
385
  // ---- not found ------------------------------------------------------
384
386
  try {
385
- const nf = await app.fetch(new Request(`${origin}${withBase("/__grimoire_not_found__", base)}`));
387
+ const nf = await app.fetch(
388
+ new Request(`${origin}${withBase("/__grimoire_not_found__", base)}`),
389
+ );
386
390
  if (
387
391
  nf.status === 404 &&
388
392
  (nf.headers.get("content-type") ?? "").includes("text/html")
@@ -412,6 +416,14 @@ export async function prerenderSite(
412
416
  if (e.code !== "ENOENT") throw e;
413
417
  },
414
418
  );
419
+ // The pages link /style.<hash>.css; a static host has only the plain name.
420
+ // Both are written, so a page that still asks for the plain one works too.
421
+ for (const [plain, hashed] of Object.entries(publicCssMap(false, root))) {
422
+ const from = stripBase(plain, base);
423
+ const to = stripBase(hashed, base);
424
+ if (from && to)
425
+ await copyFile(join(root, "public", from), join(outDir, to));
426
+ }
415
427
  for (const [rel, body] of files) {
416
428
  const target = join(outDir, rel);
417
429
  await mkdir(dirname(target), { recursive: true });
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Content-hashed URLs for the stylesheets in public/.
3
+ *
4
+ * `/style.css` is rewritten to `/style.<hash>.css` in rendered HTML, so the
5
+ * browser can cache it for a year (`isContentAddressed` already treats that
6
+ * shape as immutable) and a deploy that edits the file is still picked up,
7
+ * because the URL changes with it.
8
+ *
9
+ * The files on disk keep their plain names. The server maps a hashed URL back
10
+ * to the file, and the static build writes a hashed copy beside each one.
11
+ */
12
+ import { createHash } from "node:crypto";
13
+ import { readdirSync, readFileSync, statSync } from "node:fs";
14
+ import { join, relative, sep } from "node:path";
15
+ import { withBase } from "../base";
16
+
17
+ /** 16 hex chars of sha256: the same width the ETag uses, so the two always agree. */
18
+ const HASH_LENGTH = 16;
19
+
20
+ export function contentHash(bytes: Uint8Array | ArrayBuffer): string {
21
+ return createHash("sha256")
22
+ .update(bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : bytes)
23
+ .digest("hex")
24
+ .slice(0, HASH_LENGTH);
25
+ }
26
+
27
+ const fileHashes = new Map<string, { stamp: string; hash: string }>();
28
+
29
+ /** The content hash of a file on disk, re-read only when its size or mtime changes. */
30
+ export function hashFileSync(path: string): string | null {
31
+ let stat: ReturnType<typeof statSync>;
32
+ try {
33
+ stat = statSync(path);
34
+ } catch {
35
+ return null;
36
+ }
37
+ if (!stat.isFile()) return null;
38
+ const stamp = `${stat.size}:${stat.mtimeMs}`;
39
+ const hit = fileHashes.get(path);
40
+ if (hit && hit.stamp === stamp) return hit.hash;
41
+ const hash = contentHash(readFileSync(path));
42
+ fileHashes.set(path, { stamp, hash });
43
+ return hash;
44
+ }
45
+
46
+ /** `/a/style.css` → `/a/style.<hash>.css`. */
47
+ export function hashedName(path: string, hash: string): string {
48
+ return path.replace(/\.css$/i, `.${hash}.css`);
49
+ }
50
+
51
+ const HASHED_CSS = new RegExp(`^(.+)\\.([0-9a-f]{${HASH_LENGTH}})\\.css$`, "i");
52
+
53
+ /** `/a/style.<hash>.css` → its plain name and the hash it claims, or null. */
54
+ export function splitHashedCss(
55
+ pathname: string,
56
+ ): { plain: string; hash: string } | null {
57
+ const m = HASHED_CSS.exec(pathname);
58
+ return m ? { plain: `${m[1]}.css`, hash: m[2]!.toLowerCase() } : null;
59
+ }
60
+
61
+ function listCss(dir: string, out: string[] = []): string[] {
62
+ let entries: import("node:fs").Dirent[];
63
+ try {
64
+ entries = readdirSync(dir, { withFileTypes: true });
65
+ } catch {
66
+ return out;
67
+ }
68
+ for (const e of entries) {
69
+ const full = join(dir, e.name);
70
+ if (e.isDirectory()) listCss(full, out);
71
+ else if (e.isFile() && /\.css$/i.test(e.name)) out.push(full);
72
+ }
73
+ return out;
74
+ }
75
+
76
+ let cached: { dir: string; map: Record<string, string> } | null = null;
77
+
78
+ /**
79
+ * Every stylesheet in public/ as `{ "/style.css": "/style.<hash>.css" }`, under
80
+ * the configured base.
81
+ *
82
+ * Built once per process outside dev: a deployed public/ does not change. In
83
+ * dev it is rebuilt on each call, and cheaply, since file hashes are cached
84
+ * by size and mtime.
85
+ */
86
+ export function publicCssMap(
87
+ dev = false,
88
+ root: string = process.cwd(),
89
+ ): Record<string, string> {
90
+ const dir = join(root, "public");
91
+ if (!dev && cached?.dir === dir) return cached.map;
92
+ const map: Record<string, string> = {};
93
+ for (const file of listCss(dir)) {
94
+ const url = `/${relative(dir, file).split(sep).join("/")}`;
95
+ // a file already named for its hash is left alone
96
+ if (splitHashedCss(url)) continue;
97
+ const hash = hashFileSync(file);
98
+ if (hash) map[withBase(url)] = withBase(hashedName(url, hash));
99
+ }
100
+ if (!dev) cached = { dir, map };
101
+ return map;
102
+ }
103
+
104
+ // inline scripts, styles and templates may contain "<link" inside strings
105
+ const RAW_BLOCK = /<(script|style|template)\b[\s\S]*?<\/\1>/gi;
106
+ const LINK_TAG = /<link\b[^>]*>/gi;
107
+ const HREF = /(\shref\s*=\s*)(["'])([^"']*)\2/i;
108
+
109
+ /**
110
+ * Point `<link rel="stylesheet">` tags that name a stylesheet in public/ at its
111
+ * hashed URL. Anything else is left alone: other hosts, a `?v=` the author
112
+ * already manages, files that are not in public/.
113
+ */
114
+ export function hashPublicStylesheets(
115
+ html: string,
116
+ map: Record<string, string>,
117
+ ): string {
118
+ if (!html.includes("<link")) return html;
119
+ const tag = (t: string): string => {
120
+ if (!/\brel\s*=\s*["']?stylesheet\b/i.test(t)) return t;
121
+ return t.replace(HREF, (whole, pre: string, q: string, href: string) => {
122
+ const hashed = map[href];
123
+ return hashed ? `${pre}${q}${hashed}${q}` : whole;
124
+ });
125
+ };
126
+ let out = "";
127
+ let last = 0;
128
+ for (const m of html.matchAll(RAW_BLOCK)) {
129
+ out += html.slice(last, m.index).replace(LINK_TAG, tag) + m[0];
130
+ last = m.index + m[0].length;
131
+ }
132
+ return out + html.slice(last).replace(LINK_TAG, tag);
133
+ }
package/test/env.test.ts CHANGED
@@ -159,9 +159,9 @@ describe("$env modules in builds", () => {
159
159
  'import { db } from "$lib/db";\nimport x from "other";',
160
160
  { $lib: join(dir, "lib") },
161
161
  );
162
- // forward slashes even on Windows: the path lands in a string literal
163
- expect(out).toContain(
164
- `from "${join(dir, "lib/db.ts").replaceAll("\\", "/")}"`,
162
+ // forward slashes even on Windows: the path lands in a string literal
163
+ expect(out).toContain(
164
+ `from "${join(dir, "lib/db.ts").replaceAll("\\", "/")}"`,
165
165
  );
166
166
  expect(out).toContain('from "other"');
167
167
  } finally {
@@ -20,6 +20,7 @@ import {
20
20
  markStatic,
21
21
  prerenderSite,
22
22
  } from "../src/server/prerender";
23
+ import { publicCssMap } from "../src/server/public-assets";
23
24
 
24
25
  describe("fillPattern", () => {
25
26
  test("static, param, optional and rest segments", () => {
@@ -201,12 +202,33 @@ describe("prerenderSite", () => {
201
202
  expect(await exists(join(out, "404.html"))).toBe(true);
202
203
  });
203
204
 
205
+ test("writes a hashed copy of each public stylesheet beside the original", async () => {
206
+ const t = await setup({
207
+ routes: [route("/", "page")],
208
+ pages: { "/": () => html("home") },
209
+ });
210
+ await writeFile(join(t.root, "public", "style.css"), "a{}");
211
+ await t.run();
212
+ const out = join(t.root, "build");
213
+ const hashed = publicCssMap(false, t.root)["/style.css"]!;
214
+ expect(hashed).toMatch(/^\/style\.[0-9a-f]{16}\.css$/);
215
+ expect(await readFile(join(out, "style.css"), "utf8")).toBe("a{}");
216
+ expect(await readFile(join(out, hashed), "utf8")).toBe("a{}");
217
+ });
218
+
204
219
  test("under a base: requests and links carry it, files do not", async () => {
205
220
  const t = await setup({
206
221
  base: "/repo",
207
- routes: [route("/", "page"), route("/about", "page"), route("/gone", "page")],
222
+ routes: [
223
+ route("/", "page"),
224
+ route("/about", "page"),
225
+ route("/gone", "page"),
226
+ ],
208
227
  pages: {
209
- "/": () => html('<a href="/repo/about">about</a><a href="/about">unprefixed</a>'),
228
+ "/": () =>
229
+ html(
230
+ '<a href="/repo/about">about</a><a href="/about">unprefixed</a>',
231
+ ),
210
232
  "/about": () => html("about"),
211
233
  "/gone": () => html("never linked, so only a seed finds it"),
212
234
  },
@@ -217,7 +239,9 @@ describe("prerenderSite", () => {
217
239
  const out = join(t.root, "build");
218
240
  expect(await exists(join(out, "about/index.html"))).toBe(true);
219
241
  expect(await exists(join(out, "repo"))).toBe(false);
220
- expect(await readFile(join(out, "about/__data.json"), "utf8")).toContain("pattern");
242
+ expect(await readFile(join(out, "about/__data.json"), "utf8")).toContain(
243
+ "pattern",
244
+ );
221
245
  expect(await exists(join(out, "404.html"))).toBe(true);
222
246
  });
223
247
 
@@ -243,7 +267,8 @@ describe("prerenderSite", () => {
243
267
  matchers: { int: (v) => /^\d+$/.test(v) },
244
268
  modules: { [potion.filePath]: { entries: () => [{ index: "3" }] } },
245
269
  pages: {
246
- "/": () => html('<a href="/potions/7">p</a><a href="/potions/x">bad</a>'),
270
+ "/": () =>
271
+ html('<a href="/potions/7">p</a><a href="/potions/x">bad</a>'),
247
272
  "/potions/3": () => html("three"),
248
273
  "/potions/7": () => html("seven"),
249
274
  },
@@ -0,0 +1,108 @@
1
+ import { afterEach, describe, expect, test } from "bun:test";
2
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { isContentAddressed } from "../src/server/asset-cache.ts";
6
+ import {
7
+ contentHash,
8
+ hashedName,
9
+ hashPublicStylesheets,
10
+ publicCssMap,
11
+ splitHashedCss,
12
+ } from "../src/server/public-assets.ts";
13
+
14
+ let dirs: string[] = [];
15
+ afterEach(async () => {
16
+ for (const d of dirs) await rm(d, { recursive: true, force: true });
17
+ dirs = [];
18
+ });
19
+
20
+ async function project(files: Record<string, string>): Promise<string> {
21
+ const root = await mkdtemp(join(tmpdir(), "sigil-public-"));
22
+ dirs.push(root);
23
+ for (const [rel, body] of Object.entries(files)) {
24
+ const target = join(root, "public", rel);
25
+ await mkdir(join(target, ".."), { recursive: true });
26
+ await writeFile(target, body);
27
+ }
28
+ return root;
29
+ }
30
+
31
+ describe("hashed names", () => {
32
+ test("round-trips and is read as content-addressed", () => {
33
+ const hash = contentHash(new TextEncoder().encode("body{}"));
34
+ expect(hash).toMatch(/^[0-9a-f]{16}$/);
35
+ const url = hashedName("/css/app.css", hash);
36
+ expect(url).toBe(`/css/app.${hash}.css`);
37
+ expect(splitHashedCss(url)).toEqual({ plain: "/css/app.css", hash });
38
+ expect(isContentAddressed(url, "")).toBe(true);
39
+ });
40
+
41
+ test("a plain or short-hash name is not split", () => {
42
+ expect(splitHashedCss("/style.css")).toBeNull();
43
+ expect(splitHashedCss("/style.abc123.css")).toBeNull();
44
+ });
45
+ });
46
+
47
+ describe("publicCssMap", () => {
48
+ test("maps every stylesheet, nested ones too, by content", async () => {
49
+ const root = await project({
50
+ "style.css": "a{}",
51
+ "css/theme.css": "b{}",
52
+ "logo.svg": "<svg/>",
53
+ });
54
+ const map = publicCssMap(true, root);
55
+ expect(Object.keys(map).sort()).toEqual(["/css/theme.css", "/style.css"]);
56
+ expect(map["/style.css"]).toBe(
57
+ `/style.${contentHash(new TextEncoder().encode("a{}"))}.css`,
58
+ );
59
+ });
60
+
61
+ test("the URL follows the content", async () => {
62
+ const root = await project({ "style.css": "a{}" });
63
+ const before = publicCssMap(true, root)["/style.css"];
64
+ await writeFile(join(root, "public", "style.css"), "a{color:red}");
65
+ expect(publicCssMap(true, root)["/style.css"]).not.toBe(before);
66
+ });
67
+
68
+ test("a file already named for its hash is left alone", async () => {
69
+ const root = await project({ "style.0123456789abcdef.css": "a{}" });
70
+ expect(publicCssMap(true, root)).toEqual({});
71
+ });
72
+
73
+ test("no public/ is an empty map", async () => {
74
+ const root = await mkdtemp(join(tmpdir(), "sigil-public-"));
75
+ dirs.push(root);
76
+ expect(publicCssMap(true, root)).toEqual({});
77
+ });
78
+ });
79
+
80
+ describe("hashPublicStylesheets", () => {
81
+ const map = { "/style.css": "/style.0123456789abcdef.css" };
82
+
83
+ test("rewrites a public stylesheet link, whatever the attribute order", () => {
84
+ expect(
85
+ hashPublicStylesheets('<link rel="stylesheet" href="/style.css">', map),
86
+ ).toBe('<link rel="stylesheet" href="/style.0123456789abcdef.css">');
87
+ expect(
88
+ hashPublicStylesheets("<link href='/style.css' rel=stylesheet />", map),
89
+ ).toBe("<link href='/style.0123456789abcdef.css' rel=stylesheet />");
90
+ });
91
+
92
+ test("leaves other links, hosts, queries and unknown files alone", () => {
93
+ for (const tag of [
94
+ '<link rel="icon" href="/style.css">',
95
+ '<link rel="stylesheet" href="https://cdn.example/style.css">',
96
+ '<link rel="stylesheet" href="/style.css?v=2">',
97
+ '<link rel="stylesheet" href="/other.css">',
98
+ ]) {
99
+ expect(hashPublicStylesheets(tag, map)).toBe(tag);
100
+ }
101
+ });
102
+
103
+ test("does not touch a link inside a script or template", () => {
104
+ const html =
105
+ '<script>const s=\'<link rel="stylesheet" href="/style.css">\'</script>';
106
+ expect(hashPublicStylesheets(html, map)).toBe(html);
107
+ });
108
+ });
@@ -0,0 +1,36 @@
1
+ import { expect, test } from "bun:test";
2
+ import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { matchRoute } from "../src/routing/router";
6
+ import { scanRoutes } from "../src/routing/scanner";
7
+
8
+ // (app)/+layout.tsx has the URL "/" once the group is stripped. Matched by URL
9
+ // it wrapped every page, login included; a layout covers its folder on disk.
10
+ test("a layout inside a route group wraps only that group", async () => {
11
+ const root = mkdtempSync(join(tmpdir(), "grimoire-groups-"));
12
+ const routes = join(root, "src/routes");
13
+ for (const f of [
14
+ "+layout.tsx",
15
+ "(app)/+layout.tsx",
16
+ "(app)/plan/+page.tsx",
17
+ "(app)/+page.tsx",
18
+ "login/+page.tsx",
19
+ ]) {
20
+ mkdirSync(join(routes, f, ".."), { recursive: true });
21
+ writeFileSync(join(routes, f), "export default () => null;");
22
+ }
23
+ const tree = await scanRoutes(routes, root);
24
+ const layoutsFor = (path: string) =>
25
+ matchRoute(tree, new URL(path, "http://x"))!.layouts.map((l) => l.clientPath);
26
+
27
+ expect(layoutsFor("/plan")).toEqual([
28
+ "/src/routes/+layout.tsx",
29
+ "/src/routes/(app)/+layout.tsx",
30
+ ]);
31
+ expect(layoutsFor("/")).toEqual([
32
+ "/src/routes/+layout.tsx",
33
+ "/src/routes/(app)/+layout.tsx",
34
+ ]);
35
+ expect(layoutsFor("/login")).toEqual(["/src/routes/+layout.tsx"]);
36
+ });