@sigil-dev/grimoire 0.9.23 → 0.9.24

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/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "module": "index.ts",
12
12
  "type": "module",
13
13
  "private": false,
14
- "version": "0.9.23",
14
+ "version": "0.9.24",
15
15
  "exports": {
16
16
  ".": "./index.ts",
17
17
  "./server": "./server.ts",
@@ -35,8 +35,8 @@
35
35
  "test": "bun test"
36
36
  },
37
37
  "dependencies": {
38
- "@sigil-dev/compiler": "0.9.23",
39
- "@sigil-dev/runtime": "0.9.23",
38
+ "@sigil-dev/compiler": "0.9.24",
39
+ "@sigil-dev/runtime": "0.9.24",
40
40
  "vite": "^8.0.16"
41
41
  },
42
42
  "peerDependencies": {
@@ -30,6 +30,7 @@ import {
30
30
  } from "../base";
31
31
  import { RESERVED_EXPORTS } from "./reserved";
32
32
  import { applyRewriters } from "./rewrite";
33
+ import { shadowedRouteMessage, shadowedRoutes } from "./plugin-routes";
33
34
  import { drainAfterResponse, withAfterResponse } from "./after-response.ts";
34
35
  import { CACHE_CONTROL, cacheControlFor } from "./asset-cache";
35
36
  import { buildProject, rebuildClient } from "./build";
@@ -398,6 +399,10 @@ export async function createApp(config: GrimoireConfig = {}): Promise<GrimoireAp
398
399
  logger.warn(`failed to load param matcher "${pm.name}":`, e);
399
400
  }
400
401
  }
402
+ for (const s of shadowedRoutes(plugins, tree, paramMatcherFns)) {
403
+ logger.warn(shadowedRouteMessage(s));
404
+ }
405
+
401
406
  // Load hooks.server.ts
402
407
  const {
403
408
  handle: appHandle,
@@ -0,0 +1,38 @@
1
+ import { matchRoute } from "../routing/router";
2
+ import type { RouteTree } from "../routing/scanner";
3
+ import type { GrimoirePlugin } from "../types";
4
+
5
+ export interface ShadowedRoute {
6
+ plugin: string;
7
+ path: string;
8
+ /** The app's route file that never sees requests for `path`. */
9
+ file: string;
10
+ }
11
+
12
+ /**
13
+ * App routes that a plugin's `handle` answers first, so they never run for
14
+ * those paths. A warning rather than an error: the overlap can be deliberate.
15
+ */
16
+ export function shadowedRoutes(
17
+ plugins: GrimoirePlugin[],
18
+ tree: RouteTree,
19
+ matchers?: Record<string, (value: string) => boolean>,
20
+ ): ShadowedRoute[] {
21
+ const found: ShadowedRoute[] = [];
22
+ for (const plugin of plugins) {
23
+ for (const path of plugin.routes ?? []) {
24
+ const hit = matchRoute(tree, new URL(path, "http://localhost"), matchers);
25
+ if (hit && !hit.notFound) {
26
+ found.push({ plugin: plugin.name, path, file: hit.route.clientPath });
27
+ }
28
+ }
29
+ }
30
+ return found;
31
+ }
32
+
33
+ export function shadowedRouteMessage(s: ShadowedRoute): string {
34
+ return (
35
+ `${s.file} never runs for ${s.path}: the "${s.plugin}" plugin answers that path first. ` +
36
+ "Move the route, or if it was meant to add to what the plugin does, do that in hooks.server.ts instead."
37
+ );
38
+ }
@@ -92,7 +92,11 @@ export function rewriteAliases(
92
92
  const file = resolveFile(
93
93
  join(aliases[prefix]!, specifier.slice(prefix.length)),
94
94
  );
95
- return file ? `${keyword}${quote}${file}${quote}` : match;
95
+ // Forward slashes: this lands inside a JS string literal, where a
96
+ // Windows path's backslashes would be read as escapes (\P → P).
97
+ return file
98
+ ? `${keyword}${quote}${file.replaceAll("\\", "/")}${quote}`
99
+ : match;
96
100
  },
97
101
  );
98
102
  }
