@bleedingdev/modern-js-plugin-tanstack 3.2.0-ultramodern.0

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/cli/index.js +268 -0
  3. package/dist/cjs/cli/tanstackTypes.js +388 -0
  4. package/dist/cjs/cli.js +65 -0
  5. package/dist/cjs/runtime/DefaultNotFound.js +47 -0
  6. package/dist/cjs/runtime/basepathRewrite.js +62 -0
  7. package/dist/cjs/runtime/dataMutation.js +345 -0
  8. package/dist/cjs/runtime/hooks.js +57 -0
  9. package/dist/cjs/runtime/index.js +114 -0
  10. package/dist/cjs/runtime/lifecycle.js +125 -0
  11. package/dist/cjs/runtime/plugin.js +250 -0
  12. package/dist/cjs/runtime/plugin.node.js +304 -0
  13. package/dist/cjs/runtime/prefetchLink.js +55 -0
  14. package/dist/cjs/runtime/routeTree.js +492 -0
  15. package/dist/cjs/runtime/rsc/ClientSlot.js +53 -0
  16. package/dist/cjs/runtime/rsc/CompositeComponent.js +75 -0
  17. package/dist/cjs/runtime/rsc/ReplayableStream.js +141 -0
  18. package/dist/cjs/runtime/rsc/RscNodeRenderer.js +65 -0
  19. package/dist/cjs/runtime/rsc/SlotContext.js +54 -0
  20. package/dist/cjs/runtime/rsc/client.js +93 -0
  21. package/dist/cjs/runtime/rsc/createRscProxy.js +141 -0
  22. package/dist/cjs/runtime/rsc/index.js +42 -0
  23. package/dist/cjs/runtime/rsc/payloadRouter.js +211 -0
  24. package/dist/cjs/runtime/rsc/server.js +246 -0
  25. package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +65 -0
  26. package/dist/cjs/runtime/rsc/symbols.js +72 -0
  27. package/dist/cjs/runtime/types.js +18 -0
  28. package/dist/cjs/runtime/utils.js +142 -0
  29. package/dist/cjs/runtime.js +58 -0
  30. package/dist/esm/cli/index.mjs +201 -0
  31. package/dist/esm/cli/tanstackTypes.mjs +341 -0
  32. package/dist/esm/cli.mjs +2 -0
  33. package/dist/esm/rslib-runtime.mjs +18 -0
  34. package/dist/esm/runtime/DefaultNotFound.mjs +13 -0
  35. package/dist/esm/runtime/basepathRewrite.mjs +28 -0
  36. package/dist/esm/runtime/dataMutation.mjs +305 -0
  37. package/dist/esm/runtime/hooks.mjs +8 -0
  38. package/dist/esm/runtime/index.mjs +6 -0
  39. package/dist/esm/runtime/lifecycle.mjs +82 -0
  40. package/dist/esm/runtime/plugin.mjs +214 -0
  41. package/dist/esm/runtime/plugin.node.mjs +268 -0
  42. package/dist/esm/runtime/prefetchLink.mjs +18 -0
  43. package/dist/esm/runtime/routeTree.mjs +452 -0
  44. package/dist/esm/runtime/rsc/ClientSlot.mjs +19 -0
  45. package/dist/esm/runtime/rsc/CompositeComponent.mjs +41 -0
  46. package/dist/esm/runtime/rsc/ReplayableStream.mjs +104 -0
  47. package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +31 -0
  48. package/dist/esm/runtime/rsc/SlotContext.mjs +17 -0
  49. package/dist/esm/runtime/rsc/client.mjs +53 -0
  50. package/dist/esm/runtime/rsc/createRscProxy.mjs +107 -0
  51. package/dist/esm/runtime/rsc/index.mjs +1 -0
  52. package/dist/esm/runtime/rsc/payloadRouter.mjs +162 -0
  53. package/dist/esm/runtime/rsc/server.mjs +200 -0
  54. package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +31 -0
  55. package/dist/esm/runtime/rsc/symbols.mjs +17 -0
  56. package/dist/esm/runtime/types.mjs +0 -0
  57. package/dist/esm/runtime/utils.mjs +89 -0
  58. package/dist/esm/runtime.mjs +1 -0
  59. package/dist/esm-node/cli/index.mjs +205 -0
  60. package/dist/esm-node/cli/tanstackTypes.mjs +342 -0
  61. package/dist/esm-node/cli.mjs +3 -0
  62. package/dist/esm-node/rslib-runtime.mjs +19 -0
  63. package/dist/esm-node/runtime/DefaultNotFound.mjs +14 -0
  64. package/dist/esm-node/runtime/basepathRewrite.mjs +29 -0
  65. package/dist/esm-node/runtime/dataMutation.mjs +306 -0
  66. package/dist/esm-node/runtime/hooks.mjs +9 -0
  67. package/dist/esm-node/runtime/index.mjs +7 -0
  68. package/dist/esm-node/runtime/lifecycle.mjs +83 -0
  69. package/dist/esm-node/runtime/plugin.mjs +215 -0
  70. package/dist/esm-node/runtime/plugin.node.mjs +269 -0
  71. package/dist/esm-node/runtime/prefetchLink.mjs +19 -0
  72. package/dist/esm-node/runtime/routeTree.mjs +453 -0
  73. package/dist/esm-node/runtime/rsc/ClientSlot.mjs +20 -0
  74. package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +42 -0
  75. package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +105 -0
  76. package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +32 -0
  77. package/dist/esm-node/runtime/rsc/SlotContext.mjs +18 -0
  78. package/dist/esm-node/runtime/rsc/client.mjs +54 -0
  79. package/dist/esm-node/runtime/rsc/createRscProxy.mjs +108 -0
  80. package/dist/esm-node/runtime/rsc/index.mjs +2 -0
  81. package/dist/esm-node/runtime/rsc/payloadRouter.mjs +163 -0
  82. package/dist/esm-node/runtime/rsc/server.mjs +201 -0
  83. package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +32 -0
  84. package/dist/esm-node/runtime/rsc/symbols.mjs +18 -0
  85. package/dist/esm-node/runtime/types.mjs +1 -0
  86. package/dist/esm-node/runtime/utils.mjs +90 -0
  87. package/dist/esm-node/runtime.mjs +2 -0
  88. package/dist/types/cli/index.d.ts +20 -0
  89. package/dist/types/cli/tanstackTypes.d.ts +11 -0
  90. package/dist/types/cli.d.ts +2 -0
  91. package/dist/types/runtime/DefaultNotFound.d.ts +2 -0
  92. package/dist/types/runtime/basepathRewrite.d.ts +8 -0
  93. package/dist/types/runtime/dataMutation.d.ts +29 -0
  94. package/dist/types/runtime/hooks.d.ts +18 -0
  95. package/dist/types/runtime/index.d.ts +9 -0
  96. package/dist/types/runtime/lifecycle.d.ts +22 -0
  97. package/dist/types/runtime/plugin.d.ts +17 -0
  98. package/dist/types/runtime/plugin.node.d.ts +17 -0
  99. package/dist/types/runtime/prefetchLink.d.ts +11 -0
  100. package/dist/types/runtime/routeTree.d.ts +11 -0
  101. package/dist/types/runtime/rsc/ClientSlot.d.ts +5 -0
  102. package/dist/types/runtime/rsc/CompositeComponent.d.ts +3 -0
  103. package/dist/types/runtime/rsc/ReplayableStream.d.ts +24 -0
  104. package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +5 -0
  105. package/dist/types/runtime/rsc/SlotContext.d.ts +11 -0
  106. package/dist/types/runtime/rsc/client.d.ts +11 -0
  107. package/dist/types/runtime/rsc/createRscProxy.d.ts +7 -0
  108. package/dist/types/runtime/rsc/index.d.ts +2 -0
  109. package/dist/types/runtime/rsc/payloadRouter.d.ts +24 -0
  110. package/dist/types/runtime/rsc/server.d.ts +14 -0
  111. package/dist/types/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
  112. package/dist/types/runtime/rsc/symbols.d.ts +46 -0
  113. package/dist/types/runtime/types.d.ts +68 -0
  114. package/dist/types/runtime/utils.d.ts +36 -0
  115. package/dist/types/runtime.d.ts +1 -0
  116. package/dist/types-direct/cli/index.d.ts +20 -0
  117. package/dist/types-direct/cli/tanstackTypes.d.ts +11 -0
  118. package/dist/types-direct/cli.d.ts +2 -0
  119. package/dist/types-direct/runtime/DefaultNotFound.d.ts +2 -0
  120. package/dist/types-direct/runtime/basepathRewrite.d.ts +8 -0
  121. package/dist/types-direct/runtime/dataMutation.d.ts +29 -0
  122. package/dist/types-direct/runtime/hooks.d.ts +18 -0
  123. package/dist/types-direct/runtime/index.d.ts +9 -0
  124. package/dist/types-direct/runtime/lifecycle.d.ts +22 -0
  125. package/dist/types-direct/runtime/plugin.d.ts +17 -0
  126. package/dist/types-direct/runtime/plugin.node.d.ts +17 -0
  127. package/dist/types-direct/runtime/prefetchLink.d.ts +11 -0
  128. package/dist/types-direct/runtime/routeTree.d.ts +11 -0
  129. package/dist/types-direct/runtime/rsc/ClientSlot.d.ts +5 -0
  130. package/dist/types-direct/runtime/rsc/CompositeComponent.d.ts +3 -0
  131. package/dist/types-direct/runtime/rsc/ReplayableStream.d.ts +24 -0
  132. package/dist/types-direct/runtime/rsc/RscNodeRenderer.d.ts +5 -0
  133. package/dist/types-direct/runtime/rsc/SlotContext.d.ts +11 -0
  134. package/dist/types-direct/runtime/rsc/client.d.ts +11 -0
  135. package/dist/types-direct/runtime/rsc/createRscProxy.d.ts +7 -0
  136. package/dist/types-direct/runtime/rsc/index.d.ts +2 -0
  137. package/dist/types-direct/runtime/rsc/payloadRouter.d.ts +24 -0
  138. package/dist/types-direct/runtime/rsc/server.d.ts +14 -0
  139. package/dist/types-direct/runtime/rsc/slotUsageSanitizer.d.ts +2 -0
  140. package/dist/types-direct/runtime/rsc/symbols.d.ts +46 -0
  141. package/dist/types-direct/runtime/types.d.ts +68 -0
  142. package/dist/types-direct/runtime/utils.d.ts +36 -0
  143. package/dist/types-direct/runtime.d.ts +1 -0
  144. package/package.json +126 -0
  145. package/rslib.config.mts +4 -0
  146. package/rstest.config.mts +43 -0
  147. package/src/cli/index.ts +388 -0
  148. package/src/cli/tanstackTypes.ts +503 -0
  149. package/src/cli.ts +2 -0
  150. package/src/runtime/DefaultNotFound.tsx +15 -0
  151. package/src/runtime/basepathRewrite.ts +59 -0
  152. package/src/runtime/dataMutation.tsx +517 -0
  153. package/src/runtime/hooks.ts +34 -0
  154. package/src/runtime/index.tsx +30 -0
  155. package/src/runtime/lifecycle.ts +150 -0
  156. package/src/runtime/plugin.node.tsx +534 -0
  157. package/src/runtime/plugin.tsx +395 -0
  158. package/src/runtime/prefetchLink.tsx +87 -0
  159. package/src/runtime/routeTree.ts +942 -0
  160. package/src/runtime/rsc/ClientSlot.tsx +25 -0
  161. package/src/runtime/rsc/CompositeComponent.tsx +65 -0
  162. package/src/runtime/rsc/ReplayableStream.ts +155 -0
  163. package/src/runtime/rsc/RscNodeRenderer.tsx +45 -0
  164. package/src/runtime/rsc/SlotContext.tsx +31 -0
  165. package/src/runtime/rsc/client.tsx +90 -0
  166. package/src/runtime/rsc/createRscProxy.tsx +189 -0
  167. package/src/runtime/rsc/index.ts +10 -0
  168. package/src/runtime/rsc/payloadRouter.ts +318 -0
  169. package/src/runtime/rsc/server.tsx +303 -0
  170. package/src/runtime/rsc/slotUsageSanitizer.ts +76 -0
  171. package/src/runtime/rsc/symbols.ts +106 -0
  172. package/src/runtime/ssr-shim.d.ts +12 -0
  173. package/src/runtime/types.ts +83 -0
  174. package/src/runtime/utils.tsx +161 -0
  175. package/src/runtime.ts +1 -0
  176. package/tests/router/cli.test.ts +386 -0
  177. package/tests/router/dataMutation.test.tsx +396 -0
  178. package/tests/router/prefetchLink.test.tsx +43 -0
  179. package/tests/router/routeTree.test.ts +502 -0
  180. package/tests/router/rsc.test.tsx +256 -0
  181. package/tests/router/tanstackTypes.test.ts +62 -0
  182. package/tsconfig.json +12 -0
  183. package/tsconfig.tsgo.json +6 -0
