toiljs 0.0.11 → 0.0.14

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.
Files changed (120) hide show
  1. package/README.md +3 -1
  2. package/build/cli/.tsbuildinfo +1 -1
  3. package/build/cli/configure.js +10 -4
  4. package/build/cli/create.js +58 -30
  5. package/build/cli/diagnostics.d.ts +55 -0
  6. package/build/cli/diagnostics.js +333 -0
  7. package/build/cli/doctor.d.ts +6 -0
  8. package/build/cli/doctor.js +249 -0
  9. package/build/cli/index.js +26 -0
  10. package/build/cli/proc.d.ts +5 -0
  11. package/build/cli/proc.js +20 -0
  12. package/build/cli/ui.d.ts +1 -0
  13. package/build/cli/ui.js +1 -0
  14. package/build/cli/update.d.ts +7 -0
  15. package/build/cli/update.js +117 -0
  16. package/build/cli/updates.d.ts +10 -0
  17. package/build/cli/updates.js +45 -0
  18. package/build/client/.tsbuildinfo +1 -1
  19. package/build/client/dev/error-overlay.js +1 -1
  20. package/build/client/head/metadata.js +3 -1
  21. package/build/client/index.d.ts +5 -1
  22. package/build/client/index.js +2 -0
  23. package/build/client/navigation/navigation.js +1 -1
  24. package/build/client/routing/Router.js +2 -2
  25. package/build/client/search/search.d.ts +26 -0
  26. package/build/client/search/search.js +101 -0
  27. package/build/client/search/use-page-search.d.ts +8 -0
  28. package/build/client/search/use-page-search.js +21 -0
  29. package/build/compiler/.tsbuildinfo +1 -1
  30. package/build/compiler/generate.js +33 -24
  31. package/build/compiler/index.d.ts +2 -0
  32. package/build/compiler/index.js +1 -0
  33. package/build/compiler/pages.d.ts +8 -0
  34. package/build/compiler/pages.js +37 -0
  35. package/build/compiler/plugin.js +3 -1
  36. package/build/compiler/prerender.d.ts +1 -0
  37. package/build/compiler/prerender.js +11 -5
  38. package/build/compiler/seo.js +10 -3
  39. package/build/io/.tsbuildinfo +1 -1
  40. package/examples/basic/client/components/Header.tsx +43 -41
  41. package/examples/basic/client/components/HoneycombBackground.tsx +223 -230
  42. package/examples/basic/client/public/index.html +18 -16
  43. package/examples/basic/client/routes/(legal)/privacy.tsx +18 -19
  44. package/examples/basic/client/routes/(legal)/terms.tsx +15 -16
  45. package/examples/basic/client/routes/about.tsx +21 -22
  46. package/examples/basic/client/routes/blog/[id].tsx +26 -18
  47. package/examples/basic/client/routes/features/actions.tsx +67 -67
  48. package/examples/basic/client/routes/features/error/index.tsx +27 -27
  49. package/examples/basic/client/routes/features/head.tsx +38 -38
  50. package/examples/basic/client/routes/features/index.tsx +83 -75
  51. package/examples/basic/client/routes/features/realtime.tsx +34 -32
  52. package/examples/basic/client/routes/features/script.tsx +31 -31
  53. package/examples/basic/client/routes/features/seo.tsx +39 -39
  54. package/examples/basic/client/routes/features/template/index.tsx +20 -20
  55. package/examples/basic/client/routes/features/template/template.tsx +16 -18
  56. package/examples/basic/client/routes/gallery/@modal/(.)photo/[id].tsx +23 -23
  57. package/examples/basic/client/routes/gallery/index.tsx +42 -42
  58. package/examples/basic/client/routes/gallery/photo/[id].tsx +18 -18
  59. package/examples/basic/client/routes/get-started.tsx +157 -84
  60. package/examples/basic/client/routes/index.tsx +137 -96
  61. package/examples/basic/client/routes/loader-demo/index.tsx +59 -52
  62. package/examples/basic/client/routes/search.tsx +61 -0
  63. package/examples/basic/client/routes/test.tsx +7 -8
  64. package/examples/basic/client/styles/main.css +624 -552
  65. package/package.json +2 -2
  66. package/presets/eslint.js +10 -3
  67. package/src/cli/configure.ts +363 -353
  68. package/src/cli/create.ts +563 -530
  69. package/src/cli/diagnostics.ts +421 -0
  70. package/src/cli/doctor.ts +318 -0
  71. package/src/cli/features.ts +166 -160
  72. package/src/cli/index.ts +242 -211
  73. package/src/cli/proc.ts +30 -0
  74. package/src/cli/ui.ts +111 -103
  75. package/src/cli/update.ts +150 -0
  76. package/src/cli/updates.ts +69 -0
  77. package/src/client/components/Image.tsx +91 -89
  78. package/src/client/dev/error-overlay.tsx +193 -197
  79. package/src/client/head/metadata.ts +94 -92
  80. package/src/client/index.ts +79 -64
  81. package/src/client/navigation/Link.tsx +94 -100
  82. package/src/client/navigation/navigation.ts +215 -218
  83. package/src/client/routing/Router.tsx +210 -193
  84. package/src/client/routing/hooks.ts +110 -114
  85. package/src/client/routing/lazy.ts +77 -81
  86. package/src/client/search/search.ts +189 -0
  87. package/src/client/search/use-page-search.ts +73 -0
  88. package/src/compiler/config.ts +173 -171
  89. package/src/compiler/fonts.ts +89 -87
  90. package/src/compiler/generate.ts +45 -27
  91. package/src/compiler/image-report.ts +88 -85
  92. package/src/compiler/index.ts +2 -0
  93. package/src/compiler/pages.ts +70 -0
  94. package/src/compiler/plugin.ts +51 -47
  95. package/src/compiler/prerender.ts +152 -130
  96. package/src/compiler/routes.ts +132 -131
  97. package/src/compiler/seo.ts +381 -356
  98. package/src/compiler/vite.ts +155 -145
  99. package/src/io/FastSet.ts +99 -96
  100. package/test/configure.test.ts +94 -90
  101. package/test/doctor.test.ts +140 -0
  102. package/test/dom/Image.test.tsx +73 -46
  103. package/test/dom/Script.test.tsx +48 -45
  104. package/test/dom/action.test.tsx +146 -129
  105. package/test/dom/error-overlay.test.tsx +1 -1
  106. package/test/dom/loader.test.tsx +2 -2
  107. package/test/dom/revalidate.test.tsx +1 -1
  108. package/test/dom/route-head.test.tsx +1 -2
  109. package/test/dom/router-loading.test.tsx +1 -1
  110. package/test/dom/slot.test.tsx +131 -109
  111. package/test/dom/view-transitions.test.tsx +53 -51
  112. package/test/features.test.ts +149 -142
  113. package/test/fonts.test.ts +28 -26
  114. package/test/head.test.ts +45 -35
  115. package/test/metadata.test.ts +42 -41
  116. package/test/pages.test.ts +105 -0
  117. package/test/prerender.test.ts +54 -46
  118. package/test/search.test.ts +114 -0
  119. package/test/seo.test.ts +30 -8
  120. package/test/update.test.ts +44 -0
