toiljs 0.0.14 → 0.0.16

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 (225) hide show
  1. package/.babelrc +13 -13
  2. package/.gitattributes +2 -2
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -38
  4. package/.github/ISSUE_TEMPLATE/bug_report.yml +90 -90
  5. package/.github/ISSUE_TEMPLATE/config.yml +8 -8
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -20
  7. package/.github/PULL_REQUEST_TEMPLATE.md +43 -43
  8. package/.github/changelog-config.json +45 -45
  9. package/.github/dependabot.yml +27 -27
  10. package/.github/workflows/ci.yml +191 -191
  11. package/.prettierrc.json +11 -11
  12. package/.vscode/settings.json +9 -9
  13. package/CHANGELOG.md +5 -5
  14. package/LICENSE +187 -187
  15. package/README.md +339 -315
  16. package/as-pect.asconfig.json +34 -34
  17. package/as-pect.config.js +65 -65
  18. package/assets/logo.svg +36 -36
  19. package/build/backend/.tsbuildinfo +1 -1
  20. package/build/cli/.tsbuildinfo +1 -1
  21. package/build/cli/index.js +2926 -191
  22. package/build/client/.tsbuildinfo +1 -1
  23. package/build/client/dev/devtools.d.ts +6 -0
  24. package/build/client/dev/devtools.js +442 -0
  25. package/build/client/dev/error-overlay.d.ts +9 -0
  26. package/build/client/dev/error-overlay.js +19 -4
  27. package/build/client/head/metadata.d.ts +3 -1
  28. package/build/client/head/metadata.js +8 -0
  29. package/build/client/index.d.ts +4 -4
  30. package/build/client/index.js +2 -2
  31. package/build/client/navigation/navigation.d.ts +2 -0
  32. package/build/client/navigation/navigation.js +9 -1
  33. package/build/client/navigation/prefetch.d.ts +1 -0
  34. package/build/client/navigation/prefetch.js +35 -0
  35. package/build/client/routing/Router.js +1 -1
  36. package/build/client/routing/hooks.js +6 -2
  37. package/build/client/routing/loader.d.ts +25 -0
  38. package/build/client/routing/loader.js +53 -7
  39. package/build/client/routing/mount.js +4 -3
  40. package/build/compiler/.tsbuildinfo +1 -1
  41. package/build/compiler/config.d.ts +18 -0
  42. package/build/compiler/config.js +8 -0
  43. package/build/compiler/docs.js +16 -16
  44. package/build/compiler/generate.js +3 -0
  45. package/build/compiler/index.d.ts +2 -2
  46. package/build/compiler/index.js +3 -1
  47. package/build/compiler/plugin.js +156 -0
  48. package/build/compiler/prerender.d.ts +1 -0
  49. package/build/compiler/prerender.js +2 -1
  50. package/build/compiler/seo.d.ts +2 -2
  51. package/build/compiler/seo.js +8 -6
  52. package/build/compiler/ssg.d.ts +5 -0
  53. package/build/compiler/ssg.js +121 -0
  54. package/build/io/.tsbuildinfo +1 -1
  55. package/build/logger/.tsbuildinfo +1 -1
  56. package/build/shared/.tsbuildinfo +1 -1
  57. package/eslint.config.js +48 -48
  58. package/examples/basic/client/404.tsx +11 -11
  59. package/examples/basic/client/components/.gitkeep +1 -1
  60. package/examples/basic/client/global-error.tsx +13 -13
  61. package/examples/basic/client/layout.tsx +25 -25
  62. package/examples/basic/client/public/images/.gitkeep +1 -1
  63. package/examples/basic/client/public/images/logo.svg +36 -36
  64. package/examples/basic/client/public/robots.txt +2 -2
  65. package/examples/basic/client/routes/docs/[...slug].tsx +12 -12
  66. package/examples/basic/client/routes/features/error/error.tsx +16 -16
  67. package/examples/basic/client/routes/features/template/b.tsx +14 -14
  68. package/examples/basic/client/routes/files/[[...slug]].tsx +21 -21
  69. package/examples/basic/client/routes/gallery/layout.tsx +13 -13
  70. package/examples/basic/client/routes/io.tsx +24 -24
  71. package/examples/basic/client/routes/loader-demo/loading.tsx +13 -13
  72. package/examples/basic/client/routes/search.tsx +61 -61
  73. package/examples/basic/client/toil.tsx +5 -5
  74. package/package.json +155 -147
  75. package/presets/eslint.js +88 -88
  76. package/presets/no-uint8array-tostring.js +200 -200
  77. package/presets/prettier.json +18 -18
  78. package/presets/tsconfig.json +37 -37
  79. package/src/backend/index.ts +160 -160
  80. package/src/cli/proc.ts +50 -50
  81. package/src/cli/updates.ts +69 -69
  82. package/src/cli/validate.ts +31 -31
  83. package/src/client/channel/channel.ts +146 -146
  84. package/src/client/components/Form.tsx +65 -65
  85. package/src/client/components/Script.tsx +113 -113
  86. package/src/client/components/Slot.tsx +21 -21
  87. package/src/client/dev/devtools.tsx +973 -0
  88. package/src/client/dev/error-overlay.tsx +30 -4
  89. package/src/client/head/head.ts +167 -167
  90. package/src/client/head/metadata.ts +19 -1
  91. package/src/client/index.ts +19 -9
  92. package/src/client/navigation/NavLink.tsx +86 -86
  93. package/src/client/navigation/navigation.ts +25 -5
  94. package/src/client/navigation/prefetch.ts +169 -130
  95. package/src/client/navigation/scroll.ts +53 -53
  96. package/src/client/routing/Router.tsx +8 -2
  97. package/src/client/routing/action.ts +122 -122
  98. package/src/client/routing/error-boundary.tsx +43 -43
  99. package/src/client/routing/hooks.ts +21 -6
  100. package/src/client/routing/loader.ts +325 -225
  101. package/src/client/routing/match.ts +47 -47
  102. package/src/client/routing/mount.tsx +54 -52
  103. package/src/client/routing/params-context.ts +10 -10
  104. package/src/client/routing/slot-context.ts +7 -7
  105. package/src/client/search/search.ts +189 -189
  106. package/src/client/search/use-page-search.ts +73 -73
  107. package/src/client/types.ts +73 -73
  108. package/src/compiler/config.ts +47 -1
  109. package/src/compiler/docs.ts +228 -228
  110. package/src/compiler/generate.ts +394 -391
  111. package/src/compiler/index.ts +64 -54
  112. package/src/compiler/pages.ts +70 -70
  113. package/src/compiler/plugin.ts +170 -2
  114. package/src/compiler/prerender.ts +5 -1
  115. package/src/compiler/seo.ts +23 -7
  116. package/src/compiler/ssg.ts +162 -0
  117. package/src/io/BinaryReader.ts +340 -340
  118. package/src/io/BinaryWriter.ts +385 -385
  119. package/src/io/FastMap.ts +127 -127
  120. package/src/io/index.ts +11 -11
  121. package/src/io/lengths.ts +14 -14
  122. package/src/io/types.ts +18 -18
  123. package/src/logger/index.ts +22 -22
  124. package/src/server/index.ts +10 -10
  125. package/src/server/main.ts +13 -13
  126. package/src/server/tsconfig.json +4 -4
  127. package/src/shared/index.ts +10 -10
  128. package/std/client/index.d.ts +15 -15
  129. package/std/client/package.json +3 -3
  130. package/test/assembly/example.spec.ts +7 -7
  131. package/test/channel.test.ts +21 -21
  132. package/test/dom/Link.test.tsx +47 -47
  133. package/test/dom/NavLink.test.tsx +37 -37
  134. package/test/dom/error-overlay.test.tsx +44 -44
  135. package/test/dom/loader.test.tsx +121 -121
  136. package/test/dom/navigation.test.ts +59 -59
  137. package/test/dom/revalidate.test.tsx +38 -38
  138. package/test/dom/route-head.test.tsx +78 -78
  139. package/test/dom/router-loading.test.tsx +44 -44
  140. package/test/dom/scroll.test.ts +56 -56
  141. package/test/dom/use-metadata.test.tsx +58 -0
  142. package/test/io.test.ts +93 -93
  143. package/test/navlink.test.ts +28 -28
  144. package/test/placeholder.test.ts +9 -9
  145. package/test/routes.test.ts +76 -76
  146. package/test/seo.test.ts +175 -164
  147. package/test/slot-layouts.test.ts +69 -69
  148. package/test/ssg.test.ts +36 -0
  149. package/test/update.test.ts +44 -44
  150. package/test/validate.test.ts +42 -42
  151. package/toil-routes.d.ts +7 -0
  152. package/toilconfig.json +30 -30
  153. package/tsconfig.backend.json +13 -13
  154. package/tsconfig.base.json +35 -35
  155. package/tsconfig.cli.json +13 -13
  156. package/tsconfig.client.json +14 -14
  157. package/tsconfig.compiler.json +13 -13
  158. package/tsconfig.io.json +12 -12
  159. package/tsconfig.json +22 -22
  160. package/tsconfig.logger.json +12 -12
  161. package/tsconfig.server.json +10 -10
  162. package/tsconfig.shared.json +12 -12
  163. package/vitest.config.ts +26 -26
  164. package/.idea/codeStyles/Project.xml +0 -54
  165. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  166. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  167. package/.idea/modules.xml +0 -8
  168. package/.idea/prettier.xml +0 -7
  169. package/.idea/toiljs.iml +0 -8
  170. package/.idea/vcs.xml +0 -6
  171. package/.toil/entry.tsx +0 -9
  172. package/.toil/index.html +0 -12
  173. package/.toil/routes.ts +0 -9
  174. package/build/cli/configure.d.ts +0 -16
  175. package/build/cli/configure.js +0 -272
  176. package/build/cli/create.d.ts +0 -16
  177. package/build/cli/create.js +0 -420
  178. package/build/cli/diagnostics.d.ts +0 -55
  179. package/build/cli/diagnostics.js +0 -333
  180. package/build/cli/doctor.d.ts +0 -6
  181. package/build/cli/doctor.js +0 -249
  182. package/build/cli/features.d.ts +0 -25
  183. package/build/cli/features.js +0 -107
  184. package/build/cli/index.d.ts +0 -2
  185. package/build/cli/proc.d.ts +0 -6
  186. package/build/cli/proc.js +0 -31
  187. package/build/cli/ui.d.ts +0 -9
  188. package/build/cli/ui.js +0 -75
  189. package/build/cli/update.d.ts +0 -7
  190. package/build/cli/update.js +0 -117
  191. package/build/cli/updates.d.ts +0 -10
  192. package/build/cli/updates.js +0 -45
  193. package/build/cli/validate.d.ts +0 -4
  194. package/build/cli/validate.js +0 -19
  195. package/build/client/Link.d.ts +0 -8
  196. package/build/client/Link.js +0 -44
  197. package/build/client/NavLink.d.ts +0 -14
  198. package/build/client/NavLink.js +0 -37
  199. package/build/client/Router.d.ts +0 -7
  200. package/build/client/Router.js +0 -55
  201. package/build/client/channel.d.ts +0 -23
  202. package/build/client/channel.js +0 -94
  203. package/build/client/error-boundary.d.ts +0 -16
  204. package/build/client/error-boundary.js +0 -19
  205. package/build/client/head.d.ts +0 -26
  206. package/build/client/head.js +0 -87
  207. package/build/client/hooks.d.ts +0 -17
  208. package/build/client/hooks.js +0 -48
  209. package/build/client/lazy.d.ts +0 -16
  210. package/build/client/lazy.js +0 -53
  211. package/build/client/match.d.ts +0 -2
  212. package/build/client/match.js +0 -32
  213. package/build/client/mount.d.ts +0 -2
  214. package/build/client/mount.js +0 -13
  215. package/build/client/navigation.d.ts +0 -13
  216. package/build/client/navigation.js +0 -97
  217. package/build/client/params-context.d.ts +0 -2
  218. package/build/client/params-context.js +0 -2
  219. package/build/client/prefetch.d.ts +0 -11
  220. package/build/client/prefetch.js +0 -100
  221. package/build/client/runtime.d.ts +0 -31
  222. package/build/client/runtime.js +0 -112
  223. package/build/client/scroll.d.ts +0 -8
  224. package/build/client/scroll.js +0 -36
  225. package/toil-env.d.ts +0 -16
