@teyik0/furin 0.7.0-alpha.1 → 0.7.0-alpha.2

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.
@@ -20,22 +20,25 @@ export function buildElement(
20
20
  key: pageKey(route.pattern, data, route.page._route.remountDeps),
21
21
  });
22
22
 
23
- // Index segmentBoundaries by depth for O(1) lookup during the wrap loop.
24
- // Directory depth `d` maps 1:1 to routeChain[d] in Furin's model (routeChain
25
- // is ordered shallow→deep, with index 0 being the root).
26
- const byDepth = new Map<number, SegmentBoundary>();
23
+ // Directories without layouts share their enclosing layout's chain index.
24
+ // Keep their boundaries in inside-out order instead of overwriting them.
25
+ const byDepth = new Map<number, SegmentBoundary[]>();
27
26
  const legacyRoute = route as ResolvedRoute & {
28
27
  segmentBoundaries?: SegmentBoundary[];
29
28
  };
30
29
  for (const segment of legacyRoute.segmentBoundaries ?? []) {
31
- byDepth.set(segment.depth, segment);
30
+ const boundaries = byDepth.get(segment.depth) ?? [];
31
+ boundaries.unshift(segment);
32
+ byDepth.set(segment.depth, boundaries);
32
33
  }
33
34
 
34
35
  // Build inside-out. At each level we first wrap the accumulated subtree
35
36
  // with the boundary declared at this depth (so the boundary sits INSIDE
36
37
  // the layout at the same depth), THEN wrap with the layout itself.
37
38
  for (let i = route.routeChain.length - 1; i >= 1; i -= 1) {
38
- element = wrapSegmentBoundaries(element, byDepth.get(i), undefined);
39
+ for (const segment of byDepth.get(i) ?? []) {
40
+ element = wrapSegmentBoundaries(element, segment, undefined);
41
+ }
39
42
  const routeEntry = route.routeChain[i];
40
43
  if (routeEntry?.layout) {
41
44
  const Layout = routeEntry.layout;
@@ -43,9 +46,11 @@ export function buildElement(
43
46
  }
44
47
  }
45
48
 
46
- // Depth 0 = pagesDir itself = the root layout directory. Boundary wraps
47
- // everything below the root layout; root layout wraps the boundary.
48
- element = wrapSegmentBoundaries(element, byDepth.get(0), undefined);
49
+ // Index-0 boundaries wrap everything below the root layout, including
50
+ // pathless directories without their own layouts.
51
+ for (const segment of byDepth.get(0) ?? []) {
52
+ element = wrapSegmentBoundaries(element, segment, undefined);
53
+ }
49
54
 
50
55
  if (rootLayout.layout) {
51
56
  const RootLayoutComponent = rootLayout.layout;
@@ -13,7 +13,7 @@ import {
13
13
  import { type CompileContext, getCompileContext } from "../internal.ts";
14
14
  import { IS_DEV } from "../runtime-env.ts";
15
15
  import { adaptDefinedLayout, adaptDefinedPage, isDefinedRouteTerminal } from "./defined-route.ts";
16
- import { filePathToPattern, resolveMode } from "./patterns.ts";
16
+ import { filePathToPattern, resolveMode, routePatternKey } from "./patterns.ts";
17
17
  import { routeModuleSourceVersion } from "./source-version.ts";
18
18
  import type { ResolvedRoute, RootLayout, SegmentBoundary } from "./types.ts";
19
19
 
@@ -248,12 +248,6 @@ export async function scanRootLayout(pagesDir: string): Promise<RootLayout> {
248
248
 
249
249
  const CONVENTION_FILE_NAMES = ["not-found", "error"] as const;
250
250
  const SOURCE_MODULE_EXTENSIONS = [".tsx", ".ts", ".jsx", ".js"] as const;
251
- const DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
252
-
253
- function routePatternKey(pattern: string): string {
254
- return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
255
- }
256
-
257
251
  function isConventionFileName(name: string): boolean {
258
252
  return (CONVENTION_FILE_NAMES as readonly string[]).includes(name);
259
253
  }
@@ -346,7 +340,10 @@ async function scanPageFiles(pagesDir: string, root: RootLayout): Promise<Resolv
346
340
  const devRoute = await buildDevRoute(absolutePath, relativePath, pattern, root, pagesDir);
347
341
  devRoute.notFound = notFound;
348
342
  devRoute.error = errorComponent;
349
- devRoute.segmentBoundaries = segmentBoundaries;
343
+ devRoute.segmentBoundaries = alignSegmentBoundaryDepths(
344
+ segmentBoundaries,
345
+ devRoute.routeChain
346
+ );
350
347
  routes.push(devRoute);
351
348
  continue;
352
349
  }
@@ -372,7 +369,7 @@ async function scanPageFiles(pagesDir: string, root: RootLayout): Promise<Resolv
372
369
  path: absolutePath,
373
370
  pattern,
374
371
  routeChain,
375
- segmentBoundaries,
372
+ segmentBoundaries: alignSegmentBoundaryDepths(segmentBoundaries, routeChain),
376
373
  tags: collectRouteTags(routeChain, page),
377
374
  });
378
375
  }
