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