toiljs 0.0.12 → 0.0.15

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 (41) hide show
  1. package/README.md +1 -1
  2. package/build/cli/.tsbuildinfo +1 -1
  3. package/build/cli/index.js +2926 -191
  4. package/build/client/.tsbuildinfo +1 -1
  5. package/build/client/head/metadata.d.ts +3 -1
  6. package/build/client/head/metadata.js +8 -0
  7. package/build/client/index.d.ts +4 -4
  8. package/build/client/index.js +2 -2
  9. package/build/client/navigation/navigation.d.ts +2 -0
  10. package/build/client/navigation/navigation.js +9 -1
  11. package/build/client/routing/loader.d.ts +2 -0
  12. package/build/compiler/.tsbuildinfo +1 -1
  13. package/build/compiler/config.d.ts +2 -0
  14. package/build/compiler/config.js +1 -0
  15. package/build/compiler/generate.js +10 -1
  16. package/build/compiler/index.js +2 -0
  17. package/build/compiler/prerender.js +1 -0
  18. package/build/compiler/seo.d.ts +1 -1
  19. package/build/compiler/seo.js +3 -2
  20. package/build/compiler/ssg.d.ts +5 -0
  21. package/build/compiler/ssg.js +90 -0
  22. package/examples/basic/client/routes/search.tsx +61 -61
  23. package/package.json +4 -3
  24. package/src/client/head/metadata.ts +112 -94
  25. package/src/client/index.ts +89 -79
  26. package/src/client/navigation/navigation.ts +235 -215
  27. package/src/client/routing/loader.ts +10 -0
  28. package/src/client/search/search.ts +189 -189
  29. package/src/client/search/use-page-search.ts +73 -73
  30. package/src/compiler/config.ts +182 -173
  31. package/src/compiler/generate.ts +394 -378
  32. package/src/compiler/index.ts +3 -0
  33. package/src/compiler/pages.ts +2 -2
  34. package/src/compiler/prerender.ts +156 -152
  35. package/src/compiler/seo.ts +390 -381
  36. package/src/compiler/ssg.ts +126 -0
  37. package/test/dom/error-overlay.test.tsx +44 -44
  38. package/test/dom/router-loading.test.tsx +1 -1
  39. package/test/dom/use-metadata.test.tsx +58 -0
  40. package/test/seo.test.ts +164 -164
  41. package/test/ssg.test.ts +36 -0
