dinou 9.3.0 → 10.0.1

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 (166) hide show
  1. package/CHANGELOG.md +196 -0
  2. package/README.md +157 -132
  3. package/dinou/adapters/bun.js +34 -5
  4. package/dinou/adapters/cloudflare.js +110 -17
  5. package/dinou/adapters/deno.js +46 -8
  6. package/dinou/adapters/netlify.js +86 -50
  7. package/dinou/adapters/node.js +8 -0
  8. package/dinou/assets.d.ts +32 -5
  9. package/dinou/bun/build.js +190 -106
  10. package/dinou/cli.js +992 -532
  11. package/dinou/cloudflare/build.js +187 -106
  12. package/dinou/compiled/autoprefixer/index.js +509 -517
  13. package/dinou/compiled/babel-core/index.js +738 -716
  14. package/dinou/compiled/babel-generator/index.js +341 -299
  15. package/dinou/compiled/babel-loader/index.js +766 -744
  16. package/dinou/compiled/babel-parser/index.js +2 -2
  17. package/dinou/compiled/babel-plugin-react-compiler/index.js +178 -178
  18. package/dinou/compiled/babel-plugin-syntax-import-meta/index.js +4 -4
  19. package/dinou/compiled/babel-preset-react/index.js +752 -730
  20. package/dinou/compiled/babel-preset-typescript/index.js +738 -716
  21. package/dinou/compiled/babel-traverse/index.js +463 -433
  22. package/dinou/compiled/babel-types/index.js +176 -176
  23. package/dinou/compiled/chokidar/index.js +4 -4
  24. package/dinou/compiled/commander/index.js +15 -15
  25. package/dinou/compiled/copy-webpack-plugin/index.js +433 -425
  26. package/dinou/compiled/css-loader/index.js +289 -289
  27. package/dinou/compiled/css-loader/runtime/api.js +85 -0
  28. package/dinou/compiled/css-loader/runtime/getUrl.js +26 -0
  29. package/dinou/compiled/css-loader/runtime/noSourceMaps.js +5 -0
  30. package/dinou/compiled/css-loader/runtime/package.json +3 -0
  31. package/dinou/compiled/css-loader/runtime/sourceMaps.js +16 -0
  32. package/dinou/compiled/fast-glob/index.js +5810 -0
  33. package/dinou/compiled/generic-names/index.js +16 -16
  34. package/dinou/compiled/mini-css-extract-plugin/{chunk-YYEJWYXE.js → chunk-R6DP5ZYM.js} +36 -36
  35. package/dinou/compiled/mini-css-extract-plugin/hmr/hotModuleReplacement.js +253 -0
  36. package/dinou/compiled/mini-css-extract-plugin/hmr/normalize-url.js +39 -0
  37. package/dinou/compiled/mini-css-extract-plugin/hmr/package.json +3 -0
  38. package/dinou/compiled/mini-css-extract-plugin/index.js +1 -1
  39. package/dinou/compiled/mini-css-extract-plugin/loader.js +5 -5
  40. package/dinou/compiled/postcss/index.js +83 -83
  41. package/dinou/compiled/postcss-import/index.js +147 -117
  42. package/dinou/compiled/postcss-modules/index.js +216 -216
  43. package/dinou/compiled/resolve/index.js +1224 -0
  44. package/dinou/compiled/rollup-plugin-babel/index.js +753 -731
  45. package/dinou/compiled/rollup-plugin-commonjs/index.js +43 -38
  46. package/dinou/compiled/rollup-plugin-delete/index.js +615 -253
  47. package/dinou/compiled/rollup-plugin-json/index.js +22 -18
  48. package/dinou/compiled/rollup-plugin-node-resolve/index.js +130 -100
  49. package/dinou/compiled/rollup-plugin-postcss/index.js +3299 -3277
  50. package/dinou/compiled/rollup-plugin-replace/index.js +34 -29
  51. package/dinou/compiled/tailwindcss-postcss/index.js +214 -211
  52. package/dinou/compiled/vendors-manifest.json +59 -83
  53. package/dinou/core/asset-extensions.js +482 -33
  54. package/dinou/core/bailout-proxy.js +21 -2
  55. package/dinou/core/build-static-pages.js +523 -371
  56. package/dinou/core/builtin-asset-extensions.js +63 -0
  57. package/dinou/core/cache-revalidate.js +449 -256
  58. package/dinou/core/cache-slot.js +289 -79
  59. package/dinou/core/client-webpack.jsx +470 -39
  60. package/dinou/core/client.jsx +402 -39
  61. package/dinou/core/config.js +3 -0
  62. package/dinou/core/copy-static-dirs.js +98 -0
  63. package/dinou/core/createScopedName.js +22 -4
  64. package/dinou/core/dinou-paths.js +1 -1
  65. package/dinou/core/discover-layouts.js +200 -0
  66. package/dinou/core/edge-ssr.js +1 -1
  67. package/dinou/core/ensure-vendor-mode.js +78 -19
  68. package/dinou/core/env.js +544 -0
  69. package/dinou/core/export-static.js +41 -9
  70. package/dinou/core/generate-route-types.js +989 -95
  71. package/dinou/core/generate-static-rsc.js +5 -6
  72. package/dinou/core/generate-static.js +45 -6
  73. package/dinou/core/get-abs-path-with-ext.js +217 -75
  74. package/dinou/core/get-asset-from-manifest.js +19 -8
  75. package/dinou/core/get-error-jsx.js +4 -5
  76. package/dinou/core/get-file-path-and-dynamic-params.js +561 -338
  77. package/dinou/core/get-jsx.js +220 -53
  78. package/dinou/core/handler.js +1647 -466
  79. package/dinou/core/import-module.js +7 -0
  80. package/dinou/core/layout-functions.js +72 -13
  81. package/dinou/core/link.jsx +34 -6
  82. package/dinou/core/load-dinou-config.js +520 -89
  83. package/dinou/core/manifest-provider.js +4 -14
  84. package/dinou/core/navigation-utils.js +141 -3
  85. package/dinou/core/navigation.js +134 -50
  86. package/dinou/core/node-middleware-runner.js +165 -0
  87. package/dinou/core/postcss-pipeline.js +236 -0
  88. package/dinou/core/ppr-context.js +114 -22
  89. package/dinou/core/ppr-runtime.js +391 -127
  90. package/dinou/core/redirect.jsx +1 -1
  91. package/dinou/core/render-jsx-to-client-jsx.js +216 -48
  92. package/dinou/core/request-context.js +59 -3
  93. package/dinou/core/resolve-contextual-dinou.js +523 -0
  94. package/dinou/core/resolve-setup-files.js +85 -27
  95. package/dinou/core/revalidating.js +10 -2
  96. package/dinou/core/route-generator.js +6 -2
  97. package/dinou/core/rsc-renderer.js +4 -1
  98. package/dinou/core/run-ssg.js +6 -0
  99. package/dinou/core/runtime-env.js +265 -0
  100. package/dinou/core/server-css-modules-plugin.js +155 -0
  101. package/dinou/core/server-function-proxy-webpack.js +27 -2
  102. package/dinou/core/server-function-proxy.js +27 -2
  103. package/dinou/core/slot.js +47 -2
  104. package/dinou/core/static-assets.js +503 -0
  105. package/dinou/core/storage-adapter.js +452 -24
  106. package/dinou/core/transform-server-functions.js +272 -24
  107. package/dinou/core/tsconfig-aliases.js +964 -0
  108. package/dinou/core/uneject.js +640 -0
  109. package/dinou/core/url-resolver.js +1 -1
  110. package/dinou/core/vfs.js +11 -2
  111. package/dinou/css.d.ts +4 -1
  112. package/dinou/deno/build.js +205 -153
  113. package/dinou/deno/seed-kv.js +4 -0
  114. package/dinou/env.d.ts +22 -2
  115. package/dinou/esbuild/build.js +40 -50
  116. package/dinou/esbuild/dev.js +33 -7
  117. package/dinou/esbuild/helpers-esbuild/get-config-esbuild-prod.js +41 -16
  118. package/dinou/esbuild/helpers-esbuild/get-config-esbuild.js +51 -49
  119. package/dinou/esbuild/helpers-esbuild/get-esbuild-entries.js +73 -60
  120. package/dinou/esbuild/helpers-esbuild/swc-disk-cache.js +35 -16
  121. package/dinou/esbuild/helpers-esbuild/write.js +13 -3
  122. package/dinou/esbuild/plugins-esbuild/assets-plugin.js +100 -238
  123. package/dinou/esbuild/plugins-esbuild/babel-react-compiler-plugin.js +73 -29
  124. package/dinou/esbuild/plugins-esbuild/css-processor-plugin.js +283 -91
  125. package/dinou/esbuild/plugins-esbuild/manifest-generator-plugin.js +3 -5
  126. package/dinou/esbuild/plugins-esbuild/react-client-manifest-plugin.js +9 -3
  127. package/dinou/esbuild/plugins-esbuild/server-functions-plugin.js +4 -3
  128. package/dinou/esbuild/plugins-esbuild/stable-chunk-names-and-maps-plugin.js +10 -1
  129. package/dinou/esbuild/plugins-postcss/postcss-extract-plugin.js +84 -21
  130. package/dinou/esbuild/react-refresh/esm-hmr/client.js +31 -10
  131. package/dinou/esbuild/react-refresh/esm-hmr-plugin.js +48 -18
  132. package/dinou/esbuild/react-refresh/is-react-refresh-boundary.js +9 -1
  133. package/dinou/esbuild/react-refresh/react-refresh-runtime.js +13 -7
  134. package/dinou/index.d.ts +1028 -88
  135. package/dinou/node/build.js +138 -74
  136. package/dinou/node/bundle-dual-engine.js +393 -131
  137. package/dinou/node/dev.js +1005 -275
  138. package/dinou/package.json +11 -34
  139. package/dinou/rollup/postcss.config.js +3 -4
  140. package/dinou/rollup/react-refresh/esm-hmr/client.js +31 -10
  141. package/dinou/rollup/react-refresh/react-refresh-wrap-modules.js +16 -13
  142. package/dinou/rollup/react-refresh/rollup-plugin-esm-hmr.js +53 -7
  143. package/dinou/rollup/rollup-plugins/dinou-asset-plugin.js +58 -20
  144. package/dinou/rollup/rollup-plugins/manifest-generator-plugin.js +16 -7
  145. package/dinou/rollup/rollup-plugins/rollup-plugin-react-client-manifest.js +118 -42
  146. package/dinou/rollup/rollup-plugins/rollup-plugin-server-functions.js +48 -36
  147. package/dinou/rollup/rollup.config.js +174 -101
  148. package/dinou/scripts/build-vendors.js +123 -33
  149. package/dinou/server.d.ts +126 -15
  150. package/dinou/webpack/helpers/get-webpack-entries.js +87 -41
  151. package/dinou/webpack/helpers/patch-webpack-internals.js +93 -1
  152. package/dinou/webpack/loaders/server-functions-loader.js +1 -1
  153. package/dinou/webpack/plugins/manifest-generator-plugin.js +27 -7
  154. package/dinou/webpack/plugins/server-functions-plugin.js +39 -12
  155. package/dinou/webpack/plugins/webpack-memory-plugin.js +25 -12
  156. package/dinou/webpack/postcss.config.js +3 -4
  157. package/dinou/webpack/webpack.config.js +408 -132
  158. package/eject.js +6 -224
  159. package/package.json +7 -9
  160. package/dinou/compiled/cross-env/index.js +0 -661
  161. package/dinou/compiled/dotenv/index.js +0 -375
  162. package/dinou/compiled/esbuild-copy-static-files/index.js +0 -63
  163. package/dinou/compiled/esbuild-tsconfig-paths/index.js +0 -1314
  164. package/dinou/compiled/rollup-plugin-copy/index.js +0 -12066
  165. package/dinou/compiled/rollup-plugin-tsconfig-paths/index.js +0 -569
  166. package/dinou/compiled/tsconfig-paths-webpack-plugin/index.js +0 -3956
