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