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