@teyik0/furin 0.9.0-alpha.1 → 0.9.0-alpha.3

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 (91) hide show
  1. package/dist/adapter/runtime-build.d.ts +7 -3
  2. package/dist/adapter/vercel.js +31 -6
  3. package/dist/build/client.d.ts +1 -1
  4. package/dist/build/entry-template.d.ts +3 -12
  5. package/dist/build/entry-template.ts +3 -15
  6. package/dist/build/hydrate.d.ts +6 -1
  7. package/dist/build/index.d.ts +1 -1
  8. package/dist/build/index.js +597 -445
  9. package/dist/build/route-metadata.d.ts +2 -2
  10. package/dist/build/route-types.d.ts +1 -1
  11. package/dist/build/types.d.ts +10 -4
  12. package/dist/cli/index.js +626 -466
  13. package/dist/client/chunks/{client-xy6g38kx.js → client-a7spxfnn.js} +24 -5
  14. package/dist/client/chunks/{client-rjhxgt64.js → client-d9v49jwz.js} +1 -1
  15. package/dist/client/chunks/{client-k29262a4.js → client-rdxyrw11.js} +1 -1
  16. package/dist/client/define-route.d.ts +14 -0
  17. package/dist/client/dev-error-overlay.ts +18 -0
  18. package/dist/client/internal/runtime-types.d.ts +8 -0
  19. package/dist/client/link.js +3 -3
  20. package/dist/client/router/link-utils.d.ts +3 -2
  21. package/dist/client/router/search/index.js +2 -2
  22. package/dist/client/router/types.d.ts +9 -3
  23. package/dist/client.js +22 -6
  24. package/dist/config.d.ts +4 -0
  25. package/dist/config.js +4 -1
  26. package/dist/define-route.d.ts +53 -12
  27. package/dist/furin.d.ts +13 -3
  28. package/dist/furin.js +771 -616
  29. package/dist/plugin/client-plugin.d.ts +1 -1
  30. package/dist/plugin/index.js +5 -4
  31. package/dist/plugin/route-config.d.ts +3 -3
  32. package/dist/plugin/route-manifest.d.ts +46 -0
  33. package/dist/plugin/route-tree.d.ts +13 -29
  34. package/dist/routes.d.ts +2 -2
  35. package/dist/server/dev/runtime.d.ts +2 -1
  36. package/dist/server/internal.d.ts +6 -0
  37. package/dist/server/internal.js +57 -76
  38. package/dist/server/registrations.d.ts +4 -2
  39. package/dist/server/router/defined-route.d.ts +4 -0
  40. package/dist/server/router/discovery.d.ts +14 -4
  41. package/dist/server/router/hmr.d.ts +12 -12
  42. package/dist/server/router/patterns.d.ts +4 -12
  43. package/dist/server/router/types.d.ts +16 -1
  44. package/dist/server/static-export.js +88 -120
  45. package/dist/server/sync/drizzle/index.d.ts +8 -10
  46. package/dist/shared/route-specificity.d.ts +10 -1
  47. package/package.json +4 -7
  48. package/src/adapter/package.ts +11 -37
  49. package/src/adapter/runtime-build.ts +5 -4
  50. package/src/adapter/static.ts +1 -1
  51. package/src/adapter/vercel.ts +2 -1
  52. package/src/build/client.ts +3 -3
  53. package/src/build/discover-pages-process.ts +1 -1
  54. package/src/build/elysia-aot.ts +2 -2
  55. package/src/build/entry-template.ts +3 -15
  56. package/src/build/hydrate.ts +14 -6
  57. package/src/build/index.ts +19 -9
  58. package/src/build/route-metadata.ts +15 -8
  59. package/src/build/route-types.ts +18 -43
  60. package/src/build/types.ts +12 -5
  61. package/src/cli/config.ts +6 -0
  62. package/src/cli/index.ts +30 -14
  63. package/src/client/define-route.ts +29 -4
  64. package/src/client/dev-error-overlay.ts +18 -0
  65. package/src/client/internal/runtime-types.ts +8 -0
  66. package/src/client/router/link-utils.ts +16 -8
  67. package/src/client/router/types.ts +40 -14
  68. package/src/config.ts +5 -0
  69. package/src/define-route.ts +85 -16
  70. package/src/furin.ts +31 -7
  71. package/src/plugin/client-plugin.ts +13 -8
  72. package/src/plugin/dev-routes.ts +8 -17
  73. package/src/plugin/route-config.ts +77 -62
  74. package/src/plugin/route-manifest.ts +437 -0
  75. package/src/plugin/route-modules.ts +6 -1
  76. package/src/plugin/route-registry.ts +11 -42
  77. package/src/plugin/route-tree.ts +71 -181
  78. package/src/routes.ts +2 -2
  79. package/src/server/dev/runtime.ts +16 -22
  80. package/src/server/dev-page-plugin.ts +5 -2
  81. package/src/server/internal.ts +4 -1
  82. package/src/server/registrations.ts +5 -2
  83. package/src/server/render/static-params.ts +2 -10
  84. package/src/server/router/defined-route.ts +8 -0
  85. package/src/server/router/discovery.ts +237 -196
  86. package/src/server/router/hmr.ts +45 -63
  87. package/src/server/router/patterns.ts +41 -117
  88. package/src/server/router/types.ts +17 -1
  89. package/src/server/sync/drizzle/index.ts +18 -19
  90. package/src/shared/route-specificity.ts +34 -2
  91. package/src/shared/utils/index.ts +1 -1
