toiljs 0.0.116 → 0.0.117

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 (325) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +16 -7
  3. package/build/backend/.tsbuildinfo +1 -1
  4. package/build/cli/.tsbuildinfo +1 -1
  5. package/build/cli/index.js +2375 -258
  6. package/build/client/.tsbuildinfo +1 -1
  7. package/build/client/dev/devtools.js +1 -1
  8. package/build/client/dev/error-overlay.js +1 -1
  9. package/build/client/routing/mount.js +1 -1
  10. package/build/compiler/.tsbuildinfo +1 -1
  11. package/build/compiler/auth-emails.js +7 -3
  12. package/build/compiler/emails.js +1 -1
  13. package/build/compiler/index.d.ts +1 -0
  14. package/build/compiler/index.js +25 -4
  15. package/build/compiler/pages.js +2 -3
  16. package/build/compiler/prerender.d.ts +3 -8
  17. package/build/compiler/prerender.js +88 -127
  18. package/build/compiler/ssg.js +3 -4
  19. package/build/compiler/template-build.js +3 -4
  20. package/build/compiler/toil-docs.generated.js +6 -5
  21. package/build/compiler/vite.js +3 -6
  22. package/build/devserver/.tsbuildinfo +1 -1
  23. package/build/devserver/db/database.js +7 -3
  24. package/build/devserver/db/types.js +1 -1
  25. package/build/devserver/production.js +4 -3
  26. package/build/devserver/runtime/crypto.d.ts +1 -1
  27. package/build/io/.tsbuildinfo +1 -1
  28. package/build/logger/.tsbuildinfo +1 -1
  29. package/build/shared/.tsbuildinfo +1 -1
  30. package/examples/basic/client/routes/analytics.tsx +7 -5
  31. package/examples/basic/server/tsconfig.json +1 -3
  32. package/package.json +45 -44
  33. package/presets/no-uint8array-tostring.js +120 -153
  34. package/presets/oxlint.d.ts +5 -0
  35. package/presets/oxlint.js +224 -0
  36. package/server/runtime/tsconfig.json +1 -3
  37. package/tsconfig.json +2 -11
  38. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  39. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
  40. package/.github/ISSUE_TEMPLATE/config.yml +0 -8
  41. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  42. package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
  43. package/.github/changelog-config.json +0 -45
  44. package/.github/dependabot.yml +0 -27
  45. package/.github/workflows/ci.yml +0 -222
  46. package/.vscode/settings.json +0 -10
  47. package/docs/README.md +0 -107
  48. package/docs/auth/README.md +0 -104
  49. package/docs/auth/configuration.md +0 -105
  50. package/docs/auth/emails.md +0 -92
  51. package/docs/auth/extending.md +0 -202
  52. package/docs/auth/how-it-works.md +0 -138
  53. package/docs/auth/usage.md +0 -188
  54. package/docs/backend/README.md +0 -143
  55. package/docs/backend/data.md +0 -351
  56. package/docs/backend/rest.md +0 -402
  57. package/docs/backend/rpc.md +0 -226
  58. package/docs/background/README.md +0 -114
  59. package/docs/background/daemons.md +0 -276
  60. package/docs/background/derive.md +0 -179
  61. package/docs/cli/README.md +0 -379
  62. package/docs/concepts/ai-guide.md +0 -132
  63. package/docs/concepts/config.md +0 -416
  64. package/docs/concepts/decorators.md +0 -127
  65. package/docs/concepts/security.md +0 -108
  66. package/docs/concepts/tiers.md +0 -166
  67. package/docs/concepts/types.md +0 -216
  68. package/docs/database/README.md +0 -143
  69. package/docs/database/capacity.md +0 -350
  70. package/docs/database/counters.md +0 -174
  71. package/docs/database/documents.md +0 -277
  72. package/docs/database/events.md +0 -307
  73. package/docs/database/membership.md +0 -246
  74. package/docs/database/setup.md +0 -155
  75. package/docs/database/unique.md +0 -216
  76. package/docs/database/views.md +0 -246
  77. package/docs/frontend/README.md +0 -104
  78. package/docs/frontend/components.md +0 -286
  79. package/docs/frontend/data-fetching.md +0 -260
  80. package/docs/frontend/images.md +0 -148
  81. package/docs/frontend/metadata.md +0 -344
  82. package/docs/frontend/navigation.md +0 -296
  83. package/docs/frontend/rendering.md +0 -236
  84. package/docs/frontend/routing.md +0 -346
  85. package/docs/frontend/scripts.md +0 -118
  86. package/docs/frontend/search.md +0 -191
  87. package/docs/frontend/styling.md +0 -147
  88. package/docs/frontend/toil-global.md +0 -216
  89. package/docs/getting-started/README.md +0 -81
  90. package/docs/getting-started/create-project.md +0 -131
  91. package/docs/getting-started/deploy.md +0 -101
  92. package/docs/getting-started/first-app.md +0 -215
  93. package/docs/getting-started/installation.md +0 -124
  94. package/docs/getting-started/migrating.md +0 -125
  95. package/docs/getting-started/project-structure.md +0 -163
  96. package/docs/introduction/README.md +0 -66
  97. package/docs/introduction/design-principles.md +0 -81
  98. package/docs/introduction/distributed.md +0 -91
  99. package/docs/introduction/how-it-works.md +0 -121
  100. package/docs/introduction/hyperscale.md +0 -70
  101. package/docs/introduction/modern-stack.md +0 -85
  102. package/docs/introduction/vs-other-frameworks.md +0 -64
  103. package/docs/introduction/why-toil.md +0 -99
  104. package/docs/llms.txt +0 -95
  105. package/docs/realtime/README.md +0 -102
  106. package/docs/realtime/channels.md +0 -211
  107. package/docs/realtime/streams.md +0 -369
  108. package/docs/services/README.md +0 -60
  109. package/docs/services/analytics.md +0 -279
  110. package/docs/services/caching.md +0 -175
  111. package/docs/services/cookies.md +0 -235
  112. package/docs/services/crypto.md +0 -209
  113. package/docs/services/email.md +0 -289
  114. package/docs/services/environment.md +0 -141
  115. package/docs/services/ratelimit.md +0 -174
  116. package/docs/services/time.md +0 -85
  117. package/eslint.config.js +0 -57
  118. package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
  119. package/presets/eslint.js +0 -88
  120. package/src/backend/index.ts +0 -204
  121. package/src/cli/configure.ts +0 -363
  122. package/src/cli/create.ts +0 -924
  123. package/src/cli/db.ts +0 -158
  124. package/src/cli/diagnostics.ts +0 -665
  125. package/src/cli/doctor.ts +0 -982
  126. package/src/cli/features.ts +0 -170
  127. package/src/cli/index.ts +0 -326
  128. package/src/cli/notify.ts +0 -120
  129. package/src/cli/proc.ts +0 -56
  130. package/src/cli/ui.ts +0 -169
  131. package/src/cli/update.ts +0 -248
  132. package/src/cli/updates.ts +0 -92
  133. package/src/cli/validate.ts +0 -31
  134. package/src/cli/version-check.ts +0 -125
  135. package/src/client/auth.ts +0 -869
  136. package/src/client/channel/channel.ts +0 -146
  137. package/src/client/components/Form.tsx +0 -65
  138. package/src/client/components/Image.tsx +0 -160
  139. package/src/client/components/Script.tsx +0 -113
  140. package/src/client/components/Slot.tsx +0 -21
  141. package/src/client/dev/devtools.tsx +0 -1089
  142. package/src/client/dev/error-overlay.tsx +0 -225
  143. package/src/client/errors.ts +0 -11
  144. package/src/client/head/head.ts +0 -182
  145. package/src/client/head/metadata.ts +0 -110
  146. package/src/client/index.ts +0 -130
  147. package/src/client/navigation/Link.tsx +0 -108
  148. package/src/client/navigation/NavLink.tsx +0 -86
  149. package/src/client/navigation/navigation.ts +0 -235
  150. package/src/client/navigation/prefetch.ts +0 -169
  151. package/src/client/navigation/scroll.ts +0 -53
  152. package/src/client/routing/Router.tsx +0 -216
  153. package/src/client/routing/action.ts +0 -122
  154. package/src/client/routing/error-boundary.tsx +0 -43
  155. package/src/client/routing/hooks.ts +0 -140
  156. package/src/client/routing/lazy.ts +0 -77
  157. package/src/client/routing/loader.ts +0 -381
  158. package/src/client/routing/match.ts +0 -47
  159. package/src/client/routing/mount.tsx +0 -90
  160. package/src/client/routing/params-context.ts +0 -10
  161. package/src/client/routing/slot-context.ts +0 -7
  162. package/src/client/rpc.ts +0 -97
  163. package/src/client/search/search.ts +0 -189
  164. package/src/client/search/use-page-search.ts +0 -73
  165. package/src/client/ssr/markers.tsx +0 -172
  166. package/src/client/stream/client.ts +0 -237
  167. package/src/client/types.ts +0 -73
  168. package/src/compiler/auth-emails.ts +0 -312
  169. package/src/compiler/config.ts +0 -369
  170. package/src/compiler/docs.ts +0 -127
  171. package/src/compiler/email-preview.ts +0 -314
  172. package/src/compiler/emails.ts +0 -404
  173. package/src/compiler/fonts.ts +0 -89
  174. package/src/compiler/generate.ts +0 -471
  175. package/src/compiler/image-blur.ts +0 -42
  176. package/src/compiler/image-report.ts +0 -88
  177. package/src/compiler/index.ts +0 -1362
  178. package/src/compiler/pages.ts +0 -49
  179. package/src/compiler/plugin.ts +0 -391
  180. package/src/compiler/prerender.ts +0 -263
  181. package/src/compiler/routes.ts +0 -170
  182. package/src/compiler/seo.ts +0 -415
  183. package/src/compiler/sri.ts +0 -144
  184. package/src/compiler/ssg.ts +0 -177
  185. package/src/compiler/ssr-codegen.ts +0 -85
  186. package/src/compiler/template-build.ts +0 -676
  187. package/src/compiler/template.ts +0 -262
  188. package/src/compiler/toil-docs.generated.ts +0 -77
  189. package/src/compiler/vite.ts +0 -245
  190. package/src/devserver/analytics/index.ts +0 -317
  191. package/src/devserver/config/dotenv.ts +0 -102
  192. package/src/devserver/config/env.ts +0 -30
  193. package/src/devserver/config/ratelimit.ts +0 -123
  194. package/src/devserver/daemon/catalog.ts +0 -120
  195. package/src/devserver/daemon/cron.ts +0 -87
  196. package/src/devserver/daemon/host.ts +0 -134
  197. package/src/devserver/daemon/index.ts +0 -369
  198. package/src/devserver/daemon/runtime.ts +0 -48
  199. package/src/devserver/db/catalog.ts +0 -109
  200. package/src/devserver/db/database.ts +0 -1834
  201. package/src/devserver/db/derives.ts +0 -121
  202. package/src/devserver/db/index.ts +0 -20
  203. package/src/devserver/db/routeKinds.ts +0 -191
  204. package/src/devserver/db/types.ts +0 -168
  205. package/src/devserver/email/caps.ts +0 -0
  206. package/src/devserver/email/config.ts +0 -141
  207. package/src/devserver/email/console.ts +0 -348
  208. package/src/devserver/email/index.ts +0 -155
  209. package/src/devserver/email/providers.ts +0 -131
  210. package/src/devserver/email/status.ts +0 -23
  211. package/src/devserver/email/validate.ts +0 -37
  212. package/src/devserver/email/wire.ts +0 -55
  213. package/src/devserver/http/cache.ts +0 -0
  214. package/src/devserver/http/envelope.ts +0 -157
  215. package/src/devserver/http/proxy.ts +0 -172
  216. package/src/devserver/http/runtime.ts +0 -387
  217. package/src/devserver/index.ts +0 -43
  218. package/src/devserver/production-ipc.ts +0 -63
  219. package/src/devserver/production-worker.ts +0 -83
  220. package/src/devserver/production.ts +0 -718
  221. package/src/devserver/runtime/crypto.ts +0 -587
  222. package/src/devserver/runtime/host.ts +0 -427
  223. package/src/devserver/runtime/module.ts +0 -442
  224. package/src/devserver/server.ts +0 -251
  225. package/src/devserver/ssr.ts +0 -208
  226. package/src/devserver/stream/catalog.ts +0 -106
  227. package/src/devserver/stream/host.ts +0 -42
  228. package/src/devserver/stream/index.ts +0 -301
  229. package/src/devserver/stream/manager.ts +0 -167
  230. package/src/devserver/stream/router.ts +0 -101
  231. package/src/devserver/stream/wire.ts +0 -58
  232. package/src/devserver/stream/ws.ts +0 -78
  233. package/src/devserver/wasm/sections.ts +0 -59
  234. package/src/devserver/wasm/surface.ts +0 -115
  235. package/src/io/FastMap.ts +0 -151
  236. package/src/io/FastSet.ts +0 -113
  237. package/src/io/codec.ts +0 -362
  238. package/src/io/index.ts +0 -10
  239. package/src/io/lengths.ts +0 -14
  240. package/src/io/types.ts +0 -19
  241. package/src/logger/index.ts +0 -22
  242. package/src/shared/index.ts +0 -46
  243. package/test/analytics-dev.test.ts +0 -113
  244. package/test/assembly/aspect-shim.ts +0 -33
  245. package/test/assembly/cookie.spec.ts +0 -303
  246. package/test/assembly/example.spec.ts +0 -22
  247. package/test/assembly/ssr.spec.ts +0 -95
  248. package/test/assembly.test.ts +0 -59
  249. package/test/auth-emails.test.ts +0 -118
  250. package/test/built-ssr.test.ts +0 -148
  251. package/test/channel.test.ts +0 -21
  252. package/test/config-host-port.test.ts +0 -59
  253. package/test/configure.test.ts +0 -94
  254. package/test/daemon-build.test.ts +0 -278
  255. package/test/daemon-catalog.test.ts +0 -349
  256. package/test/daemon-emulation.test.ts +0 -256
  257. package/test/db.test.ts +0 -0
  258. package/test/devserver-database.test.ts +0 -1102
  259. package/test/devserver-email.test.ts +0 -306
  260. package/test/devserver-pqauth.test.ts +0 -93
  261. package/test/devserver-secrets.test.ts +0 -63
  262. package/test/devserver.test.ts +0 -259
  263. package/test/doctor.test.ts +0 -298
  264. package/test/dom/Image.test.tsx +0 -144
  265. package/test/dom/Link.test.tsx +0 -67
  266. package/test/dom/NavLink.test.tsx +0 -37
  267. package/test/dom/Script.test.tsx +0 -48
  268. package/test/dom/action.test.tsx +0 -146
  269. package/test/dom/error-overlay.test.tsx +0 -44
  270. package/test/dom/loader.test.tsx +0 -121
  271. package/test/dom/navigation.test.ts +0 -59
  272. package/test/dom/revalidate.test.tsx +0 -38
  273. package/test/dom/route-head.test.tsx +0 -56
  274. package/test/dom/router-loading.test.tsx +0 -44
  275. package/test/dom/scroll.test.ts +0 -56
  276. package/test/dom/slot.test.tsx +0 -131
  277. package/test/dom/use-metadata.test.tsx +0 -58
  278. package/test/dom/view-transitions.test.tsx +0 -53
  279. package/test/dynamic-routing.test.ts +0 -132
  280. package/test/email-console.test.ts +0 -86
  281. package/test/email-preview.test.ts +0 -73
  282. package/test/emails.test.ts +0 -58
  283. package/test/errors.test.ts +0 -21
  284. package/test/example-guestbook.test.ts +0 -123
  285. package/test/features.test.ts +0 -149
  286. package/test/fixtures/bignum-wire/spec.ts +0 -25
  287. package/test/fixtures/daemon-app.ts +0 -65
  288. package/test/fixtures/stream-echo.ts +0 -26
  289. package/test/fixtures/stream-gate.ts +0 -28
  290. package/test/fixtures/stream-trap.ts +0 -18
  291. package/test/fixtures/stream-typed.ts +0 -41
  292. package/test/fonts.test.ts +0 -28
  293. package/test/global-setup.ts +0 -17
  294. package/test/head.test.ts +0 -42
  295. package/test/image-blur.test.ts +0 -50
  296. package/test/io.test.ts +0 -117
  297. package/test/metadata.test.ts +0 -40
  298. package/test/navlink.test.ts +0 -28
  299. package/test/pages.test.ts +0 -105
  300. package/test/placeholder.test.ts +0 -9
  301. package/test/pqauth-e2e.test.ts +0 -216
  302. package/test/pqauth-email.test.ts +0 -801
  303. package/test/prerender.test.ts +0 -116
  304. package/test/prettier-plugin.test.ts +0 -46
  305. package/test/routes.test.ts +0 -76
  306. package/test/rpc-bignum-wire.test.ts +0 -164
  307. package/test/rpc-dispatch.test.ts +0 -132
  308. package/test/rpc-kinds.test.ts +0 -18
  309. package/test/rpc.test.ts +0 -66
  310. package/test/search.test.ts +0 -114
  311. package/test/seo.test.ts +0 -175
  312. package/test/slot-layouts.test.ts +0 -69
  313. package/test/sri.test.ts +0 -129
  314. package/test/ssg.test.ts +0 -36
  315. package/test/ssr-hydration.test.tsx +0 -122
  316. package/test/ssr-render.test.ts +0 -197
  317. package/test/ssr-template.test.tsx +0 -482
  318. package/test/stream-emulation.test.ts +0 -479
  319. package/test/ui.test.ts +0 -40
  320. package/test/update.test.ts +0 -58
  321. package/test/validate.test.ts +0 -42
  322. package/test/version-check.test.ts +0 -130
  323. package/tests/data-parity/generated-parity.ts +0 -99
  324. package/tests/data-parity/parity.ts +0 -80
  325. package/tests/data-parity/spec.ts +0 -46
