toiljs 0.0.15 → 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 (217) 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 +0 -0
  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/navigation/prefetch.d.ts +1 -0
  28. package/build/client/navigation/prefetch.js +35 -0
  29. package/build/client/routing/Router.js +1 -1
  30. package/build/client/routing/hooks.js +6 -2
  31. package/build/client/routing/loader.d.ts +23 -0
  32. package/build/client/routing/loader.js +53 -7
  33. package/build/client/routing/mount.js +4 -3
  34. package/build/compiler/.tsbuildinfo +1 -1
  35. package/build/compiler/config.d.ts +16 -0
  36. package/build/compiler/config.js +7 -0
  37. package/build/compiler/docs.js +16 -16
  38. package/build/compiler/index.d.ts +2 -2
  39. package/build/compiler/index.js +1 -1
  40. package/build/compiler/plugin.js +156 -0
  41. package/build/compiler/prerender.d.ts +1 -0
  42. package/build/compiler/prerender.js +1 -1
  43. package/build/compiler/seo.d.ts +1 -1
  44. package/build/compiler/seo.js +5 -4
  45. package/build/compiler/ssg.js +32 -1
  46. package/build/io/.tsbuildinfo +1 -1
  47. package/build/logger/.tsbuildinfo +1 -1
  48. package/build/shared/.tsbuildinfo +1 -1
  49. package/eslint.config.js +48 -48
  50. package/examples/basic/client/404.tsx +11 -11
  51. package/examples/basic/client/components/.gitkeep +1 -1
  52. package/examples/basic/client/global-error.tsx +13 -13
  53. package/examples/basic/client/layout.tsx +25 -25
  54. package/examples/basic/client/public/images/.gitkeep +1 -1
  55. package/examples/basic/client/public/images/logo.svg +36 -36
  56. package/examples/basic/client/public/robots.txt +2 -2
  57. package/examples/basic/client/routes/docs/[...slug].tsx +12 -12
  58. package/examples/basic/client/routes/features/error/error.tsx +16 -16
  59. package/examples/basic/client/routes/features/template/b.tsx +14 -14
  60. package/examples/basic/client/routes/files/[[...slug]].tsx +21 -21
  61. package/examples/basic/client/routes/gallery/layout.tsx +13 -13
  62. package/examples/basic/client/routes/io.tsx +24 -24
  63. package/examples/basic/client/routes/loader-demo/loading.tsx +13 -13
  64. package/examples/basic/client/routes/search.tsx +61 -61
  65. package/examples/basic/client/toil.tsx +5 -5
  66. package/package.json +155 -148
  67. package/presets/eslint.js +88 -88
  68. package/presets/no-uint8array-tostring.js +200 -200
  69. package/presets/prettier.json +18 -18
  70. package/presets/tsconfig.json +37 -37
  71. package/src/backend/index.ts +160 -160
  72. package/src/cli/proc.ts +50 -50
  73. package/src/cli/updates.ts +69 -69
  74. package/src/cli/validate.ts +31 -31
  75. package/src/client/channel/channel.ts +146 -146
  76. package/src/client/components/Form.tsx +65 -65
  77. package/src/client/components/Script.tsx +113 -113
  78. package/src/client/components/Slot.tsx +21 -21
  79. package/src/client/dev/devtools.tsx +973 -0
  80. package/src/client/dev/error-overlay.tsx +30 -4
  81. package/src/client/head/head.ts +167 -167
  82. package/src/client/head/metadata.ts +112 -112
  83. package/src/client/index.ts +89 -89
  84. package/src/client/navigation/NavLink.tsx +86 -86
  85. package/src/client/navigation/navigation.ts +235 -235
  86. package/src/client/navigation/prefetch.ts +169 -130
  87. package/src/client/navigation/scroll.ts +53 -53
  88. package/src/client/routing/Router.tsx +8 -2
  89. package/src/client/routing/action.ts +122 -122
  90. package/src/client/routing/error-boundary.tsx +43 -43
  91. package/src/client/routing/hooks.ts +21 -6
  92. package/src/client/routing/loader.ts +325 -235
  93. package/src/client/routing/match.ts +47 -47
  94. package/src/client/routing/mount.tsx +54 -52
  95. package/src/client/routing/params-context.ts +10 -10
  96. package/src/client/routing/slot-context.ts +7 -7
  97. package/src/client/search/search.ts +189 -189
  98. package/src/client/search/use-page-search.ts +73 -73
  99. package/src/client/types.ts +73 -73
  100. package/src/compiler/config.ts +219 -182
  101. package/src/compiler/docs.ts +228 -228
  102. package/src/compiler/generate.ts +394 -394
  103. package/src/compiler/index.ts +64 -57
  104. package/src/compiler/pages.ts +70 -70
  105. package/src/compiler/plugin.ts +170 -2
  106. package/src/compiler/prerender.ts +156 -156
  107. package/src/compiler/seo.ts +397 -390
  108. package/src/compiler/ssg.ts +162 -126
  109. package/src/io/BinaryReader.ts +340 -340
  110. package/src/io/BinaryWriter.ts +385 -385
  111. package/src/io/FastMap.ts +127 -127
  112. package/src/io/index.ts +11 -11
  113. package/src/io/lengths.ts +14 -14
  114. package/src/io/types.ts +18 -18
  115. package/src/logger/index.ts +22 -22
  116. package/src/server/index.ts +10 -10
  117. package/src/server/main.ts +13 -13
  118. package/src/server/tsconfig.json +4 -4
  119. package/src/shared/index.ts +10 -10
  120. package/std/client/index.d.ts +15 -15
  121. package/std/client/package.json +3 -3
  122. package/test/assembly/example.spec.ts +7 -7
  123. package/test/channel.test.ts +21 -21
  124. package/test/dom/Link.test.tsx +47 -47
  125. package/test/dom/NavLink.test.tsx +37 -37
  126. package/test/dom/error-overlay.test.tsx +44 -44
  127. package/test/dom/loader.test.tsx +121 -121
  128. package/test/dom/navigation.test.ts +59 -59
  129. package/test/dom/revalidate.test.tsx +38 -38
  130. package/test/dom/route-head.test.tsx +78 -78
  131. package/test/dom/router-loading.test.tsx +44 -44
  132. package/test/dom/scroll.test.ts +56 -56
  133. package/test/dom/use-metadata.test.tsx +58 -58
  134. package/test/io.test.ts +93 -93
  135. package/test/navlink.test.ts +28 -28
  136. package/test/placeholder.test.ts +9 -9
  137. package/test/routes.test.ts +76 -76
  138. package/test/seo.test.ts +175 -164
  139. package/test/slot-layouts.test.ts +69 -69
  140. package/test/ssg.test.ts +36 -36
  141. package/test/update.test.ts +44 -44
  142. package/test/validate.test.ts +42 -42
  143. package/toil-routes.d.ts +7 -0
  144. package/toilconfig.json +30 -30
  145. package/tsconfig.backend.json +13 -13
  146. package/tsconfig.base.json +35 -35
  147. package/tsconfig.cli.json +13 -13
  148. package/tsconfig.client.json +14 -14
  149. package/tsconfig.compiler.json +13 -13
  150. package/tsconfig.io.json +12 -12
  151. package/tsconfig.json +22 -22
  152. package/tsconfig.logger.json +12 -12
  153. package/tsconfig.server.json +10 -10
  154. package/tsconfig.shared.json +12 -12
  155. package/vitest.config.ts +26 -26
  156. package/.idea/codeStyles/Project.xml +0 -54
  157. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  158. package/.idea/inspectionProfiles/Project_Default.xml +0 -6
  159. package/.idea/modules.xml +0 -8
  160. package/.idea/prettier.xml +0 -7
  161. package/.idea/toiljs.iml +0 -8
  162. package/.idea/vcs.xml +0 -6
  163. package/.toil/entry.tsx +0 -9
  164. package/.toil/index.html +0 -12
  165. package/.toil/routes.ts +0 -9
  166. package/build/cli/configure.d.ts +0 -16
  167. package/build/cli/configure.js +0 -272
  168. package/build/cli/create.d.ts +0 -16
  169. package/build/cli/create.js +0 -420
  170. package/build/cli/diagnostics.d.ts +0 -55
  171. package/build/cli/diagnostics.js +0 -333
  172. package/build/cli/doctor.d.ts +0 -6
  173. package/build/cli/doctor.js +0 -249
  174. package/build/cli/features.d.ts +0 -25
  175. package/build/cli/features.js +0 -107
  176. package/build/cli/index.d.ts +0 -2
  177. package/build/cli/proc.d.ts +0 -6
  178. package/build/cli/proc.js +0 -31
  179. package/build/cli/ui.d.ts +0 -9
  180. package/build/cli/ui.js +0 -75
  181. package/build/cli/update.d.ts +0 -7
  182. package/build/cli/update.js +0 -117
  183. package/build/cli/updates.d.ts +0 -10
  184. package/build/cli/updates.js +0 -45
  185. package/build/cli/validate.d.ts +0 -4
  186. package/build/cli/validate.js +0 -19
  187. package/build/client/Link.d.ts +0 -8
  188. package/build/client/Link.js +0 -44
  189. package/build/client/NavLink.d.ts +0 -14
  190. package/build/client/NavLink.js +0 -37
  191. package/build/client/Router.d.ts +0 -7
  192. package/build/client/Router.js +0 -55
  193. package/build/client/channel.d.ts +0 -23
  194. package/build/client/channel.js +0 -94
  195. package/build/client/error-boundary.d.ts +0 -16
  196. package/build/client/error-boundary.js +0 -19
  197. package/build/client/head.d.ts +0 -26
  198. package/build/client/head.js +0 -87
  199. package/build/client/hooks.d.ts +0 -17
  200. package/build/client/hooks.js +0 -48
  201. package/build/client/lazy.d.ts +0 -16
  202. package/build/client/lazy.js +0 -53
  203. package/build/client/match.d.ts +0 -2
  204. package/build/client/match.js +0 -32
  205. package/build/client/mount.d.ts +0 -2
  206. package/build/client/mount.js +0 -13
  207. package/build/client/navigation.d.ts +0 -13
  208. package/build/client/navigation.js +0 -97
  209. package/build/client/params-context.d.ts +0 -2
  210. package/build/client/params-context.js +0 -2
  211. package/build/client/prefetch.d.ts +0 -11
  212. package/build/client/prefetch.js +0 -100
  213. package/build/client/runtime.d.ts +0 -31
  214. package/build/client/runtime.js +0 -112
  215. package/build/client/scroll.d.ts +0 -8
  216. package/build/client/scroll.js +0 -36
  217. package/toil-env.d.ts +0 -16
@@ -1,57 +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 { 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 } 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 { ToilConfig, ResolvedToilConfig } from './config.js';
57
- 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 => {