@@ -0,0 +1,205 @@
1
+ import "node:module";
2
+ import node_path from "node:path";
3
+ import { NESTED_ROUTE_SPEC_FILE, filterRoutesForServer, fs } from "@modern-js/utils";
4
+ import { generateTanstackRouterTypesSourceForEntry, isTanstackRouterFrameworkEnabled } from "./tanstackTypes.mjs";
5
+ import { __webpack_require__ } from "../rslib-runtime.mjs";
6
+ import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
7
+ import { dirname as __rspack_dirname } from "node:path";
8
+ import * as __rspack_external__modern_js_runtime_cli_401ee077 from "@modern-js/runtime/cli";
9
+ __webpack_require__.add({
10
+ "@modern-js/runtime/cli" (module) {
11
+ module.exports = __rspack_external__modern_js_runtime_cli_401ee077;
12
+ }
13
+ });
14
+ var cli_dirname = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
15
+ const DEFAULT_ROUTES_DIR = 'routes';
16
+ const DEFAULT_GENERATED_DIR_NAME = 'modern-tanstack';
17
+ const ENTRYPOINTS_KEY = '@modern-js/plugin-tanstack';
18
+ let runtimeRouterCli;
19
+ function getRuntimeRouterCli() {
20
+ if (runtimeRouterCli) return runtimeRouterCli;
21
+ const cli = __webpack_require__("@modern-js/runtime/cli");
22
+ if (cli.handleGeneratorEntryCode && cli.getEntrypointRoutesDir) {
23
+ runtimeRouterCli = cli;
24
+ return runtimeRouterCli;
25
+ }
26
+ throw new Error('@modern-js/plugin-tanstack requires @modern-js/runtime/cli router helper exports.');
27
+ }
28
+ async function writeFileIfChanged(filePath, content) {
29
+ try {
30
+ const previous = await fs.pathExists(filePath) ? await fs.readFile(filePath, 'utf-8') : null;
31
+ if (previous === content) return;
32
+ } catch {}
33
+ await fs.outputFile(filePath, content, 'utf-8');
34
+ }
35
+ function createRegisterDtsContent(opts) {
36
+ const importStatements = opts.entries.map((entryName, index)=>`import type { router as router${index} } from './${entryName}/router.gen';`).join('\n');
37
+ const routerUnionType = opts.entries.map((_, index)=>`typeof router${index}`).join(' | ');
38
+ return `// This file is auto-generated by Modern.js. Do not edit manually.
39
+
40
+ ${importStatements}
41
+
42
+ declare module '${opts.runtimeModule}' {
43
+ interface Register {
44
+ router: ${routerUnionType};
45
+ }
46
+ }
47
+ `;
48
+ }
49
+ async function writeTanstackRegisterFile(opts) {
50
+ const { entries, generatedDirName = DEFAULT_GENERATED_DIR_NAME, runtimeModule = '@modern-js/plugin-tanstack/runtime', srcDirectory } = opts;
51
+ if (0 === entries.length) return;
52
+ const registerDtsPath = node_path.join(srcDirectory, generatedDirName, 'register.gen.d.ts');
53
+ await writeFileIfChanged(registerDtsPath, createRegisterDtsContent({
54
+ entries,
55
+ runtimeModule
56
+ }));
57
+ }
58
+ async function writeTanstackRouterTypesForEntries(opts) {
59
+ const { appContext, generatedDirName = DEFAULT_GENERATED_DIR_NAME, routesByEntry } = opts;
60
+ const entryNames = Object.keys(routesByEntry);
61
+ await Promise.all(entryNames.map(async (entryName)=>{
62
+ const { routerGenTs } = await generateTanstackRouterTypesSourceForEntry({
63
+ appContext,
64
+ entryName,
65
+ generatedDirName,
66
+ routes: routesByEntry[entryName]
67
+ });
68
+ await writeFileIfChanged(node_path.join(appContext.srcDirectory, generatedDirName, entryName, 'router.gen.ts'), routerGenTs);
69
+ }));
70
+ const mainEntryName = appContext.entrypoints?.find((entrypoint)=>entrypoint.isMainEntry)?.entryName;
71
+ const registerEntries = entryNames.sort((a, b)=>{
72
+ if (mainEntryName && a === mainEntryName) return -1;
73
+ if (mainEntryName && b === mainEntryName) return 1;
74
+ return a.localeCompare(b);
75
+ });
76
+ await writeTanstackRegisterFile({
77
+ entries: registerEntries,
78
+ generatedDirName,
79
+ srcDirectory: appContext.srcDirectory
80
+ });
81
+ }
82
+ function tanstackRouterPlugin(options = {}) {
83
+ const routesDir = options.routesDir || DEFAULT_ROUTES_DIR;
84
+ const generatedDirName = options.generatedDirName || DEFAULT_GENERATED_DIR_NAME;
85
+ return {
86
+ name: '@modern-js/plugin-tanstack',
87
+ required: [
88
+ '@modern-js/runtime'
89
+ ],
90
+ setup: (api)=>{
91
+ const nestedRoutesForServer = {};
92
+ const isTanstackEntrypoint = (entrypoint)=>{
93
+ const { getEntrypointRoutesDir } = getRuntimeRouterCli();
94
+ return getEntrypointRoutesDir(entrypoint) === routesDir;
95
+ };
96
+ api._internalRuntimePlugins(({ entrypoint, plugins })=>{
97
+ if (!isTanstackEntrypoint(entrypoint)) return {
98
+ entrypoint,
99
+ plugins
100
+ };
101
+ const { metaName, serverRoutes } = api.getAppContext();
102
+ const serverBase = serverRoutes.filter((route)=>route.entryName === entrypoint.entryName).map((route)=>route.urlPath).sort((a, b)=>a.length - b.length > 0 ? -1 : 1);
103
+ plugins.push({
104
+ name: 'tanstackRouter',
105
+ path: `@${metaName}/plugin-tanstack/runtime`,
106
+ config: {
107
+ serverBase
108
+ }
109
+ });
110
+ return {
111
+ entrypoint,
112
+ plugins
113
+ };
114
+ });
115
+ api.checkEntryPoint(({ path: entryPath, entry })=>{
116
+ const { isRouteEntry } = getRuntimeRouterCli();
117
+ return {
118
+ path: entryPath,
119
+ entry: entry || isRouteEntry(entryPath, routesDir)
120
+ };
121
+ });
122
+ api.config(()=>({
123
+ source: {
124
+ include: [
125
+ /[\\/]node_modules[\\/]@tanstack[\\/]react-router[\\/]/,
126
+ node_path.resolve(cli_dirname, '../runtime').replace('cjs', 'esm')
127
+ ]
128
+ }
129
+ }));
130
+ api.modifyEntrypoints(async ({ entrypoints })=>{
131
+ const { handleModifyEntrypoints } = getRuntimeRouterCli();
132
+ return {
133
+ entrypoints: await handleModifyEntrypoints(entrypoints, routesDir, {
134
+ routesOwner: ENTRYPOINTS_KEY
135
+ })
136
+ };
137
+ });
138
+ api.generateEntryCode(async ({ entrypoints })=>{
139
+ const tanstackEntrypoints = entrypoints.filter(isTanstackEntrypoint);
140
+ if (0 === tanstackEntrypoints.length) return;
141
+ const { handleGeneratorEntryCode } = getRuntimeRouterCli();
142
+ const routesByEntry = await handleGeneratorEntryCode(api, tanstackEntrypoints, {
143
+ entrypointsKey: ENTRYPOINTS_KEY,
144
+ generateCodeOptions: {
145
+ enableTanstackTypes: false
146
+ }
147
+ });
148
+ await writeTanstackRouterTypesForEntries({
149
+ appContext: api.getAppContext(),
150
+ generatedDirName,
151
+ routesByEntry
152
+ });
153
+ });
154
+ api.onFileChanged(async (event)=>{
155
+ const { handleFileChange } = getRuntimeRouterCli();
156
+ await handleFileChange(api, event, {
157
+ entrypointsKey: ENTRYPOINTS_KEY,
158
+ includeEntry: (entrypoint)=>{
159
+ const { getEntrypointRoutesDir } = getRuntimeRouterCli();
160
+ return getEntrypointRoutesDir(entrypoint) === routesDir;
161
+ },
162
+ regenerate: async ({ api, entrypoints })=>{
163
+ const { handleGeneratorEntryCode } = getRuntimeRouterCli();
164
+ const routesByEntry = await handleGeneratorEntryCode(api, entrypoints, {
165
+ entrypointsKey: ENTRYPOINTS_KEY,
166
+ generateCodeOptions: {
167
+ enableTanstackTypes: false
168
+ }
169
+ });
170
+ await writeTanstackRouterTypesForEntries({
171
+ appContext: api.getAppContext(),
172
+ generatedDirName,
173
+ routesByEntry
174
+ });
175
+ }
176
+ });
177
+ });
178
+ api.modifyFileSystemRoutes(async ({ entrypoint, routes })=>{
179
+ if (isTanstackEntrypoint(entrypoint)) nestedRoutesForServer[entrypoint.entryName] = filterRoutesForServer(routes);
180
+ return {
181
+ entrypoint,
182
+ routes
183
+ };
184
+ });
185
+ api.onBeforeGenerateRoutes(async ({ entrypoint, code })=>{
186
+ if (isTanstackEntrypoint(entrypoint)) {
187
+ const { distDirectory } = api.getAppContext();
188
+ const nestedRoutesSpecPath = node_path.resolve(distDirectory, NESTED_ROUTE_SPEC_FILE);
189
+ const existingNestedRoutes = await fs.pathExists(nestedRoutesSpecPath) ? await fs.readJSON(nestedRoutesSpecPath) : {};
190
+ await fs.outputJSON(nestedRoutesSpecPath, {
191
+ ...existingNestedRoutes,
192
+ ...nestedRoutesForServer
193
+ });
194
+ }
195
+ return {
196
+ entrypoint,
197
+ code
198
+ };
199
+ });
200
+ }
201
+ };
202
+ }
203
+ const src_cli = tanstackRouterPlugin;
204
+ export default src_cli;
205
+ export { generateTanstackRouterTypesSourceForEntry, isTanstackRouterFrameworkEnabled, tanstackRouterPlugin, writeTanstackRegisterFile, writeTanstackRouterTypesForEntries };
@@ -0,0 +1,342 @@
1
+ import "node:module";
2
+ import { findExists, formatImportPath, fs, slash } from "@modern-js/utils";
3
+ import path from "path";
4
+ const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
5
+ const builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
6
+ const forbidList = new Set(`${reservedWords} ${builtins}`.split(' '));
7
+ function makeLegalIdentifier(str) {
8
+ const identifier = str.replace(/-(\w)/g, (_, letter)=>letter.toUpperCase()).replace(/[^$_a-zA-Z0-9]/g, '_');
9
+ if (/\d/.test(identifier[0]) || forbidList.has(identifier)) return `_${identifier}`;
10
+ return identifier || '_';
11
+ }
12
+ function getPathWithoutExt(filename) {
13
+ const extname = path.extname(filename);
14
+ return extname ? filename.slice(0, -extname.length) : filename;
15
+ }
16
+ const JS_OR_TS_EXTS = [
17
+ '.js',
18
+ '.jsx',
19
+ '.ts',
20
+ '.tsx',
21
+ '.mjs',
22
+ '.mts',
23
+ '.cjs',
24
+ '.cts'
25
+ ];
26
+ function toTanstackPath(pathname) {
27
+ return pathname.split('/').map((segment)=>{
28
+ if (!segment) return segment;
29
+ if ('*' === segment) return '$';
30
+ if (segment.startsWith(':')) {
31
+ const name = segment.slice(1);
32
+ if (name.endsWith('?')) return `{-$${name.slice(0, -1)}}`;
33
+ return `$${name}`;
34
+ }
35
+ return segment;
36
+ }).join('/');
37
+ }
38
+ async function resolveFileNoExt(inputNoExtPath) {
39
+ const file = findExists(JS_OR_TS_EXTS.map((ext)=>`${inputNoExtPath}${ext}`));
40
+ return file ? getPathWithoutExt(file) : null;
41
+ }
42
+ function quote(str) {
43
+ return JSON.stringify(str);
44
+ }
45
+ function normalizeRelativeImport(p) {
46
+ const normalized = formatImportPath(slash(p));
47
+ if (normalized.startsWith('.')) return normalized;
48
+ return `./${normalized}`;
49
+ }
50
+ function pickModernLoaderModule(route) {
51
+ const loaderPath = route.data || route.loader;
52
+ if (!loaderPath || 'string' != typeof loaderPath) return null;
53
+ const inline = Boolean(route.data);
54
+ return {
55
+ loaderPath,
56
+ inline
57
+ };
58
+ }
59
+ function isPathlessLayout(route) {
60
+ return 'nested' === route.type && 'boolean' != typeof route.index && void 0 === route.path;
61
+ }
62
+ function isIndexRoute(route) {
63
+ return 'nested' === route.type && Boolean(route.index);
64
+ }
65
+ function createRouteStaticDataSnippet(opts) {
66
+ const staticDataLines = [];
67
+ if (opts.modernRouteId) staticDataLines.push(`modernRouteId: ${quote(opts.modernRouteId)},`);
68
+ if (opts.loaderName) staticDataLines.push(`modernRouteLoader: ${opts.loaderName},`);
69
+ if (opts.actionName) staticDataLines.push(`modernRouteAction: ${opts.actionName},`);
70
+ if (!staticDataLines.length) return null;
71
+ return `staticData: createRouteStaticData({\n ${staticDataLines.join('\n ')}\n }),`;
72
+ }
73
+ async function isTanstackRouterFrameworkEnabled(appContext) {
74
+ const runtimeConfigBase = path.join(appContext.srcDirectory, appContext.runtimeConfigFile);
75
+ const runtimeConfigFile = findExists(JS_OR_TS_EXTS.map((ext)=>`${runtimeConfigBase}${ext}`));
76
+ if (!runtimeConfigFile) return false;
77
+ try {
78
+ const content = await fs.readFile(runtimeConfigFile, 'utf-8');
79
+ return /framework\s*:\s*['"]tanstack['"]/.test(content);
80
+ } catch {
81
+ return false;
82
+ }
83
+ }
84
+ async function generateTanstackRouterTypesSourceForEntry(opts) {
85
+ const { appContext, entryName, generatedDirName = 'modern-tanstack', routes } = opts;
86
+ const outDir = path.join(appContext.srcDirectory, generatedDirName, entryName);
87
+ const rootModern = routes.find((r)=>r && 'nested' === r.type && r.isRoot);
88
+ const topLevel = rootModern ? rootModern.children || [] : routes;
89
+ const imports = [];
90
+ const statements = [];
91
+ const loaderImportMap = new Map();
92
+ let loaderIndex = 0;
93
+ let routeIndex = 0;
94
+ const getImportNamesForLoader = async (aliasedNoExtPath, inline, hasAction)=>{
95
+ const key = `${inline ? 'inline' : 'default'}:${hasAction ? 'action' : 'loader'}:${aliasedNoExtPath}`;
96
+ const existing = loaderImportMap.get(key);
97
+ if (existing) return {
98
+ loaderName: existing,
99
+ actionName: hasAction ? existing.replace(/^loader_/, 'action_') : null
100
+ };
101
+ const prefix = `${appContext.internalSrcAlias}/`;
102
+ let absNoExt;
103
+ if (aliasedNoExtPath.startsWith(prefix)) {
104
+ const rel = aliasedNoExtPath.slice(prefix.length);
105
+ absNoExt = path.join(appContext.srcDirectory, rel);
106
+ } else absNoExt = path.isAbsolute(aliasedNoExtPath) ? aliasedNoExtPath : path.join(appContext.srcDirectory, aliasedNoExtPath);
107
+ const resolvedNoExt = await resolveFileNoExt(absNoExt);
108
+ if (!resolvedNoExt) return null;
109
+ const relImport = normalizeRelativeImport(path.relative(outDir, resolvedNoExt));
110
+ const importName = `loader_${loaderIndex++}`;
111
+ const actionName = hasAction ? importName.replace(/^loader_/, 'action_') : null;
112
+ if (inline) {
113
+ const specifiers = [
114
+ `loader as ${importName}`
115
+ ];
116
+ if (actionName) specifiers.push(`action as ${actionName}`);
117
+ imports.push(`import { ${specifiers.join(', ')} } from ${quote(relImport)};`);
118
+ } else imports.push(`import ${importName} from ${quote(relImport)};`);
119
+ loaderImportMap.set(key, importName);
120
+ return {
121
+ loaderName: importName,
122
+ actionName
123
+ };
124
+ };
125
+ const createRouteVarName = (route)=>{
126
+ const id = route.id;
127
+ const base = id ? makeLegalIdentifier(id) : `r_${routeIndex++}`;
128
+ return `route_${base}`;
129
+ };
130
+ const buildRoute = async (opts)=>{
131
+ const { parentVar, route } = opts;
132
+ const varName = createRouteVarName(route);
133
+ const loaderInfo = pickModernLoaderModule(route);
134
+ const routeAction = route.action;
135
+ const loaderImports = loaderInfo ? await getImportNamesForLoader(loaderInfo.loaderPath, loaderInfo.inline, Boolean(loaderInfo.inline && routeAction === loaderInfo.loaderPath)) : null;
136
+ const loaderName = loaderImports?.loaderName || null;
137
+ const actionName = loaderImports?.actionName || null;
138
+ const rawPath = route.path;
139
+ const hasSplat = 'string' == typeof rawPath && rawPath.includes('*');
140
+ const routeOpts = [
141
+ `getParentRoute: () => ${parentVar},`
142
+ ];
143
+ if (isPathlessLayout(route)) {
144
+ const id = route.id;
145
+ routeOpts.push(`id: ${quote(id || 'pathless')},`);
146
+ } else {
147
+ const p = isIndexRoute(route) ? '/' : toTanstackPath(rawPath || '');
148
+ routeOpts.push(`path: ${quote(p)},`);
149
+ }
150
+ if (loaderName) routeOpts.push(`loader: modernLoaderToTanstack({ hasSplat: ${hasSplat} }, ${loaderName}),`);
151
+ const staticDataSnippet = createRouteStaticDataSnippet({
152
+ modernRouteId: route.id,
153
+ loaderName,
154
+ actionName
155
+ });
156
+ if (staticDataSnippet) routeOpts.push(staticDataSnippet);
157
+ statements.push(`const ${varName} = createRoute({\n ${routeOpts.join('\n ')}\n});`);
158
+ const children = route.children;
159
+ if (children && children.length > 0) {
160
+ const childVars = await Promise.all(children.map((child)=>buildRoute({
161
+ parentVar: varName,
162
+ route: child
163
+ })));
164
+ statements.push(`${varName}.addChildren([${childVars.join(', ')}]);`);
165
+ }
166
+ return varName;
167
+ };
168
+ const rootLoaderInfo = rootModern ? pickModernLoaderModule(rootModern) : null;
169
+ const rootAction = rootModern?.action;
170
+ const rootLoaderImports = rootLoaderInfo?.loaderPath ? await getImportNamesForLoader(rootLoaderInfo.loaderPath, rootLoaderInfo.inline, Boolean(rootLoaderInfo.inline && rootAction === rootLoaderInfo.loaderPath)) : null;
171
+ const rootLoaderName = rootLoaderImports?.loaderName || null;
172
+ const rootActionName = rootLoaderImports?.actionName || null;
173
+ const topLevelVars = await Promise.all(topLevel.map((route)=>buildRoute({
174
+ parentVar: 'rootRoute',
175
+ route
176
+ })));
177
+ const rootOpts = [];
178
+ if (rootLoaderName) rootOpts.push(`loader: modernLoaderToTanstack({ hasSplat: false }, ${rootLoaderName}),`);
179
+ const routerGenTs = `/* eslint-disable */
180
+ // This file is auto-generated by Modern.js. Do not edit manually.
181
+
182
+ import {
183
+ createMemoryHistory,
184
+ createRootRouteWithContext,
185
+ createRoute,
186
+ createRouter,
187
+ notFound,
188
+ redirect,
189
+ } from '@modern-js/plugin-tanstack/runtime';
190
+
191
+ type ModernRouterContext = {
192
+ request?: Request;
193
+ requestContext?: unknown;
194
+ };
195
+
196
+ function isResponse(value: unknown): value is Response {
197
+ return (
198
+ value != null &&
199
+ typeof value === 'object' &&
200
+ typeof (value as any).status === 'number' &&
201
+ typeof (value as any).headers === 'object'
202
+ );
203
+ }
204
+
205
+ const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
206
+ function isRedirectResponse(res: Response) {
207
+ return redirectStatusCodes.has(res.status);
208
+ }
209
+
210
+ function throwTanstackRedirect(location: string) {
211
+ const target = location || '/';
212
+ try {
213
+ void new URL(target);
214
+ throw redirect({ href: target });
215
+ } catch {
216
+ throw redirect({ to: target });
217
+ }
218
+ }
219
+
220
+ function mapParamsForModernLoader(params: Record<string, string>, hasSplat: boolean) {
221
+ if (!hasSplat) {
222
+ return params;
223
+ }
224
+
225
+ const { _splat, ...rest } = params as any;
226
+ if (typeof _splat !== 'undefined') {
227
+ return { ...rest, '*': _splat };
228
+ }
229
+ return rest;
230
+ }
231
+
232
+ function createRouteStaticData(opts: {
233
+ modernRouteId?: string;
234
+ modernRouteAction?: unknown;
235
+ modernRouteLoader?: unknown;
236
+ }) {
237
+ const staticData: Record<string, unknown> = {};
238
+
239
+ if (opts.modernRouteId) {
240
+ staticData.modernRouteId = opts.modernRouteId;
241
+ }
242
+
243
+ if (opts.modernRouteLoader) {
244
+ staticData.modernRouteLoader = opts.modernRouteLoader;
245
+ }
246
+
247
+ if (opts.modernRouteAction) {
248
+ staticData.modernRouteAction = opts.modernRouteAction;
249
+ }
250
+
251
+ return Object.keys(staticData).length > 0 ? staticData : undefined;
252
+ }
253
+
254
+ function modernLoaderToTanstack<TLoader extends (args: any) => any>(
255
+ opts: { hasSplat: boolean },
256
+ modernLoader: TLoader,
257
+ ) {
258
+ type LoaderResult = Awaited<ReturnType<TLoader>>;
259
+
260
+ return async (ctx: any): Promise<LoaderResult> => {
261
+ try {
262
+ const signal: AbortSignal =
263
+ ctx?.abortController?.signal ||
264
+ ctx?.signal ||
265
+ new AbortController().signal;
266
+ const baseRequest: Request | undefined =
267
+ ctx?.context?.request instanceof Request ? ctx.context.request : undefined;
268
+
269
+ const href =
270
+ typeof ctx?.location === 'string'
271
+ ? ctx.location
272
+ : ctx?.location?.publicHref ||
273
+ ctx?.location?.href ||
274
+ ctx?.location?.url?.href ||
275
+ '';
276
+
277
+ const request = baseRequest
278
+ ? new Request(baseRequest, { signal })
279
+ : new Request(href, { signal });
280
+
281
+ const params = mapParamsForModernLoader(ctx?.params || {}, opts.hasSplat);
282
+
283
+ const result = await (modernLoader as any)({
284
+ request,
285
+ params,
286
+ context: ctx?.context?.requestContext,
287
+ });
288
+
289
+ if (isResponse(result)) {
290
+ if (isRedirectResponse(result)) {
291
+ const location = result.headers.get('Location') || '/';
292
+ throwTanstackRedirect(location);
293
+ }
294
+ if (result.status === 404) {
295
+ throw notFound();
296
+ }
297
+ }
298
+
299
+ return result as LoaderResult;
300
+ } catch (err) {
301
+ if (isResponse(err)) {
302
+ if (isRedirectResponse(err)) {
303
+ const location = err.headers.get('Location') || '/';
304
+ throwTanstackRedirect(location);
305
+ }
306
+ if (err.status === 404) {
307
+ throw notFound();
308
+ }
309
+ }
310
+ throw err;
311
+ }
312
+ };
313
+ }
314
+
315
+ ${imports.join('\n')}
316
+
317
+ export const rootRoute = createRootRouteWithContext<ModernRouterContext>()({
318
+ ${rootOpts.join('\n ')}
319
+ ${createRouteStaticDataSnippet({
320
+ modernRouteId: rootModern?.id,
321
+ loaderName: rootLoaderName,
322
+ actionName: rootActionName
323
+ }) || ''}
324
+ });
325
+
326
+ ${statements.join('\n\n')}
327
+
328
+ export const routeTree = rootRoute.addChildren([${topLevelVars.join(', ')}]);
329
+
330
+ export const router = createRouter({
331
+ routeTree,
332
+ history: createMemoryHistory({
333
+ initialEntries: ['/'],
334
+ }),
335
+ context: {} as ModernRouterContext,
336
+ });
337
+ `;
338
+ return {
339
+ routerGenTs
340
+ };
341
+ }
342
+ export { generateTanstackRouterTypesSourceForEntry, isTanstackRouterFrameworkEnabled };
@@ -0,0 +1,3 @@
1
+ import "node:module";
2
+ export * from "./cli/index.mjs";
3
+ export { default } from "./cli/index.mjs";
@@ -0,0 +1,19 @@
1
+ import "node:module";
2
+ var __webpack_modules__ = {};
3
+ var __webpack_module_cache__ = {};
4
+ function __webpack_require__(moduleId) {
5
+ var cachedModule = __webpack_module_cache__[moduleId];
6
+ if (void 0 !== cachedModule) return cachedModule.exports;
7
+ var module = __webpack_module_cache__[moduleId] = {
8
+ exports: {}
9
+ };
10
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
11
+ return module.exports;
12
+ }
13
+ __webpack_require__.m = __webpack_modules__;
14
+ (()=>{
15
+ __webpack_require__.add = function(modules) {
16
+ Object.assign(__webpack_require__.m, modules);
17
+ };
18
+ })();
19
+ export { __webpack_require__ };
@@ -0,0 +1,14 @@
1
+ import "node:module";
2
+ import { jsx } from "react/jsx-runtime";
3
+ import "react";
4
+ const DefaultNotFound = ()=>/*#__PURE__*/ jsx("div", {
5
+ style: {
6
+ margin: '150px auto',
7
+ textAlign: 'center',
8
+ display: 'flex',
9
+ alignItems: 'center',
10
+ justifyContent: 'center'
11
+ },
12
+ children: "404"
13
+ });
14
+ export { DefaultNotFound };
@@ -0,0 +1,29 @@
1
+ import "node:module";
2
+ function normalizeBasepath(basepath) {
3
+ if (!basepath) return '/';
4
+ let normalized = basepath.startsWith('/') ? basepath : `/${basepath}`;
5
+ if (normalized.length > 1 && normalized.endsWith('/')) normalized = normalized.slice(0, -1);
6
+ return normalized || '/';
7
+ }
8
+ function createModernBasepathRewrite(basepath, caseSensitive = false) {
9
+ const normalizedBasepath = normalizeBasepath(basepath);
10
+ if ('/' === normalizedBasepath) return;
11
+ const normalizedBasepathWithSlash = `${normalizedBasepath}/`;
12
+ const checkBasepath = caseSensitive ? normalizedBasepath : normalizedBasepath.toLowerCase();
13
+ const checkBasepathWithSlash = caseSensitive ? normalizedBasepathWithSlash : normalizedBasepathWithSlash.toLowerCase();
14
+ return {
15
+ input: ({ url })=>{
16
+ const pathname = caseSensitive ? url.pathname : url.pathname.toLowerCase();
17
+ if (pathname === checkBasepath) url.pathname = '/';
18
+ else if (pathname.startsWith(checkBasepathWithSlash)) url.pathname = url.pathname.slice(normalizedBasepath.length) || '/';
19
+ return url;
20
+ },
21
+ output: ({ url })=>{
22
+ const pathname = url.pathname || '/';
23
+ if ('/' === pathname) url.pathname = normalizedBasepath;
24
+ else url.pathname = `${normalizedBasepath}${pathname.startsWith('/') ? '' : '/'}${pathname}`;
25
+ return url;
26
+ }
27
+ };
28
+ }
29
+ export { createModernBasepathRewrite };