@@ -1,12 +1,17 @@
1
1
  import { existsSync, readFileSync, writeFileSync } from "node:fs";
2
- import { basename, join, relative, resolve } from "node:path";
2
+ import { relative, resolve } from "node:path";
3
3
  import type { CallExpression, Node, ObjectExpression, ObjectProperty, Program } from "yuku-parser";
4
4
  import type { DevDiagnosticStore } from "../server/dev/diagnostics.ts";
5
5
  import { detectLangFromPath, unwrapTSExpression } from "../server/lang-detect.ts";
6
- import { parseDynamicRouteSegment } from "../server/router/patterns.ts";
7
6
  import { parseSource } from "../shared/parser.ts";
8
7
  import { collectDefineRouteBindings, exportedRouteChain } from "./route-chains.ts";
9
- import { fileRouteId, type RouteInstanceSpec, routeSourcePaths } from "./route-tree.ts";
8
+ import {
9
+ type ManifestRoute,
10
+ type RouteInstanceSpec,
11
+ type RouteManifest,
12
+ type RouteParam,
13
+ scanRouteManifest,
14
+ } from "./route-manifest.ts";
10
15
 
11
16
  /**
12
17
  * Static route-config checks shared by the dev server and `furin build`.
@@ -21,9 +26,7 @@ export interface RouteConfigIssue {
21
26
  message: string;
22
27
  }
23
28
 
24
- const EXTENSION_RE = /\.(?:[cm]?[jt]sx?)$/;
25
29
  const IDENTIFIER_RE = /^[A-Za-z_$][\w$]*$/;
26
- const PATH_SEPARATOR_RE = /[\\/]/;
27
30
 
28
31
  interface Import {
29
32
  imported: string;
@@ -46,10 +49,6 @@ interface ParsedRoute {
46
49
  source: string;
47
50
  }
48
51
 
49
- function withoutExtension(path: string): string {
50
- return path.replace(EXTENSION_RE, "");
51
- }
52
-
53
52
  function propertyName(key: Node | null): string | undefined {
54
53
  if (key?.type === "Identifier") {
55
54
  return key.name;
@@ -149,11 +148,11 @@ function issueAt(file: string, parsed: ParsedRoute, node: Node, message: string)
149
148
  */