@@ -9,10 +9,71 @@ function safeDecode(val) {
9
9
  }
10
10
  }
11
11
 
12
- function getSlots(currentPath, reqSegments, query) {
12
+ function sortDirEntries(entries) {
13
+ return [...entries].sort((a, b) =>
14
+ a.name < b.name ? -1 : a.name > b.name ? 1 : 0
15
+ );
16
+ }
17
+
18
+ function isGroupDir(name) {
19
+ return name.startsWith("(") && name.endsWith(")");
20
+ }
21
+
22
+ function isOptionalCatchAllDir(name) {
23
+ return name.startsWith("[[...") && name.endsWith("]]");
24
+ }
25
+
26
+ function isCatchAllDir(name) {
27
+ return !isOptionalCatchAllDir(name) && name.startsWith("[...") && name.endsWith("]");
28
+ }
29
+
30
+ function isOptionalSingleDynamicDir(name) {
31
+ return !isOptionalCatchAllDir(name) && name.startsWith("[[") && name.endsWith("]]");
32
+ }
33
+
34
+ function isSingleDynamicDir(name) {
35
+ return (
36
+ !isCatchAllDir(name) &&
37
+ !isOptionalSingleDynamicDir(name) &&
38
+ !isOptionalCatchAllDir(name) &&
39
+ name.startsWith("[") &&
40
+ name.endsWith("]")
41
+ );
42
+ }
43
+
44
+ function findCandidateFile(dirPath, fileName, withExtension, possibleExtensions) {
45
+ if (withExtension) {
46
+ for (const ext of possibleExtensions) {
47
+ const candidatePath = path.join(dirPath, `${fileName}${ext}`);
48
+ if (existsSync(candidatePath)) {
49
+ return candidatePath;
50
+ }
51
+ }
52
+ return undefined;
53
+ }
54
+ const candidatePath = path.join(dirPath, fileName);
55
+ return existsSync(candidatePath) ? candidatePath : undefined;
56
+ }
57
+
58
+ function collectGroupChains(dir, prefixGroups = []) {
59
+ const entries = sortDirEntries(readdirSync(dir, { withFileTypes: true }));
60
+ const dirEntries = entries.filter(
61
+ (e) => e.isDirectory() && !e.name.startsWith("_") && !e.name.startsWith("@")
62
+ );
63
+ const chains = [{ targetDir: dir, groupDirs: prefixGroups, dirEntries }];
64
+ for (const entry of dirEntries) {
65
+ if (isGroupDir(entry.name)) {
66
+ const groupPath = path.join(dir, entry.name);
67
+ chains.push(...collectGroupChains(groupPath, [...prefixGroups, groupPath]));
68
+ }
69
+ }
70
+ return chains;
71
+ }
72
+
73
+ function getSlots(currentPath, reqSegments, query, dParams = {}) {
13
74
  let slots = {};
14
75
 
15
- const entries = readdirSync(currentPath, { withFileTypes: true });
76
+ const entries = sortDirEntries(readdirSync(currentPath, { withFileTypes: true }));
16
77
 
17
78
  for (const entry of entries) {
18
79
  if (!entry.isDirectory() || entry.name.startsWith("_")) continue;
@@ -26,7 +87,8 @@ function getSlots(currentPath, reqSegments, query) {
26
87
  true,
27
88
  true,
28
89
  undefined,
29
- reqSegments.length
90
+ reqSegments.length,
91
+ dParams
30
92
  );
31
93
 
32
94
  if (slotPath) {
@@ -36,10 +98,10 @@ function getSlots(currentPath, reqSegments, query) {
36
98
  slotParams,
37
99
  };
38
100
  }
39
- } else if (entry.name.startsWith("(") && entry.name.endsWith(")")) {
101
+ } else if (isGroupDir(entry.name)) {
40
102
  const groupPath = path.join(currentPath, entry.name);
41
103
 
42
- const nestedSlots = getSlots(groupPath, reqSegments, query);
104
+ const nestedSlots = getSlots(groupPath, reqSegments, query, dParams);
43
105
 
44
106
  slots = { ...slots, ...nestedSlots };
45
107
  }
@@ -48,378 +110,539 @@ function getSlots(currentPath, reqSegments, query) {
48
110
  return slots;
49
111
  }
50
112
 
51
- function getFilePathAndDynamicParams(
113
+ function matchRouteTree(
52
114
  reqSegments,
53
115
  query,
54
116
  currentPath,
55
- fileName = "page",
56
- withExtension = true,
57
- finalDestination = true,
58
- lastFound = undefined,
59
- index = 0,
60
- dParams = {},
61
- accumulative = false,
62
- accumulate = [],
63
- isFound = { value: false },
64
- possibleExtensions = [".tsx", ".ts", ".jsx", ".js", ".mjs"]
117
+ fileName,
118
+ withExtension,
119
+ finalDestination,
120
+ lastFound,
121
+ index,
122
+ dParams,
123
+ accumulative,
124
+ accumulate,
125
+ isFound,
126
+ possibleExtensions,
127
+ lineage,
128
+ maxDepthRef
65
129
  ) {
130
+ if (index > maxDepthRef.value) {
131
+ maxDepthRef.value = index;
132
+ }
133
+ lineage.push({ dir: currentPath, dParams, index });
134
+
66
135
  let foundInCurrentPath;
67
136
  if (index > reqSegments.length - 1 || !finalDestination) {
68
- if (withExtension) {
69
- for (const ext of possibleExtensions) {
70
- const candidatePath = path.join(currentPath, `${fileName}${ext}`);
71
- if (existsSync(candidatePath)) {
72
- if (index > reqSegments.length - 1) {
73
- isFound.value = true;
74
- if (!accumulative) return [candidatePath, dParams];
75
- const slots = getSlots(currentPath, reqSegments, query);
76
- accumulate.push([candidatePath, dParams, slots]);
77
- if (finalDestination) return accumulate;
78
- } else {
79
- if (accumulative) {
80
- const slots = getSlots(currentPath, reqSegments, query);
81
- accumulate.push([candidatePath, dParams, slots]);
82
- } else {
83
- foundInCurrentPath = candidatePath;
84
- }
85
- }
86
- }
87
- }
88
- } else {
89
- const candidatePath = path.join(currentPath, fileName);
90
- if (existsSync(candidatePath)) {
91
- if (index > reqSegments.length - 1) {
92
- isFound.value = true;
93
- if (!accumulative) return [candidatePath, dParams];
94
- const slots = getSlots(currentPath, reqSegments, query);
137
+ const candidatePath = findCandidateFile(
138
+ currentPath,
139
+ fileName,
140
+ withExtension,
141
+ possibleExtensions
142
+ );
143
+ if (candidatePath) {
144
+ if (index > reqSegments.length - 1) {
145
+ isFound.value = true;
146
+ if (!accumulative) return [candidatePath, dParams];
147
+ const slots = getSlots(currentPath, reqSegments, query, dParams);
148
+ accumulate.push([candidatePath, dParams, slots]);
149
+ if (finalDestination) return accumulate;
150
+ } else {
151
+ if (accumulative) {
152
+ const slots = getSlots(currentPath, reqSegments, query, dParams);
95
153
  accumulate.push([candidatePath, dParams, slots]);
96
- if (finalDestination) return accumulate;
97
154
  } else {
98
- if (accumulative) {
99
- const slots = getSlots(currentPath, reqSegments, query);
100
- accumulate.push([candidatePath, dParams, slots]);
101
- } else {
102
- foundInCurrentPath = candidatePath;
103
- }
155
+ foundInCurrentPath = candidatePath;
104
156
  }
105
157
  }
106
158
  }
159
+
107
160
  if (index > reqSegments.length - 1) {
108
- const entries = readdirSync(currentPath, { withFileTypes: true });
161
+ const entries = sortDirEntries(
162
+ readdirSync(currentPath, { withFileTypes: true })
163
+ ).filter(
164
+ (e) =>
165
+ e.isDirectory() && !e.name.startsWith("_") && !e.name.startsWith("@")
166
+ );
167
+
168
+ let fallbackParams;
169
+
170
+ // 1. Zero-segment route groups: (group)
171
+ for (const entry of entries) {
172
+ if (!isGroupDir(entry.name)) continue;
173
+ const groupPath = path.join(currentPath, entry.name);
174
+ const branchAccumulate = [...accumulate];
175
+ const branchLineage = [...lineage];
176
+ const branchIsFound = { value: false };
177
+ const branchMaxDepth = { value: maxDepthRef.value };
178
+ const result = matchRouteTree(
179
+ reqSegments,
180
+ query,
181
+ groupPath,
182
+ fileName,
183
+ withExtension,
184
+ finalDestination,
185
+ foundInCurrentPath ?? lastFound,
186
+ index,
187
+ dParams,
188
+ accumulative,
189
+ branchAccumulate,
190
+ branchIsFound,
191
+ possibleExtensions,
192
+ branchLineage,
193
+ branchMaxDepth
194
+ );
195
+ if (branchIsFound.value) {
196
+ isFound.value = true;
197
+ accumulate.splice(0, accumulate.length, ...branchAccumulate);
198
+ lineage.splice(0, lineage.length, ...branchLineage);
199
+ return accumulative ? accumulate : result;
200
+ }
201
+ }
202
+
203
+ // 2. Optional single dynamic segment: [[param]]
109
204
  for (const entry of entries) {
110
- if (entry.isDirectory() && !entry.name.startsWith("_")) {
111
- if (entry.name.startsWith("[[...") && entry.name.endsWith("]]")) {
112
- const paramName = entry.name.slice(5, -2);
113
- const paramValue =
114
- index < reqSegments.length
115
- ? reqSegments.slice(index).map(safeDecode)
116
- : [];
117
- const newParams = {
118
- ...dParams,
119
- [paramName]: paramValue,
120
- };
121
- const dynamicPath = path.join(currentPath, entry.name);
122
- if (withExtension) {
123
- for (const ext of possibleExtensions) {
124
- const candidatePath = path.join(
125
- dynamicPath,
126
- `${fileName}${ext}`
127
- );
128
- if (existsSync(candidatePath)) {
129
- isFound.value = true;
130
- if (accumulative) {
131
- const slots = getSlots(dynamicPath, reqSegments, query);
132
- accumulate.push([candidatePath, newParams, slots]);
133
- return accumulate;
134
- }
135
- return [candidatePath, newParams];
136
- }
137
- }
138
- } else {
139
- const candidatePath = path.join(dynamicPath, fileName);
140
- if (existsSync(candidatePath)) {
141
- isFound.value = true;
142
- if (accumulative) {
143
- const slots = getSlots(dynamicPath, reqSegments, query);
144
- accumulate.push([candidatePath, newParams, slots]);
145
- return accumulate;
146
- }
147
- return [candidatePath, newParams];
148
- }
149
- }
150
- if (accumulative) return accumulate;
151
- return finalDestination
152
- ? []
153
- : [foundInCurrentPath ?? lastFound, newParams];
154
- } else if (entry.name.startsWith("[[") && entry.name.endsWith("]]")) {
155
- const paramName = entry.name.slice(2, -2);
156
- const paramValue =
157
- index < reqSegments.length
158
- ? safeDecode(reqSegments[index])
159
- : undefined;
160
- const newParams = {
161
- ...dParams,
162
- [paramName]: paramValue,
163
- };
164
- const dynamicPath = path.join(currentPath, entry.name);
165
- if (withExtension) {
166
- for (const ext of possibleExtensions) {
167
- const candidatePath = path.join(
168
- dynamicPath,
169
- `${fileName}${ext}`
170
- );
171
- if (existsSync(candidatePath)) {
172
- isFound.value = true;
173
- if (accumulative) {
174
- const slots = getSlots(dynamicPath, reqSegments, query);
175
- accumulate.push([candidatePath, newParams, slots]);
176
- return accumulate;
177
- }
178
- return [candidatePath, newParams];
179
- }
180
- }
181
- } else {
182
- const candidatePath = path.join(dynamicPath, fileName);
183
- if (existsSync(candidatePath)) {
184
- isFound.value = true;
185
- if (accumulative) {
186
- const slots = getSlots(dynamicPath, reqSegments, query);
187
- accumulate.push([candidatePath, newParams, slots]);
188
- return accumulate;
189
- }
190
- return [candidatePath, newParams];
191
- }
192
- }
193
- const newIsFound = { value: false };
194
- const result = getFilePathAndDynamicParams(
195
- reqSegments,
196
- query,
197
- dynamicPath,
198
- fileName,
199
- withExtension,
200
- finalDestination,
201
- lastFound,
202
- index + 1,
203
- dParams,
204
- accumulative,
205
- accumulate,
206
- newIsFound
207
- );
208
- if (newIsFound.value) {
209
- isFound.value = true;
210
- return result;
211
- }
212
- if (accumulative) return accumulate;
213
- return finalDestination
214
- ? []
215
- : [foundInCurrentPath ?? lastFound, newParams];
216
- } else if (entry.name.startsWith("(") && entry.name.endsWith(")")) {
217
- const groupPath = path.join(currentPath, entry.name);
218
- const newIsFound = { value: false };
219
- const result = getFilePathAndDynamicParams(
220
- reqSegments,
221
- query,
222
- groupPath,
223
- fileName,
224
- withExtension,
225
- finalDestination,
226
- lastFound,
227
- index,
228
- dParams,
229
- accumulative,
230
- accumulate,
231
- newIsFound
232
- );
233
- if (newIsFound.value) {
234
- isFound.value = true;
235
- return result;
236
- }
205
+ if (!isOptionalSingleDynamicDir(entry.name)) continue;
206
+ const paramName = entry.name.slice(2, -2);
207
+ const paramValue =
208
+ index < reqSegments.length
209
+ ? safeDecode(reqSegments[index])
210
+ : undefined;
211
+ const newParams = {
212
+ ...dParams,
213
+ [paramName]: paramValue,
214
+ };
215
+ if (!fallbackParams) fallbackParams = newParams;
216
+ const dynamicPath = path.join(currentPath, entry.name);
217
+ const directCandidate = findCandidateFile(
218
+ dynamicPath,
219
+ fileName,
220
+ withExtension,
221
+ possibleExtensions
222
+ );
223
+ if (directCandidate) {
224
+ isFound.value = true;
225
+ lineage.push({ dir: dynamicPath, dParams: newParams, index });
226
+ if (accumulative) {
227
+ const slots = getSlots(dynamicPath, reqSegments, query, newParams);
228
+ accumulate.push([directCandidate, newParams, slots]);
229
+ return accumulate;
237
230
  }
231
+ return [directCandidate, newParams];
238
232
  }
233
+
234
+ const branchAccumulate = [...accumulate];
235
+ const branchLineage = [...lineage];
236
+ const branchIsFound = { value: false };
237
+ const branchMaxDepth = { value: maxDepthRef.value };
238
+ const result = matchRouteTree(
239
+ reqSegments,
240
+ query,
241
+ dynamicPath,
242
+ fileName,
243
+ withExtension,
244
+ finalDestination,
245
+ foundInCurrentPath ?? lastFound,
246
+ index + 1,
247
+ dParams,
248
+ accumulative,
249
+ branchAccumulate,
250
+ branchIsFound,
251
+ possibleExtensions,
252
+ branchLineage,
253
+ branchMaxDepth
254
+ );
255
+ if (branchIsFound.value) {
256
+ isFound.value = true;
257
+ accumulate.splice(0, accumulate.length, ...branchAccumulate);
258
+ lineage.splice(0, lineage.length, ...branchLineage);
259
+ return accumulative ? accumulate : result;
260
+ }
261
+ }
262
+
263
+ // 3. Optional catch-all segment: [[...param]]
264
+ for (const entry of entries) {
265
+ if (!isOptionalCatchAllDir(entry.name)) continue;
266
+ const paramName = entry.name.slice(5, -2);
267
+ const paramValue =
268
+ index < reqSegments.length
269
+ ? reqSegments.slice(index).map(safeDecode)
270
+ : [];
271
+ const newParams = {
272
+ ...dParams,
273
+ [paramName]: paramValue,
274
+ };
275
+ if (!fallbackParams) fallbackParams = newParams;
276
+ const dynamicPath = path.join(currentPath, entry.name);
277
+ const branchAccumulate = [...accumulate];
278
+ const branchLineage = [...lineage];
279
+ const branchIsFound = { value: false };
280
+ const branchMaxDepth = { value: maxDepthRef.value };
281
+ const result = matchRouteTree(
282
+ reqSegments,
283
+ query,
284
+ dynamicPath,
285
+ fileName,
286
+ withExtension,
287
+ finalDestination,
288
+ foundInCurrentPath ?? lastFound,
289
+ index,
290
+ newParams,
291
+ accumulative,
292
+ branchAccumulate,
293
+ branchIsFound,
294
+ possibleExtensions,
295
+ branchLineage,
296
+ branchMaxDepth
297
+ );
298
+ if (branchIsFound.value) {
299
+ isFound.value = true;
300
+ accumulate.splice(0, accumulate.length, ...branchAccumulate);
301
+ lineage.splice(0, lineage.length, ...branchLineage);
302
+ return accumulative ? accumulate : result;
303
+ }
304
+ }
305
+
306
+ if (!accumulative) {
307
+ return finalDestination
308
+ ? []
309
+ : [foundInCurrentPath ?? lastFound, fallbackParams ?? dParams];
239
310
  }
240
- if (!accumulative) return finalDestination ? [] : [lastFound, dParams];
241
311
  return accumulate;
242
312
  }
243
313
  }
244
- const staticPath = path.join(currentPath, reqSegments[index]);
314
+
315
+ const rawSeg = reqSegments[index];
316
+ const seg =
317
+ rawSeg === undefined || rawSeg === null ? rawSeg : String(rawSeg);
245
318
  const isRouterSyntaxInSegment =
246
- reqSegments[index] &&
247
- ((reqSegments[index].startsWith("(") && reqSegments[index].endsWith(")")) ||
248
- (reqSegments[index].startsWith("[") &&
249
- reqSegments[index].endsWith("]")) ||
250
- reqSegments[index].startsWith("@") ||
251
- reqSegments[index].startsWith("_"));
252
- if (existsSync(staticPath) && !isRouterSyntaxInSegment) {
253
- return getFilePathAndDynamicParams(
319
+ seg &&
320
+ ((seg.startsWith("(") && seg.endsWith(")")) ||
321
+ (seg.startsWith("[") && seg.endsWith("]")) ||
322
+ seg.startsWith("@") ||
323
+ seg.startsWith("_"));
324
+
325
+ const baseLastFound = finalDestination
326
+ ? lastFound
327
+ : (foundInCurrentPath ?? lastFound);
328
+ const chains = collectGroupChains(currentPath);
329
+ let bestFallback = null;
330
+
331
+ function runCandidateBranch(
332
+ nextPath,
333
+ nextIndex,
334
+ nextParams,
335
+ groupDirs,
336
+ capDepthAt = null
337
+ ) {
338
+ const chainAccumulate = [...accumulate];
339
+ let chainLastFound = baseLastFound;
340
+ const chainLineage = [...lineage];
341
+
342
+ for (const gDir of groupDirs) {
343
+ chainLineage.push({ dir: gDir, dParams, index });
344
+ if (!finalDestination) {
345
+ const gCand = findCandidateFile(
346
+ gDir,
347
+ fileName,
348
+ withExtension,
349
+ possibleExtensions
350
+ );
351
+ if (gCand) {
352
+ if (accumulative) {
353
+ const slots = getSlots(gDir, reqSegments, query, dParams);
354
+ chainAccumulate.push([gCand, dParams, slots]);
355
+ } else {
356
+ chainLastFound = gCand;
357
+ }
358
+ }
359
+ }
360
+ }
361
+
362
+ const branchIsFound = { value: false };
363
+ const branchMaxDepth = { value: maxDepthRef.value };
364
+ const result = matchRouteTree(
254
365
  reqSegments,
255
366
  query,
256
- staticPath,
367
+ nextPath,
257
368
  fileName,
258
369
  withExtension,
259
370
  finalDestination,
260
- finalDestination ? lastFound : foundInCurrentPath ?? lastFound,
261
- index + 1,
262
- dParams,
371
+ chainLastFound,
372
+ nextIndex,
373
+ nextParams,
263
374
  accumulative,
264
- accumulate,
265
- isFound
375
+ chainAccumulate,
376
+ branchIsFound,
377
+ possibleExtensions,
378
+ chainLineage,
379
+ branchMaxDepth
266
380
  );
267
- } else {
268
- const entries = readdirSync(currentPath, { withFileTypes: true });
269
- for (const entry of entries) {
270
- if (entry.isDirectory() && !entry.name.startsWith("_")) {
271
- if (entry.name.startsWith("[[...") && entry.name.endsWith("]]")) {
272
- const paramName = entry.name.slice(5, -2);
273
- const paramValue =
274
- index < reqSegments.length
275
- ? reqSegments.slice(index).map(safeDecode)
276
- : [];
277
- const newParams = {
278
- ...dParams,
279
- [paramName]: paramValue,
280
- };
281
- const dynamicPath = path.join(currentPath, entry.name);
282
- if (withExtension) {
283
- for (const ext of possibleExtensions) {
284
- const candidatePath = path.join(dynamicPath, `${fileName}${ext}`);
285
- if (existsSync(candidatePath)) {
286
- isFound.value = true;
287
- if (accumulative) {
288
- const slots = getSlots(dynamicPath, reqSegments, query);
289
- accumulate.push([candidatePath, newParams, slots]);
290
- return accumulate;
291
- }
292
- return [candidatePath, newParams];
293
- }
294
- }
295
- } else {
296
- const candidatePath = path.join(dynamicPath, fileName);
297
- if (existsSync(candidatePath)) {
298
- isFound.value = true;
299
- if (accumulative) {
300
- const slots = getSlots(dynamicPath, reqSegments, query);
301
- accumulate.push([candidatePath, newParams, slots]);
302
- return accumulate;
303
- }
304
- return [candidatePath, newParams];
305
- }
306
- }
307
- if (accumulative) return accumulate;
308
- return finalDestination
309
- ? []
310
- : [foundInCurrentPath ?? lastFound, newParams];
311
- } else if (entry.name.startsWith("[...") && entry.name.endsWith("]")) {
312
- const paramName = entry.name.slice(4, -1);
313
- const paramValue = reqSegments.slice(index).map(safeDecode);
314
- const newParams = {
315
- ...dParams,
316
- [paramName]: paramValue,
317
- };
318
- const dynamicPath = path.join(currentPath, entry.name);
319
- if (withExtension) {
320
- for (const ext of possibleExtensions) {
321
- const candidatePath = path.join(dynamicPath, `${fileName}${ext}`);
322
- if (existsSync(candidatePath)) {
323
- isFound.value = true;
324
- if (accumulative) {
325
- const slots = getSlots(dynamicPath, reqSegments, query);
326
- accumulate.push([candidatePath, newParams, slots]);
327
- return accumulate;
328
- }
329
- return [candidatePath, newParams];
330
- }
331
- }
332
- } else {
333
- const candidatePath = path.join(dynamicPath, fileName);
334
- if (existsSync(candidatePath)) {
335
- isFound.value = true;
336
- if (accumulative) {
337
- const slots = getSlots(dynamicPath, reqSegments, query);
338
- accumulate.push([candidatePath, newParams, slots]);
339
- return accumulate;
340
- }
341
- return [candidatePath, newParams];
342
- }
343
- }
344
- if (accumulative) return accumulate;
345
- return finalDestination
346
- ? []
347
- : [foundInCurrentPath ?? lastFound, newParams];
348
- } else if (entry.name.startsWith("[[") && entry.name.endsWith("]]")) {
349
- const paramName = entry.name.slice(2, -2);
350
- const paramValue =
351
- index < reqSegments.length
352
- ? safeDecode(reqSegments[index])
353
- : undefined;
354
- const newParams = {
355
- ...dParams,
356
- [paramName]: paramValue,
357
- };
358
- const dynamicPath = path.join(currentPath, entry.name);
359
- return getFilePathAndDynamicParams(
360
- reqSegments,
361
- query,
362
- dynamicPath,
363
- fileName,
364
- withExtension,
365
- finalDestination,
366
- finalDestination ? lastFound : foundInCurrentPath ?? lastFound,
367
- index + 1,
368
- newParams,
369
- accumulative,
370
- accumulate,
371
- isFound
372
- );
373
- } else if (entry.name.startsWith("[") && entry.name.endsWith("]")) {
374
- const paramName = entry.name.slice(1, -1);
375
- const paramValue = safeDecode(reqSegments[index]);
376
- const newParams = {
377
- ...dParams,
378
- [paramName]: paramValue,
379
- };
380
- const dynamicPath = path.join(currentPath, entry.name);
381
- return getFilePathAndDynamicParams(
382
- reqSegments,
383
- query,
384
- dynamicPath,
381
+
382
+ if (branchIsFound.value) {
383
+ isFound.value = true;
384
+ accumulate.splice(0, accumulate.length, ...chainAccumulate);
385
+ lineage.splice(0, lineage.length, ...chainLineage);
386
+ maxDepthRef.value = branchMaxDepth.value;
387
+ return { matched: true, value: accumulative ? accumulate : result };
388
+ }
389
+
390
+ if (!finalDestination) {
391
+ const effectiveDepth =
392
+ capDepthAt !== null
393
+ ? Math.min(branchMaxDepth.value, capDepthAt)
394
+ : branchMaxDepth.value;
395
+ if (!bestFallback || effectiveDepth > bestFallback.depth) {
396
+ bestFallback = {
397
+ depth: effectiveDepth,
398
+ result,
399
+ branchAccumulate: chainAccumulate,
400
+ };
401
+ }
402
+ }
403
+
404
+ return { matched: false };
405
+ }
406
+
407
+ // Tier 1: Static directory match (directly or inside route groups)
408
+ if (!isRouterSyntaxInSegment) {
409
+ for (const { targetDir, groupDirs, dirEntries } of chains) {
410
+ const hasStaticSubdir = dirEntries.some(
411
+ (e) =>
412
+ e.name === seg &&
413
+ !isGroupDir(e.name) &&
414
+ !isSingleDynamicDir(e.name) &&
415
+ !isOptionalSingleDynamicDir(e.name) &&
416
+ !isCatchAllDir(e.name) &&
417
+ !isOptionalCatchAllDir(e.name)
418
+ );
419
+ if (!hasStaticSubdir) continue;
420
+ const staticPath = path.join(targetDir, seg);
421
+ const attempt = runCandidateBranch(
422
+ staticPath,
423
+ index + 1,
424
+ dParams,
425
+ groupDirs
426
+ );
427
+ if (attempt.matched) return attempt.value;
428
+ }
429
+ }
430
+
431
+ // Tier 2: Required single dynamic segment [param]
432
+ for (const { targetDir, groupDirs, dirEntries } of chains) {
433
+ for (const entry of dirEntries) {
434
+ if (!isSingleDynamicDir(entry.name)) continue;
435
+ const paramName = entry.name.slice(1, -1);
436
+ const paramValue = safeDecode(seg);
437
+ const newParams = {
438
+ ...dParams,
439
+ [paramName]: paramValue,
440
+ };
441
+ const attempt = runCandidateBranch(
442
+ path.join(targetDir, entry.name),
443
+ index + 1,
444
+ newParams,
445
+ groupDirs
446
+ );
447
+ if (attempt.matched) return attempt.value;
448
+ }
449
+ }
450
+
451
+ // Tier 3: Optional single dynamic segment [[param]]
452
+ for (const { targetDir, groupDirs, dirEntries } of chains) {
453
+ for (const entry of dirEntries) {
454
+ if (!isOptionalSingleDynamicDir(entry.name)) continue;
455
+ const paramName = entry.name.slice(2, -2);
456
+ const paramValue = safeDecode(seg);
457
+ const newParams = {
458
+ ...dParams,
459
+ [paramName]: paramValue,
460
+ };
461
+ const attempt = runCandidateBranch(
462
+ path.join(targetDir, entry.name),
463
+ index + 1,
464
+ newParams,
465
+ groupDirs
466
+ );
467
+ if (attempt.matched) return attempt.value;
468
+ }
469
+ }
470
+
471
+ // Tier 4: Required catch-all segment [...param]
472
+ for (const { targetDir, groupDirs, dirEntries } of chains) {
473
+ for (const entry of dirEntries) {
474
+ if (!isCatchAllDir(entry.name)) continue;
475
+ const paramName = entry.name.slice(4, -1);
476
+ const paramValue = reqSegments.slice(index).map(safeDecode);
477
+ const newParams = {
478
+ ...dParams,
479
+ [paramName]: paramValue,
480
+ };
481
+ const attempt = runCandidateBranch(
482
+ path.join(targetDir, entry.name),
483
+ reqSegments.length,
484
+ newParams,
485
+ groupDirs,
486
+ index + 1
487
+ );
488
+ if (attempt.matched) return attempt.value;
489
+ }
490
+ }
491
+
492
+ // Tier 5: Optional catch-all segment [[...param]]
493
+ for (const { targetDir, groupDirs, dirEntries } of chains) {
494
+ for (const entry of dirEntries) {
495
+ if (!isOptionalCatchAllDir(entry.name)) continue;
496
+ const paramName = entry.name.slice(5, -2);
497
+ const paramValue = reqSegments.slice(index).map(safeDecode);
498
+ const newParams = {
499
+ ...dParams,
500
+ [paramName]: paramValue,
501
+ };
502
+ const attempt = runCandidateBranch(
503
+ path.join(targetDir, entry.name),
504
+ reqSegments.length,
505
+ newParams,
506
+ groupDirs,
507
+ index + 1
508
+ );
509
+ if (attempt.matched) return attempt.value;
510
+ }
511
+ }
512
+
513
+ if (!finalDestination && bestFallback) {
514
+ maxDepthRef.value = bestFallback.depth;
515
+ if (accumulative) {
516
+ accumulate.splice(
517
+ 0,
518
+ accumulate.length,
519
+ ...bestFallback.branchAccumulate
520
+ );
521
+ return accumulate;
522
+ }
523
+ return bestFallback.result;
524
+ }
525
+
526
+ if (!accumulative) {
527
+ return finalDestination ? [] : [baseLastFound, dParams];
528
+ }
529
+ return accumulate;
530
+ }
531
+
532
+ function getFilePathAndDynamicParams(
533
+ reqSegments,
534
+ query,
535
+ currentPath,
536
+ fileName = "page",
537
+ withExtension = true,
538
+ finalDestination = true,
539
+ lastFound = undefined,
540
+ index = 0,
541
+ dParams = {},
542
+ accumulative = false,
543
+ accumulate = [],
544
+ isFound = { value: false },
545
+ possibleExtensions = [".tsx", ".ts", ".jsx", ".js", ".mjs"]
546
+ ) {
547
+ const normCurrentPath = String(currentPath).replace(/\\/g, "/");
548
+ const isCompanionLookup =
549
+ index === 0 &&
550
+ !normCurrentPath.includes("/@") &&
551
+ (fileName === "layout" ||
552
+ fileName === "no_layout" ||
553
+ fileName === "error" ||
554
+ fileName === "not_found");
555
+
556
+ if (isCompanionLookup) {
557
+ const pageLineage = [];
558
+ const pageIsFound = { value: false };
559
+ matchRouteTree(
560
+ reqSegments,
561
+ query,
562
+ currentPath,
563
+ "page",
564
+ true,
565
+ true,
566
+ undefined,
567
+ 0,
568
+ dParams,
569
+ false,
570
+ [],
571
+ pageIsFound,
572
+ [".tsx", ".ts", ".jsx", ".js", ".mjs"],
573
+ pageLineage,
574
+ { value: 0 }
575
+ );
576
+
577
+ if (pageIsFound.value && pageLineage.length > 0) {
578
+ if (accumulative) {
579
+ for (const step of pageLineage) {
580
+ const candidatePath = findCandidateFile(
581
+ step.dir,
385
582
  fileName,
386
583
  withExtension,
387
- finalDestination,
388
- finalDestination ? lastFound : foundInCurrentPath ?? lastFound,
389
- index + 1,
390
- newParams,
391
- accumulative,
392
- accumulate,
393
- isFound
584
+ possibleExtensions
394
585
  );
395
- } else if (entry.name.startsWith("(") && entry.name.endsWith(")")) {
396
- const groupPath = path.join(currentPath, entry.name);
397
- const newIsFound = { value: false };
398
- const result = getFilePathAndDynamicParams(
399
- reqSegments,
400
- query,
401
- groupPath,
586
+ if (candidatePath) {
587
+ isFound.value = true;
588
+ const slots = getSlots(step.dir, reqSegments, query, step.dParams);
589
+ accumulate.push([candidatePath, step.dParams, slots]);
590
+ }
591
+ }
592
+ return accumulate;
593
+ }
594
+
595
+ if (finalDestination) {
596
+ for (let i = pageLineage.length - 1; i >= 0; i--) {
597
+ if (pageLineage[i].index < reqSegments.length) break;
598
+ const candidatePath = findCandidateFile(
599
+ pageLineage[i].dir,
402
600
  fileName,
403
601
  withExtension,
404
- finalDestination,
405
- lastFound,
406
- index,
407
- dParams,
408
- accumulative,
409
- accumulate,
410
- newIsFound
602
+ possibleExtensions
411
603
  );
412
- if (newIsFound.value) {
604
+ if (candidatePath) {
413
605
  isFound.value = true;
414
- return result;
606
+ return [candidatePath, pageLineage[i].dParams];
415
607
  }
416
608
  }
609
+ return [];
417
610
  }
611
+
612
+ for (let i = pageLineage.length - 1; i >= 0; i--) {
613
+ const candidatePath = findCandidateFile(
614
+ pageLineage[i].dir,
615
+ fileName,
616
+ withExtension,
617
+ possibleExtensions
618
+ );
619
+ if (candidatePath) {
620
+ isFound.value = true;
621
+ return [candidatePath, pageLineage[i].dParams];
622
+ }
623
+ }
624
+ const leafParams = pageLineage[pageLineage.length - 1].dParams;
625
+ return [lastFound, leafParams];
418
626
  }
419
- if (!accumulative)
420
- return finalDestination ? [] : [foundInCurrentPath ?? lastFound, dParams];
421
- return accumulate;
422
627
  }
628
+
629
+ return matchRouteTree(
630
+ reqSegments,
631
+ query,
632
+ currentPath,
633
+ fileName,
634
+ withExtension,
635
+ finalDestination,
636
+ lastFound,
637
+ index,
638
+ dParams,
639
+ accumulative,
640
+ accumulate,
641
+ isFound,
642
+ possibleExtensions,
643
+ [],
644
+ { value: index }
645
+ );
423
646
  }
424
647
 
425
648
  export {