toiljs 0.0.11 → 0.0.14
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 +3 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/configure.js +10 -4
- package/build/cli/create.js +58 -30
- 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 +33 -24
- 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/io/.tsbuildinfo +1 -1
- package/examples/basic/client/components/Header.tsx +43 -41
- package/examples/basic/client/components/HoneycombBackground.tsx +223 -230
- package/examples/basic/client/public/index.html +18 -16
- package/examples/basic/client/routes/(legal)/privacy.tsx +18 -19
- package/examples/basic/client/routes/(legal)/terms.tsx +15 -16
- package/examples/basic/client/routes/about.tsx +21 -22
- package/examples/basic/client/routes/blog/[id].tsx +26 -18
- package/examples/basic/client/routes/features/actions.tsx +67 -67
- package/examples/basic/client/routes/features/error/index.tsx +27 -27
- package/examples/basic/client/routes/features/head.tsx +38 -38
- package/examples/basic/client/routes/features/index.tsx +83 -75
- package/examples/basic/client/routes/features/realtime.tsx +34 -32
- package/examples/basic/client/routes/features/script.tsx +31 -31
- package/examples/basic/client/routes/features/seo.tsx +39 -39
- package/examples/basic/client/routes/features/template/index.tsx +20 -20
- package/examples/basic/client/routes/features/template/template.tsx +16 -18
- package/examples/basic/client/routes/gallery/@modal/(.)photo/[id].tsx +23 -23
- package/examples/basic/client/routes/gallery/index.tsx +42 -42
- package/examples/basic/client/routes/gallery/photo/[id].tsx +18 -18
- package/examples/basic/client/routes/get-started.tsx +157 -84
- package/examples/basic/client/routes/index.tsx +137 -96
- package/examples/basic/client/routes/loader-demo/index.tsx +59 -52
- 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/package.json +2 -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 +45 -27
- 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 -145
- 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 +1 -1
- package/test/dom/loader.test.tsx +2 -2
- package/test/dom/revalidate.test.tsx +1 -1
- package/test/dom/route-head.test.tsx +1 -2
- package/test/dom/router-loading.test.tsx +1 -1
- 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 +30 -8
- package/test/update.test.ts +44 -0
package/src/compiler/generate.ts
CHANGED
|
@@ -3,6 +3,7 @@ import path from 'node:path';
|
|
|
3
3
|
|
|
4
4
|
import { type ResolvedToilConfig } from './config.js';
|
|
5
5
|
import { writeDocs } from './docs.js';
|
|
6
|
+
import { buildPageIndex, pagesModuleSource } from './pages.js';
|
|
6
7
|
import { scanRoutes, type ScannedRoute } from './routes.js';
|
|
7
8
|
import { llmsTxt, robotsTxt, sitemapXml } from './seo.js';
|
|
8
9
|
|
|
@@ -17,28 +18,28 @@ import { llmsTxt, robotsTxt, sitemapXml } from './seo.js';
|
|
|
17
18
|
/** Side-effect style imports (e.g. `import './styles/main.css'`). */
|
|
18
19
|
const STYLE_EXTENSIONS = ['css', 'scss', 'sass', 'less', 'styl', 'stylus', 'pcss', 'sss'];
|
|
19
20
|
/** Asset imports whose default export is the resolved URL string (e.g. `import logo from './logo.svg'`). */
|
|
20
|
-
const ASSET_EXTENSIONS = [
|
|
21
|
-
'svg',
|
|
22
|
-
'png',
|
|
23
|
-
'jpg',
|
|
24
|
-
'jpeg',
|
|
25
|
-
'gif',
|
|
26
|
-
'webp',
|
|
27
|
-
'avif',
|
|
28
|
-
'ico',
|
|
29
|
-
'bmp',
|
|
30
|
-
'apng',
|
|
31
|
-
];
|
|
21
|
+
const ASSET_EXTENSIONS = ['svg', 'png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'ico', 'bmp', 'apng'];
|
|
32
22
|
|
|
33
23
|
const STYLE_MODULES = STYLE_EXTENSIONS.map((ext) => `declare module '*.${ext}' {}`).join('\n');
|
|
34
24
|
const ASSET_MODULES = ASSET_EXTENSIONS.map(
|
|
35
25
|
(ext) => `declare module '*.${ext}' {\n const src: string;\n export default src;\n}`,
|
|
36
26
|
).join('\n');
|
|
37
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
|
+
|
|
38
41
|
export const TOIL_ENV_DTS =
|
|
39
42
|
`// AUTO-GENERATED by toil, do not edit.\n` +
|
|
40
|
-
// Types for image-optimization query imports (`import img from './x.png?w=400&format=webp'`).
|
|
41
|
-
`/// <reference types="vite-imagetools/client" />\n` +
|
|
42
43
|
`declare const Toil: typeof import('toiljs/client');\n` +
|
|
43
44
|
`declare namespace Toil {\n` +
|
|
44
45
|
` type LoaderArgs = import('toiljs/client').LoaderArgs;\n` +
|
|
@@ -49,6 +50,8 @@ export const TOIL_ENV_DTS =
|
|
|
49
50
|
` type RouteErrorProps = import('toiljs/client').RouteErrorProps;\n` +
|
|
50
51
|
` type Href = import('toiljs/client').Href;\n` +
|
|
51
52
|
` type RoutePath = import('toiljs/client').RoutePath;\n` +
|
|
53
|
+
` type PageMeta = import('toiljs/client').PageMeta;\n` +
|
|
54
|
+
` type SearchHints = import('toiljs/client').SearchHints;\n` +
|
|
52
55
|
`}\n` +
|
|
53
56
|
`declare const BinaryWriter: typeof import('toiljs/io').BinaryWriter;\n` +
|
|
54
57
|
`declare const BinaryReader: typeof import('toiljs/io').BinaryReader;\n` +
|
|
@@ -59,12 +62,15 @@ export const TOIL_ENV_DTS =
|
|
|
59
62
|
`\n` +
|
|
60
63
|
`${ASSET_MODULES}\n` +
|
|
61
64
|
`\n` +
|
|
65
|
+
`${IMAGETOOLS_MODULES}\n` +
|
|
66
|
+
`\n` +
|
|
62
67
|
`declare module 'toiljs/routes' {\n` +
|
|
63
68
|
` export const routes: import('toiljs/client').RouteDef[];\n` +
|
|
64
69
|
` export const layout: import('toiljs/client').LayoutLoader;\n` +
|
|
65
70
|
` export const notFound: import('toiljs/client').NotFoundLoader;\n` +
|
|
66
71
|
` export const globalError: import('toiljs/client').ErrorComponentLoader;\n` +
|
|
67
72
|
` export const slots: Record<string, import('toiljs/client').RouteDef[]>;\n` +
|
|
73
|
+
` export const pages: import('toiljs/client').PageMeta[];\n` +
|
|
68
74
|
`}\n`;
|
|
69
75
|
|
|
70
76
|
/**
|
|
@@ -73,7 +79,10 @@ export const TOIL_ENV_DTS =
|
|
|
73
79
|
* `allowImportingTsExtensions` (TS5097) when the generated files are checked; Vite still resolves it.
|
|
74
80
|
*/
|
|
75
81
|
function relFromToil(cfg: ResolvedToilConfig, abs: string): string {
|
|
76
|
-
let rel = path
|
|
82
|
+
let rel = path
|
|
83
|
+
.relative(cfg.toilDir, abs)
|
|
84
|
+
.replace(/\\/g, '/')
|
|
85
|
+
.replace(/\.(tsx|jsx)$/, '');
|
|
77
86
|
if (!rel.startsWith('.')) rel = './' + rel;
|
|
78
87
|
return rel;
|
|
79
88
|
}
|
|
@@ -111,7 +120,9 @@ function routePathUnion(routes: ScannedRoute[]): string {
|
|
|
111
120
|
members.add(`'${route.pattern}'`);
|
|
112
121
|
continue;
|
|
113
122
|
}
|
|
114
|
-
const parts = segments.map((s) =>
|
|
123
|
+
const parts = segments.map((s) =>
|
|
124
|
+
s.startsWith(':') || s.startsWith('*') ? '${string}' : s,
|
|
125
|
+
);
|
|
115
126
|
members.add('`/' + parts.join('/') + '`');
|
|
116
127
|
const optionalIdx = segments.findIndex((s) => s.startsWith('**'));
|
|
117
128
|
if (optionalIdx !== -1) {
|
|
@@ -128,14 +139,19 @@ function routePathUnion(routes: ScannedRoute[]): string {
|
|
|
128
139
|
*/
|
|
129
140
|
function routesDts(cfg: ResolvedToilConfig, routes: ScannedRoute[]): string {
|
|
130
141
|
// Type-only namespace import of every route module (erased at build) so editors don't flag a
|
|
131
|
-
// route's `loader` / `metadata` / `generateMetadata` / `revalidate` / `default` exports as unused
|
|
132
|
-
//
|
|
142
|
+
// route's `loader` / `metadata` / `generateMetadata` / `revalidate` / `default` exports as unused,
|
|
143
|
+
// the compiler consumes them via dynamic `import()`, which editors don't count as a reference.
|
|
133
144
|
const refs = routes.map((route, i) => {
|
|
134
|
-
let rel = path
|
|
145
|
+
let rel = path
|
|
146
|
+
.relative(cfg.root, route.file)
|
|
147
|
+
.replace(/\\/g, '/')
|
|
148
|
+
.replace(/\.(tsx|jsx)$/, '');
|
|
135
149
|
if (!rel.startsWith('.')) rel = `./${rel}`;
|
|
136
150
|
return { name: `_toilRoute${String(i)}`, rel };
|
|
137
151
|
});
|
|
138
|
-
const imports = refs
|
|
152
|
+
const imports = refs
|
|
153
|
+
.map((m) => `import type * as ${m.name} from ${JSON.stringify(m.rel)};\n`)
|
|
154
|
+
.join('');
|
|
139
155
|
const referenced = refs.length
|
|
140
156
|
? `export type _ToilRouteModules = [${refs.map((m) => `typeof ${m.name}`).join(', ')}];\n`
|
|
141
157
|
: `export {};\n`;
|
|
@@ -255,24 +271,28 @@ export function generate(cfg: ResolvedToilConfig): ScannedRoute[] {
|
|
|
255
271
|
return ` ${JSON.stringify(name)}: [\n${items}\n ],`;
|
|
256
272
|
})
|
|
257
273
|
.join('\n');
|
|
274
|
+
const pages = buildPageIndex(cfg.root, routes);
|
|
258
275
|
const routesSrc =
|
|
259
276
|
`// @ts-nocheck\n` +
|
|
260
277
|
`// AUTO-GENERATED by toil, do not edit.\n` +
|
|
261
|
-
`import type { RouteDef, LayoutLoader, NotFoundLoader } from 'toiljs/client';\n\n` +
|
|
278
|
+
`import type { RouteDef, LayoutLoader, NotFoundLoader, PageMeta } from 'toiljs/client';\n\n` +
|
|
262
279
|
`export const routes: RouteDef[] = [\n${mainRoutes.map((r) => ` ${routeObj(r)},`).join('\n')}\n];\n\n` +
|
|
263
280
|
`export const slots: Record<string, RouteDef[]> = {\n${slotsBody}\n};\n\n` +
|
|
264
281
|
`export const layout: LayoutLoader = ${layoutFile ? `() => import(${JSON.stringify(relFromToil(cfg, layoutFile))})` : 'null'};\n` +
|
|
265
282
|
`export const notFound: NotFoundLoader = ${notFoundFile ? `() => import(${JSON.stringify(relFromToil(cfg, notFoundFile))})` : 'null'};\n` +
|
|
266
|
-
`export const globalError = ${globalErrorFile ? `() => import(${JSON.stringify(relFromToil(cfg, globalErrorFile))})` : 'null'};\n
|
|
283
|
+
`export const globalError = ${globalErrorFile ? `() => import(${JSON.stringify(relFromToil(cfg, globalErrorFile))})` : 'null'};\n\n` +
|
|
284
|
+
pagesModuleSource(pages);
|
|
267
285
|
fs.writeFileSync(path.join(cfg.toilDir, 'routes.ts'), routesSrc);
|
|
268
286
|
|
|
269
287
|
const globalsSrc =
|
|
270
288
|
`// @ts-nocheck\n` +
|
|
271
289
|
`// AUTO-GENERATED by toil, do not edit.\n` +
|
|
272
290
|
`import * as Toil from 'toiljs/client';\n` +
|
|
273
|
-
`import { BinaryWriter, BinaryReader, FastMap, FastSet } from 'toiljs/io';\n
|
|
291
|
+
`import { BinaryWriter, BinaryReader, FastMap, FastSet } from 'toiljs/io';\n` +
|
|
292
|
+
`import { pages } from './routes';\n\n` +
|
|
274
293
|
`Object.assign(globalThis, { Toil, BinaryWriter, BinaryReader, FastMap, FastSet });\n` +
|
|
275
|
-
`Toil.setViewTransitions(${String(cfg.viewTransitions)});\n
|
|
294
|
+
`Toil.setViewTransitions(${String(cfg.viewTransitions)});\n` +
|
|
295
|
+
`Toil.registerPages(pages);\n`;
|
|
276
296
|
fs.writeFileSync(path.join(cfg.toilDir, 'globals.ts'), globalsSrc);
|
|
277
297
|
|
|
278
298
|
const entryFile = findEntry(cfg);
|
|
@@ -338,9 +358,7 @@ const ENTRY_SCRIPT = `<script type="module" src="./entry.tsx"></script>`;
|
|
|
338
358
|
*/
|
|
339
359
|
function buildHtml(cfg: ResolvedToilConfig): string {
|
|
340
360
|
const templatePath = path.join(cfg.publicDir, 'index.html');
|
|
341
|
-
let html = fs.existsSync(templatePath)
|
|
342
|
-
? fs.readFileSync(templatePath, 'utf8')
|
|
343
|
-
: DEFAULT_HTML;
|
|
361
|
+
let html = fs.existsSync(templatePath) ? fs.readFileSync(templatePath, 'utf8') : DEFAULT_HTML;
|
|
344
362
|
// Inject the entry only if the template doesn't already reference it as a module script
|
|
345
363
|
// (matching the literal filename anywhere in the file would be too eager).
|
|
346
364
|
if (!/src=["']\.\/entry\.tsx["']/.test(html)) {
|
|
@@ -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
|
+
}
|