@@ -1,378 +1,394 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
-
4
- import { type ResolvedToilConfig } from './config.js';
5
- import { writeDocs } from './docs.js';
6
- import { buildPageIndex, pagesModuleSource } from './pages.js';
7
- import { scanRoutes, type ScannedRoute } from './routes.js';
8
- import { llmsTxt, robotsTxt, sitemapXml } from './seo.js';
9
-
10
- /**
11
- * Contents of the root `toil-env.d.ts`: ambient global types so `new BinaryWriter()` etc. resolve
12
- * in the IDE without an import. Script-mode declaration (no top-level import/export → the
13
- * `declare const`s are truly global, and it's not a module that could confuse ESLint's project
14
- * service); the inline `import('toiljs/io')` type only needs the normal `toiljs/io` export.
15
- * Lives at the project root because TypeScript's `include` globs skip dot-directories.
16
- * Exported so `toiljs create` can write it during scaffolding, before the first dev/build.
17
- */
18
- /** Side-effect style imports (e.g. `import './styles/main.css'`). */
19
- const STYLE_EXTENSIONS = ['css', 'scss', 'sass', 'less', 'styl', 'stylus', 'pcss', 'sss'];
20
- /** Asset imports whose default export is the resolved URL string (e.g. `import logo from './logo.svg'`). */
21
- const ASSET_EXTENSIONS = ['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'ico', 'bmp', 'apng'];
22
-
23
- const STYLE_MODULES = STYLE_EXTENSIONS.map((ext) => `declare module '*.${ext}' {}`).join('\n');
24
- const ASSET_MODULES = ASSET_EXTENSIONS.map(
25
- (ext) => `declare module '*.${ext}' {\n const src: string;\n export default src;\n}`,
26
- ).join('\n');
27
-
28
- export const TOIL_ENV_DTS =
29
- `// AUTO-GENERATED by toil, do not edit.\n` +
30
- // Types for image-optimization query imports (`import img from './x.png?w=400&format=webp'`).
31
- `/// <reference types="vite-imagetools/client" />\n` +
32
- `declare const Toil: typeof import('toiljs/client');\n` +
33
- `declare namespace Toil {\n` +
34
- ` type LoaderArgs = import('toiljs/client').LoaderArgs;\n` +
35
- ` type LoaderFunction<T = unknown> = import('toiljs/client').LoaderFunction<T>;\n` +
36
- ` type Revalidate = import('toiljs/client').Revalidate;\n` +
37
- ` type Metadata = import('toiljs/client').Metadata;\n` +
38
- ` type GenerateMetadata<T = unknown> = import('toiljs/client').GenerateMetadata<T>;\n` +
39
- ` type RouteErrorProps = import('toiljs/client').RouteErrorProps;\n` +
40
- ` type Href = import('toiljs/client').Href;\n` +
41
- ` type RoutePath = import('toiljs/client').RoutePath;\n` +
42
- ` type PageMeta = import('toiljs/client').PageMeta;\n` +
43
- ` type SearchHints = import('toiljs/client').SearchHints;\n` +
44
- `}\n` +
45
- `declare const BinaryWriter: typeof import('toiljs/io').BinaryWriter;\n` +
46
- `declare const BinaryReader: typeof import('toiljs/io').BinaryReader;\n` +
47
- `declare const FastMap: typeof import('toiljs/io').FastMap;\n` +
48
- `declare const FastSet: typeof import('toiljs/io').FastSet;\n` +
49
- `\n` +
50
- `${STYLE_MODULES}\n` +
51
- `\n` +
52
- `${ASSET_MODULES}\n` +
53
- `\n` +
54
- `declare module 'toiljs/routes' {\n` +
55
- ` export const routes: import('toiljs/client').RouteDef[];\n` +
56
- ` export const layout: import('toiljs/client').LayoutLoader;\n` +
57
- ` export const notFound: import('toiljs/client').NotFoundLoader;\n` +
58
- ` export const globalError: import('toiljs/client').ErrorComponentLoader;\n` +
59
- ` export const slots: Record<string, import('toiljs/client').RouteDef[]>;\n` +
60
- ` export const pages: import('toiljs/client').PageMeta[];\n` +
61
- `}\n`;
62
-
63
- /**
64
- * Returns a `./`-prefixed, **extensionless** POSIX module specifier from `.toil` to `abs`, for use
65
- * in generated `import(...)` calls. Extensionless so TypeScript doesn't demand
66
- * `allowImportingTsExtensions` (TS5097) when the generated files are checked; Vite still resolves it.
67
- */
68
- function relFromToil(cfg: ResolvedToilConfig, abs: string): string {
69
- let rel = path
70
- .relative(cfg.toilDir, abs)
71
- .replace(/\\/g, '/')
72
- .replace(/\.(tsx|jsx)$/, '');
73
- if (!rel.startsWith('.')) rel = './' + rel;
74
- return rel;
75
- }
76
-
77
- function findLayout(cfg: ResolvedToilConfig): string | undefined {
78
- return ['layout.tsx', 'layout.jsx']
79
- .map((name) => path.join(cfg.clientAbsDir, name))
80
- .find((p) => fs.existsSync(p));
81
- }
82
-
83
- /** Finds an optional custom not-found page at `client/404.{tsx,jsx}`. */
84
- function findNotFound(cfg: ResolvedToilConfig): string | undefined {
85
- return ['404.tsx', '404.jsx']
86
- .map((name) => path.join(cfg.clientAbsDir, name))
87
- .find((p) => fs.existsSync(p));
88
- }
89
-
90
- /** Finds an optional root error boundary at `client/global-error.{tsx,jsx}`. */
91
- function findGlobalError(cfg: ResolvedToilConfig): string | undefined {
92
- return ['global-error.tsx', 'global-error.jsx']
93
- .map((name) => path.join(cfg.clientAbsDir, name))
94
- .find((p) => fs.existsSync(p));
95
- }
96
-
97
- /**
98
- * Builds the `RoutePath` union for typed `Link`/`navigate` hrefs: static routes as string literals,
99
- * dynamic/catch-all as `` `…/${string}` `` templates (optional catch-all also emits its bare prefix).
100
- */
101
- function routePathUnion(routes: ScannedRoute[]): string {
102
- const members = new Set<string>();
103
- for (const route of routes) {
104
- const segments = route.pattern.split('/').filter(Boolean);
105
- const isDynamic = segments.some((s) => s.startsWith(':') || s.startsWith('*'));
106
- if (!isDynamic) {
107
- members.add(`'${route.pattern}'`);
108
- continue;
109
- }
110
- const parts = segments.map((s) =>
111
- s.startsWith(':') || s.startsWith('*') ? '${string}' : s,
112
- );
113
- members.add('`/' + parts.join('/') + '`');
114
- const optionalIdx = segments.findIndex((s) => s.startsWith('**'));
115
- if (optionalIdx !== -1) {
116
- const prefix = '/' + segments.slice(0, optionalIdx).join('/');
117
- members.add(`'${prefix}'`);
118
- }
119
- }
120
- return members.size ? [...members].join(' | ') : 'string';
121
- }
122
-
123
- /**
124
- * The `toil-routes.d.ts` contents: a module augmentation registering the project's route paths so
125
- * `Link`/`navigate`/`useRouter` hrefs are type-checked. Regenerated each dev/build.
126
- */
127
- function routesDts(cfg: ResolvedToilConfig, routes: ScannedRoute[]): string {
128
- // Type-only namespace import of every route module (erased at build) so editors don't flag a
129
- // route's `loader` / `metadata` / `generateMetadata` / `revalidate` / `default` exports as unused
130
- // the compiler consumes them via dynamic `import()`, which editors don't count as a reference.
131
- const refs = routes.map((route, i) => {
132
- let rel = path
133
- .relative(cfg.root, route.file)
134
- .replace(/\\/g, '/')
135
- .replace(/\.(tsx|jsx)$/, '');
136
- if (!rel.startsWith('.')) rel = `./${rel}`;
137
- return { name: `_toilRoute${String(i)}`, rel };
138
- });
139
- const imports = refs
140
- .map((m) => `import type * as ${m.name} from ${JSON.stringify(m.rel)};\n`)
141
- .join('');
142
- const referenced = refs.length
143
- ? `export type _ToilRouteModules = [${refs.map((m) => `typeof ${m.name}`).join(', ')}];\n`
144
- : `export {};\n`;
145
- return (
146
- `// AUTO-GENERATED by toil, do not edit.\n` +
147
- imports +
148
- referenced +
149
- `declare module 'toiljs/client' {\n` +
150
- ` interface Register {\n` +
151
- ` routePath: ${routePathUnion(routes)};\n` +
152
- ` }\n` +
153
- `}\n`
154
- );
155
- }
156
-
157
- /** Finds the user-owned app entry at `client/toil.{tsx,jsx}` (where `mount` is called). */
158
- function findEntry(cfg: ResolvedToilConfig): string | undefined {
159
- return ['toil.tsx', 'toil.jsx']
160
- .map((name) => path.join(cfg.clientAbsDir, name))
161
- .find((p) => fs.existsSync(p));
162
- }
163
-
164
- /** A `<base>.{tsx,jsx}` in `dir`, or undefined. */
165
- function specialIn(dir: string, base: string): string | undefined {
166
- return [`${base}.tsx`, `${base}.jsx`]
167
- .map((name) => path.join(dir, name))
168
- .find((p) => fs.existsSync(p));
169
- }
170
-
171
- /**
172
- * Chain of `<base>.{tsx,jsx}` files wrapping a route, shallowest → deepest: the routes root and each
173
- * ancestor directory down to the file's own. With `includeClientRoot`, `client/<base>` is prepended
174
- * as the outermost (used by templates; the root `client/layout.tsx` is instead the top-level layout).
175
- */
176
- function findSpecialChain(
177
- cfg: ResolvedToilConfig,
178
- routeFile: string,
179
- base: string,
180
- includeClientRoot: boolean,
181
- ): string[] {
182
- const chain: string[] = [];
183
- const relDir = path.dirname(path.relative(cfg.routesAbsDir, routeFile));
184
- const segments = relDir === '.' ? [] : relDir.split(path.sep);
185
- // A parallel-slot route (one under an `@slot` segment) is rendered INTO a parent layout's
186
- // `<Slot>`. Its own layout/template chain must therefore start at the `@slot` directory, not the
187
- // routes root: the parent segments' layouts already wrap the slot, so re-including them here
188
- // would nest the slot inside itself and recurse. For non-slot routes this is the full chain.
189
- const slotIdx = segments.findIndex((s) => s.startsWith('@'));
190
- const startAt = slotIdx < 0 ? 0 : slotIdx + 1;
191
- if (includeClientRoot && slotIdx < 0) {
192
- const root = specialIn(cfg.clientAbsDir, base);
193
- if (root) chain.push(root);
194
- }
195
- let dir = cfg.routesAbsDir;
196
- for (let i = 0; i <= segments.length; i++) {
197
- if (i > 0) dir = path.join(dir, segments[i - 1]);
198
- if (i < startAt) continue;
199
- const found = specialIn(dir, base);
200
- if (found) chain.push(found);
201
- }
202
- return chain;
203
- }
204
-
205
- /** Nearest special file named `base` (e.g. `loading`/`error`) from the route's dir up to the routes root. */
206
- function findNearest(cfg: ResolvedToilConfig, routeFile: string, base: string): string | undefined {
207
- const root = path.resolve(cfg.routesAbsDir);
208
- let dir = path.dirname(routeFile);
209
- for (;;) {
210
- const found = [`${base}.tsx`, `${base}.jsx`]
211
- .map((name) => path.join(dir, name))
212
- .find((p) => fs.existsSync(p));
213
- if (found) return found;
214
- if (path.resolve(dir) === root) return undefined;
215
- const parent = path.dirname(dir);
216
- if (parent === dir) return undefined;
217
- dir = parent;
218
- }
219
- }
220
-
221
- /**
222
- * Generates the `.toil/` working dir (routes table, mount entry, the HTML entry built from the
223
- * project's `public/index.html` template, and mirrored `public/` assets) and returns the scanned
224
- * routes. Called before every dev/build and on route add/remove during dev.
225
- */
226
- export function generate(cfg: ResolvedToilConfig): ScannedRoute[] {
227
- const routes = scanRoutes(cfg.routesAbsDir);
228
- fs.mkdirSync(cfg.toilDir, { recursive: true });
229
-
230
- const layoutFile = findLayout(cfg);
231
- const notFoundFile = findNotFound(cfg);
232
- const globalErrorFile = findGlobalError(cfg);
233
- const imp = (f: string): string => `() => import(${JSON.stringify(relFromToil(cfg, f))})`;
234
- const routeObj = (r: ScannedRoute): string => {
235
- const layouts = findSpecialChain(cfg, r.file, 'layout', false).map(imp).join(', ');
236
- const templates = findSpecialChain(cfg, r.file, 'template', true).map(imp).join(', ');
237
- const parts = [
238
- `pattern: ${JSON.stringify(r.pattern)}`,
239
- `load: ${imp(r.file)}`,
240
- `layouts: [${layouts}]`,
241
- ];
242
- if (templates) parts.push(`templates: [${templates}]`);
243
- const loadingFile = findNearest(cfg, r.file, 'loading');
244
- if (loadingFile) parts.push(`loading: ${imp(loadingFile)}`);
245
- const errorFile = findNearest(cfg, r.file, 'error');
246
- if (errorFile) parts.push(`errorComponent: ${imp(errorFile)}`);
247
- if (r.intercept) parts.push(`intercept: true`);
248
- return `{ ${parts.join(', ')} }`;
249
- };
250
- const mainRoutes = routes.filter((r) => r.slot === undefined);
251
- const slotNames = [...new Set(routes.flatMap((r) => (r.slot ? [r.slot] : [])))];
252
- const slotsBody = slotNames
253
- .map((name) => {
254
- const items = routes
255
- .filter((r) => r.slot === name)
256
- .map((r) => ` ${routeObj(r)},`)
257
- .join('\n');
258
- return ` ${JSON.stringify(name)}: [\n${items}\n ],`;
259
- })
260
- .join('\n');
261
- const pages = buildPageIndex(cfg.root, routes);
262
- const routesSrc =
263
- `// @ts-nocheck\n` +
264
- `// AUTO-GENERATED by toil, do not edit.\n` +
265
- `import type { RouteDef, LayoutLoader, NotFoundLoader, PageMeta } from 'toiljs/client';\n\n` +
266
- `export const routes: RouteDef[] = [\n${mainRoutes.map((r) => ` ${routeObj(r)},`).join('\n')}\n];\n\n` +
267
- `export const slots: Record<string, RouteDef[]> = {\n${slotsBody}\n};\n\n` +
268
- `export const layout: LayoutLoader = ${layoutFile ? `() => import(${JSON.stringify(relFromToil(cfg, layoutFile))})` : 'null'};\n` +
269
- `export const notFound: NotFoundLoader = ${notFoundFile ? `() => import(${JSON.stringify(relFromToil(cfg, notFoundFile))})` : 'null'};\n` +
270
- `export const globalError = ${globalErrorFile ? `() => import(${JSON.stringify(relFromToil(cfg, globalErrorFile))})` : 'null'};\n\n` +
271
- pagesModuleSource(pages);
272
- fs.writeFileSync(path.join(cfg.toilDir, 'routes.ts'), routesSrc);
273
-
274
- const globalsSrc =
275
- `// @ts-nocheck\n` +
276
- `// AUTO-GENERATED by toil, do not edit.\n` +
277
- `import * as Toil from 'toiljs/client';\n` +
278
- `import { BinaryWriter, BinaryReader, FastMap, FastSet } from 'toiljs/io';\n` +
279
- `import { pages } from './routes';\n\n` +
280
- `Object.assign(globalThis, { Toil, BinaryWriter, BinaryReader, FastMap, FastSet });\n` +
281
- `Toil.setViewTransitions(${String(cfg.viewTransitions)});\n` +
282
- `Toil.registerPages(pages);\n`;
283
- fs.writeFileSync(path.join(cfg.toilDir, 'globals.ts'), globalsSrc);
284
-
285
- const entryFile = findEntry(cfg);
286
- const entrySrc = entryFile
287
- ? `// @ts-nocheck\n` +
288
- `// AUTO-GENERATED by toil, do not edit.\n` +
289
- `import './globals';\n` +
290
- `import ${JSON.stringify(relFromToil(cfg, entryFile))};\n`
291
- : `// @ts-nocheck\n` +
292
- `// AUTO-GENERATED by toil, do not edit.\n` +
293
- `import './globals';\n` +
294
- `import { mount } from 'toiljs/client';\n` +
295
- `import { routes, layout, notFound, globalError, slots } from './routes';\n\n` +
296
- `mount(routes, layout, notFound, globalError, slots);\n`;
297
- fs.writeFileSync(path.join(cfg.toilDir, 'entry.tsx'), entrySrc);
298
-
299
- fs.writeFileSync(path.join(cfg.root, 'toil-env.d.ts'), TOIL_ENV_DTS);
300
- fs.writeFileSync(path.join(cfg.root, 'toil-routes.d.ts'), routesDts(cfg, routes));
301
-
302
- fs.writeFileSync(path.join(cfg.toilDir, 'index.html'), buildHtml(cfg));
303
- syncPublicAssets(cfg);
304
- writeSeoFiles(cfg, routes);
305
- writeDocs(cfg.toilDir);
306
-
307
- return routes;
308
- }
309
-
310
- /**
311
- * Writes the build-time SEO crawler files into `.toil/public` (Vite's publicDir, served in dev and
312
- * copied to the output root in build): `robots.txt` (incl. AI-crawler directives), `sitemap.xml`
313
- * (static routes), and `llms.txt` (AI guidance). No-op when SEO isn't configured.
314
- */
315
- function writeSeoFiles(cfg: ResolvedToilConfig, routes: ScannedRoute[]): void {
316
- if (!cfg.seo) return;
317
- const dest = path.join(cfg.toilDir, 'public');
318
- const files: [string, string][] = [
319
- ['robots.txt', robotsTxt(cfg.seo)],
320
- ['sitemap.xml', sitemapXml(cfg.seo, routes)],
321
- ['llms.txt', llmsTxt(cfg.seo, routes)],
322
- ];
323
- const present = files.filter(([, content]) => content !== '');
324
- if (present.length === 0) return;
325
- fs.mkdirSync(dest, { recursive: true });
326
- for (const [name, content] of present) fs.writeFileSync(path.join(dest, name), content);
327
- }
328
-
329
- /** Fallback HTML when the project has no `public/index.html` template. The entry script is added
330
- * by {@link buildHtml}. */
331
- const DEFAULT_HTML =
332
- `<!doctype html>\n<html lang="en">\n <head>\n <meta charset="utf-8" />\n` +
333
- ` <meta name="viewport" content="width=device-width, initial-scale=1" />\n` +
334
- ` <meta name="description" content="" />\n` +
335
- ` <title>Toil App</title>\n </head>\n <body>\n <div id="root"></div>\n` +
336
- ` </body>\n</html>\n`;
337
-
338
- /** The module entry that boots the app, injected into the HTML (resolved relative to `.toil`). */
339
- const ENTRY_SCRIPT = `<script type="module" src="./entry.tsx"></script>`;
340
-
341
- /**
342
- * Produces the `.toil/index.html` Vite entry from the project's `public/index.html` template (or
343
- * the built-in default if absent), ensuring the generated module entry script is present. Users
344
- * own the template, toil only guarantees the entry is wired, so it stays the SPA root.
345
- */
346
- function buildHtml(cfg: ResolvedToilConfig): string {
347
- const templatePath = path.join(cfg.publicDir, 'index.html');
348
- let html = fs.existsSync(templatePath) ? fs.readFileSync(templatePath, 'utf8') : DEFAULT_HTML;
349
- // Inject the entry only if the template doesn't already reference it as a module script
350
- // (matching the literal filename anywhere in the file would be too eager).
351
- if (!/src=["']\.\/entry\.tsx["']/.test(html)) {
352
- html = html.includes('</body>')
353
- ? html.replace('</body>', ` ${ENTRY_SCRIPT}\n </body>`)
354
- : `${html}\n${ENTRY_SCRIPT}\n`;
355
- }
356
- return html;
357
- }
358
-
359
- /**
360
- * Mirrors the project's `public/` assets into `.toil/public/` (Vite's publicDir under the `.toil`
361
- * root), excluding the `index.html` template, that is processed into the entry above, and copying
362
- * it here would clobber the built, asset-hashed page. Cleared each run so deletions propagate.
363
- */
364
- function syncPublicAssets(cfg: ResolvedToilConfig): void {
365
- const dest = path.join(cfg.toilDir, 'public');
366
- fs.rmSync(dest, { recursive: true, force: true });
367
- if (!fs.existsSync(cfg.publicDir)) return;
368
-
369
- let copied = 0;
370
- for (const entry of fs.readdirSync(cfg.publicDir, { withFileTypes: true })) {
371
- if (entry.name === 'index.html') continue;
372
- fs.cpSync(path.join(cfg.publicDir, entry.name), path.join(dest, entry.name), {
373
- recursive: true,
374
- });
375
- copied++;
376
- }
377
- if (copied === 0) fs.rmSync(dest, { recursive: true, force: true });
378
- }
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ import { type ResolvedToilConfig } from './config.js';
5
+ import { writeDocs } from './docs.js';
6
+ import { buildPageIndex, pagesModuleSource } from './pages.js';
7
+ import { scanRoutes, type ScannedRoute } from './routes.js';
8
+ import { llmsTxt, robotsTxt, sitemapXml } from './seo.js';
9
+
10
+ /**
11
+ * Contents of the root `toil-env.d.ts`: ambient global types so `new BinaryWriter()` etc. resolve
12
+ * in the IDE without an import. Script-mode declaration (no top-level import/export → the
13
+ * `declare const`s are truly global, and it's not a module that could confuse ESLint's project
14
+ * service); the inline `import('toiljs/io')` type only needs the normal `toiljs/io` export.
15
+ * Lives at the project root because TypeScript's `include` globs skip dot-directories.
16
+ * Exported so `toiljs create` can write it during scaffolding, before the first dev/build.
17
+ */
18
+ /** Side-effect style imports (e.g. `import './styles/main.css'`). */
19
+ const STYLE_EXTENSIONS = ['css', 'scss', 'sass', 'less', 'styl', 'stylus', 'pcss', 'sss'];
20
+ /** Asset imports whose default export is the resolved URL string (e.g. `import logo from './logo.svg'`). */
21
+ const ASSET_EXTENSIONS = ['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'ico', 'bmp', 'apng'];
22
+
23
+ const STYLE_MODULES = STYLE_EXTENSIONS.map((ext) => `declare module '*.${ext}' {}`).join('\n');
24
+ const ASSET_MODULES = ASSET_EXTENSIONS.map(
25
+ (ext) => `declare module '*.${ext}' {\n const src: string;\n export default src;\n}`,
26
+ ).join('\n');
27
+
28
+ /**
29
+ * Types for vite-imagetools query imports (e.g. `import src from './hero.png?w=800&as=srcset'`).
30
+ * Declared inline rather than via `/// <reference types="vite-imagetools/client" />` because
31
+ * vite-imagetools v10 ships no `client` types entry, and the package lives in toiljs's own
32
+ * node_modules (unresolvable from a symlinked consumer). One `*` wildcard each, matching any
33
+ * specifier ending in the directive.
34
+ */
35
+ const IMAGETOOLS_MODULES = [
36
+ `declare module '*as=srcset' {\n const src: string;\n export default src;\n}`,
37
+ `declare module '*as=url' {\n const src: string;\n export default src;\n}`,
38
+ `declare module '*as=metadata' {\n const metadata: { src: string; width?: number; height?: number; format?: string }[];\n export default metadata;\n}`,
39
+ ].join('\n');
40
+
41
+ export const TOIL_ENV_DTS =
42
+ `// AUTO-GENERATED by toil, do not edit.\n` +
43
+ `declare const Toil: typeof import('toiljs/client');\n` +
44
+ `declare namespace Toil {\n` +
45
+ ` type LoaderArgs = import('toiljs/client').LoaderArgs;\n` +
46
+ ` type LoaderFunction<T = unknown> = import('toiljs/client').LoaderFunction<T>;\n` +
47
+ ` type Revalidate = import('toiljs/client').Revalidate;\n` +
48
+ ` type Metadata = import('toiljs/client').Metadata;\n` +
49
+ ` type GenerateMetadata<T = unknown> = import('toiljs/client').GenerateMetadata<T>;\n` +
50
+ ` type GenerateStaticParams = import('toiljs/client').GenerateStaticParams;\n` +
51
+ ` type StaticParams = import('toiljs/client').StaticParams;\n` +
52
+ ` type RouteErrorProps = import('toiljs/client').RouteErrorProps;\n` +
53
+ ` type Href = import('toiljs/client').Href;\n` +
54
+ ` type RoutePath = import('toiljs/client').RoutePath;\n` +
55
+ ` type PageMeta = import('toiljs/client').PageMeta;\n` +
56
+ ` type SearchHints = import('toiljs/client').SearchHints;\n` +
57
+ `}\n` +
58
+ `declare const BinaryWriter: typeof import('toiljs/io').BinaryWriter;\n` +
59
+ `declare const BinaryReader: typeof import('toiljs/io').BinaryReader;\n` +
60
+ `declare const FastMap: typeof import('toiljs/io').FastMap;\n` +
61
+ `declare const FastSet: typeof import('toiljs/io').FastSet;\n` +
62
+ `\n` +
63
+ `${STYLE_MODULES}\n` +
64
+ `\n` +
65
+ `${ASSET_MODULES}\n` +
66
+ `\n` +
67
+ `${IMAGETOOLS_MODULES}\n` +
68
+ `\n` +
69
+ `declare module 'toiljs/routes' {\n` +
70
+ ` export const routes: import('toiljs/client').RouteDef[];\n` +
71
+ ` export const layout: import('toiljs/client').LayoutLoader;\n` +
72
+ ` export const notFound: import('toiljs/client').NotFoundLoader;\n` +
73
+ ` export const globalError: import('toiljs/client').ErrorComponentLoader;\n` +
74
+ ` export const slots: Record<string, import('toiljs/client').RouteDef[]>;\n` +
75
+ ` export const pages: import('toiljs/client').PageMeta[];\n` +
76
+ `}\n`;
77
+
78
+ /**
79
+ * Returns a `./`-prefixed, **extensionless** POSIX module specifier from `.toil` to `abs`, for use
80
+ * in generated `import(...)` calls. Extensionless so TypeScript doesn't demand
81
+ * `allowImportingTsExtensions` (TS5097) when the generated files are checked; Vite still resolves it.
82
+ */
83
+ function relFromToil(cfg: ResolvedToilConfig, abs: string): string {
84
+ let rel = path
85
+ .relative(cfg.toilDir, abs)
86
+ .replace(/\\/g, '/')
87
+ .replace(/\.(tsx|jsx)$/, '');
88
+ if (!rel.startsWith('.')) rel = './' + rel;
89
+ return rel;
90
+ }
91
+
92
+ function findLayout(cfg: ResolvedToilConfig): string | undefined {
93
+ return ['layout.tsx', 'layout.jsx']
94
+ .map((name) => path.join(cfg.clientAbsDir, name))
95
+ .find((p) => fs.existsSync(p));
96
+ }
97
+
98
+ /** Finds an optional custom not-found page at `client/404.{tsx,jsx}`. */
99
+ function findNotFound(cfg: ResolvedToilConfig): string | undefined {
100
+ return ['404.tsx', '404.jsx']
101
+ .map((name) => path.join(cfg.clientAbsDir, name))
102
+ .find((p) => fs.existsSync(p));
103
+ }
104
+
105
+ /** Finds an optional root error boundary at `client/global-error.{tsx,jsx}`. */
106
+ function findGlobalError(cfg: ResolvedToilConfig): string | undefined {
107
+ return ['global-error.tsx', 'global-error.jsx']
108
+ .map((name) => path.join(cfg.clientAbsDir, name))
109
+ .find((p) => fs.existsSync(p));
110
+ }
111
+
112
+ /**
113
+ * Builds the `RoutePath` union for typed `Link`/`navigate` hrefs: static routes as string literals,
114
+ * dynamic/catch-all as `` `…/${string}` `` templates (optional catch-all also emits its bare prefix).
115
+ */
116
+ function routePathUnion(routes: ScannedRoute[]): string {
117
+ const members = new Set<string>();
118
+ for (const route of routes) {
119
+ const segments = route.pattern.split('/').filter(Boolean);
120
+ const isDynamic = segments.some((s) => s.startsWith(':') || s.startsWith('*'));
121
+ if (!isDynamic) {
122
+ members.add(`'${route.pattern}'`);
123
+ continue;
124
+ }
125
+ const parts = segments.map((s) =>
126
+ s.startsWith(':') || s.startsWith('*') ? '${string}' : s,
127
+ );
128
+ members.add('`/' + parts.join('/') + '`');
129
+ const optionalIdx = segments.findIndex((s) => s.startsWith('**'));
130
+ if (optionalIdx !== -1) {
131
+ const prefix = '/' + segments.slice(0, optionalIdx).join('/');
132
+ members.add(`'${prefix}'`);
133
+ }
134
+ }
135
+ return members.size ? [...members].join(' | ') : 'string';
136
+ }
137
+
138
+ /**
139
+ * The `toil-routes.d.ts` contents: a module augmentation registering the project's route paths so
140
+ * `Link`/`navigate`/`useRouter` hrefs are type-checked. Regenerated each dev/build.
141
+ */
142
+ function routesDts(cfg: ResolvedToilConfig, routes: ScannedRoute[]): string {
143
+ // Type-only namespace import of every route module (erased at build) so editors don't flag a
144
+ // route's `loader` / `metadata` / `generateMetadata` / `revalidate` / `default` exports as unused,
145
+ // the compiler consumes them via dynamic `import()`, which editors don't count as a reference.
146
+ const refs = routes.map((route, i) => {
147
+ let rel = path
148
+ .relative(cfg.root, route.file)
149
+ .replace(/\\/g, '/')
150
+ .replace(/\.(tsx|jsx)$/, '');
151
+ if (!rel.startsWith('.')) rel = `./${rel}`;
152
+ return { name: `_toilRoute${String(i)}`, rel };
153
+ });
154
+ const imports = refs
155
+ .map((m) => `import type * as ${m.name} from ${JSON.stringify(m.rel)};\n`)
156
+ .join('');
157
+ const referenced = refs.length
158
+ ? `export type _ToilRouteModules = [${refs.map((m) => `typeof ${m.name}`).join(', ')}];\n`
159
+ : `export {};\n`;
160
+ return (
161
+ `// AUTO-GENERATED by toil, do not edit.\n` +
162
+ imports +
163
+ referenced +
164
+ `declare module 'toiljs/client' {\n` +
165
+ ` interface Register {\n` +
166
+ ` routePath: ${routePathUnion(routes)};\n` +
167
+ ` }\n` +
168
+ `}\n`
169
+ );
170
+ }
171
+
172
+ /** Finds the user-owned app entry at `client/toil.{tsx,jsx}` (where `mount` is called). */
173
+ function findEntry(cfg: ResolvedToilConfig): string | undefined {
174
+ return ['toil.tsx', 'toil.jsx']
175
+ .map((name) => path.join(cfg.clientAbsDir, name))
176
+ .find((p) => fs.existsSync(p));
177
+ }
178
+
179
+ /** A `<base>.{tsx,jsx}` in `dir`, or undefined. */
180
+ function specialIn(dir: string, base: string): string | undefined {
181
+ return [`${base}.tsx`, `${base}.jsx`]
182
+ .map((name) => path.join(dir, name))
183
+ .find((p) => fs.existsSync(p));
184
+ }
185
+
186
+ /**
187
+ * Chain of `<base>.{tsx,jsx}` files wrapping a route, shallowest → deepest: the routes root and each
188
+ * ancestor directory down to the file's own. With `includeClientRoot`, `client/<base>` is prepended
189
+ * as the outermost (used by templates; the root `client/layout.tsx` is instead the top-level layout).
190
+ */
191
+ function findSpecialChain(
192
+ cfg: ResolvedToilConfig,
193
+ routeFile: string,
194
+ base: string,
195
+ includeClientRoot: boolean,
196
+ ): string[] {
197
+ const chain: string[] = [];
198
+ const relDir = path.dirname(path.relative(cfg.routesAbsDir, routeFile));
199
+ const segments = relDir === '.' ? [] : relDir.split(path.sep);
200
+ // A parallel-slot route (one under an `@slot` segment) is rendered INTO a parent layout's
201
+ // `<Slot>`. Its own layout/template chain must therefore start at the `@slot` directory, not the
202
+ // routes root: the parent segments' layouts already wrap the slot, so re-including them here
203
+ // would nest the slot inside itself and recurse. For non-slot routes this is the full chain.
204
+ const slotIdx = segments.findIndex((s) => s.startsWith('@'));
205
+ const startAt = slotIdx < 0 ? 0 : slotIdx + 1;
206
+ if (includeClientRoot && slotIdx < 0) {
207
+ const root = specialIn(cfg.clientAbsDir, base);
208
+ if (root) chain.push(root);
209
+ }
210
+ let dir = cfg.routesAbsDir;
211
+ for (let i = 0; i <= segments.length; i++) {
212
+ if (i > 0) dir = path.join(dir, segments[i - 1]);
213
+ if (i < startAt) continue;
214
+ const found = specialIn(dir, base);
215
+ if (found) chain.push(found);
216
+ }
217
+ return chain;
218
+ }
219
+
220
+ /** Nearest special file named `base` (e.g. `loading`/`error`) from the route's dir up to the routes root. */
221
+ function findNearest(cfg: ResolvedToilConfig, routeFile: string, base: string): string | undefined {
222
+ const root = path.resolve(cfg.routesAbsDir);
223
+ let dir = path.dirname(routeFile);
224
+ for (;;) {
225
+ const found = [`${base}.tsx`, `${base}.jsx`]
226
+ .map((name) => path.join(dir, name))
227
+ .find((p) => fs.existsSync(p));
228
+ if (found) return found;
229
+ if (path.resolve(dir) === root) return undefined;
230
+ const parent = path.dirname(dir);
231
+ if (parent === dir) return undefined;
232
+ dir = parent;
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Generates the `.toil/` working dir (routes table, mount entry, the HTML entry built from the
238
+ * project's `public/index.html` template, and mirrored `public/` assets) and returns the scanned
239
+ * routes. Called before every dev/build and on route add/remove during dev.
240
+ */
241
+ export function generate(cfg: ResolvedToilConfig): ScannedRoute[] {
242
+ const routes = scanRoutes(cfg.routesAbsDir);
243
+ fs.mkdirSync(cfg.toilDir, { recursive: true });
244
+
245
+ const layoutFile = findLayout(cfg);
246
+ const notFoundFile = findNotFound(cfg);
247
+ const globalErrorFile = findGlobalError(cfg);
248
+ const imp = (f: string): string => `() => import(${JSON.stringify(relFromToil(cfg, f))})`;
249
+ const routeObj = (r: ScannedRoute): string => {
250
+ const layouts = findSpecialChain(cfg, r.file, 'layout', false).map(imp).join(', ');
251
+ const templates = findSpecialChain(cfg, r.file, 'template', true).map(imp).join(', ');
252
+ const parts = [
253
+ `pattern: ${JSON.stringify(r.pattern)}`,
254
+ `load: ${imp(r.file)}`,
255
+ `layouts: [${layouts}]`,
256
+ ];
257
+ if (templates) parts.push(`templates: [${templates}]`);
258
+ const loadingFile = findNearest(cfg, r.file, 'loading');
259
+ if (loadingFile) parts.push(`loading: ${imp(loadingFile)}`);
260
+ const errorFile = findNearest(cfg, r.file, 'error');
261
+ if (errorFile) parts.push(`errorComponent: ${imp(errorFile)}`);
262
+ if (r.intercept) parts.push(`intercept: true`);
263
+ return `{ ${parts.join(', ')} }`;
264
+ };
265
+ const mainRoutes = routes.filter((r) => r.slot === undefined);
266
+ const slotNames = [...new Set(routes.flatMap((r) => (r.slot ? [r.slot] : [])))];
267
+ const slotsBody = slotNames
268
+ .map((name) => {
269
+ const items = routes
270
+ .filter((r) => r.slot === name)
271
+ .map((r) => ` ${routeObj(r)},`)
272
+ .join('\n');
273
+ return ` ${JSON.stringify(name)}: [\n${items}\n ],`;
274
+ })
275
+ .join('\n');
276
+ const pages = buildPageIndex(cfg.root, routes);
277
+ const routesSrc =
278
+ `// @ts-nocheck\n` +
279
+ `// AUTO-GENERATED by toil, do not edit.\n` +
280
+ `import type { RouteDef, LayoutLoader, NotFoundLoader, PageMeta } from 'toiljs/client';\n\n` +
281
+ `export const routes: RouteDef[] = [\n${mainRoutes.map((r) => ` ${routeObj(r)},`).join('\n')}\n];\n\n` +
282
+ `export const slots: Record<string, RouteDef[]> = {\n${slotsBody}\n};\n\n` +
283
+ `export const layout: LayoutLoader = ${layoutFile ? `() => import(${JSON.stringify(relFromToil(cfg, layoutFile))})` : 'null'};\n` +
284
+ `export const notFound: NotFoundLoader = ${notFoundFile ? `() => import(${JSON.stringify(relFromToil(cfg, notFoundFile))})` : 'null'};\n` +
285
+ `export const globalError = ${globalErrorFile ? `() => import(${JSON.stringify(relFromToil(cfg, globalErrorFile))})` : 'null'};\n\n` +
286
+ pagesModuleSource(pages);
287
+ fs.writeFileSync(path.join(cfg.toilDir, 'routes.ts'), routesSrc);
288
+
289
+ const globalsSrc =
290
+ `// @ts-nocheck\n` +
291
+ `// AUTO-GENERATED by toil, do not edit.\n` +
292
+ `import * as Toil from 'toiljs/client';\n` +
293
+ `import { BinaryWriter, BinaryReader, FastMap, FastSet } from 'toiljs/io';\n` +
294
+ `import { pages } from './routes';\n\n` +
295
+ `Object.assign(globalThis, { Toil, BinaryWriter, BinaryReader, FastMap, FastSet });\n` +
296
+ `Toil.setViewTransitions(${String(cfg.viewTransitions)});\n` +
297
+ `Toil.setTransitions(${String(cfg.transitions)});\n` +
298
+ `Toil.registerPages(pages);\n`;
299
+ fs.writeFileSync(path.join(cfg.toilDir, 'globals.ts'), globalsSrc);
300
+
301
+ const entryFile = findEntry(cfg);
302
+ const entrySrc = entryFile
303
+ ? `// @ts-nocheck\n` +
304
+ `// AUTO-GENERATED by toil, do not edit.\n` +
305
+ `import './globals';\n` +
306
+ `import ${JSON.stringify(relFromToil(cfg, entryFile))};\n`
307
+ : `// @ts-nocheck\n` +
308
+ `// AUTO-GENERATED by toil, do not edit.\n` +
309
+ `import './globals';\n` +
310
+ `import { mount } from 'toiljs/client';\n` +
311
+ `import { routes, layout, notFound, globalError, slots } from './routes';\n\n` +
312
+ `mount(routes, layout, notFound, globalError, slots);\n`;
313
+ fs.writeFileSync(path.join(cfg.toilDir, 'entry.tsx'), entrySrc);
314
+
315
+ fs.writeFileSync(path.join(cfg.root, 'toil-env.d.ts'), TOIL_ENV_DTS);
316
+ fs.writeFileSync(path.join(cfg.root, 'toil-routes.d.ts'), routesDts(cfg, routes));
317
+
318
+ fs.writeFileSync(path.join(cfg.toilDir, 'index.html'), buildHtml(cfg));
319
+ syncPublicAssets(cfg);
320
+ writeSeoFiles(cfg, routes);
321
+ writeDocs(cfg.toilDir);
322
+
323
+ return routes;
324
+ }
325
+
326
+ /**
327
+ * Writes the build-time SEO crawler files into `.toil/public` (Vite's publicDir, served in dev and
328
+ * copied to the output root in build): `robots.txt` (incl. AI-crawler directives), `sitemap.xml`
329
+ * (static routes), and `llms.txt` (AI guidance). No-op when SEO isn't configured.
330
+ */
331
+ function writeSeoFiles(cfg: ResolvedToilConfig, routes: ScannedRoute[]): void {
332
+ if (!cfg.seo) return;
333
+ const dest = path.join(cfg.toilDir, 'public');
334
+ const files: [string, string][] = [
335
+ ['robots.txt', robotsTxt(cfg.seo)],
336
+ ['sitemap.xml', sitemapXml(cfg.seo, routes)],
337
+ ['llms.txt', llmsTxt(cfg.seo, routes)],
338
+ ];
339
+ const present = files.filter(([, content]) => content !== '');
340
+ if (present.length === 0) return;
341
+ fs.mkdirSync(dest, { recursive: true });
342
+ for (const [name, content] of present) fs.writeFileSync(path.join(dest, name), content);
343
+ }
344
+
345
+ /** Fallback HTML when the project has no `public/index.html` template. The entry script is added
346
+ * by {@link buildHtml}. */
347
+ const DEFAULT_HTML =
348
+ `<!doctype html>\n<html lang="en">\n <head>\n <meta charset="utf-8" />\n` +
349
+ ` <meta name="viewport" content="width=device-width, initial-scale=1" />\n` +
350
+ ` <meta name="description" content="" />\n` +
351
+ ` <title>Toil App</title>\n </head>\n <body>\n <div id="root"></div>\n` +
352
+ ` </body>\n</html>\n`;
353
+
354
+ /** The module entry that boots the app, injected into the HTML (resolved relative to `.toil`). */
355
+ const ENTRY_SCRIPT = `<script type="module" src="./entry.tsx"></script>`;
356
+
357
+ /**
358
+ * Produces the `.toil/index.html` Vite entry from the project's `public/index.html` template (or
359
+ * the built-in default if absent), ensuring the generated module entry script is present. Users
360
+ * own the template, toil only guarantees the entry is wired, so it stays the SPA root.
361
+ */
362
+ function buildHtml(cfg: ResolvedToilConfig): string {
363
+ const templatePath = path.join(cfg.publicDir, 'index.html');
364
+ let html = fs.existsSync(templatePath) ? fs.readFileSync(templatePath, 'utf8') : DEFAULT_HTML;
365
+ // Inject the entry only if the template doesn't already reference it as a module script
366
+ // (matching the literal filename anywhere in the file would be too eager).
367
+ if (!/src=["']\.\/entry\.tsx["']/.test(html)) {
368
+ html = html.includes('</body>')
369
+ ? html.replace('</body>', ` ${ENTRY_SCRIPT}\n </body>`)
370
+ : `${html}\n${ENTRY_SCRIPT}\n`;
371
+ }
372
+ return html;
373
+ }
374
+
375
+ /**
376
+ * Mirrors the project's `public/` assets into `.toil/public/` (Vite's publicDir under the `.toil`
377
+ * root), excluding the `index.html` template, that is processed into the entry above, and copying
378
+ * it here would clobber the built, asset-hashed page. Cleared each run so deletions propagate.
379
+ */
380
+ function syncPublicAssets(cfg: ResolvedToilConfig): void {
381
+ const dest = path.join(cfg.toilDir, 'public');
382
+ fs.rmSync(dest, { recursive: true, force: true });
383
+ if (!fs.existsSync(cfg.publicDir)) return;
384
+
385
+ let copied = 0;
386
+ for (const entry of fs.readdirSync(cfg.publicDir, { withFileTypes: true })) {
387
+ if (entry.name === 'index.html') continue;
388
+ fs.cpSync(path.join(cfg.publicDir, entry.name), path.join(dest, entry.name), {
389
+ recursive: true,
390
+ });
391
+ copied++;
392
+ }
393
+ if (copied === 0) fs.rmSync(dest, { recursive: true, force: true });
394
+ }