@@ -1,54 +1,64 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
-
4
- import { build as viteBuild, createServer, type ViteDevServer } from 'vite';
5
- import { startBackend, type RunningBackend } from 'toiljs/backend';
6
-
7
- import { loadConfig } from './config.js';
8
- import { generate } from './generate.js';
9
- import { createViteConfig } from './vite.js';
10
-
11
- export interface ToilCommandOptions {
12
- readonly root?: string;
13
- readonly port?: number;
14
- }
15
-
16
- /** Starts the Vite dev server (HMR + transforms) for the client app. Returns the running server. */
17
- export async function dev(opts: ToilCommandOptions = {}): Promise<ViteDevServer> {
18
- const cfg = await loadConfig(opts);
19
- generate(cfg);
20
- const server = await createServer(await createViteConfig(cfg));
21
- await server.listen();
22
- server.printUrls();
23
- return server;
24
- }
25
-
26
- /** Produces an optimized production SPA bundle in the configured `outDir`. */
27
- export async function build(opts: ToilCommandOptions = {}): Promise<void> {
28
- const cfg = await loadConfig(opts);
29
- generate(cfg);
30
- await viteBuild(await createViteConfig(cfg));
31
- }
32
-
33
- /**
34
- * Self-hosts the built client over the high-performance hyper-express backend (uWebSockets.js),
35
- * serving the configured `outDir` with an SPA fallback plus a WebSocket channel. Requires a prior
36
- * `build`. Returns the running backend.
37
- */
38
- export async function start(opts: ToilCommandOptions = {}): Promise<RunningBackend> {
39
- const cfg = await loadConfig(opts);
40
- const outDir = path.resolve(cfg.root, cfg.outDir);
41
- if (!fs.existsSync(path.join(outDir, 'index.html'))) {
42
- throw new Error(`No build found in ${outDir}. Run \`toiljs build\` first.`);
43
- }
44
- return startBackend({ root: outDir, port: cfg.port });
45
- }
46
-
47
- export { defineConfig, loadConfig } from './config.js';
48
- export { scanRoutes } from './routes.js';
49
- export type { ScannedRoute } from './routes.js';
50
- export { TOIL_ENV_DTS } from './generate.js';
51
- export { AI_HELPERS, AI_HELPER_IDS, aiHelperFiles, TOIL_DOCS } from './docs.js';
52
- export type { AiHelper } from './docs.js';
53
- export type { ToilConfig, ResolvedToilConfig } from './config.js';
54
- export type { RunningBackend, BackendOptions } from 'toiljs/backend';
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ import { build as viteBuild, createServer, type ViteDevServer } from 'vite';
5
+ import { startBackend, type RunningBackend } from 'toiljs/backend';
6
+
7
+ import { loadConfig } from './config.js';
8
+ import { generate } from './generate.js';
9
+ import { prerenderStaticParams } from './ssg.js';
10
+ import { createViteConfig } from './vite.js';
11
+
12
+ export interface ToilCommandOptions {
13
+ readonly root?: string;
14
+ readonly port?: number;
15
+ }
16
+
17
+ /** Starts the Vite dev server (HMR + transforms) for the client app. Returns the running server. */
18
+ export async function dev(opts: ToilCommandOptions = {}): Promise<ViteDevServer> {
19
+ const cfg = await loadConfig(opts);
20
+ generate(cfg);
21
+ const server = await createServer(await createViteConfig(cfg));
22
+ await server.listen();
23
+ server.printUrls();
24
+ return server;
25
+ }
26
+
27
+ /** Produces an optimized production SPA bundle in the configured `outDir`. */
28
+ export async function build(opts: ToilCommandOptions = {}): Promise<void> {
29
+ const cfg = await loadConfig(opts);
30
+ generate(cfg);
31
+ await viteBuild(await createViteConfig(cfg));
32
+ // SSG: bake per-URL HTML + sitemap for dynamic routes that opt in via `generateStaticParams`.
33
+ await prerenderStaticParams(cfg);
34
+ }
35
+
36
+ /**
37
+ * Self-hosts the built client over the high-performance hyper-express backend (uWebSockets.js),
38
+ * serving the configured `outDir` with an SPA fallback plus a WebSocket channel. Requires a prior
39
+ * `build`. Returns the running backend.
40
+ */
41
+ export async function start(opts: ToilCommandOptions = {}): Promise<RunningBackend> {
42
+ const cfg = await loadConfig(opts);
43
+ const outDir = path.resolve(cfg.root, cfg.outDir);
44
+ if (!fs.existsSync(path.join(outDir, 'index.html'))) {
45
+ throw new Error(`No build found in ${outDir}. Run \`toiljs build\` first.`);
46
+ }
47
+ return startBackend({ root: outDir, port: cfg.port });
48
+ }
49
+
50
+ export { defineConfig, loadConfig, AiProvider } from './config.js';
51
+ export { scanRoutes } from './routes.js';
52
+ export type { ScannedRoute } from './routes.js';
53
+ export { TOIL_ENV_DTS } from './generate.js';
54
+ export { AI_HELPERS, AI_HELPER_IDS, aiHelperFiles, TOIL_DOCS } from './docs.js';
55
+ export type { AiHelper } from './docs.js';
56
+ export type {
57
+ ToilConfig,
58
+ ResolvedToilConfig,
59
+ ClientConfig,
60
+ ServerConfig,
61
+ DevtoolsConfig,
62
+ DevtoolsAiConfig,
63
+ } from './config.js';
64
+ export type { RunningBackend, BackendOptions } from 'toiljs/backend';
@@ -1,70 +1,70 @@
1
- import { createRequire } from 'node:module';
2
- import path from 'node:path';
3
-
4
- import type * as TS from 'typescript';
5
-
6
- import { extractStaticExports } from './prerender.js';
7
- import type { ScannedRoute } from './routes.js';
8
-
9
- type Ts = typeof TS;
10
-
11
- /**
12
- * A page in the build-time search index: its URL pattern, whether it's dynamic, and the
13
- * statically-extracted `metadata` literal (the searchable subset; dynamic `generateMetadata` and
14
- * computed values can't be known at build, so they're absent). Serialized into the generated
15
- * `routes` module and registered client-side for {@link searchPages}.
16
- */
17
- export interface PageIndexEntry {
18
- readonly path: string;
19
- readonly dynamic: boolean;
20
- readonly metadata: Record<string, unknown>;
21
- }
22
-
23
- /**
24
- * Loads the project's TypeScript synchronously (so {@link buildPageIndex} can run inside the sync
25
- * `generate()`), or `null` if it isn't installed, in which case pages are indexed by path only.
26
- */
27
- function loadTypeScriptSync(root: string): Ts | null {
28
- try {
29
- const require = createRequire(path.join(root, 'package.json'));
30
- const mod = require('typescript') as { default?: Ts } & Ts;
31
- return mod.default ?? mod;
32
- } catch {
33
- return null;
34
- }
35
- }
36
-
37
- /** True when a route pattern has dynamic (`:param` / `*catch-all`) segments. */
38
- function isDynamic(pattern: string): boolean {
39
- return /[:*]/.test(pattern);
40
- }
41
-
42
- /**
43
- * Builds the searchable page index from the scanned routes: every main-tree page (slots and
44
- * intercepting routes are excluded, they don't own a distinct URL) paired with its statically
45
- * extracted `metadata`. A route may also `export const searchHints` (a static `title`/`description`/
46
- * `keywords` object) to feed the index even when its real metadata is dynamic (`generateMetadata`);
47
- * hints are merged over the static `metadata`, winning ties. Reads each route file once.
48
- */
49
- export function buildPageIndex(root: string, routes: readonly ScannedRoute[]): PageIndexEntry[] {
50
- const ts = loadTypeScriptSync(root);
51
- const seen = new Set<string>();
52
- const pages: PageIndexEntry[] = [];
53
- for (const route of routes) {
54
- if (route.slot !== undefined || route.intercept) continue;
55
- if (seen.has(route.pattern)) continue;
56
- seen.add(route.pattern);
57
- const exports = ts ? extractStaticExports(ts, route.file, ['metadata', 'searchHints']) : {};
58
- const metadata = { ...exports.metadata, ...exports.searchHints };
59
- pages.push({ path: route.pattern, dynamic: isDynamic(route.pattern), metadata });
60
- }
61
- // Stable order (by path) so the generated module is deterministic across runs.
62
- pages.sort((a, b) => a.path.localeCompare(b.path));
63
- return pages;
64
- }
65
-
66
- /** Serializes the page index to the `export const pages` literal embedded in the routes module. */
67
- export function pagesModuleSource(pages: readonly PageIndexEntry[]): string {
68
- const body = pages.map((p) => ` ${JSON.stringify(p)},`).join('\n');
69
- return `export const pages: PageMeta[] = [\n${body}\n];\n`;
70
- }
1
+ import { createRequire } from 'node:module';
2
+ import path from 'node:path';
3
+
4
+ import type * as TS from 'typescript';
5
+
6
+ import { extractStaticExports } from './prerender.js';
7
+ import type { ScannedRoute } from './routes.js';
8
+
9
+ type Ts = typeof TS;
10
+
11
+ /**
12
+ * A page in the build-time search index: its URL pattern, whether it's dynamic, and the
13
+ * statically-extracted `metadata` literal (the searchable subset; dynamic `generateMetadata` and
14
+ * computed values can't be known at build, so they're absent). Serialized into the generated
15
+ * `routes` module and registered client-side for {@link searchPages}.
16
+ */
17
+ export interface PageIndexEntry {
18
+ readonly path: string;
19
+ readonly dynamic: boolean;
20
+ readonly metadata: Record<string, unknown>;
21
+ }
22
+
23
+ /**
24
+ * Loads the project's TypeScript synchronously (so {@link buildPageIndex} can run inside the sync
25
+ * `generate()`), or `null` if it isn't installed, in which case pages are indexed by path only.
26
+ */
27
+ function loadTypeScriptSync(root: string): Ts | null {
28
+ try {
29
+ const require = createRequire(path.join(root, 'package.json'));
30
+ const mod = require('typescript') as { default?: Ts } & Ts;
31
+ return mod.default ?? mod;
32
+ } catch {
33
+ return null;
34
+ }
35
+ }
36
+
37
+ /** True when a route pattern has dynamic (`:param` / `*catch-all`) segments. */
38
+ function isDynamic(pattern: string): boolean {
39
+ return /[:*]/.test(pattern);
40
+ }
41
+
42
+ /**
43
+ * Builds the searchable page index from the scanned routes: every main-tree page (slots and
44
+ * intercepting routes are excluded, they don't own a distinct URL) paired with its statically
45
+ * extracted `metadata`. A route may also `export const searchHints` (a static `title`/`description`/
46
+ * `keywords` object) to feed the index even when its real metadata is dynamic (`generateMetadata`);
47
+ * hints are merged over the static `metadata`, winning ties. Reads each route file once.
48
+ */
49
+ export function buildPageIndex(root: string, routes: readonly ScannedRoute[]): PageIndexEntry[] {
50
+ const ts = loadTypeScriptSync(root);
51
+ const seen = new Set<string>();
52
+ const pages: PageIndexEntry[] = [];
53
+ for (const route of routes) {
54
+ if (route.slot !== undefined || route.intercept) continue;
55
+ if (seen.has(route.pattern)) continue;
56
+ seen.add(route.pattern);
57
+ const exports = ts ? extractStaticExports(ts, route.file, ['metadata', 'searchHints']) : {};
58
+ const metadata = { ...exports.metadata, ...exports.searchHints };
59
+ pages.push({ path: route.pattern, dynamic: isDynamic(route.pattern), metadata });
60
+ }
61
+ // Stable order (by path) so the generated module is deterministic across runs.
62
+ pages.sort((a, b) => a.path.localeCompare(b.path));
63
+ return pages;
64
+ }
65
+
66
+ /** Serializes the page index to the `export const pages` literal embedded in the routes module. */
67
+ export function pagesModuleSource(pages: readonly PageIndexEntry[]): string {
68
+ const body = pages.map((p) => ` ${JSON.stringify(p)},`).join('\n');
69
+ return `export const pages: PageMeta[] = [\n${body}\n];\n`;
70
+ }
@@ -1,7 +1,120 @@
1
- import { type Plugin } from 'vite';
1
+ import { spawn } from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import { createRequire } from 'node:module';
4
+ import path from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
2
6
 
