@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
package/src/cli/index.ts
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import type {
|
|
3
|
+
AppNormalizedConfig,
|
|
4
|
+
AppTools,
|
|
5
|
+
AppToolsContext,
|
|
6
|
+
CliPlugin,
|
|
7
|
+
} from '@modern-js/app-tools';
|
|
8
|
+
import type { CLIPluginAPI } from '@modern-js/plugin';
|
|
9
|
+
import type {
|
|
10
|
+
Entrypoint,
|
|
11
|
+
NestedRouteForCli,
|
|
12
|
+
PageRoute,
|
|
13
|
+
ServerRoute,
|
|
14
|
+
} from '@modern-js/types';
|
|
15
|
+
import {
|
|
16
|
+
filterRoutesForServer,
|
|
17
|
+
fs,
|
|
18
|
+
NESTED_ROUTE_SPEC_FILE,
|
|
19
|
+
} from '@modern-js/utils';
|
|
20
|
+
import {
|
|
21
|
+
generateTanstackRouterTypesSourceForEntry,
|
|
22
|
+
isTanstackRouterFrameworkEnabled,
|
|
23
|
+
} from './tanstackTypes';
|
|
24
|
+
|
|
25
|
+
export {
|
|
26
|
+
generateTanstackRouterTypesSourceForEntry,
|
|
27
|
+
isTanstackRouterFrameworkEnabled,
|
|
28
|
+
} from './tanstackTypes';
|
|
29
|
+
|
|
30
|
+
const DEFAULT_ROUTES_DIR = 'routes';
|
|
31
|
+
const DEFAULT_GENERATED_DIR_NAME = 'modern-tanstack';
|
|
32
|
+
const ENTRYPOINTS_KEY = '@modern-js/plugin-tanstack';
|
|
33
|
+
|
|
34
|
+
export type TanstackRouterPluginOptions = {
|
|
35
|
+
routesDir?: string;
|
|
36
|
+
generatedDirName?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type RuntimeRouterCliHelpers = {
|
|
40
|
+
getEntrypointRoutesDir: (entrypoint: Entrypoint) => string | null;
|
|
41
|
+
handleFileChange: (
|
|
42
|
+
api: CLIPluginAPI<AppTools>,
|
|
43
|
+
event: unknown,
|
|
44
|
+
options?: {
|
|
45
|
+
includeEntry?: (entrypoint: Entrypoint) => boolean;
|
|
46
|
+
regenerate?: (params: {
|
|
47
|
+
api: CLIPluginAPI<AppTools>;
|
|
48
|
+
appContext: ReturnType<CLIPluginAPI<AppTools>['getAppContext']>;
|
|
49
|
+
resolvedConfig: AppNormalizedConfig;
|
|
50
|
+
entrypoints: Entrypoint[];
|
|
51
|
+
}) => Promise<void>;
|
|
52
|
+
entrypointsKey?: string;
|
|
53
|
+
},
|
|
54
|
+
) => Promise<void>;
|
|
55
|
+
handleGeneratorEntryCode: (
|
|
56
|
+
api: CLIPluginAPI<AppTools>,
|
|
57
|
+
entrypoints: Entrypoint[],
|
|
58
|
+
options?: {
|
|
59
|
+
entrypointsKey?: string;
|
|
60
|
+
generateCodeOptions?: {
|
|
61
|
+
enableTanstackTypes?: boolean;
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
) => Promise<Record<string, (NestedRouteForCli | PageRoute)[]>>;
|
|
65
|
+
handleModifyEntrypoints: (
|
|
66
|
+
entrypoints: Entrypoint[],
|
|
67
|
+
routesDir?: string,
|
|
68
|
+
options?: {
|
|
69
|
+
routesOwner?: string;
|
|
70
|
+
},
|
|
71
|
+
) => Promise<Entrypoint[]>;
|
|
72
|
+
isRouteEntry: (dir: string, routesDir?: string) => string | false;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
let runtimeRouterCli: RuntimeRouterCliHelpers | undefined;
|
|
76
|
+
|
|
77
|
+
function getRuntimeRouterCli(): RuntimeRouterCliHelpers {
|
|
78
|
+
if (runtimeRouterCli) {
|
|
79
|
+
return runtimeRouterCli;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const cli =
|
|
83
|
+
require('@modern-js/runtime/cli') as Partial<RuntimeRouterCliHelpers>;
|
|
84
|
+
if (cli.handleGeneratorEntryCode && cli.getEntrypointRoutesDir) {
|
|
85
|
+
runtimeRouterCli = cli as RuntimeRouterCliHelpers;
|
|
86
|
+
return runtimeRouterCli;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
throw new Error(
|
|
90
|
+
'@modern-js/plugin-tanstack requires @modern-js/runtime/cli router helper exports.',
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function writeFileIfChanged(filePath: string, content: string) {
|
|
95
|
+
try {
|
|
96
|
+
const previous = (await fs.pathExists(filePath))
|
|
97
|
+
? await fs.readFile(filePath, 'utf-8')
|
|
98
|
+
: null;
|
|
99
|
+
if (previous === content) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
// Fall through and rewrite the generated file.
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
await fs.outputFile(filePath, content, 'utf-8');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function createRegisterDtsContent(opts: {
|
|
110
|
+
entries: string[];
|
|
111
|
+
runtimeModule: string;
|
|
112
|
+
}) {
|
|
113
|
+
const importStatements = opts.entries
|
|
114
|
+
.map(
|
|
115
|
+
(entryName, index) =>
|
|
116
|
+
`import type { router as router${index} } from './${entryName}/router.gen';`,
|
|
117
|
+
)
|
|
118
|
+
.join('\n');
|
|
119
|
+
const routerUnionType = opts.entries
|
|
120
|
+
.map((_, index) => `typeof router${index}`)
|
|
121
|
+
.join(' | ');
|
|
122
|
+
|
|
123
|
+
return `// This file is auto-generated by Modern.js. Do not edit manually.
|
|
124
|
+
|
|
125
|
+
${importStatements}
|
|
126
|
+
|
|
127
|
+
declare module '${opts.runtimeModule}' {
|
|
128
|
+
interface Register {
|
|
129
|
+
router: ${routerUnionType};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function writeTanstackRegisterFile(opts: {
|
|
136
|
+
entries: string[];
|
|
137
|
+
generatedDirName?: string;
|
|
138
|
+
runtimeModule?: string;
|
|
139
|
+
srcDirectory: string;
|
|
140
|
+
}) {
|
|
141
|
+
const {
|
|
142
|
+
entries,
|
|
143
|
+
generatedDirName = DEFAULT_GENERATED_DIR_NAME,
|
|
144
|
+
runtimeModule = '@modern-js/plugin-tanstack/runtime',
|
|
145
|
+
srcDirectory,
|
|
146
|
+
} = opts;
|
|
147
|
+
|
|
148
|
+
if (entries.length === 0) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const registerDtsPath = path.join(
|
|
153
|
+
srcDirectory,
|
|
154
|
+
generatedDirName,
|
|
155
|
+
'register.gen.d.ts',
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
await writeFileIfChanged(
|
|
159
|
+
registerDtsPath,
|
|
160
|
+
createRegisterDtsContent({ entries, runtimeModule }),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function writeTanstackRouterTypesForEntries(opts: {
|
|
165
|
+
appContext: AppToolsContext;
|
|
166
|
+
generatedDirName?: string;
|
|
167
|
+
routesByEntry: Record<string, (NestedRouteForCli | PageRoute)[]>;
|
|
168
|
+
}) {
|
|
169
|
+
const {
|
|
170
|
+
appContext,
|
|
171
|
+
generatedDirName = DEFAULT_GENERATED_DIR_NAME,
|
|
172
|
+
routesByEntry,
|
|
173
|
+
} = opts;
|
|
174
|
+
|
|
175
|
+
const entryNames = Object.keys(routesByEntry);
|
|
176
|
+
|
|
177
|
+
await Promise.all(
|
|
178
|
+
entryNames.map(async entryName => {
|
|
179
|
+
const { routerGenTs } = await generateTanstackRouterTypesSourceForEntry({
|
|
180
|
+
appContext,
|
|
181
|
+
entryName,
|
|
182
|
+
generatedDirName,
|
|
183
|
+
routes: routesByEntry[entryName],
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
await writeFileIfChanged(
|
|
187
|
+
path.join(
|
|
188
|
+
appContext.srcDirectory,
|
|
189
|
+
generatedDirName,
|
|
190
|
+
entryName,
|
|
191
|
+
'router.gen.ts',
|
|
192
|
+
),
|
|
193
|
+
routerGenTs,
|
|
194
|
+
);
|
|
195
|
+
}),
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
const mainEntryName = appContext.entrypoints?.find(
|
|
199
|
+
entrypoint => entrypoint.isMainEntry,
|
|
200
|
+
)?.entryName;
|
|
201
|
+
const registerEntries = entryNames.sort((a, b) => {
|
|
202
|
+
if (mainEntryName && a === mainEntryName) {
|
|
203
|
+
return -1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (mainEntryName && b === mainEntryName) {
|
|
207
|
+
return 1;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return a.localeCompare(b);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
await writeTanstackRegisterFile({
|
|
214
|
+
entries: registerEntries,
|
|
215
|
+
generatedDirName,
|
|
216
|
+
srcDirectory: appContext.srcDirectory,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function tanstackRouterPlugin(
|
|
221
|
+
options: TanstackRouterPluginOptions = {},
|
|
222
|
+
): CliPlugin<AppTools> {
|
|
223
|
+
const routesDir = options.routesDir || DEFAULT_ROUTES_DIR;
|
|
224
|
+
const generatedDirName =
|
|
225
|
+
options.generatedDirName || DEFAULT_GENERATED_DIR_NAME;
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
name: '@modern-js/plugin-tanstack',
|
|
229
|
+
required: ['@modern-js/runtime'],
|
|
230
|
+
setup: api => {
|
|
231
|
+
const nestedRoutesForServer: Record<string, unknown> = {};
|
|
232
|
+
|
|
233
|
+
const isTanstackEntrypoint = (entrypoint: Entrypoint) => {
|
|
234
|
+
const { getEntrypointRoutesDir } = getRuntimeRouterCli();
|
|
235
|
+
return getEntrypointRoutesDir(entrypoint) === routesDir;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
api._internalRuntimePlugins(({ entrypoint, plugins }) => {
|
|
239
|
+
if (!isTanstackEntrypoint(entrypoint as Entrypoint)) {
|
|
240
|
+
return { entrypoint, plugins };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const { metaName, serverRoutes } = api.getAppContext();
|
|
244
|
+
const serverBase = serverRoutes
|
|
245
|
+
.filter(
|
|
246
|
+
(route: ServerRoute) => route.entryName === entrypoint.entryName,
|
|
247
|
+
)
|
|
248
|
+
.map(route => route.urlPath)
|
|
249
|
+
.sort((a, b) => (a.length - b.length > 0 ? -1 : 1));
|
|
250
|
+
|
|
251
|
+
plugins.push({
|
|
252
|
+
name: 'tanstackRouter',
|
|
253
|
+
path: `@${metaName}/plugin-tanstack/runtime`,
|
|
254
|
+
config: { serverBase },
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
return { entrypoint, plugins };
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
api.checkEntryPoint(({ path: entryPath, entry }) => {
|
|
261
|
+
const { isRouteEntry } = getRuntimeRouterCli();
|
|
262
|
+
return {
|
|
263
|
+
path: entryPath,
|
|
264
|
+
entry: entry || isRouteEntry(entryPath, routesDir),
|
|
265
|
+
};
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
api.config(() => {
|
|
269
|
+
return {
|
|
270
|
+
source: {
|
|
271
|
+
include: [
|
|
272
|
+
/[\\/]node_modules[\\/]@tanstack[\\/]react-router[\\/]/,
|
|
273
|
+
path.resolve(__dirname, '../runtime').replace('cjs', 'esm'),
|
|
274
|
+
],
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
api.modifyEntrypoints(async ({ entrypoints }) => {
|
|
280
|
+
const { handleModifyEntrypoints } = getRuntimeRouterCli();
|
|
281
|
+
return {
|
|
282
|
+
entrypoints: await handleModifyEntrypoints(entrypoints, routesDir, {
|
|
283
|
+
routesOwner: ENTRYPOINTS_KEY,
|
|
284
|
+
}),
|
|
285
|
+
};
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
api.generateEntryCode(async ({ entrypoints }) => {
|
|
289
|
+
const tanstackEntrypoints = entrypoints.filter(isTanstackEntrypoint);
|
|
290
|
+
|
|
291
|
+
if (tanstackEntrypoints.length === 0) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const { handleGeneratorEntryCode } = getRuntimeRouterCli();
|
|
296
|
+
const routesByEntry = await handleGeneratorEntryCode(
|
|
297
|
+
api,
|
|
298
|
+
tanstackEntrypoints,
|
|
299
|
+
{
|
|
300
|
+
entrypointsKey: ENTRYPOINTS_KEY,
|
|
301
|
+
generateCodeOptions: {
|
|
302
|
+
enableTanstackTypes: false,
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
await writeTanstackRouterTypesForEntries({
|
|
308
|
+
appContext: api.getAppContext(),
|
|
309
|
+
generatedDirName,
|
|
310
|
+
routesByEntry,
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
api.onFileChanged(async event => {
|
|
315
|
+
const { handleFileChange } = getRuntimeRouterCli();
|
|
316
|
+
await handleFileChange(api, event, {
|
|
317
|
+
entrypointsKey: ENTRYPOINTS_KEY,
|
|
318
|
+
includeEntry: entrypoint => {
|
|
319
|
+
const { getEntrypointRoutesDir } = getRuntimeRouterCli();
|
|
320
|
+
return getEntrypointRoutesDir(entrypoint) === routesDir;
|
|
321
|
+
},
|
|
322
|
+
regenerate: async ({ api, entrypoints }) => {
|
|
323
|
+
const { handleGeneratorEntryCode } = getRuntimeRouterCli();
|
|
324
|
+
const routesByEntry = await handleGeneratorEntryCode(
|
|
325
|
+
api,
|
|
326
|
+
entrypoints,
|
|
327
|
+
{
|
|
328
|
+
entrypointsKey: ENTRYPOINTS_KEY,
|
|
329
|
+
generateCodeOptions: {
|
|
330
|
+
enableTanstackTypes: false,
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
);
|
|
334
|
+
|
|
335
|
+
await writeTanstackRouterTypesForEntries({
|
|
336
|
+
appContext: api.getAppContext(),
|
|
337
|
+
generatedDirName,
|
|
338
|
+
routesByEntry,
|
|
339
|
+
});
|
|
340
|
+
},
|
|
341
|
+
});
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
api.modifyFileSystemRoutes(async ({ entrypoint, routes }) => {
|
|
345
|
+
if (isTanstackEntrypoint(entrypoint)) {
|
|
346
|
+
nestedRoutesForServer[entrypoint.entryName] = filterRoutesForServer(
|
|
347
|
+
routes as (NestedRouteForCli | PageRoute)[],
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return {
|
|
352
|
+
entrypoint,
|
|
353
|
+
routes,
|
|
354
|
+
};
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
api.onBeforeGenerateRoutes(async ({ entrypoint, code }) => {
|
|
358
|
+
if (isTanstackEntrypoint(entrypoint)) {
|
|
359
|
+
const { distDirectory } = api.getAppContext();
|
|
360
|
+
const nestedRoutesSpecPath = path.resolve(
|
|
361
|
+
distDirectory,
|
|
362
|
+
NESTED_ROUTE_SPEC_FILE,
|
|
363
|
+
);
|
|
364
|
+
const existingNestedRoutes = (await fs.pathExists(
|
|
365
|
+
nestedRoutesSpecPath,
|
|
366
|
+
))
|
|
367
|
+
? ((await fs.readJSON(nestedRoutesSpecPath)) as Record<
|
|
368
|
+
string,
|
|
369
|
+
unknown
|
|
370
|
+
>)
|
|
371
|
+
: {};
|
|
372
|
+
|
|
373
|
+
await fs.outputJSON(nestedRoutesSpecPath, {
|
|
374
|
+
...existingNestedRoutes,
|
|
375
|
+
...nestedRoutesForServer,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return {
|
|
380
|
+
entrypoint,
|
|
381
|
+
code,
|
|
382
|
+
};
|
|
383
|
+
});
|
|
384
|
+
},
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export default tanstackRouterPlugin;
|