@@ -427,6 +424,25 @@ async function resolveDefinedLayoutParent(
427
424
  return parent;
428
425
  }
429
426
 
427
+ function alignSegmentBoundaryDepths(
428
+ boundaries: SegmentBoundary[],
429
+ routeChain: RuntimeRoute[]
430
+ ): SegmentBoundary[] {
431
+ return boundaries.map((boundary) => {
432
+ let depth = 0;
433
+ for (const [index, entry] of routeChain.entries()) {
434
+ if (!entry.sourcePath) {
435
+ continue;
436
+ }
437
+ const directory = entry.sourcePath.slice(0, entry.sourcePath.lastIndexOf("/"));
438
+ if (boundary.path === directory || boundary.path.startsWith(`${directory}/`)) {
439
+ depth = index;
440
+ }
441
+ }
442
+ return { ...boundary, depth };
443
+ });
444
+ }
445
+
430
446
  /**
431
447
  * Walks every directory from `pagesDir` down to the directory containing
432
448
  * `pageAbsolutePath` and records the OWN (not inherited) error.tsx /
@@ -108,6 +108,18 @@ export function routeSegmentToPattern(segment: string): string {
108
108
  return dynamic.catchAll ? "*" : `:${dynamic.name}`;
109
109
  }
110
110
 
111
+ const ROUTE_GROUP_RE = /^\([^()]+\)$/;
112
+
113
+ export function isRouteGroup(segment: string): boolean {
114
+ return ROUTE_GROUP_RE.test(segment);
115
+ }
116
+
117
+ const DYNAMIC_ROUTE_SEGMENT_RE = /(^|\/):[^/]+/g;
118
+
119
+ export function routePatternKey(pattern: string): string {
120
+ return pattern.replace(DYNAMIC_ROUTE_SEGMENT_RE, "$1:param");
121
+ }
122
+
111
123
  export function filePathToPattern(path: string): string {
112
124
  const parts = path.replaceAll("\\", "/").split("/");
113
125
  const segments: string[] = [];
@@ -125,6 +137,10 @@ export function filePathToPattern(path: string): string {
125
137
  const isFile = idx === lastIndex;
126
138
  const name = isFile ? parse(part).name : part;
127
139
 
140
+ if (!isFile && isRouteGroup(name)) {
141
+ continue;
142
+ }
143
+
128
144
  // `index` collapses to its parent ONLY as a leaf filename. A directory
129
145
  // literally named `index` is a real route segment and must be preserved.
130
146
  if (isFile && name === "index") {
@@ -12,7 +12,7 @@ import type { NotFoundComponent } from "../../shared/not-found.ts";
12
12
  * exact nesting level where the user authored them (Next.js app-router model).
13
13
  */
14
14
  export interface SegmentBoundary {
15
- /** 0 = `pagesDir`; increments with each nested subdirectory. */
15
+ /** Index of the enclosing layout in routeChain; 0 = the root layout. */
16
16
  depth: number;
17
17
  error?: ErrorComponent;
18
18
  /**