@@ -1,263 +0,0 @@
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 { patternToBracketFile, scanRoutes, staticSectionPattern } from './routes.js';
11
- import { injectSeoHtml, routeSeo } from './seo.js';
12
-
13
- type Ts = typeof TS;
14
-
15
- /**
16
- * True when `mod` exposes the classic TypeScript compiler API the static extractor drives.
17
- *
18
- * Resolving `typescript` is not enough to know it can parse: TypeScript 7 (the native port) moved
19
- * the compiler API off the package's main entry, which now exports only `version`. That module is a
20
- * perfectly good object, so a truthy check passes and the first `ts.ScriptTarget.Latest` then dies
21
- * with `Cannot read properties of undefined (reading 'Latest')`. Probe the members we actually use.
22
- */
23
- function isCompilerApi(mod: unknown): mod is Ts {
24
- const ts = mod as Partial<Ts> | null | undefined;
25
- return (
26
- typeof ts?.createSourceFile === 'function' &&
27
- typeof ts.isVariableStatement === 'function' &&
28
- typeof ts.ScriptTarget === 'object' &&
29
- typeof ts.ScriptKind === 'object' &&
30
- typeof ts.SyntaxKind === 'object'
31
- );
32
- }
33
-
34
- /** Warn once per process: an unusable TypeScript silently costs baked metadata, so say so. */
35
- let warnedUnusable = false;
36
- function warnUnusableTypeScript(mod: unknown): void {
37
- if (warnedUnusable) return;
38
- warnedUnusable = true;
39
- const version = (mod as { version?: string } | null)?.version;
40
- process.stderr.write(
41
- ` toil: typescript${version ? `@${version}` : ''} does not expose the compiler API ` +
42
- `(TypeScript 7 moved it to 'typescript/unstable/*').\n` +
43
- ` toil: route metadata will NOT be baked into the built HTML. Install typescript ^6 to restore it.\n`,
44
- );
45
- }
46
-
47
- /**
48
- * Resolves the project's TypeScript, used to read each route's static `metadata`. Returns `null`
49
- * when it isn't installed (callers then index pages by path only) *or* when the resolved version
50
- * doesn't expose the compiler API, which is warned about rather than crashing the build.
51
- */
52
- export async function loadTypeScript(root: string): Promise<Ts | null> {
53
- let mod: unknown;
54
- try {
55
- const resolved = createRequire(path.join(root, 'package.json')).resolve('typescript');
56
- const ns = (await import(pathToFileURL(resolved).href)) as { default?: Ts } & Ts;
57
- mod = ns.default ?? ns;
58
- } catch {
59
- return null;
60
- }
61
- if (isCompilerApi(mod)) return mod;
62
- warnUnusableTypeScript(mod);
63
- return null;
64
- }
65
-
66
- /** The sync twin of {@link loadTypeScript}, for callers that can't await (e.g. `buildPageIndex`). */
67
- export function loadTypeScriptSync(root: string): Ts | null {
68
- let mod: unknown;
69
- try {
70
- mod = createRequire(path.join(root, 'package.json'))('typescript');
71
- } catch {
72
- return null;
73
- }
74
- const ts = (mod as { default?: Ts })?.default ?? mod;
75
- if (isCompilerApi(ts)) return ts;
76
- warnUnusableTypeScript(ts);
77
- return null;
78
- }
79
-
80
- /** Marks an AST node that isn't a static literal (so its value can't be baked at build). */
81
- const UNRESOLVED = Symbol('unresolved');
82
-
83
- /** Statically evaluates a literal expression node to a JS value, or `UNRESOLVED` if it isn't one. */
84
- function evalNode(ts: Ts, node: TS.Expression): unknown {
85
- if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
86
- if (ts.isNumericLiteral(node)) return Number(node.text);
87
- if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
88
- if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
89
- if (node.kind === ts.SyntaxKind.NullKeyword) return null;
90
- if (ts.isArrayLiteralExpression(node)) {
91
- const out: unknown[] = [];
92
- for (const el of node.elements) {
93
- const value = evalNode(ts, el);
94
- if (value === UNRESOLVED) return UNRESOLVED;
95
- out.push(value);
96
- }
97
- return out;
98
- }
99
- if (ts.isObjectLiteralExpression(node)) return evalObject(ts, node);
100
- return UNRESOLVED;
101
- }
102
-
103
- /** Evaluates an object literal to a plain object, skipping any property that isn't a static literal. */
104
- function evalObject(ts: Ts, node: TS.ObjectLiteralExpression): Record<string, unknown> {
105
- const obj: Record<string, unknown> = {};
106
- for (const prop of node.properties) {
107
- if (!ts.isPropertyAssignment(prop)) continue;
108
- const key = ts.isIdentifier(prop.name)
109
- ? prop.name.text
110
- : ts.isStringLiteral(prop.name)
111
- ? prop.name.text
112
- : null;
113
- if (key === null) continue;
114
- const value = evalNode(ts, prop.initializer);
115
- if (value !== UNRESOLVED) obj[key] = value;
116
- }
117
- return obj;
118
- }
119
-
120
- /**
121
- * Extracts the named `export const <name> = { … }` object-literal exports from a route file in a
122
- * single parse, returning the statically-evaluable subset of each (dynamic and computed values are
123
- * skipped). Names that are absent or not object literals are omitted from the result.
124
- */
125
- export function extractStaticExports(
126
- ts: Ts,
127
- filePath: string,
128
- names: readonly string[],
129
- ): Record<string, Record<string, unknown>> {
130
- let source: string;
131
- try {
132
- source = fs.readFileSync(filePath, 'utf8');
133
- } catch {
134
- return {};
135
- }
136
- const wanted = new Set(names);
137
- const out: Record<string, Record<string, unknown>> = {};
138
- const sf = ts.createSourceFile(
139
- filePath,
140
- source,
141
- ts.ScriptTarget.Latest,
142
- true,
143
- ts.ScriptKind.TSX,
144
- );
145
- for (const stmt of sf.statements) {
146
- if (!ts.isVariableStatement(stmt)) continue;
147
- if (!stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) continue;
148
- for (const decl of stmt.declarationList.declarations) {
149
- if (
150
- ts.isIdentifier(decl.name) &&
151
- wanted.has(decl.name.text) &&
152
- !(decl.name.text in out) &&
153
- decl.initializer &&
154
- ts.isObjectLiteralExpression(decl.initializer)
155
- ) {
156
- out[decl.name.text] = evalObject(ts, decl.initializer);
157
- }
158
- }
159
- }
160
- return out;
161
- }
162
-
163
- /**
164
- * Extracts a route's `export const metadata = { … }` if it's a static object literal, returning the
165
- * statically-evaluable subset (dynamic `generateMetadata` and computed values are skipped). `null`
166
- * when the file has no static metadata.
167
- */
168
- export function extractStaticMetadata(ts: Ts, filePath: string): Record<string, unknown> | null {
169
- return extractStaticExports(ts, filePath, ['metadata']).metadata ?? null;
170
- }
171
-
172
- /**
173
- * True when the route file has a literal `export const <name> = true`. Used to detect the edge-SSR
174
- * opt-in (`export const ssr = true`) without spinning up a Vite SSR server, so the static prerender
175
- * can leave SSR routes to the SSR path.
176
- */
177
- export function exportsTrue(ts: Ts, filePath: string, name: string): boolean {
178
- let source: string;
179
- try {
180
- source = fs.readFileSync(filePath, 'utf8');
181
- } catch {
182
- return false;
183
- }
184
- const sf = ts.createSourceFile(
185
- filePath,
186
- source,
187
- ts.ScriptTarget.Latest,
188
- true,
189
- ts.ScriptKind.TSX,
190
- );
191
- for (const stmt of sf.statements) {
192
- if (!ts.isVariableStatement(stmt)) continue;
193
- if (!stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) continue;
194
- for (const decl of stmt.declarationList.declarations) {
195
- if (
196
- ts.isIdentifier(decl.name) &&
197
- decl.name.text === name &&
198
- decl.initializer?.kind === ts.SyntaxKind.TrueKeyword
199
- ) {
200
- return true;
201
- }
202
- }
203
- }
204
- return false;
205
- }
206
-
207
- /**
208
- * Build-only plugin that statically pre-renders per-route HTML for SEO. After the bundle is written,
209
- * it takes the built shell (`index.html`), and for each route bakes that route's `metadata` (merged
210
- * over the site-wide `seo` defaults) into a browsable HTML file so a JS-less crawler gets correct
211
- * per-page tags:
212
- * - a STATIC route (`/about`) -> `<route>/index.html`;
213
- * - a DYNAMIC route (`/blog/:id`) -> a literal bracket template `blog/[id].html` that the static
214
- * server serves for any concrete `/blog/<x>` and the client router hydrates. Its canonical points
215
- * at the route's static section (`/blog`); the client sets the exact per-value URL on hydration.
216
- * A dynamic template is a file, never a `<seg>/index.html` folder, so it never shadows a sibling.
217
- */
218
- export function prerenderPlugin(cfg: ResolvedToilConfig): Plugin {
219
- return {
220
- name: 'toil:prerender-seo',
221
- apply: 'build',
222
- async closeBundle() {
223
- if (!cfg.seo) return;
224
- const outDir = path.resolve(cfg.root, cfg.outDir);
225
- const shellPath = path.join(outDir, 'index.html');
226
- if (!fs.existsSync(shellPath)) return;
227
- const shell = fs.readFileSync(shellPath, 'utf8');
228
- // Stash the clean built shell (asset tags, no per-route SEO yet) so the post-build SSG
229
- // pass bakes dynamic routes from it rather than from this file once it's been overwritten
230
- // with the `/` route's head (which would duplicate canonical/og tags).
231
- fs.writeFileSync(path.join(cfg.toilDir, 'shell.html'), shell);
232
- const ts = await loadTypeScript(cfg.root);
233
-
234
- const routes = scanRoutes(cfg.routesAbsDir).filter(
235
- (r) => r.slot === undefined && !r.intercept,
236
- );
237
- for (const route of routes) {
238
- const isDynamic = /[:*]/.test(route.pattern);
239
- // A dynamic route that opts into edge SSR (`export const ssr = true`) is rendered by
240
- // the SSR template path. Baking a static bracket template would let the edge's
241
- // static-first serving shadow it, so leave SSR routes to SSR. (When `ts` is null the
242
- // check is skipped and we bake anyway: fail-OPEN deliberately favors the common
243
- // non-SSR dynamic route, which would 404 without a template; typescript is always
244
- // present in a real toiljs project, so this branch is effectively unreachable.)
245
- if (isDynamic && ts && exportsTrue(ts, route.file, 'ssr')) continue;
246
- const metadata = ts ? extractStaticMetadata(ts, route.file) : null;
247
- // A dynamic route's concrete URL is unknown at build, so its canonical/OG URL points
248
- // at the static section (`/blog/:id` -> `/blog`); the client refines it on hydration.
249
- const seoPattern = isDynamic ? staticSectionPattern(route.pattern) : route.pattern;
250
- const html = injectSeoHtml(shell, routeSeo(cfg.seo, metadata, seoPattern));
251
- // Dynamic -> a literal bracket template (`blog/[id].html`); static -> `<route>/index.html`
252
- // (the `/` route is the shell itself). The bracket file is never a folder+index.html.
253
- const target = isDynamic
254
- ? path.join(outDir, patternToBracketFile(route.pattern))
255
- : route.pattern === '/'
256
- ? shellPath
257
- : path.join(outDir, route.pattern.replace(/^\//, ''), 'index.html');
258
- fs.mkdirSync(path.dirname(target), { recursive: true });
259
- fs.writeFileSync(target, html);
260
- }
261
- },
262
- };
263
- }
@@ -1,170 +0,0 @@
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
- /** Inverse of {@link toUrlSegment}: turns a URL pattern segment back into its on-disk bracket
38
- * form (`:id`→`[id]`, `*x`→`[...x]`, `**x`→`[[...x]]`). Static segments pass through unchanged. */
39
- function bracketSegment(segment: string): string {
40
- if (segment.startsWith('**')) return `[[...${segment.slice(2)}]]`;
41
- if (segment.startsWith('*')) return `[...${segment.slice(1)}]`;
42
- if (segment.startsWith(':')) return `[${segment.slice(1)}]`;
43
- return segment;
44
- }
45
-
46
- /**
47
- * The on-disk HTML filename (relative to the build output dir) a DYNAMIC route pattern bakes to:
48
- * the inverse of the scan mapping, with the last segment as the file and the rest as directories.
49
- * This is the literal bracket template the static server serves for any matching URL and the client
50
- * router hydrates, so it must not become a `<seg>/index.html` folder (which would shadow siblings).
51
- * /blog/:id -> blog/[id].html
52
- * /docs/*slug -> docs/[...slug].html
53
- * /docs/**slug -> docs/[[...slug]].html
54
- * /:category/:id -> [category]/[id].html
55
- */
56
- export function patternToBracketFile(pattern: string): string {
57
- return pattern.split('/').filter(Boolean).map(bracketSegment).join('/') + '.html';
58
- }
59
-
60
- /**
61
- * The static "section" of a pattern: the same pattern with every dynamic (`:`/`*`) segment dropped.
62
- * Used as the canonical/OG base URL when baking a dynamic route's build-time SEO (the concrete
63
- * per-value URL is unknown at build; the client sets it on hydration).
64
- * /blog/:id -> /blog
65
- * /:category/:id -> /
66
- */
67
- export function staticSectionPattern(pattern: string): string {
68
- const kept = pattern
69
- .split('/')
70
- .filter(Boolean)
71
- .filter((s) => !s.startsWith(':') && !s.startsWith('*'));
72
- return '/' + kept.join('/');
73
- }
74
-
75
- /** Interception markers: `(.)` same level, `(..)` up one, `(...)` from the routes root. */
76
- const INTERCEPT_RE = /^\((\.{1,3})\)(.+)$/;
77
-
78
- export function filePathToRoute(relPath: string): string {
79
- const withoutExt = relPath.replace(/\\/g, '/').replace(ROUTE_EXT, '');
80
- const segments = withoutExt.split('/').filter(Boolean);
81
- const out: string[] = [];
82
- for (let i = 0; i < segments.length; i++) {
83
- const segment = segments[i];
84
- if (/^\(.+\)$/.test(segment)) continue;
85
- if (/^@/.test(segment)) continue; // parallel-slot marker, contributes no URL segment
86
- if (segment === 'index' && i === segments.length - 1) continue;
87
- out.push(toUrlSegment(segment));
88
- }
89
- return '/' + out.join('/');
90
- }
91
-
92
- /**
93
- * The URL an intercepting route targets, or `null` if the path has no `(.)`/`(..)`/`(...)` marker.
94
- * The marker resolves the target relative to the route's position (ignoring `@slot`/`(group)`
95
- * segments): `(.)` keeps the current level, `(..)` drops one, `(...)` resets to the root.
96
- * @modal/(.)photo/[id].tsx -> /photo/:id
97
- * feed/@modal/(..)photo/[id].tsx -> /photo/:id
98
- */
99
- export function interceptTarget(relPath: string): string | null {
100
- const segments = relPath.replace(/\\/g, '/').replace(ROUTE_EXT, '').split('/').filter(Boolean);
101
- const out: string[] = [];
102
- let marked = false;
103
- for (let i = 0; i < segments.length; i++) {
104
- const segment = segments[i];
105
- if (/^@/.test(segment)) continue;
106
- const marker = INTERCEPT_RE.exec(segment);
107
- if (marker) {
108
- marked = true;
109
- const dots = marker[1].length;
110
- if (dots === 2)
111
- out.pop(); // (..) up one level
112
- else if (dots === 3) out.length = 0; // (...) from the routes root
113
- out.push(toUrlSegment(marker[2]));
114
- continue;
115
- }
116
- if (/^\(.+\)$/.test(segment)) continue;
117
- if (segment === 'index' && i === segments.length - 1) continue;
118
- out.push(toUrlSegment(segment));
119
- }
120
- return marked ? '/' + out.join('/') : null;
121
- }
122
-
123
- /**
124
- * Ranks a pattern so more specific routes match first: static segments beat dynamic (`:x`),
125
- * which beat catch-all (`*x`); deeper routes beat shallower ones.
126
- */
127
- function specificity(pattern: string): number {
128
- const segments = pattern.split('/').filter(Boolean);
129
- let score = segments.length * 10;
130
- for (const segment of segments) {
131
- if (segment.startsWith('*')) score -= 5;
132
- else if (!segment.startsWith(':')) score += 5;
133
- }
134
- return score;
135
- }
136
-
137
- /** The parallel-slot name for a route path (the first `@slot` segment), or `undefined`. */
138
- function slotOf(relPath: string): string | undefined {
139
- for (const segment of relPath.replace(/\\/g, '/').split('/')) {
140
- const match = /^@(.+)$/.exec(segment);
141
- if (match) return match[1];
142
- }
143
- return undefined;
144
- }
145
-
146
- /** Recursively scans `routesDir` for `.tsx`/`.jsx` files, returning routes sorted by specificity. */
147
- export function scanRoutes(routesDir: string): ScannedRoute[] {
148
- if (!fs.existsSync(routesDir)) return [];
149
- const found: ScannedRoute[] = [];
150
- const walk = (dir: string): void => {
151
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
152
- const full = path.join(dir, entry.name);
153
- if (entry.isDirectory()) {
154
- walk(full);
155
- } else if (ROUTE_EXT.test(entry.name) && !SPECIAL_FILE.test(entry.name)) {
156
- const rel = path.relative(routesDir, full);
157
- const target = interceptTarget(rel);
158
- found.push({
159
- file: full,
160
- pattern: target ?? filePathToRoute(rel),
161
- slot: slotOf(rel),
162
- intercept: target !== null,
163
- });
164
- }
165
- }
166
- };
167
- walk(routesDir);
168
- found.sort((a, b) => specificity(b.pattern) - specificity(a.pattern));
169
- return found;
170
- }