150
149
  function checkKey(
151
150
  file: string,
152
- instance: RouteInstanceSpec,
151
+ route: ManifestRoute,
153
152
  parsed: ParsedRoute
154
153
  ): RouteConfigIssue | undefined {
155
154
  const { config, head, id } = parsed.route;
156
- const expected = fileRouteId(instance, file);
155
+ const expected = route.id;
157
156
  if (!id) {
158
157
  return issueAt(
159
158
  file,
@@ -200,11 +199,11 @@ function suggestedMode(route: RouteExport, isRoot: boolean): string {
200
199
 
201
200
  function checkFactory(
202
201
  file: string,
203
- instance: RouteInstanceSpec,
202
+ manifestRoute: ManifestRoute | undefined,
204
203
  parsed: ParsedRoute
205
204
  ): RouteConfigIssue | undefined {
206
205
  const { route } = parsed;
207
- const isRoot = withoutExtension(file) === join(instance.pagesDir, "root");
206
+ const isRoot = manifestRoute === undefined;
208
207
  if (isRoot === route.isRootFactory) {
209
208
  return;
210
209
  }
@@ -214,14 +213,14 @@ function checkFactory(
214
213
  file,
215
214
  parsed,
216
215
  route.head,
217
- `root.tsx must use defineRootRoute(), which has no parent layout:\n import { defineRootRoute } from "@teyik0/furin";\n export const route = defineRootRoute().config({ mode: "${mode}" })`
216
+ `The root layout must use defineRootRoute(), which has no parent layout:\n import { defineRootRoute } from "@teyik0/furin";\n export const route = defineRootRoute().config({ mode: "${mode}" })`
218
217
  );
219
218
  }
220
219
  return issueAt(
221
220
  file,
222
221
  parsed,
223
222
  route.head,
224
- `defineRootRoute() is reserved for root.tsx. Use defineRoute with this file's id:\n import { defineRoute } from "@teyik0/furin";\n export const route = defineRoute("${fileRouteId(instance, file)}").config({ mode: "${mode}" })`
223
+ `defineRootRoute() is reserved for the root layout. Use defineRoute with this file's id:\n import { defineRoute } from "@teyik0/furin";\n export const route = defineRoute("${manifestRoute.id}").config({ mode: "${mode}" })`
225
224
  );
226
225
  }
227
226
 
@@ -269,24 +268,15 @@ function declaredParams(route: RouteExport): Set<string> | true {
269
268
  return new Set(keys.filter((key) => key !== undefined));
270
269
  }
271
270
 
272
- /** Dynamic segments of the file's path missing from `declared`. */
273
- function undeclaredSegments(
274
- file: string,
275
- pagesDir: string,
276
- declared: Set<string> | true
277
- ): { key: string; segment: string }[] {
278
- return withoutExtension(relative(pagesDir, file))
279
- .split(PATH_SEPARATOR_RE)
280
- .flatMap((segment) => {
281
- const dynamic = parseDynamicRouteSegment(segment, file);
282
- return dynamic ? [{ key: dynamic.catchAll ? "*" : dynamic.name, segment }] : [];
283
- })
284
- .filter(({ key }) => declared !== true && !declared.has(key));
271
+ /** Path params of the route missing from `declared`. */
272
+ function undeclaredParams(route: ManifestRoute, declared: Set<string> | true): RouteParam[] {
273
+ return route.params.filter(({ name }) => declared !== true && !declared.has(name));
285
274
  }
286
275
 
287
- function paramsSchema(segments: { key: string }[]): string {
288
- const fields = segments.map(
289
- ({ key }) => `${IDENTIFIER_RE.test(key) ? key : JSON.stringify(key)}: t.String()`
276
+ function paramsSchema(params: RouteParam[]): string {
277
+ const fields = params.map(
278
+ ({ name, optional }) =>
279
+ `${IDENTIFIER_RE.test(name) ? name : JSON.stringify(name)}: ${optional ? "t.Optional(t.String())" : "t.String()"}`
290
280
  );
291
281
  return `params: t.Object({ ${fields.join(", ")} })`;
292
282
  }
@@ -294,10 +284,10 @@ function paramsSchema(segments: { key: string }[]): string {
294
284
  /** Static twin of `validateRouteParams()`, which rejects the page once its module loads. */
295
285
  function checkParams(
296
286
  file: string,
297
- pagesDir: string,
287
+ route: ManifestRoute,
298
288
  parsed: ParsedRoute
299
289
  ): RouteConfigIssue | undefined {
300
- const missing = undeclaredSegments(file, pagesDir, declaredParams(parsed.route));
290
+ const missing = undeclaredParams(route, declaredParams(parsed.route));
301
291
  if (missing.length === 0) {
302
292
  return;
303
293
  }
@@ -308,43 +298,68 @@ function checkParams(
308
298
  file,
309
299
  parsed,
310
300
  parsed.route.config ?? parsed.route.head,
311
- `The URL segment${plural ? "s" : ""} ${missing.map(({ segment }) => segment).join(", ")} ${plural ? "are" : "is"} missing from this page's params schema. Each page declares every dynamic segment of its path, including those of its layouts. Add to config():${tImport}\n ${paramsSchema(missing)}`
301
+ `The path param${plural ? "s" : ""} ${missing.map(({ name }) => name).join(", ")} ${plural ? "are" : "is"} missing from this page's params schema. Each page declares every dynamic segment of its path, including those of its layouts. Add to config():${tImport}\n ${paramsSchema(missing)}`
302
+ );
303
+ }
304
+
305
+ /** Next.js-style apps declare boundaries as files, TanStack-style ones on the route. */
306
+ function checkBoundaryMethods(
307
+ file: string,
308
+ instance: RouteInstanceSpec,
309
+ parsed: ParsedRoute
310
+ ): RouteConfigIssue | undefined {
311
+ const method = ["error", "notFound"].find((name) => parsed.route.methods.has(name));
312
+ if (instance.style !== "nextjs" || method === undefined) {
313
+ return;
314
+ }
315
+ const convention = method === "error" ? "an error.tsx" : "a not-found.tsx";
316
+ return issueAt(
317
+ file,
318
+ parsed,
319
+ parsed.route.head,
320
+ `.${method}() is the TanStack style. In the nextjs style, default-export the component from ${convention} next to this route.`
312
321
  );
313
322
  }
314
323
 
315
324
  function checkRoute(
316
325
  file: string,
317
326
  instance: RouteInstanceSpec,
327
+ route: ManifestRoute | undefined,
318
328
  parsed: ParsedRoute
319
329
  ): (RouteConfigIssue | undefined)[] {
320
- const factory = checkFactory(file, instance, parsed);
330
+ const factory = checkFactory(file, route, parsed);
321
331
  if (factory) {
322
332
  return [factory];
323
333
  }
324
- const isLayout = basename(withoutExtension(file)) === "_route";
325
334
  return [
326
335
  checkConfig(file, parsed),
327
- parsed.route.isRootFactory ? undefined : checkKey(file, instance, parsed),
336
+ route === undefined ? undefined : checkKey(file, route, parsed),
328
337
  checkMode(file, parsed),
329
- parsed.route.isRootFactory || isLayout
330
- ? undefined
331
- : checkParams(file, instance.pagesDir, parsed),
338
+ route === undefined || route.kind === "layout" ? undefined : checkParams(file, route, parsed),
339
+ checkBoundaryMethods(file, instance, parsed),
332
340
  ];
333
341
  }
334
342
 
335
- function routeFiles(pagesDir: string): string[] {
336
- const root = join(pagesDir, "root.tsx");
337
- return [
338
- ...(existsSync(root) ? [root] : []),
339
- ...routeSourcePaths({ pagesDir, prefix: "" }),
340
- ].toSorted((left, right) => left.localeCompare(right));
343
+ /** The root layout (`undefined` route) and every route file, sorted. */
344
+ function routeFiles(manifest: RouteManifest): [string, ManifestRoute | undefined][] {
345
+ // Issues and scaffolded files report native paths, as the editor opens them.
346
+ const files: [string, ManifestRoute | undefined][] = manifest.routes.map((route) => [
347
+ resolve(route.sourcePath),
348
+ route,
349
+ ]);
350
+ if (existsSync(manifest.rootPath)) {
351
+ files.push([resolve(manifest.rootPath), undefined]);
352
+ }
353
+ return files.toSorted(([left], [right]) => left.localeCompare(right));
341
354
  }
342
355
 
343
- export function validateRouteConfig({ pagesDir, prefix }: RouteInstanceSpec): RouteConfigIssue[] {
344
- const instance = { pagesDir: resolve(pagesDir), prefix };
345
- return routeFiles(instance.pagesDir).flatMap((file) => {
356
+ export function validateRouteConfig(spec: RouteInstanceSpec): RouteConfigIssue[] {
357
+ const instance = { ...spec, pagesDir: resolve(spec.pagesDir) };
358
+ return routeFiles(scanRouteManifest(instance)).flatMap(([file, route]) => {
346
359
  const parsed = parseRoute(file);
347
- return parsed ? checkRoute(file, instance, parsed).filter((issue) => issue !== undefined) : [];
360
+ return parsed
361
+ ? checkRoute(file, instance, route, parsed).filter((issue) => issue !== undefined)
362
+ : [];
348
363
  });
349
364
  }
350
365
 
@@ -420,15 +435,15 @@ export const route = defineRootRoute()
420
435
  ));
421
436
  `;
422
437
 
423
- function scaffoldTemplate(file: string, instance: RouteInstanceSpec): string {
424
- if (withoutExtension(file) === join(instance.pagesDir, "root")) {
438
+ function scaffoldTemplate(route: ManifestRoute | undefined): string {
439
+ if (route === undefined) {
425
440
  return ROOT_TEMPLATE;
426
441
  }
427
- const isLayout = basename(withoutExtension(file)) === "_route";
428
- const params = undeclaredSegments(file, instance.pagesDir, new Set());
442
+ const isLayout = route.kind === "layout";
443
+ const { params } = route;
429
444
  return `import { defineRoute } from "@teyik0/furin";${params.length > 0 ? '\nimport { t } from "elysia";' : ""}
430
445
 
431
- export const route = defineRoute("${fileRouteId(instance, file)}")
446
+ export const route = defineRoute("${route.id}")
432
447
  .config({ mode: "${isLayout ? "ssr" : "ssg"}"${params.length > 0 ? `, ${paramsSchema(params)}` : ""} })
433
448
  .${isLayout ? "layout(({ children }) => children)" : "page(() => null)"};
434
449
  `;
@@ -438,14 +453,14 @@ export const route = defineRoute("${fileRouteId(instance, file)}")
438
453
  * Writes a starter route, keyed by the file's id, into route files that are
439
454
  * empty or whitespace-only, the only case where Furin writes to a source file.
440
455
  */
441
- export function scaffoldEmptyRoutes({ pagesDir, prefix }: RouteInstanceSpec): string[] {
442
- const instance = { pagesDir: resolve(pagesDir), prefix };
456
+ export function scaffoldEmptyRoutes(spec: RouteInstanceSpec): string[] {
457
+ const instance = { ...spec, pagesDir: resolve(spec.pagesDir) };
443
458
  const written: string[] = [];
444
- for (const file of routeFiles(instance.pagesDir).filter(
445
- (path) => readSource(path)?.trim() === ""
446
- )) {
447
- writeFileSync(file, scaffoldTemplate(file, instance));
448
- written.push(file);
459
+ for (const [file, route] of routeFiles(scanRouteManifest(instance))) {
460
+ if (readSource(file)?.trim() === "") {
461
+ writeFileSync(file, scaffoldTemplate(route));
462
+ written.push(file);
463
+ }
449
464
  }
450
465
  return written;
451
466
  }