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