3
- import { type ResolvedToilConfig } from './config.js';
7
+ import { type Plugin, version as viteVersion } from 'vite';
8
+
9
+ import { AiProvider, type DevtoolsAiConfig, type ResolvedToilConfig } from './config.js';
4
10
  import { generate } from './generate.js';
11
+ import { scanRoutes } from './routes.js';
12
+
13
+ /** Calls the configured AI provider (server-side, so the key never reaches the browser). */
14
+ async function aiComplete(ai: DevtoolsAiConfig, prompt: string): Promise<string> {
15
+ const key = ai.apiKeyEnv ? process.env[ai.apiKeyEnv] : undefined;
16
+ if (ai.endpoint) {
17
+ const r = await fetch(ai.endpoint, {
18
+ method: 'POST',
19
+ headers: { 'content-type': 'application/json' },
20
+ body: JSON.stringify({ prompt }),
21
+ });
22
+ const j = (await r.json()) as { text?: string };
23
+ return j.text ?? '';
24
+ }
25
+ if (ai.provider === AiProvider.OpenAI) {
26
+ if (!key) throw new Error(`missing API key (set env ${ai.apiKeyEnv ?? 'OPENAI_API_KEY'})`);
27
+ const r = await fetch('https://api.openai.com/v1/chat/completions', {
28
+ method: 'POST',
29
+ headers: { 'content-type': 'application/json', authorization: `Bearer ${key}` },
30
+ body: JSON.stringify({
31
+ model: ai.model ?? 'gpt-4o',
32
+ messages: [{ role: 'user', content: prompt }],
33
+ }),
34
+ });
35
+ const j = (await r.json()) as { choices?: { message?: { content?: string } }[] };
36
+ return j.choices?.[0]?.message?.content ?? '';
37
+ }
38
+ // default: anthropic
39
+ if (!key) throw new Error(`missing API key (set env ${ai.apiKeyEnv ?? 'ANTHROPIC_API_KEY'})`);
40
+ const r = await fetch('https://api.anthropic.com/v1/messages', {
41
+ method: 'POST',
42
+ headers: {
43
+ 'content-type': 'application/json',
44
+ 'x-api-key': key,
45
+ 'anthropic-version': '2023-06-01',
46
+ },
47
+ body: JSON.stringify({
48
+ model: ai.model ?? 'claude-sonnet-4-6',
49
+ max_tokens: 1024,
50
+ messages: [{ role: 'user', content: prompt }],
51
+ }),
52
+ });
53
+ const j = (await r.json()) as { content?: { text?: string }[] };
54
+ return (j.content ?? []).map((c) => c.text ?? '').join('');
55
+ }
56
+
57
+ /** Reads a package's version resolved from `<fromDir>`, or 'unknown'. */
58
+ function depVersion(fromDir: string, name: string): string {
59
+ try {
60
+ const pkgPath = createRequire(path.join(fromDir, 'package.json')).resolve(`${name}/package.json`);
61
+ const raw = JSON.parse(fs.readFileSync(pkgPath, 'utf8')) as { version?: string };
62
+ return raw.version ?? 'unknown';
63
+ } catch {
64
+ return 'unknown';
65
+ }
66
+ }
67
+
68
+ /** toiljs's own version (package.json two levels up from build/compiler). */
69
+ function frameworkVersion(): string {
70
+ try {
71
+ const p = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'package.json');
72
+ const raw = JSON.parse(fs.readFileSync(p, 'utf8')) as { version?: string };
73
+ return raw.version ?? '0.0.0';
74
+ } catch {
75
+ return '0.0.0';
76
+ }
77
+ }
78
+
79
+ /** Build/config snapshot served to the dev toolbar at `/__toil/devinfo`. */
80
+ function devInfo(cfg: ResolvedToilConfig, port: number): Record<string, unknown> {
81
+ const routes: Record<string, string> = {};
82
+ for (const r of scanRoutes(cfg.routesAbsDir)) {
83
+ if (r.slot === undefined && !r.intercept) routes[r.pattern] = r.file;
84
+ }
85
+ return {
86
+ toiljs: frameworkVersion(),
87
+ // vite is a dependency of the framework, not the app, so resolving it from the app root
88
+ // fails; read the running vite's own exported version instead.
89
+ vite: viteVersion,
90
+ react: depVersion(cfg.root, 'react'),
91
+ port,
92
+ enabled: cfg.devtools,
93
+ flags: {
94
+ images: cfg.images,
95
+ fonts: cfg.fonts,
96
+ viewTransitions: cfg.viewTransitions,
97
+ transitions: cfg.transitions,
98
+ seo: cfg.seo != null,
99
+ },
100
+ routes,
101
+ ai: cfg.devtoolsAi !== null,
102
+ };
103
+ }
104
+
105
+ /** Opens `file` in the user's editor (best-effort): `$EDITOR file`, else `code -g file`. */
106
+ function openInEditor(file: string): void {
107
+ try {
108
+ const editor = process.env.EDITOR ?? process.env.VISUAL;
109
+ const child = editor
110
+ ? spawn(editor, [file], { stdio: 'ignore', detached: true })
111
+ : spawn('code', ['-g', file], { stdio: 'ignore', detached: true });
112
+ child.on('error', () => undefined);
113
+ child.unref();
114
+ } catch {
115
+ /* ignore */
116
+ }
117
+ }
5
118
 
