@bleedingdev/modern-js-plugin-tanstack 3.5.0-ultramodern.9 → 3.5.0-ultramodern.90
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/dist/cjs/cli/artifacts.js +157 -0
- package/dist/cjs/cli/index.js +8 -79
- package/dist/cjs/cli/tanstackTypes/canonicalRoutes.js +113 -0
- package/dist/cjs/cli/tanstackTypes/shared.js +123 -0
- package/dist/cjs/cli/tanstackTypes/source.js +255 -0
- package/dist/cjs/cli/tanstackTypes.js +4 -350
- package/dist/cjs/runtime/blockingSubscribe.js +57 -0
- package/dist/cjs/runtime/{pluginCore.js → clientBasepath.js} +12 -19
- package/dist/cjs/runtime/clientHydration.js +118 -0
- package/dist/cjs/runtime/dataMutation.js +11 -176
- package/dist/cjs/runtime/formData.js +116 -0
- package/dist/cjs/runtime/loaderBridge.js +7 -11
- package/dist/cjs/runtime/plugin.js +21 -125
- package/dist/cjs/runtime/plugin.node.js +45 -125
- package/dist/cjs/runtime/pluginShared.js +87 -0
- package/dist/cjs/runtime/prefetchLink.js +15 -2
- package/dist/cjs/runtime/register.js +3 -16
- package/dist/cjs/runtime/routeTree/components.js +102 -0
- package/dist/cjs/runtime/routeTree/construction.js +152 -0
- package/dist/cjs/runtime/routeTree/index.js +47 -0
- package/dist/cjs/runtime/routeTree/loaders.js +158 -0
- package/dist/cjs/runtime/routeTree/paths.js +57 -0
- package/dist/cjs/runtime/routeTree/staticData.js +63 -0
- package/dist/cjs/runtime/routeTree/tanstackRoutes.js +57 -0
- package/dist/cjs/runtime/routeTree/types.js +18 -0
- package/dist/cjs/runtime/routeTree.js +7 -320
- package/dist/cjs/runtime/router.js +15 -2
- package/dist/cjs/runtime/rsc/CompositeComponent.js +18 -13
- package/dist/cjs/runtime/rsc/ReplayableStream.js +0 -4
- package/dist/cjs/runtime/rsc/RscNodeRenderer.js +5 -16
- package/dist/cjs/runtime/rsc/client.js +8 -25
- package/dist/cjs/runtime/rsc/flightSerialization.js +132 -0
- package/dist/cjs/runtime/rsc/payloadErrors.js +99 -0
- package/dist/cjs/runtime/rsc/payloadFetch.js +105 -0
- package/dist/cjs/runtime/rsc/payloadRouter.js +21 -142
- package/dist/cjs/runtime/rsc/payloadRoutes.js +94 -0
- package/dist/cjs/runtime/rsc/payloadStorage.js +58 -0
- package/dist/cjs/runtime/rsc/server.js +14 -18
- package/dist/cjs/runtime/rsc/shared.js +68 -0
- package/dist/cjs/runtime/rsc/slotUsageSanitizer.js +1 -0
- package/dist/cjs/runtime/rsc/treeGetter.js +72 -0
- package/dist/cjs/runtime/shared/isAbsoluteUrl.js +47 -0
- package/dist/cjs/runtime/ssrManagedTags.js +103 -0
- package/dist/cjs/runtime/ssrPreload.js +89 -0
- package/dist/cjs/runtime/ssrTypes.js +18 -0
- package/dist/cjs/runtime/state.js +1 -1
- package/dist/cjs/runtime/submitAction.js +161 -0
- package/dist/cjs/runtime/utils.js +49 -86
- package/dist/esm/cli/artifacts.mjs +103 -0
- package/dist/esm/cli/index.mjs +8 -82
- package/dist/esm/cli/tanstackTypes/canonicalRoutes.mjs +75 -0
- package/dist/esm/cli/tanstackTypes/shared.mjs +61 -0
- package/dist/esm/cli/tanstackTypes/source.mjs +207 -0
- package/dist/esm/cli/tanstackTypes.mjs +2 -339
- package/dist/esm/runtime/blockingSubscribe.mjs +19 -0
- package/dist/esm/runtime/clientBasepath.mjs +10 -0
- package/dist/esm/runtime/clientHydration.mjs +71 -0
- package/dist/esm/runtime/dataMutation.mjs +2 -167
- package/dist/esm/runtime/formData.mjs +63 -0
- package/dist/esm/runtime/loaderBridge.mjs +5 -9
- package/dist/esm/runtime/plugin.mjs +14 -118
- package/dist/esm/runtime/plugin.node.mjs +41 -121
- package/dist/esm/runtime/pluginShared.mjs +40 -0
- package/dist/esm/runtime/prefetchLink.mjs +18 -5
- package/dist/esm/runtime/register.mjs +1 -14
- package/dist/esm/runtime/routeTree/components.mjs +55 -0
- package/dist/esm/runtime/routeTree/construction.mjs +111 -0
- package/dist/esm/runtime/routeTree/index.mjs +2 -0
- package/dist/esm/runtime/routeTree/loaders.mjs +117 -0
- package/dist/esm/runtime/routeTree/paths.mjs +16 -0
- package/dist/esm/runtime/routeTree/staticData.mjs +22 -0
- package/dist/esm/runtime/routeTree/tanstackRoutes.mjs +13 -0
- package/dist/esm/runtime/routeTree/types.mjs +0 -0
- package/dist/esm/runtime/routeTree.mjs +1 -318
- package/dist/esm/runtime/router.mjs +16 -2
- package/dist/esm/runtime/rsc/CompositeComponent.mjs +18 -13
- package/dist/esm/runtime/rsc/ReplayableStream.mjs +1 -1
- package/dist/esm/runtime/rsc/RscNodeRenderer.mjs +4 -15
- package/dist/esm/runtime/rsc/client.mjs +6 -26
- package/dist/esm/runtime/rsc/flightSerialization.mjs +88 -0
- package/dist/esm/runtime/rsc/payloadErrors.mjs +49 -0
- package/dist/esm/runtime/rsc/payloadFetch.mjs +55 -0
- package/dist/esm/runtime/rsc/payloadRouter.mjs +12 -133
- package/dist/esm/runtime/rsc/payloadRoutes.mjs +50 -0
- package/dist/esm/runtime/rsc/payloadStorage.mjs +14 -0
- package/dist/esm/runtime/rsc/server.mjs +17 -21
- package/dist/esm/runtime/rsc/shared.mjs +21 -0
- package/dist/esm/runtime/rsc/slotUsageSanitizer.mjs +1 -0
- package/dist/esm/runtime/rsc/treeGetter.mjs +34 -0
- package/dist/esm/runtime/shared/isAbsoluteUrl.mjs +9 -0
- package/dist/esm/runtime/ssrManagedTags.mjs +53 -0
- package/dist/esm/runtime/ssrPreload.mjs +51 -0
- package/dist/esm/runtime/ssrTypes.mjs +0 -0
- package/dist/esm/runtime/state.mjs +1 -1
- package/dist/esm/runtime/submitAction.mjs +120 -0
- package/dist/esm/runtime/utils.mjs +47 -75
- package/dist/esm-node/cli/artifacts.mjs +104 -0
- package/dist/esm-node/cli/index.mjs +8 -82
- package/dist/esm-node/cli/tanstackTypes/canonicalRoutes.mjs +76 -0
- package/dist/esm-node/cli/tanstackTypes/shared.mjs +62 -0
- package/dist/esm-node/cli/tanstackTypes/source.mjs +208 -0
- package/dist/esm-node/cli/tanstackTypes.mjs +2 -339
- package/dist/esm-node/runtime/blockingSubscribe.mjs +20 -0
- package/dist/esm-node/runtime/clientBasepath.mjs +11 -0
- package/dist/esm-node/runtime/clientHydration.mjs +72 -0
- package/dist/esm-node/runtime/dataMutation.mjs +2 -167
- package/dist/esm-node/runtime/formData.mjs +64 -0
- package/dist/esm-node/runtime/loaderBridge.mjs +5 -9
- package/dist/esm-node/runtime/plugin.mjs +14 -118
- package/dist/esm-node/runtime/plugin.node.mjs +41 -121
- package/dist/esm-node/runtime/pluginShared.mjs +41 -0
- package/dist/esm-node/runtime/prefetchLink.mjs +18 -5
- package/dist/esm-node/runtime/register.mjs +1 -14
- package/dist/esm-node/runtime/routeTree/components.mjs +56 -0
- package/dist/esm-node/runtime/routeTree/construction.mjs +112 -0
- package/dist/esm-node/runtime/routeTree/index.mjs +3 -0
- package/dist/esm-node/runtime/routeTree/loaders.mjs +118 -0
- package/dist/esm-node/runtime/routeTree/paths.mjs +17 -0
- package/dist/esm-node/runtime/routeTree/staticData.mjs +23 -0
- package/dist/esm-node/runtime/routeTree/tanstackRoutes.mjs +14 -0
- package/dist/esm-node/runtime/routeTree/types.mjs +1 -0
- package/dist/esm-node/runtime/routeTree.mjs +1 -318
- package/dist/esm-node/runtime/router.mjs +16 -2
- package/dist/esm-node/runtime/rsc/CompositeComponent.mjs +18 -13
- package/dist/esm-node/runtime/rsc/ReplayableStream.mjs +1 -1
- package/dist/esm-node/runtime/rsc/RscNodeRenderer.mjs +4 -15
- package/dist/esm-node/runtime/rsc/client.mjs +6 -26
- package/dist/esm-node/runtime/rsc/flightSerialization.mjs +89 -0
- package/dist/esm-node/runtime/rsc/payloadErrors.mjs +50 -0
- package/dist/esm-node/runtime/rsc/payloadFetch.mjs +56 -0
- package/dist/esm-node/runtime/rsc/payloadRouter.mjs +12 -133
- package/dist/esm-node/runtime/rsc/payloadRoutes.mjs +51 -0
- package/dist/esm-node/runtime/rsc/payloadStorage.mjs +15 -0
- package/dist/esm-node/runtime/rsc/server.mjs +17 -21
- package/dist/esm-node/runtime/rsc/shared.mjs +22 -0
- package/dist/esm-node/runtime/rsc/slotUsageSanitizer.mjs +1 -0
- package/dist/esm-node/runtime/rsc/treeGetter.mjs +35 -0
- package/dist/esm-node/runtime/shared/isAbsoluteUrl.mjs +10 -0
- package/dist/esm-node/runtime/ssrManagedTags.mjs +54 -0
- package/dist/esm-node/runtime/ssrPreload.mjs +52 -0
- package/dist/esm-node/runtime/ssrTypes.mjs +1 -0
- package/dist/esm-node/runtime/state.mjs +1 -1
- package/dist/esm-node/runtime/submitAction.mjs +121 -0
- package/dist/esm-node/runtime/utils.mjs +47 -75
- package/dist/types/cli/artifacts.d.ts +33 -0
- package/dist/types/cli/index.d.ts +2 -22
- package/dist/types/cli/tanstackTypes/canonicalRoutes.d.ts +22 -0
- package/dist/types/cli/tanstackTypes/shared.d.ts +20 -0
- package/dist/types/cli/tanstackTypes/source.d.ts +12 -0
- package/dist/types/cli/tanstackTypes.d.ts +3 -31
- package/dist/types/runtime/blockingSubscribe.d.ts +1 -0
- package/dist/types/runtime/clientBasepath.d.ts +1 -0
- package/dist/types/runtime/clientHydration.d.ts +10 -0
- package/dist/types/runtime/dataMutation.d.ts +3 -12
- package/dist/types/runtime/formData.d.ts +24 -0
- package/dist/types/runtime/loaderBridge.d.ts +2 -1
- package/dist/types/runtime/plugin.d.ts +1 -1
- package/dist/types/runtime/plugin.node.d.ts +1 -1
- package/dist/types/runtime/pluginShared.d.ts +38 -0
- package/dist/types/runtime/register.d.ts +10 -9
- package/dist/types/runtime/routeTree/components.d.ts +7 -0
- package/dist/types/runtime/routeTree/construction.d.ts +5 -0
- package/dist/types/runtime/routeTree/index.d.ts +2 -0
- package/dist/types/runtime/routeTree/loaders.d.ts +4 -0
- package/dist/types/runtime/routeTree/paths.d.ts +3 -0
- package/dist/types/runtime/routeTree/staticData.d.ts +19 -0
- package/dist/types/runtime/routeTree/tanstackRoutes.d.ts +4 -0
- package/dist/types/runtime/routeTree/types.d.ts +76 -0
- package/dist/types/runtime/routeTree.d.ts +1 -9
- package/dist/types/runtime/router.d.ts +6 -7
- package/dist/types/runtime/rsc/CompositeComponent.d.ts +1 -2
- package/dist/types/runtime/rsc/ReplayableStream.d.ts +3 -2
- package/dist/types/runtime/rsc/RscNodeRenderer.d.ts +2 -3
- package/dist/types/runtime/rsc/SlotContext.d.ts +2 -1
- package/dist/types/runtime/rsc/client.d.ts +2 -0
- package/dist/types/runtime/rsc/flightSerialization.d.ts +3 -0
- package/dist/types/runtime/rsc/payloadErrors.d.ts +6 -0
- package/dist/types/runtime/rsc/payloadFetch.d.ts +8 -0
- package/dist/types/runtime/rsc/payloadRouter.d.ts +2 -10
- package/dist/types/runtime/rsc/payloadRoutes.d.ts +40 -0
- package/dist/types/runtime/rsc/payloadStorage.d.ts +5 -0
- package/dist/types/runtime/rsc/shared.d.ts +18 -0
- package/dist/types/runtime/rsc/treeGetter.d.ts +1 -0
- package/dist/types/runtime/shared/isAbsoluteUrl.d.ts +1 -0
- package/dist/types/runtime/ssrManagedTags.d.ts +7 -0
- package/dist/types/runtime/ssrPreload.d.ts +2 -0
- package/dist/types/runtime/ssrTypes.d.ts +44 -0
- package/dist/types/runtime/state.d.ts +1 -1
- package/dist/types/runtime/submitAction.d.ts +14 -0
- package/dist/types/runtime/types.d.ts +4 -10
- package/dist/types/runtime/utils.d.ts +4 -24
- package/package.json +37 -17
- package/rstest.config.mts +23 -0
- package/src/cli/artifacts.ts +255 -0
- package/src/cli/index.ts +7 -185
- package/src/cli/tanstackTypes/canonicalRoutes.ts +157 -0
- package/src/cli/tanstackTypes/shared.ts +113 -0
- package/src/cli/tanstackTypes/source.ts +414 -0
- package/src/cli/tanstackTypes.ts +4 -647
- package/src/runtime/blockingSubscribe.ts +48 -0
- package/src/runtime/clientBasepath.ts +12 -0
- package/src/runtime/clientHydration.ts +149 -0
- package/src/runtime/dataMutation.tsx +9 -306
- package/src/runtime/formData.ts +153 -0
- package/src/runtime/loaderBridge.ts +11 -12
- package/src/runtime/plugin.node.tsx +76 -291
- package/src/runtime/plugin.tsx +21 -257
- package/src/runtime/pluginShared.ts +112 -0
- package/src/runtime/prefetchLink.tsx +78 -15
- package/src/runtime/register.ts +10 -42
- package/src/runtime/routeTree/components.ts +130 -0
- package/src/runtime/routeTree/construction.ts +238 -0
- package/src/runtime/routeTree/index.ts +7 -0
- package/src/runtime/routeTree/loaders.ts +232 -0
- package/src/runtime/routeTree/paths.ts +36 -0
- package/src/runtime/routeTree/staticData.ts +66 -0
- package/src/runtime/routeTree/tanstackRoutes.ts +36 -0
- package/src/runtime/routeTree/types.ts +87 -0
- package/src/runtime/routeTree.ts +5 -750
- package/src/runtime/router.ts +35 -7
- package/src/runtime/rsc/CompositeComponent.tsx +29 -20
- package/src/runtime/rsc/ReplayableStream.ts +2 -2
- package/src/runtime/rsc/RscNodeRenderer.tsx +9 -27
- package/src/runtime/rsc/SlotContext.tsx +1 -1
- package/src/runtime/rsc/client.tsx +12 -40
- package/src/runtime/rsc/flightSerialization.ts +158 -0
- package/src/runtime/rsc/payloadErrors.ts +73 -0
- package/src/runtime/rsc/payloadFetch.ts +92 -0
- package/src/runtime/rsc/payloadRouter.ts +29 -234
- package/src/runtime/rsc/payloadRoutes.ts +122 -0
- package/src/runtime/rsc/payloadStorage.ts +35 -0
- package/src/runtime/rsc/server.tsx +20 -29
- package/src/runtime/rsc/shared.ts +70 -0
- package/src/runtime/rsc/slotUsageSanitizer.ts +4 -0
- package/src/runtime/rsc/treeGetter.tsx +52 -0
- package/src/runtime/shared/isAbsoluteUrl.ts +8 -0
- package/src/runtime/ssrManagedTags.ts +107 -0
- package/src/runtime/ssrPreload.ts +125 -0
- package/src/runtime/ssrTypes.ts +51 -0
- package/src/runtime/state.ts +9 -3
- package/src/runtime/submitAction.ts +201 -0
- package/src/runtime/types.ts +4 -18
- package/src/runtime/utils.tsx +76 -110
- package/src/types/react-server-dom-rspack.d.ts +56 -0
- package/tests/castRatchet.test.ts +85 -0
- package/tests/realm-isolation.test.ts +114 -0
- package/tests/router/cli.test.ts +7 -0
- package/tests/router/clientHydration.test.tsx +61 -0
- package/tests/router/dataMutation.test.tsx +59 -0
- package/tests/router/fixtures/legacyRuntimeRouter.ts +10 -0
- package/tests/router/fixtures/router-gen.golden.txt +71 -0
- package/tests/router/fixtures/router-gen.oxlint.json +5 -0
- package/tests/router/flightSerialization.roundtrip.test.tsx +137 -0
- package/tests/router/generateRouteArtifacts.test.ts +106 -0
- package/tests/router/hooks.test.ts +14 -0
- package/tests/router/loaderBridge.test.ts +21 -0
- package/tests/router/prefetchLink.test.tsx +106 -59
- package/tests/router/prefetchLinkPreload.test.tsx +85 -0
- package/tests/router/register.test.ts +1 -39
- package/tests/router/routeTree.test.ts +51 -11
- package/tests/router/routerCompatibility.test.ts +245 -0
- package/tests/router/rsc.test.tsx +189 -0
- package/tests/router/rscPayloadRouterMatrix.test.ts +197 -0
- package/tests/router/slotUsageSanitizer.test.ts +23 -0
- package/tests/router/ssrPreload.test.ts +30 -0
- package/tests/router/tanstackTypes.test.ts +298 -1
- package/dist/esm/runtime/pluginCore.mjs +0 -14
- package/dist/esm-node/runtime/pluginCore.mjs +0 -15
- package/dist/types/runtime/pluginCore.d.ts +0 -21
- package/src/runtime/pluginCore.ts +0 -48
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { makeLegalIdentifier } from "@modern-js/runtime/cli";
|
|
3
|
+
import { createRouteStaticDataSnippet, isIndexRoute, isPathlessLayout, normalizeRelativeImport, pickModernLoaderModule, pickRouteSearchContractModules, quote, resolveFileNoExt, toTanstackPath } from "./shared.mjs";
|
|
4
|
+
async function generateTanstackRouterTypesSourceForEntry(opts) {
|
|
5
|
+
const { appContext, entryName, generatedDirName = 'modern-tanstack', routes } = opts;
|
|
6
|
+
const outDir = node_path.join(appContext.srcDirectory, generatedDirName, entryName);
|
|
7
|
+
const rootModern = routes.find((r)=>{
|
|
8
|
+
const extras = r;
|
|
9
|
+
return r && 'nested' === extras.type && extras.isRoot;
|
|
10
|
+
});
|
|
11
|
+
const rootExtras = rootModern;
|
|
12
|
+
const topLevel = rootModern ? rootExtras?.children || [] : routes;
|
|
13
|
+
const imports = [];
|
|
14
|
+
const statements = [];
|
|
15
|
+
const loaderImportMap = new Map();
|
|
16
|
+
const componentImportMap = new Map();
|
|
17
|
+
const searchContractImportMap = new Map();
|
|
18
|
+
const usedRouteVarNames = new Set();
|
|
19
|
+
let loaderIndex = 0;
|
|
20
|
+
let componentIndex = 0;
|
|
21
|
+
let validateSearchIndex = 0;
|
|
22
|
+
let loaderDepsIndex = 0;
|
|
23
|
+
let routeIndex = 0;
|
|
24
|
+
const getImportNameForComponent = (componentPath)=>{
|
|
25
|
+
if ('string' != typeof componentPath || 0 === componentPath.length) return Promise.resolve(null);
|
|
26
|
+
let pendingImportName = componentImportMap.get(componentPath);
|
|
27
|
+
if (!pendingImportName) {
|
|
28
|
+
pendingImportName = (async ()=>{
|
|
29
|
+
const resolvedNoExt = await resolveRouteModuleNoExt(componentPath);
|
|
30
|
+
if (!resolvedNoExt) return null;
|
|
31
|
+
const relImport = normalizeRelativeImport(node_path.relative(outDir, resolvedNoExt));
|
|
32
|
+
const componentName = `component_${componentIndex++}`;
|
|
33
|
+
imports.push(`import ${componentName} from ${quote(relImport)};`);
|
|
34
|
+
return componentName;
|
|
35
|
+
})();
|
|
36
|
+
componentImportMap.set(componentPath, pendingImportName);
|
|
37
|
+
}
|
|
38
|
+
return pendingImportName;
|
|
39
|
+
};
|
|
40
|
+
const resolveRouteModuleNoExt = async (aliasedNoExtPath)=>{
|
|
41
|
+
const prefix = `${appContext.internalSrcAlias}/`;
|
|
42
|
+
let absNoExt;
|
|
43
|
+
if (aliasedNoExtPath.startsWith(prefix)) {
|
|
44
|
+
const rel = aliasedNoExtPath.slice(prefix.length);
|
|
45
|
+
absNoExt = node_path.join(appContext.srcDirectory, rel);
|
|
46
|
+
} else absNoExt = node_path.isAbsolute(aliasedNoExtPath) ? aliasedNoExtPath : node_path.join(appContext.srcDirectory, aliasedNoExtPath);
|
|
47
|
+
return resolveFileNoExt(absNoExt);
|
|
48
|
+
};
|
|
49
|
+
const getImportNamesForLoader = async (aliasedNoExtPath, inline, hasAction)=>{
|
|
50
|
+
const key = `${inline ? 'inline' : 'default'}:${hasAction ? 'action' : 'loader'}:${aliasedNoExtPath}`;
|
|
51
|
+
const existing = loaderImportMap.get(key);
|
|
52
|
+
if (existing) return {
|
|
53
|
+
loaderName: existing,
|
|
54
|
+
actionName: hasAction ? existing.replace(/^loader_/, 'action_') : null
|
|
55
|
+
};
|
|
56
|
+
const resolvedNoExt = await resolveRouteModuleNoExt(aliasedNoExtPath);
|
|
57
|
+
if (!resolvedNoExt) return null;
|
|
58
|
+
const relImport = normalizeRelativeImport(node_path.relative(outDir, resolvedNoExt));
|
|
59
|
+
const importName = `loader_${loaderIndex++}`;
|
|
60
|
+
const actionName = hasAction ? importName.replace(/^loader_/, 'action_') : null;
|
|
61
|
+
if (inline) {
|
|
62
|
+
const specifiers = [
|
|
63
|
+
`loader as ${importName}`
|
|
64
|
+
];
|
|
65
|
+
if (actionName) specifiers.push(`action as ${actionName}`);
|
|
66
|
+
imports.push(`import { ${specifiers.join(', ')} } from ${quote(relImport)};`);
|
|
67
|
+
} else imports.push(`import ${importName} from ${quote(relImport)};`);
|
|
68
|
+
loaderImportMap.set(key, importName);
|
|
69
|
+
return {
|
|
70
|
+
loaderName: importName,
|
|
71
|
+
actionName
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
const getImportNameForSearchContract = async (aliasedNoExtPath, exportName)=>{
|
|
75
|
+
const key = `${exportName}:${aliasedNoExtPath}`;
|
|
76
|
+
const existing = searchContractImportMap.get(key);
|
|
77
|
+
if (existing) return existing;
|
|
78
|
+
const resolvedNoExt = await resolveRouteModuleNoExt(aliasedNoExtPath);
|
|
79
|
+
if (!resolvedNoExt) return null;
|
|
80
|
+
const relImport = normalizeRelativeImport(node_path.relative(outDir, resolvedNoExt));
|
|
81
|
+
const importName = 'validateSearch' === exportName ? `validateSearch_${validateSearchIndex++}` : `loaderDeps_${loaderDepsIndex++}`;
|
|
82
|
+
imports.push(`import { ${exportName} as ${importName} } from ${quote(relImport)};`);
|
|
83
|
+
searchContractImportMap.set(key, importName);
|
|
84
|
+
return importName;
|
|
85
|
+
};
|
|
86
|
+
const reserveRouteVarName = (preferred)=>{
|
|
87
|
+
let candidate = preferred;
|
|
88
|
+
let suffix = 1;
|
|
89
|
+
while(usedRouteVarNames.has(candidate))candidate = `${preferred}_${suffix++}`;
|
|
90
|
+
usedRouteVarNames.add(candidate);
|
|
91
|
+
return candidate;
|
|
92
|
+
};
|
|
93
|
+
const createRouteVarName = (route)=>{
|
|
94
|
+
const id = route.id;
|
|
95
|
+
const base = id ? makeLegalIdentifier(id) : `r_${routeIndex++}`;
|
|
96
|
+
return reserveRouteVarName(`route_${base}`);
|
|
97
|
+
};
|
|
98
|
+
const buildRoute = async (opts)=>{
|
|
99
|
+
const { parentVar, route } = opts;
|
|
100
|
+
const varName = createRouteVarName(route);
|
|
101
|
+
const routeExtras = route;
|
|
102
|
+
const loaderInfo = pickModernLoaderModule(route);
|
|
103
|
+
const routeAction = routeExtras.action;
|
|
104
|
+
const loaderImports = loaderInfo ? await getImportNamesForLoader(loaderInfo.loaderPath, loaderInfo.inline, Boolean(loaderInfo.inline && routeAction === loaderInfo.loaderPath)) : null;
|
|
105
|
+
const loaderName = loaderImports?.loaderName || null;
|
|
106
|
+
const actionName = loaderImports?.actionName || null;
|
|
107
|
+
const searchContractInfo = pickRouteSearchContractModules(route);
|
|
108
|
+
const validateSearchName = searchContractInfo.validateSearchPath ? await getImportNameForSearchContract(searchContractInfo.validateSearchPath, 'validateSearch') : null;
|
|
109
|
+
const loaderDepsName = searchContractInfo.loaderDepsPath ? await getImportNameForSearchContract(searchContractInfo.loaderDepsPath, 'loaderDeps') : null;
|
|
110
|
+
const rawPath = routeExtras.path;
|
|
111
|
+
const hasSplat = 'string' == typeof rawPath && rawPath.includes('*');
|
|
112
|
+
const routeOpts = [
|
|
113
|
+
`getParentRoute: () => ${parentVar},`
|
|
114
|
+
];
|
|
115
|
+
const componentName = await getImportNameForComponent(routeExtras._component);
|
|
116
|
+
if (componentName) routeOpts.push(`component: ${componentName},`);
|
|
117
|
+
if (isPathlessLayout(route)) {
|
|
118
|
+
const id = routeExtras.id;
|
|
119
|
+
routeOpts.push(`id: ${quote(id || 'pathless')},`);
|
|
120
|
+
} else {
|
|
121
|
+
const p = isIndexRoute(route) ? '/' : toTanstackPath(rawPath || '');
|
|
122
|
+
routeOpts.push(`path: ${quote(p)},`);
|
|
123
|
+
}
|
|
124
|
+
if (loaderName) routeOpts.push(`loader: modernLoaderToTanstack({ hasSplat: ${hasSplat} }, ${loaderName}),`);
|
|
125
|
+
if (validateSearchName) routeOpts.push(`validateSearch: ${validateSearchName},`);
|
|
126
|
+
if (loaderDepsName) routeOpts.push(`loaderDeps: ${loaderDepsName},`);
|
|
127
|
+
const staticDataSnippet = createRouteStaticDataSnippet({
|
|
128
|
+
modernRouteId: routeExtras.id,
|
|
129
|
+
loaderName,
|
|
130
|
+
actionName
|
|
131
|
+
});
|
|
132
|
+
if (staticDataSnippet) routeOpts.push(staticDataSnippet);
|
|
133
|
+
const children = routeExtras.children;
|
|
134
|
+
const hasChildren = Boolean(children && children.length > 0);
|
|
135
|
+
const routeCtorVarName = hasChildren ? reserveRouteVarName(`${varName}__base`) : varName;
|
|
136
|
+
statements.push(`const ${routeCtorVarName} = createRoute({\n ${routeOpts.join('\n ')}\n});`);
|
|
137
|
+
if (children && children.length > 0) {
|
|
138
|
+
const childVars = [];
|
|
139
|
+
for (const child of children)childVars.push(await buildRoute({
|
|
140
|
+
parentVar: routeCtorVarName,
|
|
141
|
+
route: child
|
|
142
|
+
}));
|
|
143
|
+
statements.push(`const ${varName} = ${routeCtorVarName}.addChildren([${childVars.join(', ')}]);`);
|
|
144
|
+
}
|
|
145
|
+
return varName;
|
|
146
|
+
};
|
|
147
|
+
const rootLoaderInfo = rootModern ? pickModernLoaderModule(rootModern) : null;
|
|
148
|
+
const rootAction = rootExtras?.action;
|
|
149
|
+
const rootLoaderImports = rootLoaderInfo?.loaderPath ? await getImportNamesForLoader(rootLoaderInfo.loaderPath, rootLoaderInfo.inline, Boolean(rootLoaderInfo.inline && rootAction === rootLoaderInfo.loaderPath)) : null;
|
|
150
|
+
const rootLoaderName = rootLoaderImports?.loaderName || null;
|
|
151
|
+
const rootActionName = rootLoaderImports?.actionName || null;
|
|
152
|
+
const rootSearchContractInfo = rootModern ? pickRouteSearchContractModules(rootModern) : null;
|
|
153
|
+
const rootValidateSearchName = rootSearchContractInfo?.validateSearchPath ? await getImportNameForSearchContract(rootSearchContractInfo.validateSearchPath, 'validateSearch') : null;
|
|
154
|
+
const rootLoaderDepsName = rootSearchContractInfo?.loaderDepsPath ? await getImportNameForSearchContract(rootSearchContractInfo.loaderDepsPath, 'loaderDeps') : null;
|
|
155
|
+
const topLevelVars = [];
|
|
156
|
+
for (const route of topLevel)topLevelVars.push(await buildRoute({
|
|
157
|
+
parentVar: 'rootRoute',
|
|
158
|
+
route
|
|
159
|
+
}));
|
|
160
|
+
const rootOpts = [];
|
|
161
|
+
const rootComponentName = await getImportNameForComponent(rootExtras?._component);
|
|
162
|
+
if (rootComponentName) rootOpts.push(`component: ${rootComponentName},`);
|
|
163
|
+
if (rootLoaderName) rootOpts.push(`loader: modernLoaderToTanstack({ hasSplat: false }, ${rootLoaderName}),`);
|
|
164
|
+
if (rootValidateSearchName) rootOpts.push(`validateSearch: ${rootValidateSearchName},`);
|
|
165
|
+
if (rootLoaderDepsName) rootOpts.push(`loaderDeps: ${rootLoaderDepsName},`);
|
|
166
|
+
const routerGenTs = `// This file is auto-generated by Modern.js. Do not edit manually.
|
|
167
|
+
|
|
168
|
+
import {
|
|
169
|
+
createMemoryHistory,
|
|
170
|
+
createRootRouteWithContext,
|
|
171
|
+
createRoute,
|
|
172
|
+
createRouter,
|
|
173
|
+
createRouteStaticData,
|
|
174
|
+
type ModernRouterContext,
|
|
175
|
+
modernLoaderToTanstack,
|
|
176
|
+
modernTanstackRouterFastDefaults,
|
|
177
|
+
} from '@modern-js/plugin-tanstack/runtime';
|
|
178
|
+
|
|
179
|
+
${imports.join('\n')}
|
|
180
|
+
|
|
181
|
+
export const rootRoute = createRootRouteWithContext<ModernRouterContext>()({
|
|
182
|
+
${rootOpts.join('\n ')}
|
|
183
|
+
${createRouteStaticDataSnippet({
|
|
184
|
+
modernRouteId: rootExtras?.id,
|
|
185
|
+
loaderName: rootLoaderName,
|
|
186
|
+
actionName: rootActionName
|
|
187
|
+
}) || ''}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
${statements.join('\n\n')}
|
|
191
|
+
|
|
192
|
+
export const routeTree = rootRoute.addChildren([${topLevelVars.join(', ')}]);
|
|
193
|
+
|
|
194
|
+
export const router = createRouter({
|
|
195
|
+
...modernTanstackRouterFastDefaults,
|
|
196
|
+
routeTree,
|
|
197
|
+
history: createMemoryHistory({
|
|
198
|
+
initialEntries: ['/'],
|
|
199
|
+
}),
|
|
200
|
+
context: {} as ModernRouterContext,
|
|
201
|
+
});
|
|
202
|
+
`;
|
|
203
|
+
return {
|
|
204
|
+
routerGenTs
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
export { generateTanstackRouterTypesSourceForEntry };
|
|
@@ -1,339 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import path from "path";
|
|
4
|
-
const JS_OR_TS_EXTS = [
|
|
5
|
-
'.js',
|
|
6
|
-
'.jsx',
|
|
7
|
-
'.ts',
|
|
8
|
-
'.tsx',
|
|
9
|
-
'.mjs',
|
|
10
|
-
'.mts',
|
|
11
|
-
'.cjs',
|
|
12
|
-
'.cts'
|
|
13
|
-
];
|
|
14
|
-
function toTanstackPath(pathname) {
|
|
15
|
-
return pathname.split('/').map((segment)=>{
|
|
16
|
-
if (!segment) return segment;
|
|
17
|
-
if ('*' === segment) return '$';
|
|
18
|
-
if (segment.startsWith(':')) {
|
|
19
|
-
const name = segment.slice(1);
|
|
20
|
-
if (name.endsWith('?')) return `{-$${name.slice(0, -1)}}`;
|
|
21
|
-
return `$${name}`;
|
|
22
|
-
}
|
|
23
|
-
return segment;
|
|
24
|
-
}).join('/');
|
|
25
|
-
}
|
|
26
|
-
async function resolveFileNoExt(inputNoExtPath) {
|
|
27
|
-
const file = findExists(JS_OR_TS_EXTS.map((ext)=>`${inputNoExtPath}${ext}`));
|
|
28
|
-
return file ? getPathWithoutExt(file) : null;
|
|
29
|
-
}
|
|
30
|
-
function quote(str) {
|
|
31
|
-
return JSON.stringify(str);
|
|
32
|
-
}
|
|
33
|
-
function normalizeRelativeImport(p) {
|
|
34
|
-
const normalized = formatImportPath(slash(p));
|
|
35
|
-
if (normalized.startsWith('.')) return normalized;
|
|
36
|
-
return `./${normalized}`;
|
|
37
|
-
}
|
|
38
|
-
function pickModernLoaderModule(route) {
|
|
39
|
-
const loaderPath = route.data || route.loader;
|
|
40
|
-
if (!loaderPath || 'string' != typeof loaderPath) return null;
|
|
41
|
-
const inline = Boolean(route.data);
|
|
42
|
-
return {
|
|
43
|
-
loaderPath,
|
|
44
|
-
inline
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
function pickRouteSearchContractModules(route) {
|
|
48
|
-
const validateSearchPath = route.validateSearch;
|
|
49
|
-
const loaderDepsPath = route.loaderDeps;
|
|
50
|
-
return {
|
|
51
|
-
validateSearchPath: 'string' == typeof validateSearchPath ? validateSearchPath : null,
|
|
52
|
-
loaderDepsPath: 'string' == typeof loaderDepsPath ? loaderDepsPath : null
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
function isPathlessLayout(route) {
|
|
56
|
-
return 'nested' === route.type && 'boolean' != typeof route.index && void 0 === route.path;
|
|
57
|
-
}
|
|
58
|
-
function isIndexRoute(route) {
|
|
59
|
-
return 'nested' === route.type && Boolean(route.index);
|
|
60
|
-
}
|
|
61
|
-
function createRouteStaticDataSnippet(opts) {
|
|
62
|
-
const staticDataLines = [];
|
|
63
|
-
if (opts.modernRouteId) staticDataLines.push(`modernRouteId: ${quote(opts.modernRouteId)},`);
|
|
64
|
-
if (opts.loaderName) staticDataLines.push(`modernRouteLoader: ${opts.loaderName},`);
|
|
65
|
-
if (opts.actionName) staticDataLines.push(`modernRouteAction: ${opts.actionName},`);
|
|
66
|
-
if (!staticDataLines.length) return null;
|
|
67
|
-
return `staticData: createRouteStaticData({\n ${staticDataLines.join('\n ')}\n }),`;
|
|
68
|
-
}
|
|
69
|
-
const LOCALE_PARAM_SEGMENTS = new Set([
|
|
70
|
-
':lang',
|
|
71
|
-
':locale',
|
|
72
|
-
':language',
|
|
73
|
-
'$lang',
|
|
74
|
-
'$locale',
|
|
75
|
-
'$language'
|
|
76
|
-
]);
|
|
77
|
-
function paramsTypeForCanonicalPath(canonicalPath) {
|
|
78
|
-
const fields = [];
|
|
79
|
-
for (const segment of canonicalPath.split('/'))if (segment) {
|
|
80
|
-
if ('*' === segment || '$' === segment) {
|
|
81
|
-
fields.push("'_splat'?: string");
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
if (segment.startsWith('{-$') && segment.endsWith('}')) {
|
|
85
|
-
fields.push(`${JSON.stringify(segment.slice(3, -1))}?: string`);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
if (segment.startsWith('$')) {
|
|
89
|
-
fields.push(`${JSON.stringify(segment.slice(1))}: string`);
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (segment.startsWith(':')) {
|
|
93
|
-
const optional = segment.endsWith('?');
|
|
94
|
-
const name = segment.slice(1, optional ? void 0 : segment.length);
|
|
95
|
-
fields.push(`${JSON.stringify(optional ? name.slice(0, -1) : name)}${optional ? '?' : ''}: string`);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return fields.length > 0 ? `{ ${fields.join('; ')} }` : 'Record<string, never>';
|
|
99
|
-
}
|
|
100
|
-
function collectCanonicalRoutesForEntry(routes, options = {}) {
|
|
101
|
-
const { localeParamHeuristic = true } = options;
|
|
102
|
-
const canonicalParams = new Map();
|
|
103
|
-
let hasI18nSurface = false;
|
|
104
|
-
const normalizeJoined = (joined)=>{
|
|
105
|
-
const collapsed = joined.replace(/\/+/g, '/');
|
|
106
|
-
const withLeading = collapsed.startsWith('/') ? collapsed : `/${collapsed}`;
|
|
107
|
-
return withLeading.length > 1 ? withLeading.replace(/\/+$/, '') : withLeading;
|
|
108
|
-
};
|
|
109
|
-
const record = (canonicalPath)=>{
|
|
110
|
-
const normalized = normalizeJoined(canonicalPath || '/');
|
|
111
|
-
const key = toTanstackPath(normalized);
|
|
112
|
-
if (!canonicalParams.has(key)) canonicalParams.set(key, paramsTypeForCanonicalPath(normalized));
|
|
113
|
-
};
|
|
114
|
-
const visit = (route, parentPath)=>{
|
|
115
|
-
let currentPath = parentPath;
|
|
116
|
-
if ('string' == typeof route.modernCanonicalPath) {
|
|
117
|
-
hasI18nSurface = true;
|
|
118
|
-
currentPath = normalizeJoined(route.modernCanonicalPath);
|
|
119
|
-
} else if ('string' == typeof route.path && route.path.length > 0) {
|
|
120
|
-
const segments = route.path.replace(/\[(.+?)\]/g, ':$1').split('/').filter(Boolean);
|
|
121
|
-
if (localeParamHeuristic && '' === parentPath && LOCALE_PARAM_SEGMENTS.has(segments[0])) {
|
|
122
|
-
hasI18nSurface = true;
|
|
123
|
-
segments.shift();
|
|
124
|
-
}
|
|
125
|
-
currentPath = segments.length ? normalizeJoined(`${parentPath}/${segments.join('/')}`) : parentPath;
|
|
126
|
-
}
|
|
127
|
-
const children = route.children;
|
|
128
|
-
if (children && children.length > 0) {
|
|
129
|
-
for (const child of children)visit(child, currentPath);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
record(currentPath || '/');
|
|
133
|
-
};
|
|
134
|
-
const rootModern = routes.find((route)=>route.isRoot);
|
|
135
|
-
const topLevel = rootModern ? rootModern.children ?? [] : routes;
|
|
136
|
-
for (const route of topLevel)visit(route, '');
|
|
137
|
-
if (!hasI18nSurface || 0 === canonicalParams.size) return null;
|
|
138
|
-
return Object.fromEntries([
|
|
139
|
-
...canonicalParams.entries()
|
|
140
|
-
].sort(([a], [b])=>a.localeCompare(b)));
|
|
141
|
-
}
|
|
142
|
-
async function generateTanstackRouterTypesSourceForEntry(opts) {
|
|
143
|
-
const { appContext, entryName, generatedDirName = 'modern-tanstack', routes } = opts;
|
|
144
|
-
const outDir = path.join(appContext.srcDirectory, generatedDirName, entryName);
|
|
145
|
-
const rootModern = routes.find((r)=>r && 'nested' === r.type && r.isRoot);
|
|
146
|
-
const topLevel = rootModern ? rootModern.children || [] : routes;
|
|
147
|
-
const imports = [];
|
|
148
|
-
const statements = [];
|
|
149
|
-
const loaderImportMap = new Map();
|
|
150
|
-
const componentImportMap = new Map();
|
|
151
|
-
const searchContractImportMap = new Map();
|
|
152
|
-
const usedRouteVarNames = new Set();
|
|
153
|
-
let loaderIndex = 0;
|
|
154
|
-
let componentIndex = 0;
|
|
155
|
-
let validateSearchIndex = 0;
|
|
156
|
-
let loaderDepsIndex = 0;
|
|
157
|
-
let routeIndex = 0;
|
|
158
|
-
const getImportNameForComponent = (componentPath)=>{
|
|
159
|
-
if ('string' != typeof componentPath || 0 === componentPath.length) return Promise.resolve(null);
|
|
160
|
-
let pendingImportName = componentImportMap.get(componentPath);
|
|
161
|
-
if (!pendingImportName) {
|
|
162
|
-
pendingImportName = (async ()=>{
|
|
163
|
-
const resolvedNoExt = await resolveRouteModuleNoExt(componentPath);
|
|
164
|
-
if (!resolvedNoExt) return null;
|
|
165
|
-
const relImport = normalizeRelativeImport(path.relative(outDir, resolvedNoExt));
|
|
166
|
-
const componentName = `component_${componentIndex++}`;
|
|
167
|
-
imports.push(`import ${componentName} from ${quote(relImport)};`);
|
|
168
|
-
return componentName;
|
|
169
|
-
})();
|
|
170
|
-
componentImportMap.set(componentPath, pendingImportName);
|
|
171
|
-
}
|
|
172
|
-
return pendingImportName;
|
|
173
|
-
};
|
|
174
|
-
const resolveRouteModuleNoExt = async (aliasedNoExtPath)=>{
|
|
175
|
-
const prefix = `${appContext.internalSrcAlias}/`;
|
|
176
|
-
let absNoExt;
|
|
177
|
-
if (aliasedNoExtPath.startsWith(prefix)) {
|
|
178
|
-
const rel = aliasedNoExtPath.slice(prefix.length);
|
|
179
|
-
absNoExt = path.join(appContext.srcDirectory, rel);
|
|
180
|
-
} else absNoExt = path.isAbsolute(aliasedNoExtPath) ? aliasedNoExtPath : path.join(appContext.srcDirectory, aliasedNoExtPath);
|
|
181
|
-
return resolveFileNoExt(absNoExt);
|
|
182
|
-
};
|
|
183
|
-
const getImportNamesForLoader = async (aliasedNoExtPath, inline, hasAction)=>{
|
|
184
|
-
const key = `${inline ? 'inline' : 'default'}:${hasAction ? 'action' : 'loader'}:${aliasedNoExtPath}`;
|
|
185
|
-
const existing = loaderImportMap.get(key);
|
|
186
|
-
if (existing) return {
|
|
187
|
-
loaderName: existing,
|
|
188
|
-
actionName: hasAction ? existing.replace(/^loader_/, 'action_') : null
|
|
189
|
-
};
|
|
190
|
-
const resolvedNoExt = await resolveRouteModuleNoExt(aliasedNoExtPath);
|
|
191
|
-
if (!resolvedNoExt) return null;
|
|
192
|
-
const relImport = normalizeRelativeImport(path.relative(outDir, resolvedNoExt));
|
|
193
|
-
const importName = `loader_${loaderIndex++}`;
|
|
194
|
-
const actionName = hasAction ? importName.replace(/^loader_/, 'action_') : null;
|
|
195
|
-
if (inline) {
|
|
196
|
-
const specifiers = [
|
|
197
|
-
`loader as ${importName}`
|
|
198
|
-
];
|
|
199
|
-
if (actionName) specifiers.push(`action as ${actionName}`);
|
|
200
|
-
imports.push(`import { ${specifiers.join(', ')} } from ${quote(relImport)};`);
|
|
201
|
-
} else imports.push(`import ${importName} from ${quote(relImport)};`);
|
|
202
|
-
loaderImportMap.set(key, importName);
|
|
203
|
-
return {
|
|
204
|
-
loaderName: importName,
|
|
205
|
-
actionName
|
|
206
|
-
};
|
|
207
|
-
};
|
|
208
|
-
const getImportNameForSearchContract = async (aliasedNoExtPath, exportName)=>{
|
|
209
|
-
const key = `${exportName}:${aliasedNoExtPath}`;
|
|
210
|
-
const existing = searchContractImportMap.get(key);
|
|
211
|
-
if (existing) return existing;
|
|
212
|
-
const resolvedNoExt = await resolveRouteModuleNoExt(aliasedNoExtPath);
|
|
213
|
-
if (!resolvedNoExt) return null;
|
|
214
|
-
const relImport = normalizeRelativeImport(path.relative(outDir, resolvedNoExt));
|
|
215
|
-
const importName = 'validateSearch' === exportName ? `validateSearch_${validateSearchIndex++}` : `loaderDeps_${loaderDepsIndex++}`;
|
|
216
|
-
imports.push(`import { ${exportName} as ${importName} } from ${quote(relImport)};`);
|
|
217
|
-
searchContractImportMap.set(key, importName);
|
|
218
|
-
return importName;
|
|
219
|
-
};
|
|
220
|
-
const reserveRouteVarName = (preferred)=>{
|
|
221
|
-
let candidate = preferred;
|
|
222
|
-
let suffix = 1;
|
|
223
|
-
while(usedRouteVarNames.has(candidate))candidate = `${preferred}_${suffix++}`;
|
|
224
|
-
usedRouteVarNames.add(candidate);
|
|
225
|
-
return candidate;
|
|
226
|
-
};
|
|
227
|
-
const createRouteVarName = (route)=>{
|
|
228
|
-
const id = route.id;
|
|
229
|
-
const base = id ? makeLegalIdentifier(id) : `r_${routeIndex++}`;
|
|
230
|
-
return reserveRouteVarName(`route_${base}`);
|
|
231
|
-
};
|
|
232
|
-
const buildRoute = async (opts)=>{
|
|
233
|
-
const { parentVar, route } = opts;
|
|
234
|
-
const varName = createRouteVarName(route);
|
|
235
|
-
const loaderInfo = pickModernLoaderModule(route);
|
|
236
|
-
const routeAction = route.action;
|
|
237
|
-
const loaderImports = loaderInfo ? await getImportNamesForLoader(loaderInfo.loaderPath, loaderInfo.inline, Boolean(loaderInfo.inline && routeAction === loaderInfo.loaderPath)) : null;
|
|
238
|
-
const loaderName = loaderImports?.loaderName || null;
|
|
239
|
-
const actionName = loaderImports?.actionName || null;
|
|
240
|
-
const searchContractInfo = pickRouteSearchContractModules(route);
|
|
241
|
-
const validateSearchName = searchContractInfo.validateSearchPath ? await getImportNameForSearchContract(searchContractInfo.validateSearchPath, 'validateSearch') : null;
|
|
242
|
-
const loaderDepsName = searchContractInfo.loaderDepsPath ? await getImportNameForSearchContract(searchContractInfo.loaderDepsPath, 'loaderDeps') : null;
|
|
243
|
-
const rawPath = route.path;
|
|
244
|
-
const hasSplat = 'string' == typeof rawPath && rawPath.includes('*');
|
|
245
|
-
const routeOpts = [
|
|
246
|
-
`getParentRoute: () => ${parentVar},`
|
|
247
|
-
];
|
|
248
|
-
const componentName = await getImportNameForComponent(route._component);
|
|
249
|
-
if (componentName) routeOpts.push(`component: ${componentName},`);
|
|
250
|
-
if (isPathlessLayout(route)) {
|
|
251
|
-
const id = route.id;
|
|
252
|
-
routeOpts.push(`id: ${quote(id || 'pathless')},`);
|
|
253
|
-
} else {
|
|
254
|
-
const p = isIndexRoute(route) ? '/' : toTanstackPath(rawPath || '');
|
|
255
|
-
routeOpts.push(`path: ${quote(p)},`);
|
|
256
|
-
}
|
|
257
|
-
if (loaderName) routeOpts.push(`loader: modernLoaderToTanstack({ hasSplat: ${hasSplat} }, ${loaderName}),`);
|
|
258
|
-
if (validateSearchName) routeOpts.push(`validateSearch: ${validateSearchName},`);
|
|
259
|
-
if (loaderDepsName) routeOpts.push(`loaderDeps: ${loaderDepsName},`);
|
|
260
|
-
const staticDataSnippet = createRouteStaticDataSnippet({
|
|
261
|
-
modernRouteId: route.id,
|
|
262
|
-
loaderName,
|
|
263
|
-
actionName
|
|
264
|
-
});
|
|
265
|
-
if (staticDataSnippet) routeOpts.push(staticDataSnippet);
|
|
266
|
-
const children = route.children;
|
|
267
|
-
const hasChildren = Boolean(children && children.length > 0);
|
|
268
|
-
const routeCtorVarName = hasChildren ? reserveRouteVarName(`${varName}__base`) : varName;
|
|
269
|
-
statements.push(`const ${routeCtorVarName} = createRoute({\n ${routeOpts.join('\n ')}\n});`);
|
|
270
|
-
if (children && children.length > 0) {
|
|
271
|
-
const childVars = await Promise.all(children.map((child)=>buildRoute({
|
|
272
|
-
parentVar: routeCtorVarName,
|
|
273
|
-
route: child
|
|
274
|
-
})));
|
|
275
|
-
statements.push(`const ${varName} = ${routeCtorVarName}.addChildren([${childVars.join(', ')}]);`);
|
|
276
|
-
}
|
|
277
|
-
return varName;
|
|
278
|
-
};
|
|
279
|
-
const rootLoaderInfo = rootModern ? pickModernLoaderModule(rootModern) : null;
|
|
280
|
-
const rootAction = rootModern?.action;
|
|
281
|
-
const rootLoaderImports = rootLoaderInfo?.loaderPath ? await getImportNamesForLoader(rootLoaderInfo.loaderPath, rootLoaderInfo.inline, Boolean(rootLoaderInfo.inline && rootAction === rootLoaderInfo.loaderPath)) : null;
|
|
282
|
-
const rootLoaderName = rootLoaderImports?.loaderName || null;
|
|
283
|
-
const rootActionName = rootLoaderImports?.actionName || null;
|
|
284
|
-
const rootSearchContractInfo = rootModern ? pickRouteSearchContractModules(rootModern) : null;
|
|
285
|
-
const rootValidateSearchName = rootSearchContractInfo?.validateSearchPath ? await getImportNameForSearchContract(rootSearchContractInfo.validateSearchPath, 'validateSearch') : null;
|
|
286
|
-
const rootLoaderDepsName = rootSearchContractInfo?.loaderDepsPath ? await getImportNameForSearchContract(rootSearchContractInfo.loaderDepsPath, 'loaderDeps') : null;
|
|
287
|
-
const topLevelVars = await Promise.all(topLevel.map((route)=>buildRoute({
|
|
288
|
-
parentVar: 'rootRoute',
|
|
289
|
-
route
|
|
290
|
-
})));
|
|
291
|
-
const rootOpts = [];
|
|
292
|
-
const rootComponentName = await getImportNameForComponent(rootModern?._component);
|
|
293
|
-
if (rootComponentName) rootOpts.push(`component: ${rootComponentName},`);
|
|
294
|
-
if (rootLoaderName) rootOpts.push(`loader: modernLoaderToTanstack({ hasSplat: false }, ${rootLoaderName}),`);
|
|
295
|
-
if (rootValidateSearchName) rootOpts.push(`validateSearch: ${rootValidateSearchName},`);
|
|
296
|
-
if (rootLoaderDepsName) rootOpts.push(`loaderDeps: ${rootLoaderDepsName},`);
|
|
297
|
-
const routerGenTs = `/* eslint-disable */
|
|
298
|
-
// This file is auto-generated by Modern.js. Do not edit manually.
|
|
299
|
-
|
|
300
|
-
import {
|
|
301
|
-
createMemoryHistory,
|
|
302
|
-
createRootRouteWithContext,
|
|
303
|
-
createRoute,
|
|
304
|
-
createRouter,
|
|
305
|
-
createRouteStaticData,
|
|
306
|
-
type ModernRouterContext,
|
|
307
|
-
modernLoaderToTanstack,
|
|
308
|
-
modernTanstackRouterFastDefaults,
|
|
309
|
-
} from '@modern-js/plugin-tanstack/runtime';
|
|
310
|
-
|
|
311
|
-
${imports.join('\n')}
|
|
312
|
-
|
|
313
|
-
export const rootRoute = createRootRouteWithContext<ModernRouterContext>()({
|
|
314
|
-
${rootOpts.join('\n ')}
|
|
315
|
-
${createRouteStaticDataSnippet({
|
|
316
|
-
modernRouteId: rootModern?.id,
|
|
317
|
-
loaderName: rootLoaderName,
|
|
318
|
-
actionName: rootActionName
|
|
319
|
-
}) || ''}
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
${statements.join('\n\n')}
|
|
323
|
-
|
|
324
|
-
export const routeTree = rootRoute.addChildren([${topLevelVars.join(', ')}]);
|
|
325
|
-
|
|
326
|
-
export const router = createRouter({
|
|
327
|
-
...modernTanstackRouterFastDefaults,
|
|
328
|
-
routeTree,
|
|
329
|
-
history: createMemoryHistory({
|
|
330
|
-
initialEntries: ['/'],
|
|
331
|
-
}),
|
|
332
|
-
context: {} as ModernRouterContext,
|
|
333
|
-
});
|
|
334
|
-
`;
|
|
335
|
-
return {
|
|
336
|
-
routerGenTs
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
export { collectCanonicalRoutesForEntry, generateTanstackRouterTypesSourceForEntry };
|
|
1
|
+
export { collectCanonicalRoutesForEntry } from "./tanstackTypes/canonicalRoutes.mjs";
|
|
2
|
+
export { generateTanstackRouterTypesSourceForEntry } from "./tanstackTypes/source.mjs";
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const BLOCKING_SUBSCRIBE_SYMBOL = Symbol.for('@modern-js/plugin-tanstack:blocking-subscribe');
|
|
2
|
+
const BLOCKING_STATE_SYMBOL = Symbol.for('@modern-js/plugin-tanstack:blocking-state');
|
|
3
|
+
function wrapRouterSubscribeWithBlockState(router, getBlockNavState) {
|
|
4
|
+
if (!router || 'object' != typeof router) return;
|
|
5
|
+
const target = router;
|
|
6
|
+
target[BLOCKING_STATE_SYMBOL] = getBlockNavState;
|
|
7
|
+
if (target[BLOCKING_SUBSCRIBE_SYMBOL] || 'function' != typeof target.subscribe) return;
|
|
8
|
+
const originSubscribe = target.subscribe.bind(target);
|
|
9
|
+
target.subscribe = (eventType, listener)=>{
|
|
10
|
+
const wrappedListener = (...args)=>{
|
|
11
|
+
const blockRoute = target[BLOCKING_STATE_SYMBOL]?.() || false;
|
|
12
|
+
if (blockRoute) return;
|
|
13
|
+
return listener(...args);
|
|
14
|
+
};
|
|
15
|
+
return originSubscribe(eventType, wrappedListener);
|
|
16
|
+
};
|
|
17
|
+
target[BLOCKING_SUBSCRIBE_SYMBOL] = true;
|
|
18
|
+
}
|
|
19
|
+
export { wrapRouterSubscribeWithBlockState };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function normalizeBase(b) {
|
|
2
|
+
if (b.length > 1 && b.endsWith('/')) return b.slice(0, -1);
|
|
3
|
+
return b || '/';
|
|
4
|
+
}
|
|
5
|
+
function isSegmentPrefix(pathname, base) {
|
|
6
|
+
const b = normalizeBase(base);
|
|
7
|
+
const p = pathname || '/';
|
|
8
|
+
return p === b || p.startsWith(`${b}/`);
|
|
9
|
+
}
|
|
10
|
+
export { isSegmentPrefix };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { RouterProvider } from "@tanstack/react-router";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { withModernRouteMatchContext } from "./outlet.mjs";
|
|
4
|
+
import { pickRouteModuleComponent } from "./routeTree.mjs";
|
|
5
|
+
const routerHydrationRecords = new WeakMap();
|
|
6
|
+
const routeModulesKey = '_routeModules';
|
|
7
|
+
function getCachedRouteModule(routeId) {
|
|
8
|
+
if ("u" < typeof window) return;
|
|
9
|
+
return window[routeModulesKey]?.[routeId];
|
|
10
|
+
}
|
|
11
|
+
function preloadHydratedRouteComponents(router) {
|
|
12
|
+
const preloadableRouter = router;
|
|
13
|
+
const routesById = preloadableRouter.routesById || {};
|
|
14
|
+
const matches = preloadableRouter.stores.matches.get();
|
|
15
|
+
return Promise.all(matches.map((match)=>{
|
|
16
|
+
if (void 0 === match.routeId || '' === match.routeId) return;
|
|
17
|
+
const route = routesById[match.routeId];
|
|
18
|
+
const component = route?.options?.component;
|
|
19
|
+
const preload = component?.load || component?.preload;
|
|
20
|
+
if ('function' != typeof preload) return;
|
|
21
|
+
return Promise.resolve(preload.call(component)).then((routeModule)=>{
|
|
22
|
+
const modernRouteId = route?.options?.staticData?.modernRouteId;
|
|
23
|
+
const cachedRouteModule = 'string' == typeof modernRouteId && '' !== modernRouteId ? getCachedRouteModule(modernRouteId) : void 0;
|
|
24
|
+
const resolvedComponent = pickRouteModuleComponent(cachedRouteModule ?? routeModule);
|
|
25
|
+
if (void 0 !== resolvedComponent && 'string' == typeof modernRouteId && '' !== modernRouteId) route.options.component = withModernRouteMatchContext(resolvedComponent, modernRouteId);
|
|
26
|
+
});
|
|
27
|
+
})).then(()=>void 0);
|
|
28
|
+
}
|
|
29
|
+
function hydrateTanstackRouter(router) {
|
|
30
|
+
return import("@tanstack/react-router/ssr/client").then(({ hydrate })=>hydrate(router));
|
|
31
|
+
}
|
|
32
|
+
function getTanstackSsrHydrationRecord(router) {
|
|
33
|
+
const existingHydrationRecord = routerHydrationRecords.get(router);
|
|
34
|
+
if (void 0 !== existingHydrationRecord) return existingHydrationRecord;
|
|
35
|
+
const hydrationRecord = {
|
|
36
|
+
promise: Promise.resolve(),
|
|
37
|
+
status: 'pending'
|
|
38
|
+
};
|
|
39
|
+
routerHydrationRecords.set(router, hydrationRecord);
|
|
40
|
+
try {
|
|
41
|
+
hydrationRecord.promise = hydrateTanstackRouter(router).then((value)=>preloadHydratedRouteComponents(router).then(()=>value)).then((value)=>{
|
|
42
|
+
hydrationRecord.status = 'fulfilled';
|
|
43
|
+
return value;
|
|
44
|
+
}, (error)=>{
|
|
45
|
+
hydrationRecord.status = 'rejected';
|
|
46
|
+
hydrationRecord.error = error;
|
|
47
|
+
throw error;
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
hydrationRecord.status = 'rejected';
|
|
51
|
+
hydrationRecord.error = error;
|
|
52
|
+
hydrationRecord.promise = Promise.reject(error);
|
|
53
|
+
hydrationRecord.promise.catch(()=>{});
|
|
54
|
+
}
|
|
55
|
+
return hydrationRecord;
|
|
56
|
+
}
|
|
57
|
+
function getTanstackSsrHydrationPromise(router) {
|
|
58
|
+
return getTanstackSsrHydrationRecord(router).promise;
|
|
59
|
+
}
|
|
60
|
+
function hasTanstackSsrHydrationRecord(router) {
|
|
61
|
+
return routerHydrationRecords.has(router);
|
|
62
|
+
}
|
|
63
|
+
function ModernRouterClient({ router }) {
|
|
64
|
+
const hydrationRecord = getTanstackSsrHydrationRecord(router);
|
|
65
|
+
if ('pending' === hydrationRecord.status) throw hydrationRecord.promise;
|
|
66
|
+
if ('rejected' === hydrationRecord.status) throw hydrationRecord.error;
|
|
67
|
+
return createElement(RouterProvider, {
|
|
68
|
+
router
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
export { ModernRouterClient, getTanstackSsrHydrationPromise, hasTanstackSsrHydrationRecord, hydrateTanstackRouter };
|