@@ -1,130 +1,152 @@
1
- import { createRequire } from 'node:module';
2
- import fs from 'node:fs';
3
- import path from 'node:path';
4
- import { pathToFileURL } from 'node:url';
5
-
6
- import type * as TS from 'typescript';
7
- import type { Plugin } from 'vite';
8
-
9
- import { type ResolvedToilConfig } from './config.js';
10
- import { scanRoutes } from './routes.js';
11
- import { injectSeoHtml, routeSeo } from './seo.js';
12
-
13
- type Ts = typeof TS;
14
-
15
- /** Loads the project's TypeScript (used to read each route's static `metadata`), or `null` if absent. */
16
- async function loadTypeScript(root: string): Promise<Ts | null> {
17
- try {
18
- const resolved = createRequire(path.join(root, 'package.json')).resolve('typescript');
19
- const mod = (await import(pathToFileURL(resolved).href)) as { default?: Ts } & Ts;
20
- return mod.default ?? mod;
21
- } catch {
22
- return null;
23
- }
24
- }
25
-
26
- /** Marks an AST node that isn't a static literal (so its value can't be baked at build). */
27
- const UNRESOLVED = Symbol('unresolved');
28
-
29
- /** Statically evaluates a literal expression node to a JS value, or `UNRESOLVED` if it isn't one. */
30
- function evalNode(ts: Ts, node: TS.Expression): unknown {
31
- if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
32
- if (ts.isNumericLiteral(node)) return Number(node.text);
33
- if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
34
- if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
35
- if (node.kind === ts.SyntaxKind.NullKeyword) return null;
36
- if (ts.isArrayLiteralExpression(node)) {
37
- const out: unknown[] = [];
38
- for (const el of node.elements) {
39
- const value = evalNode(ts, el);
40
- if (value === UNRESOLVED) return UNRESOLVED;
41
- out.push(value);
42
- }
43
- return out;
44
- }
45
- if (ts.isObjectLiteralExpression(node)) return evalObject(ts, node);
46
- return UNRESOLVED;
47
- }
48
-
49
- /** Evaluates an object literal to a plain object, skipping any property that isn't a static literal. */
50
- function evalObject(ts: Ts, node: TS.ObjectLiteralExpression): Record<string, unknown> {
51
- const obj: Record<string, unknown> = {};
52
- for (const prop of node.properties) {
53
- if (!ts.isPropertyAssignment(prop)) continue;
54
- const key = ts.isIdentifier(prop.name)
55
- ? prop.name.text
56
- : ts.isStringLiteral(prop.name)
57
- ? prop.name.text
58
- : null;
59
- if (key === null) continue;
60
- const value = evalNode(ts, prop.initializer);
61
- if (value !== UNRESOLVED) obj[key] = value;
62
- }
63
- return obj;
64
- }
65
-
66
- /**
67
- * Extracts a route's `export const metadata = { … }` if it's a static object literal, returning the
68
- * statically-evaluable subset (dynamic `generateMetadata` and computed values are skipped). `null`
69
- * when the file has no static metadata.
70
- */
71
- export function extractStaticMetadata(ts: Ts, filePath: string): Record<string, unknown> | null {
72
- let source: string;
73
- try {
74
- source = fs.readFileSync(filePath, 'utf8');
75
- } catch {
76
- return null;
77
- }
78
- const sf = ts.createSourceFile(filePath, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
79
- for (const stmt of sf.statements) {
80
- if (!ts.isVariableStatement(stmt)) continue;
81
- if (!stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) continue;
82
- for (const decl of stmt.declarationList.declarations) {
83
- if (
84
- ts.isIdentifier(decl.name) &&
85
- decl.name.text === 'metadata' &&
86
- decl.initializer &&
87
- ts.isObjectLiteralExpression(decl.initializer)
88
- ) {
89
- return evalObject(ts, decl.initializer);
90
- }
91
- }
92
- }
93
- return null;
94
- }
95
-
96
- /**
97
- * Build-only plugin that statically pre-renders per-route HTML for SEO. After the bundle is written,
98
- * it takes the built shell (`index.html`), and for each static route bakes that route's
99
- * `metadata` (merged over the site-wide `seo` defaults) into a `<route>/index.html` so a JS-less
100
- * crawler hitting the route gets correct per-page tags. Dynamic (`generateMetadata`) and `:param`
101
- * routes are skipped (no data at build) and fall back to the client-rendered shell.
102
- */
103
- export function prerenderPlugin(cfg: ResolvedToilConfig): Plugin {
104
- return {
105
- name: 'toil:prerender-seo',
106
- apply: 'build',
107
- async closeBundle() {
108
- if (!cfg.seo) return;
109
- const outDir = path.resolve(cfg.root, cfg.outDir);
110
- const shellPath = path.join(outDir, 'index.html');
111
- if (!fs.existsSync(shellPath)) return;
112
- const shell = fs.readFileSync(shellPath, 'utf8');
113
- const ts = await loadTypeScript(cfg.root);
114
-
115
- const routes = scanRoutes(cfg.routesAbsDir).filter(
116
- (r) => r.slot === undefined && !r.intercept && !/[:*]/.test(r.pattern),
117
- );
118
- for (const route of routes) {
119
- const metadata = ts ? extractStaticMetadata(ts, route.file) : null;
120
- const html = injectSeoHtml(shell, routeSeo(cfg.seo, metadata, route.pattern));
121
- const target =
122
- route.pattern === '/'
123
- ? shellPath
124
- : path.join(outDir, route.pattern.replace(/^\//, ''), 'index.html');
125
- fs.mkdirSync(path.dirname(target), { recursive: true });
126
- fs.writeFileSync(target, html);
127
- }
128
- },
129
- };
130
- }
1
+ import { createRequire } from 'node:module';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { pathToFileURL } from 'node:url';
5
+
6
+ import type * as TS from 'typescript';
7
+ import type { Plugin } from 'vite';
8
+
9
+ import { type ResolvedToilConfig } from './config.js';
10
+ import { scanRoutes } from './routes.js';
11
+ import { injectSeoHtml, routeSeo } from './seo.js';
12
+
13
+ type Ts = typeof TS;
14
+
15
+ /** Loads the project's TypeScript (used to read each route's static `metadata`), or `null` if absent. */
16
+ async function loadTypeScript(root: string): Promise<Ts | null> {
17
+ try {
18
+ const resolved = createRequire(path.join(root, 'package.json')).resolve('typescript');
19
+ const mod = (await import(pathToFileURL(resolved).href)) as { default?: Ts } & Ts;
20
+ return mod.default ?? mod;
21
+ } catch {
22
+ return null;
23
+ }
24
+ }
25
+
26
+ /** Marks an AST node that isn't a static literal (so its value can't be baked at build). */
27
+ const UNRESOLVED = Symbol('unresolved');
28
+
29
+ /** Statically evaluates a literal expression node to a JS value, or `UNRESOLVED` if it isn't one. */
30
+ function evalNode(ts: Ts, node: TS.Expression): unknown {
31
+ if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
32
+ if (ts.isNumericLiteral(node)) return Number(node.text);
33
+ if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
34
+ if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
35
+ if (node.kind === ts.SyntaxKind.NullKeyword) return null;
36
+ if (ts.isArrayLiteralExpression(node)) {
37
+ const out: unknown[] = [];
38
+ for (const el of node.elements) {
39
+ const value = evalNode(ts, el);
40
+ if (value === UNRESOLVED) return UNRESOLVED;
41
+ out.push(value);
42
+ }
43
+ return out;
44
+ }
45
+ if (ts.isObjectLiteralExpression(node)) return evalObject(ts, node);
46
+ return UNRESOLVED;
47
+ }
48
+
49
+ /** Evaluates an object literal to a plain object, skipping any property that isn't a static literal. */
50
+ function evalObject(ts: Ts, node: TS.ObjectLiteralExpression): Record<string, unknown> {
51
+ const obj: Record<string, unknown> = {};
52
+ for (const prop of node.properties) {
53
+ if (!ts.isPropertyAssignment(prop)) continue;
54
+ const key = ts.isIdentifier(prop.name)
55
+ ? prop.name.text
56
+ : ts.isStringLiteral(prop.name)
57
+ ? prop.name.text
58
+ : null;
59
+ if (key === null) continue;
60
+ const value = evalNode(ts, prop.initializer);
61
+ if (value !== UNRESOLVED) obj[key] = value;
62
+ }
63
+ return obj;
64
+ }
65
+
66
+ /**
67
+ * Extracts the named `export const <name> = { … }` object-literal exports from a route file in a
68
+ * single parse, returning the statically-evaluable subset of each (dynamic and computed values are
69
+ * skipped). Names that are absent or not object literals are omitted from the result.
70
+ */
71
+ export function extractStaticExports(
72
+ ts: Ts,
73
+ filePath: string,
74
+ names: readonly string[],
75
+ ): Record<string, Record<string, unknown>> {
76
+ let source: string;
77
+ try {
78
+ source = fs.readFileSync(filePath, 'utf8');
79
+ } catch {
80
+ return {};
81
+ }
82
+ const wanted = new Set(names);
83
+ const out: Record<string, Record<string, unknown>> = {};
84
+ const sf = ts.createSourceFile(
85
+ filePath,
86
+ source,
87
+ ts.ScriptTarget.Latest,
88
+ true,
89
+ ts.ScriptKind.TSX,
90
+ );
91
+ for (const stmt of sf.statements) {
92
+ if (!ts.isVariableStatement(stmt)) continue;
93
+ if (!stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) continue;
94
+ for (const decl of stmt.declarationList.declarations) {
95
+ if (
96
+ ts.isIdentifier(decl.name) &&
97
+ wanted.has(decl.name.text) &&
98
+ !(decl.name.text in out) &&
99
+ decl.initializer &&
100
+ ts.isObjectLiteralExpression(decl.initializer)
101
+ ) {
102
+ out[decl.name.text] = evalObject(ts, decl.initializer);
103
+ }
104
+ }
105
+ }
106
+ return out;
107
+ }
108
+
109
+ /**
110
+ * Extracts a route's `export const metadata = { … }` if it's a static object literal, returning the
111
+ * statically-evaluable subset (dynamic `generateMetadata` and computed values are skipped). `null`
112
+ * when the file has no static metadata.
113
+ */
114
+ export function extractStaticMetadata(ts: Ts, filePath: string): Record<string, unknown> | null {
115
+ return extractStaticExports(ts, filePath, ['metadata']).metadata ?? null;
116
+ }
117
+
118
+ /**
119
+ * Build-only plugin that statically pre-renders per-route HTML for SEO. After the bundle is written,
120
+ * it takes the built shell (`index.html`), and for each static route bakes that route's
121
+ * `metadata` (merged over the site-wide `seo` defaults) into a `<route>/index.html` so a JS-less
122
+ * crawler hitting the route gets correct per-page tags. Dynamic (`generateMetadata`) and `:param`
123
+ * routes are skipped (no data at build) and fall back to the client-rendered shell.
124
+ */
125
+ export function prerenderPlugin(cfg: ResolvedToilConfig): Plugin {
126
+ return {
127
+ name: 'toil:prerender-seo',
128
+ apply: 'build',
129
+ async closeBundle() {
130
+ if (!cfg.seo) return;
131
+ const outDir = path.resolve(cfg.root, cfg.outDir);
132
+ const shellPath = path.join(outDir, 'index.html');
133
+ if (!fs.existsSync(shellPath)) return;
134
+ const shell = fs.readFileSync(shellPath, 'utf8');
135
+ const ts = await loadTypeScript(cfg.root);
136
+
137
+ const routes = scanRoutes(cfg.routesAbsDir).filter(
138
+ (r) => r.slot === undefined && !r.intercept && !/[:*]/.test(r.pattern),
139
+ );
140
+ for (const route of routes) {
141
+ const metadata = ts ? extractStaticMetadata(ts, route.file) : null;
142
+ const html = injectSeoHtml(shell, routeSeo(cfg.seo, metadata, route.pattern));
143
+ const target =
144
+ route.pattern === '/'
145
+ ? shellPath
146
+ : path.join(outDir, route.pattern.replace(/^\//, ''), 'index.html');
147
+ fs.mkdirSync(path.dirname(target), { recursive: true });
148
+ fs.writeFileSync(target, html);
149
+ }
150
+ },
151
+ };
152
+ }
@@ -1,131 +1,132 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
-
4
- /** A discovered route: the source file and the URL pattern it serves. */
5
- export interface ScannedRoute {
6
- readonly file: string;
7
- readonly pattern: string;
8
- /** Named parallel slot this route belongs to (from an `@slot` dir), or `undefined` for the main tree. */
9
- readonly slot?: string;
10
- /** True for an intercepting route (`(.)`/`(..)`/`(...)`), matched only on soft navigation. */
11
- readonly intercept?: boolean;
12
- }
13
-
14
- const ROUTE_EXT = /\.(tsx|jsx)$/;
15
- /** Special files that live alongside routes but are not themselves pages. */
16
- const SPECIAL_FILE = /^(layout|template|loading|error|global-error|404|not-found)\.(tsx|jsx)$/;
17
-
18
- /**
19
- * Derives a route pattern from a route file path (relative to the routes dir).
20
- * index.tsx -> /
21
- * about.tsx -> /about
22
- * blog/index.tsx -> /blog
23
- * blog/[id].tsx -> /blog/:id
24
- * docs/[...slug].tsx -> /docs/*slug (catch-all)
25
- * docs/[[...slug]].tsx -> /docs/**slug (optional catch-all)
26
- * (marketing)/about.tsx -> /about (route group: parens add no URL segment)
27
- * @modal/photo/[id].tsx -> /photo/:id (parallel slot: `@slot` adds no URL segment)
28
- */
29
- /** Converts a path segment's dynamic brackets to URL params (`[id]`→`:id`, `[...x]`→`*x`, `[[...x]]`→`**x`). */
30
- function toUrlSegment(segment: string): string {
31
- return segment
32
- .replace(/^\[\[\.\.\.(.+)\]\]$/, '**$1')
33
- .replace(/^\[\.\.\.(.+)\]$/, '*$1')
34
- .replace(/^\[(.+)\]$/, ':$1');
35
- }
36
-
37
- /** Interception markers: `(.)` same level, `(..)` up one, `(...)` from the routes root. */
38
- const INTERCEPT_RE = /^\((\.{1,3})\)(.+)$/;
39
-
40
- export function filePathToRoute(relPath: string): string {
41
- const withoutExt = relPath.replace(/\\/g, '/').replace(ROUTE_EXT, '');
42
- const segments = withoutExt.split('/').filter(Boolean);
43
- const out: string[] = [];
44
- for (let i = 0; i < segments.length; i++) {
45
- const segment = segments[i];
46
- if (/^\(.+\)$/.test(segment)) continue;
47
- if (/^@/.test(segment)) continue; // parallel-slot marker, contributes no URL segment
48
- if (segment === 'index' && i === segments.length - 1) continue;
49
- out.push(toUrlSegment(segment));
50
- }
51
- return '/' + out.join('/');
52
- }
53
-
54
- /**
55
- * The URL an intercepting route targets, or `null` if the path has no `(.)`/`(..)`/`(...)` marker.
56
- * The marker resolves the target relative to the route's position (ignoring `@slot`/`(group)`
57
- * segments): `(.)` keeps the current level, `(..)` drops one, `(...)` resets to the root.
58
- * @modal/(.)photo/[id].tsx -> /photo/:id
59
- * feed/@modal/(..)photo/[id].tsx -> /photo/:id
60
- */
61
- export function interceptTarget(relPath: string): string | null {
62
- const segments = relPath.replace(/\\/g, '/').replace(ROUTE_EXT, '').split('/').filter(Boolean);
63
- const out: string[] = [];
64
- let marked = false;
65
- for (let i = 0; i < segments.length; i++) {
66
- const segment = segments[i];
67
- if (/^@/.test(segment)) continue;
68
- const marker = INTERCEPT_RE.exec(segment);
69
- if (marker) {
70
- marked = true;
71
- const dots = marker[1].length;
72
- if (dots === 2) out.pop(); // (..) up one level
73
- else if (dots === 3) out.length = 0; // (...) from the routes root
74
- out.push(toUrlSegment(marker[2]));
75
- continue;
76
- }
77
- if (/^\(.+\)$/.test(segment)) continue;
78
- if (segment === 'index' && i === segments.length - 1) continue;
79
- out.push(toUrlSegment(segment));
80
- }
81
- return marked ? '/' + out.join('/') : null;
82
- }
83
-
84
- /**
85
- * Ranks a pattern so more specific routes match first: static segments beat dynamic (`:x`),
86
- * which beat catch-all (`*x`); deeper routes beat shallower ones.
87
- */
88
- function specificity(pattern: string): number {
89
- const segments = pattern.split('/').filter(Boolean);
90
- let score = segments.length * 10;
91
- for (const segment of segments) {
92
- if (segment.startsWith('*')) score -= 5;
93
- else if (!segment.startsWith(':')) score += 5;
94
- }
95
- return score;
96
- }
97
-
98
- /** The parallel-slot name for a route path (the first `@slot` segment), or `undefined`. */
99
- function slotOf(relPath: string): string | undefined {
100
- for (const segment of relPath.replace(/\\/g, '/').split('/')) {
101
- const match = /^@(.+)$/.exec(segment);
102
- if (match) return match[1];
103
- }
104
- return undefined;
105
- }
106
-
107
- /** Recursively scans `routesDir` for `.tsx`/`.jsx` files, returning routes sorted by specificity. */
108
- export function scanRoutes(routesDir: string): ScannedRoute[] {
109
- if (!fs.existsSync(routesDir)) return [];
110
- const found: ScannedRoute[] = [];
111
- const walk = (dir: string): void => {
112
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
113
- const full = path.join(dir, entry.name);
114
- if (entry.isDirectory()) {
115
- walk(full);
116
- } else if (ROUTE_EXT.test(entry.name) && !SPECIAL_FILE.test(entry.name)) {
117
- const rel = path.relative(routesDir, full);
118
- const target = interceptTarget(rel);
119
- found.push({
120
- file: full,
121
- pattern: target ?? filePathToRoute(rel),
122
- slot: slotOf(rel),
123
- intercept: target !== null,
124
- });
125
- }
126
- }
127
- };
128
- walk(routesDir);
129
- found.sort((a, b) => specificity(b.pattern) - specificity(a.pattern));
130
- return found;
131
- }
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ /** A discovered route: the source file and the URL pattern it serves. */
5
+ export interface ScannedRoute {
6
+ readonly file: string;
7
+ readonly pattern: string;
8
+ /** Named parallel slot this route belongs to (from an `@slot` dir), or `undefined` for the main tree. */
9
+ readonly slot?: string;
10
+ /** True for an intercepting route (`(.)`/`(..)`/`(...)`), matched only on soft navigation. */
11
+ readonly intercept?: boolean;
12
+ }
13
+
14
+ const ROUTE_EXT = /\.(tsx|jsx)$/;
15
+ /** Special files that live alongside routes but are not themselves pages. */
16
+ const SPECIAL_FILE = /^(layout|template|loading|error|global-error|404|not-found)\.(tsx|jsx)$/;
17
+
18
+ /**
19
+ * Derives a route pattern from a route file path (relative to the routes dir).
20
+ * index.tsx -> /
21
+ * about.tsx -> /about
22
+ * blog/index.tsx -> /blog
23
+ * blog/[id].tsx -> /blog/:id
24
+ * docs/[...slug].tsx -> /docs/*slug (catch-all)
25
+ * docs/[[...slug]].tsx -> /docs/**slug (optional catch-all)
26
+ * (marketing)/about.tsx -> /about (route group: parens add no URL segment)
27
+ * @modal/photo/[id].tsx -> /photo/:id (parallel slot: `@slot` adds no URL segment)
28
+ */
29
+ /** Converts a path segment's dynamic brackets to URL params (`[id]`→`:id`, `[...x]`→`*x`, `[[...x]]`→`**x`). */
30
+ function toUrlSegment(segment: string): string {
31
+ return segment
32
+ .replace(/^\[\[\.\.\.(.+)\]\]$/, '**$1')
33
+ .replace(/^\[\.\.\.(.+)\]$/, '*$1')
34
+ .replace(/^\[(.+)\]$/, ':$1');
35
+ }
36
+
37
+ /** Interception markers: `(.)` same level, `(..)` up one, `(...)` from the routes root. */
38
+ const INTERCEPT_RE = /^\((\.{1,3})\)(.+)$/;
39
+
40
+ export function filePathToRoute(relPath: string): string {
41
+ const withoutExt = relPath.replace(/\\/g, '/').replace(ROUTE_EXT, '');
42
+ const segments = withoutExt.split('/').filter(Boolean);
43
+ const out: string[] = [];
44
+ for (let i = 0; i < segments.length; i++) {
45
+ const segment = segments[i];
46
+ if (/^\(.+\)$/.test(segment)) continue;
47
+ if (/^@/.test(segment)) continue; // parallel-slot marker, contributes no URL segment
48
+ if (segment === 'index' && i === segments.length - 1) continue;
49
+ out.push(toUrlSegment(segment));
50
+ }
51
+ return '/' + out.join('/');
52
+ }
53
+
54
+ /**
55
+ * The URL an intercepting route targets, or `null` if the path has no `(.)`/`(..)`/`(...)` marker.
56
+ * The marker resolves the target relative to the route's position (ignoring `@slot`/`(group)`
57
+ * segments): `(.)` keeps the current level, `(..)` drops one, `(...)` resets to the root.
58
+ * @modal/(.)photo/[id].tsx -> /photo/:id
59
+ * feed/@modal/(..)photo/[id].tsx -> /photo/:id
60
+ */
61
+ export function interceptTarget(relPath: string): string | null {
62
+ const segments = relPath.replace(/\\/g, '/').replace(ROUTE_EXT, '').split('/').filter(Boolean);
63
+ const out: string[] = [];
64
+ let marked = false;
65
+ for (let i = 0; i < segments.length; i++) {
66
+ const segment = segments[i];
67
+ if (/^@/.test(segment)) continue;
68
+ const marker = INTERCEPT_RE.exec(segment);
69
+ if (marker) {
70
+ marked = true;
71
+ const dots = marker[1].length;
72
+ if (dots === 2)
73
+ out.pop(); // (..) up one level
74
+ else if (dots === 3) out.length = 0; // (...) from the routes root
75
+ out.push(toUrlSegment(marker[2]));
76
+ continue;
77
+ }
78
+ if (/^\(.+\)$/.test(segment)) continue;
79
+ if (segment === 'index' && i === segments.length - 1) continue;
80
+ out.push(toUrlSegment(segment));
81
+ }
82
+ return marked ? '/' + out.join('/') : null;
83
+ }
84
+
85
+ /**
86
+ * Ranks a pattern so more specific routes match first: static segments beat dynamic (`:x`),
87
+ * which beat catch-all (`*x`); deeper routes beat shallower ones.
88
+ */
89
+ function specificity(pattern: string): number {
90
+ const segments = pattern.split('/').filter(Boolean);
91
+ let score = segments.length * 10;
92
+ for (const segment of segments) {
93
+ if (segment.startsWith('*')) score -= 5;
94
+ else if (!segment.startsWith(':')) score += 5;
95
+ }
96
+ return score;
97
+ }
98
+
99
+ /** The parallel-slot name for a route path (the first `@slot` segment), or `undefined`. */
100
+ function slotOf(relPath: string): string | undefined {
101
+ for (const segment of relPath.replace(/\\/g, '/').split('/')) {
102
+ const match = /^@(.+)$/.exec(segment);
103
+ if (match) return match[1];
104
+ }
105
+ return undefined;
106
+ }
107
+
108
+ /** Recursively scans `routesDir` for `.tsx`/`.jsx` files, returning routes sorted by specificity. */
109
+ export function scanRoutes(routesDir: string): ScannedRoute[] {
110
+ if (!fs.existsSync(routesDir)) return [];
111
+ const found: ScannedRoute[] = [];
112
+ const walk = (dir: string): void => {
113
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
114
+ const full = path.join(dir, entry.name);
115
+ if (entry.isDirectory()) {
116
+ walk(full);
117
+ } else if (ROUTE_EXT.test(entry.name) && !SPECIAL_FILE.test(entry.name)) {
118
+ const rel = path.relative(routesDir, full);
119
+ const target = interceptTarget(rel);
120
+ found.push({
121
+ file: full,
122
+ pattern: target ?? filePathToRoute(rel),
123
+ slot: slotOf(rel),
124
+ intercept: target !== null,
125
+ });
126
+ }
127
+ }
128
+ };
129
+ walk(routesDir);
130
+ found.sort((a, b) => specificity(b.pattern) - specificity(a.pattern));
131
+ return found;
132
+ }