6
119
  /**
7
120
  * Vite plugin that keeps the generated route table in sync during dev: when a route file is
@@ -35,6 +148,61 @@ export function toilPlugin(cfg: ResolvedToilConfig): Plugin {
35
148
  return null;
36
149
  },
37
150
  configureServer(server) {
151
+ // Dev toolbar endpoints (dev only). `/__toil/devinfo` -> build/config snapshot;
152
+ // `/__toil/open?file=` -> open the file in the editor.
153
+ server.middlewares.use('/__toil/devinfo', (_req, res) => {
154
+ const port = server.config.server.port ?? cfg.port;
155
+ res.setHeader('content-type', 'application/json');
156
+ res.end(JSON.stringify(devInfo(cfg, port)));
157
+ });
158
+ // `/__toil/ai` -> server-side AI proxy. The key is read from the env here and never
159
+ // reaches the browser; 404 when AI isn't configured (the toolbar then only hands off).
160
+ server.middlewares.use('/__toil/ai', (req, res) => {
161
+ if (req.method !== 'POST') {
162
+ res.statusCode = 405;
163
+ res.end();
164
+ return;
165
+ }
166
+ const ai = cfg.devtoolsAi;
167
+ if (!ai) {
168
+ res.statusCode = 404;
169
+ res.end();
170
+ return;
171
+ }
172
+ let body = '';
173
+ req.on('data', (chunk) => (body += String(chunk)));
174
+ req.on('end', () => {
175
+ void (async () => {
176
+ try {
177
+ const { prompt } = JSON.parse(body || '{}') as { prompt?: string };
178
+ const text = await aiComplete(ai, prompt ?? '');
179
+ res.setHeader('content-type', 'application/json');
180
+ res.end(JSON.stringify({ text }));
181
+ } catch (e) {
182
+ res.statusCode = 500;
183
+ res.setHeader('content-type', 'application/json');
184
+ res.end(
185
+ JSON.stringify({ error: e instanceof Error ? e.message : String(e) }),
186
+ );
187
+ }
188
+ })();
189
+ });
190
+ });
191
+ server.middlewares.use('/__toil/open', (req, res) => {
192
+ try {
193
+ const url = new URL(req.url ?? '', 'http://localhost');
194
+ const file = url.searchParams.get('file');
195
+ const abs = file ? path.resolve(file) : '';
196
+ // Only files inside the project root, never an arbitrary path.
197
+ if (abs && abs.startsWith(cfg.root) && fs.existsSync(abs)) openInEditor(abs);
198
+ res.statusCode = 204;
199
+ res.end();
200
+ } catch {
201
+ res.statusCode = 400;
202
+ res.end();
203
+ }
204
+ });
205
+
38
206
  // Trailing slash so a sibling like `routes-extra/` doesn't match the `routes/` prefix.
39
207
  const routesPrefix = cfg.routesAbsDir.replace(/\\/g, '/').replace(/\/?$/, '/');
40
208
  const onChange = (file: string): void => {
@@ -13,7 +13,7 @@ import { injectSeoHtml, routeSeo } from './seo.js';
13
13
  type Ts = typeof TS;
14
14
 
15
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> {
16
+ export async function loadTypeScript(root: string): Promise<Ts | null> {
17
17
  try {
18
18
  const resolved = createRequire(path.join(root, 'package.json')).resolve('typescript');
19
19
  const mod = (await import(pathToFileURL(resolved).href)) as { default?: Ts } & Ts;
@@ -132,6 +132,10 @@ export function prerenderPlugin(cfg: ResolvedToilConfig): Plugin {
132
132
  const shellPath = path.join(outDir, 'index.html');
133
133
  if (!fs.existsSync(shellPath)) return;
134
134
  const shell = fs.readFileSync(shellPath, 'utf8');
135
+ // Stash the clean built shell (asset tags, no per-route SEO yet) so the post-build SSG
136
+ // pass bakes dynamic routes from it rather than from this file once it's been overwritten
137
+ // with the `/` route's head (which would duplicate canonical/og tags).
138
+ fs.writeFileSync(path.join(cfg.toilDir, 'shell.html'), shell);
135
139
  const ts = await loadTypeScript(cfg.root);
136
140
 
137
141
  const routes = scanRoutes(cfg.routesAbsDir).filter(
@@ -340,17 +340,30 @@ export function robotsTxt(seo: SeoConfig): string {
340
340
  return blocks.join('\n\n') + '\n';
341
341
  }
342
342
 
343
- /** `sitemap.xml` from the site's static routes (requires `seo.url`); empty when no base URL. */
344
- export function sitemapXml(seo: SeoConfig, routes: readonly ScannedRoute[]): string {
343
+ /**
344
+ * `sitemap.xml` from the site's static routes plus any `extra` concrete paths (e.g. SSG URLs from
345
+ * `generateStaticParams`); requires `seo.url`, empty when no base URL. `extra` is deduped against the
346
+ * static paths.
347
+ */
348
+ export function sitemapXml(
349
+ seo: SeoConfig,
350
+ routes: readonly ScannedRoute[],
351
+ extra: readonly string[] = [],
352
+ ): string {
345
353
  if (seo.url === undefined || seo.sitemap === false) return '';
346
- const urls = staticPaths(routes)
354
+ const paths = [...new Set([...staticPaths(routes), ...extra])];
355
+ const urls = paths
347
356
  .map((p) => ` <url><loc>${escapeHtml(joinUrl(seo.url ?? '', p))}</loc></url>`)
348
357
  .join('\n');
349
358
  return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>\n`;
350
359
  }
351
360
 
352
361
  /** `llms.txt` (AI-crawler guidance) contents; empty when disabled. */
353
- export function llmsTxt(seo: SeoConfig, routes: readonly ScannedRoute[]): string {
362
+ export function llmsTxt(
363
+ seo: SeoConfig,
364
+ routes: readonly ScannedRoute[],
365
+ pages?: readonly LlmsPage[],
366
+ ): string {
354
367
  if (seo.llms === false) return '';
355
368
  const cfg: LlmsConfig = seo.llms === true || seo.llms === undefined ? {} : seo.llms;
356
369
  const title = cfg.title ?? seo.title ?? seo.url ?? 'Site';
@@ -359,8 +372,11 @@ export function llmsTxt(seo: SeoConfig, routes: readonly ScannedRoute[]): string
359
372
  if (summary !== undefined) out.push(`\n> ${summary}`);
360
373
  if (cfg.instructions !== undefined) out.push(`\n${cfg.instructions}`);
361
374
 
362
- const pages: readonly LlmsPage[] =
375
+ // Page list precedence: an explicit `seo.llms.pages`, else the build-supplied list (every route's
376
+ // resolved title/description, including SSG-enumerated dynamic pages), else just the static paths.
377
+ const resolvedPages: readonly LlmsPage[] =
363
378
  cfg.pages ??
379
+ pages ??
364
380
  (seo.url !== undefined
365
381
  ? staticPaths(routes).map(
366
382
  (p): LlmsPage => ({
@@ -369,9 +385,9 @@ export function llmsTxt(seo: SeoConfig, routes: readonly ScannedRoute[]): string
369
385
  }),
370
386
  )
371
387
  : []);
372
- if (pages.length) {
388
+ if (resolvedPages.length) {
373
389
  out.push('\n## Pages\n');
374
- for (const page of pages) {
390
+ for (const page of resolvedPages) {
375
391
  out.push(
376
392
  `- [${page.title}](${page.url})${page.description !== undefined ? `: ${page.description}` : ''}`,
377
393
  );