package/src/types.ts CHANGED
@@ -16,6 +16,7 @@ export interface CliCommand {
16
16
  alias(alias: string): CliCommand;
17
17
  option(flags: string, description?: string, defaultValue?: unknown): CliCommand;
18
18
  argument(name: string, description?: string): CliCommand;
19
+ addHelpText(position: "before" | "after", text: string): CliCommand;
19
20
  action(fn: (...args: never[]) => unknown): CliCommand;
20
21
  }
21
22
 
@@ -194,6 +195,12 @@ export interface GrimoirePlugin {
194
195
  * the app's own handle sees whatever the plugins put on `locals`.
195
196
  */
196
197
  handle?: Handle;
198
+ /**
199
+ * Paths `handle` answers without calling `resolve`, such as auth's
200
+ * `/auth/github/callback`. Listing them lets Grimoire warn at startup when
201
+ * an app route sits on one and would never run.
202
+ */
203
+ routes?: readonly string[];
197
204
 
198
205
  // request pipeline
199
206
  onRequest?(
package/test/env.test.ts CHANGED
@@ -159,7 +159,10 @@ 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
- expect(out).toContain(`from "${join(dir, "lib/db.ts")}"`);
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
+ );
163
166
  expect(out).toContain('from "other"');
164
167
  } finally {
165
168
  await rm(dir, { recursive: true, force: true });
@@ -0,0 +1,42 @@
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 { scanRoutes } from "../src/routing/scanner";
6
+ import { shadowedRouteMessage, shadowedRoutes } from "../src/server/plugin-routes";
7
+
8
+ async function treeWith(files: string[]) {
9
+ const root = mkdtempSync(join(tmpdir(), "grimoire-shadow-"));
10
+ const routes = join(root, "src/routes");
11
+ for (const f of files) {
12
+ mkdirSync(join(routes, f, ".."), { recursive: true });
13
+ writeFileSync(join(routes, f), "export default () => null;");
14
+ }
15
+ return scanRoutes(routes, root);
16
+ }
17
+
18
+ test("an app route on a path a plugin answers is reported, by file", async () => {
19
+ const tree = await treeWith([
20
+ "+page.tsx",
21
+ "auth/github/callback/+page.tsx",
22
+ "auth/[provider]/+page.tsx",
23
+ ]);
24
+ const found = shadowedRoutes(
25
+ [
26
+ { name: "tome", routes: ["/auth/github", "/auth/github/callback"] },
27
+ { name: "quiet" },
28
+ ],
29
+ tree,
30
+ );
31
+ expect(found.map((f) => [f.plugin, f.path])).toEqual([
32
+ ["tome", "/auth/github"],
33
+ ["tome", "/auth/github/callback"],
34
+ ]);
35
+ expect(found[1]!.file).toContain("auth/github/callback/+page.tsx");
36
+ expect(shadowedRouteMessage(found[1]!)).toContain("hooks.server.ts");
37
+ });
38
+
39
+ test("no overlap, no warning", async () => {
40
+ const tree = await treeWith(["+page.tsx", "login/+page.tsx"]);
41
+ expect(shadowedRoutes([{ name: "tome", routes: ["/auth/github"] }], tree)).toEqual([]);
42
+ });
@@ -0,0 +1,23 @@
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 { rewriteAliases } from "../src/server/resolver";
6
+
7
+ // On Windows the resolved file is D:\Projects\app\src\lib\x.ts. Written into a
8
+ // string literal as is, every backslash is an escape and the import becomes
9
+ // "D:Projectsappsrclibx.ts". The specifier must use forward slashes.
10
+ test("aliased imports are written with forward slashes", () => {
11
+ const root = mkdtempSync(join(tmpdir(), "grimoire-alias-"));
12
+ mkdirSync(join(root, "src/lib/server"), { recursive: true });
13
+ writeFileSync(join(root, "src/lib/server/auth.ts"), "export const auth = 1;");
14
+
15
+ const out = rewriteAliases(
16
+ 'import { auth } from "$lib/server/auth";',
17
+ { $lib: join(root, "src/lib") },
18
+ );
19
+
20
+ const specifier = out.match(/from "([^"]+)"/)?.[1] ?? "";
21
+ expect(specifier).not.toContain("\\");
22
+ expect(specifier.endsWith("/src/lib/server/auth.ts")).toBe(true);
23
+ });