toiljs 0.0.10 → 0.0.12
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/README.md +315 -1
- package/assets/logo.svg +37 -0
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/configure.js +10 -4
- package/build/cli/create.js +60 -32
- package/build/cli/diagnostics.d.ts +55 -0
- package/build/cli/diagnostics.js +333 -0
- package/build/cli/doctor.d.ts +6 -0
- package/build/cli/doctor.js +249 -0
- package/build/cli/index.js +26 -0
- package/build/cli/proc.d.ts +5 -0
- package/build/cli/proc.js +20 -0
- package/build/cli/ui.d.ts +1 -0
- package/build/cli/ui.js +1 -0
- package/build/cli/update.d.ts +7 -0
- package/build/cli/update.js +117 -0
- package/build/cli/updates.d.ts +10 -0
- package/build/cli/updates.js +45 -0
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/head/metadata.js +3 -1
- package/build/client/index.d.ts +5 -1
- package/build/client/index.js +2 -0
- package/build/client/navigation/navigation.js +1 -1
- package/build/client/routing/Router.js +2 -2
- package/build/client/search/search.d.ts +26 -0
- package/build/client/search/search.js +101 -0
- package/build/client/search/use-page-search.d.ts +8 -0
- package/build/client/search/use-page-search.js +21 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/generate.js +35 -26
- package/build/compiler/index.d.ts +2 -0
- package/build/compiler/index.js +1 -0
- package/build/compiler/pages.d.ts +8 -0
- package/build/compiler/pages.js +37 -0
- package/build/compiler/plugin.js +3 -1
- package/build/compiler/prerender.d.ts +1 -0
- package/build/compiler/prerender.js +11 -5
- package/build/compiler/seo.js +10 -3
- package/build/compiler/vite.js +7 -0
- package/build/io/.tsbuildinfo +1 -1
- package/examples/basic/client/components/Header.tsx +43 -38
- package/examples/basic/client/components/HoneycombBackground.tsx +223 -230
- package/examples/basic/client/layout.tsx +4 -1
- package/examples/basic/client/public/index.html +18 -16
- package/examples/basic/client/routes/(legal)/privacy.tsx +18 -0
- package/examples/basic/client/routes/(legal)/terms.tsx +15 -0
- package/examples/basic/client/routes/about.tsx +21 -19
- package/examples/basic/client/routes/blog/[id].tsx +26 -12
- package/examples/basic/client/routes/features/actions.tsx +67 -0
- package/examples/basic/client/routes/features/error/error.tsx +16 -0
- package/examples/basic/client/routes/features/error/index.tsx +27 -0
- package/examples/basic/client/routes/features/head.tsx +38 -0
- package/examples/basic/client/routes/features/index.tsx +83 -0
- package/examples/basic/client/routes/features/realtime.tsx +34 -0
- package/examples/basic/client/routes/features/script.tsx +31 -0
- package/examples/basic/client/routes/features/seo.tsx +39 -0
- package/examples/basic/client/routes/features/template/b.tsx +14 -0
- package/examples/basic/client/routes/features/template/index.tsx +20 -0
- package/examples/basic/client/routes/features/template/template.tsx +16 -0
- package/examples/basic/client/routes/files/[[...slug]].tsx +21 -0
- package/examples/basic/client/routes/gallery/@modal/(.)photo/[id].tsx +23 -0
- package/examples/basic/client/routes/gallery/index.tsx +42 -0
- package/examples/basic/client/routes/gallery/layout.tsx +13 -0
- package/examples/basic/client/routes/gallery/photo/[id].tsx +18 -0
- package/examples/basic/client/routes/get-started.tsx +157 -84
- package/examples/basic/client/routes/index.tsx +137 -87
- package/examples/basic/client/routes/loader-demo/index.tsx +59 -50
- package/examples/basic/client/routes/search.tsx +61 -0
- package/examples/basic/client/routes/test.tsx +7 -8
- package/examples/basic/client/styles/main.css +624 -552
- package/examples/basic/client/toil.tsx +2 -4
- package/package.json +3 -2
- package/presets/eslint.js +10 -3
- package/src/cli/configure.ts +363 -353
- package/src/cli/create.ts +563 -530
- package/src/cli/diagnostics.ts +421 -0
- package/src/cli/doctor.ts +318 -0
- package/src/cli/features.ts +166 -160
- package/src/cli/index.ts +242 -211
- package/src/cli/proc.ts +30 -0
- package/src/cli/ui.ts +111 -103
- package/src/cli/update.ts +150 -0
- package/src/cli/updates.ts +69 -0
- package/src/client/components/Image.tsx +91 -89
- package/src/client/dev/error-overlay.tsx +193 -197
- package/src/client/head/metadata.ts +94 -92
- package/src/client/index.ts +79 -64
- package/src/client/navigation/Link.tsx +94 -100
- package/src/client/navigation/navigation.ts +215 -218
- package/src/client/routing/Router.tsx +210 -193
- package/src/client/routing/hooks.ts +110 -114
- package/src/client/routing/lazy.ts +77 -81
- package/src/client/search/search.ts +189 -0
- package/src/client/search/use-page-search.ts +73 -0
- package/src/compiler/config.ts +173 -171
- package/src/compiler/fonts.ts +89 -87
- package/src/compiler/generate.ts +378 -364
- package/src/compiler/image-report.ts +88 -85
- package/src/compiler/index.ts +2 -0
- package/src/compiler/pages.ts +70 -0
- package/src/compiler/plugin.ts +51 -47
- package/src/compiler/prerender.ts +152 -130
- package/src/compiler/routes.ts +132 -131
- package/src/compiler/seo.ts +381 -356
- package/src/compiler/vite.ts +155 -130
- package/src/io/FastSet.ts +99 -96
- package/test/configure.test.ts +94 -90
- package/test/doctor.test.ts +140 -0
- package/test/dom/Image.test.tsx +73 -46
- package/test/dom/Script.test.tsx +48 -45
- package/test/dom/action.test.tsx +146 -129
- package/test/dom/error-overlay.test.tsx +44 -44
- package/test/dom/loader.test.tsx +2 -2
- package/test/dom/revalidate.test.tsx +1 -1
- package/test/dom/route-head.test.tsx +35 -2
- package/test/dom/slot.test.tsx +131 -109
- package/test/dom/view-transitions.test.tsx +53 -51
- package/test/features.test.ts +149 -142
- package/test/fonts.test.ts +28 -26
- package/test/head.test.ts +45 -35
- package/test/metadata.test.ts +42 -41
- package/test/pages.test.ts +105 -0
- package/test/prerender.test.ts +54 -46
- package/test/search.test.ts +114 -0
- package/test/seo.test.ts +164 -142
- package/test/slot-layouts.test.ts +69 -0
- package/test/update.test.ts +44 -0
|
@@ -1,85 +1,88 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
import pc from 'picocolors';
|
|
5
|
-
import type { Logger, Plugin } from 'vite';
|
|
6
|
-
|
|
7
|
-
/** Raster/vector outputs the image pipeline may emit. */
|
|
8
|
-
const IMAGE_RE = /\.(png|jpe?g|webp|avif|gif|tiff|svg)$/i;
|
|
9
|
-
|
|
10
|
-
/** Formats a byte count like Vite's asset table (kB, base 1000). */
|
|
11
|
-
function kb(bytes: number): string {
|
|
12
|
-
return `${(bytes / 1000).toFixed(2)} kB`;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface Variant {
|
|
16
|
-
readonly out: string;
|
|
17
|
-
readonly outSize: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Build-only plugin that reports which imported images the pipeline optimized, each source image,
|
|
22
|
-
* its emitted variant(s), and the size saved. `public/` assets (copied as-is) never enter the
|
|
23
|
-
* bundle, so they don't appear here. Logs nothing when no images were processed.
|
|
24
|
-
*
|
|
25
|
-
* `viteRoot` is Vite's root (the `.toil` dir) that emitted assets' `originalFileNames` are relative
|
|
26
|
-
* to; `projectRoot` is used only to print friendly source paths.
|
|
27
|
-
*/
|
|
28
|
-
export function imageReportPlugin(projectRoot: string, viteRoot: string): Plugin {
|
|
29
|
-
let logger: Logger | undefined;
|
|
30
|
-
return {
|
|
31
|
-
name: 'toil:image-report',
|
|
32
|
-
apply: 'build',
|
|
33
|
-
configResolved(config) {
|
|
34
|
-
logger = config.logger;
|
|
35
|
-
},
|
|
36
|
-
writeBundle(_options, bundle) {
|
|
37
|
-
// Group emitted image assets by their source file.
|
|
38
|
-
const bySource = new Map<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
logger
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import pc from 'picocolors';
|
|
5
|
+
import type { Logger, Plugin } from 'vite';
|
|
6
|
+
|
|
7
|
+
/** Raster/vector outputs the image pipeline may emit. */
|
|
8
|
+
const IMAGE_RE = /\.(png|jpe?g|webp|avif|gif|tiff|svg)$/i;
|
|
9
|
+
|
|
10
|
+
/** Formats a byte count like Vite's asset table (kB, base 1000). */
|
|
11
|
+
function kb(bytes: number): string {
|
|
12
|
+
return `${(bytes / 1000).toFixed(2)} kB`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface Variant {
|
|
16
|
+
readonly out: string;
|
|
17
|
+
readonly outSize: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Build-only plugin that reports which imported images the pipeline optimized, each source image,
|
|
22
|
+
* its emitted variant(s), and the size saved. `public/` assets (copied as-is) never enter the
|
|
23
|
+
* bundle, so they don't appear here. Logs nothing when no images were processed.
|
|
24
|
+
*
|
|
25
|
+
* `viteRoot` is Vite's root (the `.toil` dir) that emitted assets' `originalFileNames` are relative
|
|
26
|
+
* to; `projectRoot` is used only to print friendly source paths.
|
|
27
|
+
*/
|
|
28
|
+
export function imageReportPlugin(projectRoot: string, viteRoot: string): Plugin {
|
|
29
|
+
let logger: Logger | undefined;
|
|
30
|
+
return {
|
|
31
|
+
name: 'toil:image-report',
|
|
32
|
+
apply: 'build',
|
|
33
|
+
configResolved(config) {
|
|
34
|
+
logger = config.logger;
|
|
35
|
+
},
|
|
36
|
+
writeBundle(_options, bundle) {
|
|
37
|
+
// Group emitted image assets by their source file.
|
|
38
|
+
const bySource = new Map<
|
|
39
|
+
string,
|
|
40
|
+
{ label: string; inSize: number | null; variants: Variant[] }
|
|
41
|
+
>();
|
|
42
|
+
for (const file of Object.values(bundle)) {
|
|
43
|
+
if (file.type !== 'asset' || !IMAGE_RE.test(file.fileName)) continue;
|
|
44
|
+
const source = file.originalFileNames[0];
|
|
45
|
+
const key = source ?? file.fileName;
|
|
46
|
+
const outSize =
|
|
47
|
+
typeof file.source === 'string'
|
|
48
|
+
? Buffer.byteLength(file.source)
|
|
49
|
+
: file.source.byteLength;
|
|
50
|
+
|
|
51
|
+
let entry = bySource.get(key);
|
|
52
|
+
if (!entry) {
|
|
53
|
+
let inSize: number | null = null;
|
|
54
|
+
let label = '(generated)';
|
|
55
|
+
if (source !== undefined) {
|
|
56
|
+
const abs = path.resolve(viteRoot, source);
|
|
57
|
+
label = path.relative(projectRoot, abs);
|
|
58
|
+
try {
|
|
59
|
+
inSize = fs.statSync(abs).size;
|
|
60
|
+
} catch {
|
|
61
|
+
inSize = null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
entry = { label, inSize, variants: [] };
|
|
65
|
+
bySource.set(key, entry);
|
|
66
|
+
}
|
|
67
|
+
entry.variants.push({ out: file.fileName, outSize });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (bySource.size === 0 || !logger) return;
|
|
71
|
+
|
|
72
|
+
const count = bySource.size;
|
|
73
|
+
logger.info('');
|
|
74
|
+
logger.info(pc.green(` ✓ optimized ${String(count)} image${count === 1 ? '' : 's'}`));
|
|
75
|
+
for (const { label, inSize, variants } of bySource.values()) {
|
|
76
|
+
logger.info(` ${pc.dim(label)}`);
|
|
77
|
+
for (const v of variants) {
|
|
78
|
+
const saved =
|
|
79
|
+
inSize && inSize > 0
|
|
80
|
+
? pc.green(` -${String(Math.round((1 - v.outSize / inSize) * 100))}%`)
|
|
81
|
+
: '';
|
|
82
|
+
const from = inSize ? `${kb(inSize)} → ` : '';
|
|
83
|
+
logger.info(` ${pc.dim('→')} ${v.out} ${from}${kb(v.outSize)}${saved}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
package/src/compiler/index.ts
CHANGED
|
@@ -45,6 +45,8 @@ export async function start(opts: ToilCommandOptions = {}): Promise<RunningBacke
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
export { defineConfig, loadConfig } from './config.js';
|
|
48
|
+
export { scanRoutes } from './routes.js';
|
|
49
|
+
export type { ScannedRoute } from './routes.js';
|
|
48
50
|
export { TOIL_ENV_DTS } from './generate.js';
|
|
49
51
|
export { AI_HELPERS, AI_HELPER_IDS, aiHelperFiles, TOIL_DOCS } from './docs.js';
|
|
50
52
|
export type { AiHelper } from './docs.js';
|
|
@@ -0,0 +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
|
+
}
|
package/src/compiler/plugin.ts
CHANGED
|
@@ -1,47 +1,51 @@
|
|
|
1
|
-
import { type Plugin } from 'vite';
|
|
2
|
-
|
|
3
|
-
import { type ResolvedToilConfig } from './config.js';
|
|
4
|
-
import { generate } from './generate.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Vite plugin that keeps the generated route table in sync during dev: when a route file is
|
|
8
|
-
* added or removed, it regenerates `.toil/routes.ts` and triggers a full reload. Editing a
|
|
9
|
-
* route file's contents hot-reloads through `@vitejs/plugin-react` as usual.
|
|
10
|
-
*/
|
|
11
|
-
export function toilPlugin(cfg: ResolvedToilConfig): Plugin {
|
|
12
|
-
return {
|
|
13
|
-
name: 'toil',
|
|
14
|
-
// Catch empty import specifiers in source and report the file, rolldown otherwise fails
|
|
15
|
-
// resolution with a cryptic "The specifiers must be a non-empty string. Received ''".
|
|
16
|
-
transform(code, id) {
|
|
17
|
-
const file = id.split('?')[0];
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
1
|
+
import { type Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
import { type ResolvedToilConfig } from './config.js';
|
|
4
|
+
import { generate } from './generate.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Vite plugin that keeps the generated route table in sync during dev: when a route file is
|
|
8
|
+
* added or removed, it regenerates `.toil/routes.ts` and triggers a full reload. Editing a
|
|
9
|
+
* route file's contents hot-reloads through `@vitejs/plugin-react` as usual.
|
|
10
|
+
*/
|
|
11
|
+
export function toilPlugin(cfg: ResolvedToilConfig): Plugin {
|
|
12
|
+
return {
|
|
13
|
+
name: 'toil',
|
|
14
|
+
// Catch empty import specifiers in source and report the file, rolldown otherwise fails
|
|
15
|
+
// resolution with a cryptic "The specifiers must be a non-empty string. Received ''".
|
|
16
|
+
transform(code, id) {
|
|
17
|
+
const file = id.split('?')[0];
|
|
18
|
+
if (
|
|
19
|
+
id.includes('\0') ||
|
|
20
|
+
file.includes('/node_modules/') ||
|
|
21
|
+
!/\.[mc]?[jt]sx?$/.test(file)
|
|
22
|
+
) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const empty =
|
|
26
|
+
/^[ \t]*import\s+(['"])\1\s*;?[ \t]*$/m.test(code) ||
|
|
27
|
+
/^[ \t]*import\b[^'"\n]*\bfrom\s+(['"])\1/m.test(code) ||
|
|
28
|
+
/^[ \t]*export\b[^'"\n]*\bfrom\s+(['"])\1/m.test(code) ||
|
|
29
|
+
/\bimport\s*\(\s*(['"])\1\s*\)/.test(code);
|
|
30
|
+
if (empty) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`toil: empty import specifier (e.g. \`import '';\`) in ${file}, remove or complete the import.`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
},
|
|
37
|
+
configureServer(server) {
|
|
38
|
+
// Trailing slash so a sibling like `routes-extra/` doesn't match the `routes/` prefix.
|
|
39
|
+
const routesPrefix = cfg.routesAbsDir.replace(/\\/g, '/').replace(/\/?$/, '/');
|
|
40
|
+
const onChange = (file: string): void => {
|
|
41
|
+
if (file.replace(/\\/g, '/').startsWith(routesPrefix)) {
|
|
42
|
+
generate(cfg);
|
|
43
|
+
server.ws.send({ type: 'full-reload' });
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
server.watcher.add(cfg.routesAbsDir);
|
|
47
|
+
server.watcher.on('add', onChange);
|
|
48
|
+
server.watcher.on('unlink', onChange);
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -1,130 +1,152 @@
|
|
|
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 { scanRoutes } from './routes.js';
|
|
11
|
-
import { injectSeoHtml, routeSeo } from './seo.js';
|
|
12
|
-
|
|
13
|
-
type Ts = typeof TS;
|
|
14
|
-
|
|
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> {
|
|
17
|
-
try {
|
|
18
|
-
const resolved = createRequire(path.join(root, 'package.json')).resolve('typescript');
|
|
19
|
-
const mod = (await import(pathToFileURL(resolved).href)) as { default?: Ts } & Ts;
|
|
20
|
-
return mod.default ?? mod;
|
|
21
|
-
} catch {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Marks an AST node that isn't a static literal (so its value can't be baked at build). */
|
|
27
|
-
const UNRESOLVED = Symbol('unresolved');
|
|
28
|
-
|
|
29
|
-
/** Statically evaluates a literal expression node to a JS value, or `UNRESOLVED` if it isn't one. */
|
|
30
|
-
function evalNode(ts: Ts, node: TS.Expression): unknown {
|
|
31
|
-
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
|
|
32
|
-
if (ts.isNumericLiteral(node)) return Number(node.text);
|
|
33
|
-
if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
34
|
-
if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
35
|
-
if (node.kind === ts.SyntaxKind.NullKeyword) return null;
|
|
36
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
37
|
-
const out: unknown[] = [];
|
|
38
|
-
for (const el of node.elements) {
|
|
39
|
-
const value = evalNode(ts, el);
|
|
40
|
-
if (value === UNRESOLVED) return UNRESOLVED;
|
|
41
|
-
out.push(value);
|
|
42
|
-
}
|
|
43
|
-
return out;
|
|
44
|
-
}
|
|
45
|
-
if (ts.isObjectLiteralExpression(node)) return evalObject(ts, node);
|
|
46
|
-
return UNRESOLVED;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** Evaluates an object literal to a plain object, skipping any property that isn't a static literal. */
|
|
50
|
-
function evalObject(ts: Ts, node: TS.ObjectLiteralExpression): Record<string, unknown> {
|
|
51
|
-
const obj: Record<string, unknown> = {};
|
|
52
|
-
for (const prop of node.properties) {
|
|
53
|
-
if (!ts.isPropertyAssignment(prop)) continue;
|
|
54
|
-
const key = ts.isIdentifier(prop.name)
|
|
55
|
-
? prop.name.text
|
|
56
|
-
: ts.isStringLiteral(prop.name)
|
|
57
|
-
? prop.name.text
|
|
58
|
-
: null;
|
|
59
|
-
if (key === null) continue;
|
|
60
|
-
const value = evalNode(ts, prop.initializer);
|
|
61
|
-
if (value !== UNRESOLVED) obj[key] = value;
|
|
62
|
-
}
|
|
63
|
-
return obj;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Extracts
|
|
68
|
-
* statically-evaluable subset (dynamic
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
71
|
-
export function
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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 { scanRoutes } from './routes.js';
|
|
11
|
+
import { injectSeoHtml, routeSeo } from './seo.js';
|
|
12
|
+
|
|
13
|
+
type Ts = typeof TS;
|
|
14
|
+
|
|
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> {
|
|
17
|
+
try {
|
|
18
|
+
const resolved = createRequire(path.join(root, 'package.json')).resolve('typescript');
|
|
19
|
+
const mod = (await import(pathToFileURL(resolved).href)) as { default?: Ts } & Ts;
|
|
20
|
+
return mod.default ?? mod;
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Marks an AST node that isn't a static literal (so its value can't be baked at build). */
|
|
27
|
+
const UNRESOLVED = Symbol('unresolved');
|
|
28
|
+
|
|
29
|
+
/** Statically evaluates a literal expression node to a JS value, or `UNRESOLVED` if it isn't one. */
|
|
30
|
+
function evalNode(ts: Ts, node: TS.Expression): unknown {
|
|
31
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) return node.text;
|
|
32
|
+
if (ts.isNumericLiteral(node)) return Number(node.text);
|
|
33
|
+
if (node.kind === ts.SyntaxKind.TrueKeyword) return true;
|
|
34
|
+
if (node.kind === ts.SyntaxKind.FalseKeyword) return false;
|
|
35
|
+
if (node.kind === ts.SyntaxKind.NullKeyword) return null;
|
|
36
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
37
|
+
const out: unknown[] = [];
|
|
38
|
+
for (const el of node.elements) {
|
|
39
|
+
const value = evalNode(ts, el);
|
|
40
|
+
if (value === UNRESOLVED) return UNRESOLVED;
|
|
41
|
+
out.push(value);
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
if (ts.isObjectLiteralExpression(node)) return evalObject(ts, node);
|
|
46
|
+
return UNRESOLVED;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Evaluates an object literal to a plain object, skipping any property that isn't a static literal. */
|
|
50
|
+
function evalObject(ts: Ts, node: TS.ObjectLiteralExpression): Record<string, unknown> {
|
|
51
|
+
const obj: Record<string, unknown> = {};
|
|
52
|
+
for (const prop of node.properties) {
|
|
53
|
+
if (!ts.isPropertyAssignment(prop)) continue;
|
|
54
|
+
const key = ts.isIdentifier(prop.name)
|
|
55
|
+
? prop.name.text
|
|
56
|
+
: ts.isStringLiteral(prop.name)
|
|
57
|
+
? prop.name.text
|
|
58
|
+
: null;
|
|
59
|
+
if (key === null) continue;
|
|
60
|
+
const value = evalNode(ts, prop.initializer);
|
|
61
|
+
if (value !== UNRESOLVED) obj[key] = value;
|
|
62
|
+
}
|
|
63
|
+
return obj;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Extracts the named `export const <name> = { … }` object-literal exports from a route file in a
|
|
68
|
+
* single parse, returning the statically-evaluable subset of each (dynamic and computed values are
|
|
69
|
+
* skipped). Names that are absent or not object literals are omitted from the result.
|
|
70
|
+
*/
|
|
71
|
+
export function extractStaticExports(
|
|
72
|
+
ts: Ts,
|
|
73
|
+
filePath: string,
|
|
74
|
+
names: readonly string[],
|
|
75
|
+
): Record<string, Record<string, unknown>> {
|
|
76
|
+
let source: string;
|
|
77
|
+
try {
|
|
78
|
+
source = fs.readFileSync(filePath, 'utf8');
|
|
79
|
+
} catch {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
const wanted = new Set(names);
|
|
83
|
+
const out: Record<string, Record<string, unknown>> = {};
|
|
84
|
+
const sf = ts.createSourceFile(
|
|
85
|
+
filePath,
|
|
86
|
+
source,
|
|
87
|
+
ts.ScriptTarget.Latest,
|
|
88
|
+
true,
|
|
89
|
+
ts.ScriptKind.TSX,
|
|
90
|
+
);
|
|
91
|
+
for (const stmt of sf.statements) {
|
|
92
|
+
if (!ts.isVariableStatement(stmt)) continue;
|
|
93
|
+
if (!stmt.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) continue;
|
|
94
|
+
for (const decl of stmt.declarationList.declarations) {
|
|
95
|
+
if (
|
|
96
|
+
ts.isIdentifier(decl.name) &&
|
|
97
|
+
wanted.has(decl.name.text) &&
|
|
98
|
+
!(decl.name.text in out) &&
|
|
99
|
+
decl.initializer &&
|
|
100
|
+
ts.isObjectLiteralExpression(decl.initializer)
|
|
101
|
+
) {
|
|
102
|
+
out[decl.name.text] = evalObject(ts, decl.initializer);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Extracts a route's `export const metadata = { … }` if it's a static object literal, returning the
|
|
111
|
+
* statically-evaluable subset (dynamic `generateMetadata` and computed values are skipped). `null`
|
|
112
|
+
* when the file has no static metadata.
|
|
113
|
+
*/
|
|
114
|
+
export function extractStaticMetadata(ts: Ts, filePath: string): Record<string, unknown> | null {
|
|
115
|
+
return extractStaticExports(ts, filePath, ['metadata']).metadata ?? null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Build-only plugin that statically pre-renders per-route HTML for SEO. After the bundle is written,
|
|
120
|
+
* it takes the built shell (`index.html`), and for each static route bakes that route's
|
|
121
|
+
* `metadata` (merged over the site-wide `seo` defaults) into a `<route>/index.html` so a JS-less
|
|
122
|
+
* crawler hitting the route gets correct per-page tags. Dynamic (`generateMetadata`) and `:param`
|
|
123
|
+
* routes are skipped (no data at build) and fall back to the client-rendered shell.
|
|
124
|
+
*/
|
|
125
|
+
export function prerenderPlugin(cfg: ResolvedToilConfig): Plugin {
|
|
126
|
+
return {
|
|
127
|
+
name: 'toil:prerender-seo',
|
|
128
|
+
apply: 'build',
|
|
129
|
+
async closeBundle() {
|
|
130
|
+
if (!cfg.seo) return;
|
|
131
|
+
const outDir = path.resolve(cfg.root, cfg.outDir);
|
|
132
|
+
const shellPath = path.join(outDir, 'index.html');
|
|
133
|
+
if (!fs.existsSync(shellPath)) return;
|
|
134
|
+
const shell = fs.readFileSync(shellPath, 'utf8');
|
|
135
|
+
const ts = await loadTypeScript(cfg.root);
|
|
136
|
+
|
|
137
|
+
const routes = scanRoutes(cfg.routesAbsDir).filter(
|
|
138
|
+
(r) => r.slot === undefined && !r.intercept && !/[:*]/.test(r.pattern),
|
|
139
|
+
);
|
|
140
|
+
for (const route of routes) {
|
|
141
|
+
const metadata = ts ? extractStaticMetadata(ts, route.file) : null;
|
|
142
|
+
const html = injectSeoHtml(shell, routeSeo(cfg.seo, metadata, route.pattern));
|
|
143
|
+
const target =
|
|
144
|
+
route.pattern === '/'
|
|
145
|
+
? shellPath
|
|
146
|
+
: path.join(outDir, route.pattern.replace(/^\//, ''), 'index.html');
|
|
147
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
148
|
+
fs.writeFileSync(target, html);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|