@tanstack/start-plugin-core 1.20.3-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/cjs/compilers.cjs +402 -0
- package/dist/cjs/compilers.cjs.map +1 -0
- package/dist/cjs/compilers.d.cts +21 -0
- package/dist/cjs/extractHtmlScripts.cjs +35 -0
- package/dist/cjs/extractHtmlScripts.cjs.map +1 -0
- package/dist/cjs/extractHtmlScripts.d.cts +4 -0
- package/dist/cjs/index.cjs +15 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +7 -0
- package/dist/cjs/nitro/build-nitro.cjs +18 -0
- package/dist/cjs/nitro/build-nitro.cjs.map +1 -0
- package/dist/cjs/nitro/build-nitro.d.cts +2 -0
- package/dist/cjs/nitro/build-sitemap.cjs +54 -0
- package/dist/cjs/nitro/build-sitemap.cjs.map +1 -0
- package/dist/cjs/nitro/build-sitemap.d.cts +9 -0
- package/dist/cjs/nitro/dev-server-plugin.cjs +128 -0
- package/dist/cjs/nitro/dev-server-plugin.cjs.map +1 -0
- package/dist/cjs/nitro/dev-server-plugin.d.cts +5 -0
- package/dist/cjs/nitro/nitro-plugin.cjs +128 -0
- package/dist/cjs/nitro/nitro-plugin.cjs.map +1 -0
- package/dist/cjs/nitro/nitro-plugin.d.cts +3 -0
- package/dist/cjs/plugin.cjs +117 -0
- package/dist/cjs/plugin.cjs.map +1 -0
- package/dist/cjs/plugin.d.cts +2713 -0
- package/dist/cjs/prerender.cjs +171 -0
- package/dist/cjs/prerender.cjs.map +1 -0
- package/dist/cjs/prerender.d.cts +8 -0
- package/dist/cjs/queue.cjs +131 -0
- package/dist/cjs/queue.cjs.map +1 -0
- package/dist/cjs/queue.d.cts +32 -0
- package/dist/cjs/routesManifestPlugin.cjs +165 -0
- package/dist/cjs/routesManifestPlugin.cjs.map +1 -0
- package/dist/cjs/routesManifestPlugin.d.cts +3 -0
- package/dist/cjs/schema.cjs +136 -0
- package/dist/cjs/schema.cjs.map +1 -0
- package/dist/cjs/schema.d.cts +8128 -0
- package/dist/cjs/start-compiler-plugin.cjs +72 -0
- package/dist/cjs/start-compiler-plugin.cjs.map +1 -0
- package/dist/cjs/start-compiler-plugin.d.cts +13 -0
- package/dist/cjs/start-server-routes-plugin/config.d.cts +49 -0
- package/dist/cjs/start-server-routes-plugin/plugin.cjs +608 -0
- package/dist/cjs/start-server-routes-plugin/plugin.cjs.map +1 -0
- package/dist/cjs/start-server-routes-plugin/plugin.d.cts +3 -0
- package/dist/cjs/start-server-routes-plugin/template.cjs +111 -0
- package/dist/cjs/start-server-routes-plugin/template.cjs.map +1 -0
- package/dist/cjs/start-server-routes-plugin/template.d.cts +34 -0
- package/dist/esm/compilers.d.ts +21 -0
- package/dist/esm/compilers.js +384 -0
- package/dist/esm/compilers.js.map +1 -0
- package/dist/esm/extractHtmlScripts.d.ts +4 -0
- package/dist/esm/extractHtmlScripts.js +18 -0
- package/dist/esm/extractHtmlScripts.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +15 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/nitro/build-nitro.d.ts +2 -0
- package/dist/esm/nitro/build-nitro.js +18 -0
- package/dist/esm/nitro/build-nitro.js.map +1 -0
- package/dist/esm/nitro/build-sitemap.d.ts +9 -0
- package/dist/esm/nitro/build-sitemap.js +54 -0
- package/dist/esm/nitro/build-sitemap.js.map +1 -0
- package/dist/esm/nitro/dev-server-plugin.d.ts +5 -0
- package/dist/esm/nitro/dev-server-plugin.js +128 -0
- package/dist/esm/nitro/dev-server-plugin.js.map +1 -0
- package/dist/esm/nitro/nitro-plugin.d.ts +3 -0
- package/dist/esm/nitro/nitro-plugin.js +128 -0
- package/dist/esm/nitro/nitro-plugin.js.map +1 -0
- package/dist/esm/plugin.d.ts +2713 -0
- package/dist/esm/plugin.js +117 -0
- package/dist/esm/plugin.js.map +1 -0
- package/dist/esm/prerender.d.ts +8 -0
- package/dist/esm/prerender.js +171 -0
- package/dist/esm/prerender.js.map +1 -0
- package/dist/esm/queue.d.ts +32 -0
- package/dist/esm/queue.js +131 -0
- package/dist/esm/queue.js.map +1 -0
- package/dist/esm/routesManifestPlugin.d.ts +3 -0
- package/dist/esm/routesManifestPlugin.js +165 -0
- package/dist/esm/routesManifestPlugin.js.map +1 -0
- package/dist/esm/schema.d.ts +8128 -0
- package/dist/esm/schema.js +136 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/start-compiler-plugin.d.ts +13 -0
- package/dist/esm/start-compiler-plugin.js +72 -0
- package/dist/esm/start-compiler-plugin.js.map +1 -0
- package/dist/esm/start-server-routes-plugin/config.d.ts +49 -0
- package/dist/esm/start-server-routes-plugin/plugin.d.ts +3 -0
- package/dist/esm/start-server-routes-plugin/plugin.js +608 -0
- package/dist/esm/start-server-routes-plugin/plugin.js.map +1 -0
- package/dist/esm/start-server-routes-plugin/template.d.ts +34 -0
- package/dist/esm/start-server-routes-plugin/template.js +111 -0
- package/dist/esm/start-server-routes-plugin/template.js.map +1 -0
- package/package.json +72 -0
- package/src/compilers.ts +759 -0
- package/src/extractHtmlScripts.ts +19 -0
- package/src/index.ts +15 -0
- package/src/nitro/build-nitro.ts +27 -0
- package/src/nitro/build-sitemap.ts +79 -0
- package/src/nitro/dev-server-plugin.ts +159 -0
- package/src/nitro/nitro-plugin.ts +161 -0
- package/src/plugin.ts +145 -0
- package/src/prerender.ts +245 -0
- package/src/queue.ts +153 -0
- package/src/routesManifestPlugin.ts +216 -0
- package/src/schema.ts +193 -0
- package/src/start-compiler-plugin.ts +111 -0
- package/src/start-server-routes-plugin/config.ts +8 -0
- package/src/start-server-routes-plugin/plugin.ts +890 -0
- package/src/start-server-routes-plugin/template.ts +164 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const fsp = require("node:fs/promises");
|
|
6
|
+
const routerGenerator = require("@tanstack/router-generator");
|
|
7
|
+
const routerCore = require("@tanstack/router-core");
|
|
8
|
+
const template = require("./template.cjs");
|
|
9
|
+
let lock = false;
|
|
10
|
+
const checkLock = () => lock;
|
|
11
|
+
const setLock = (bool) => {
|
|
12
|
+
lock = bool;
|
|
13
|
+
};
|
|
14
|
+
function TanStackStartServerRoutesVite(config) {
|
|
15
|
+
let ROOT = process.cwd();
|
|
16
|
+
const getRoutesDirectoryPath = () => {
|
|
17
|
+
return path.isAbsolute(config.routesDirectory) ? config.routesDirectory : path.join(ROOT, config.routesDirectory);
|
|
18
|
+
};
|
|
19
|
+
const generate = async () => {
|
|
20
|
+
if (checkLock()) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
setLock(true);
|
|
24
|
+
try {
|
|
25
|
+
await generator(config, ROOT);
|
|
26
|
+
} catch (err) {
|
|
27
|
+
console.error(err);
|
|
28
|
+
console.info();
|
|
29
|
+
} finally {
|
|
30
|
+
setLock(false);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const handleFile = async (file) => {
|
|
34
|
+
const filePath = path.normalize(file);
|
|
35
|
+
const routesDirectoryPath = getRoutesDirectoryPath();
|
|
36
|
+
if (filePath.startsWith(routesDirectoryPath)) {
|
|
37
|
+
await generate();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
return {
|
|
41
|
+
name: "tanstack-start-server-routes-plugin",
|
|
42
|
+
configureServer(server) {
|
|
43
|
+
server.watcher.on("all", (event, path2) => {
|
|
44
|
+
handleFile(path2);
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
configResolved(config2) {
|
|
48
|
+
ROOT = config2.root;
|
|
49
|
+
},
|
|
50
|
+
async buildStart() {
|
|
51
|
+
await generate();
|
|
52
|
+
},
|
|
53
|
+
sharedDuringBuild: true,
|
|
54
|
+
resolveId(id) {
|
|
55
|
+
if (id === "tanstack:server-routes") {
|
|
56
|
+
const generatedRouteTreePath = getGeneratedRouteTreePath(ROOT);
|
|
57
|
+
return generatedRouteTreePath;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
let latestTask = 0;
|
|
64
|
+
const routeGroupPatternRegex = /\(.+\)/g;
|
|
65
|
+
const possiblyNestedRouteGroupPatternRegex = /\([^/]+\)\/?/g;
|
|
66
|
+
function getGeneratedRouteTreePath(root) {
|
|
67
|
+
return path.resolve(root, ".tanstack-start/server-routes/routeTree.gen.ts");
|
|
68
|
+
}
|
|
69
|
+
async function generator(config, root) {
|
|
70
|
+
const generatedServerRouteTreePath = getGeneratedRouteTreePath(root);
|
|
71
|
+
const ROUTE_TEMPLATE = template.getTargetTemplate(config.target);
|
|
72
|
+
const logger = routerGenerator.logging({ disabled: config.disableLogging });
|
|
73
|
+
const taskId = latestTask + 1;
|
|
74
|
+
latestTask = taskId;
|
|
75
|
+
const checkLatest = () => {
|
|
76
|
+
if (latestTask !== taskId) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
};
|
|
81
|
+
let getRouteNodesResult;
|
|
82
|
+
if (config.virtualRouteConfig) {
|
|
83
|
+
getRouteNodesResult = await routerGenerator.virtualGetRouteNodes(config, root);
|
|
84
|
+
} else {
|
|
85
|
+
getRouteNodesResult = await routerGenerator.physicalGetRouteNodes(config, root);
|
|
86
|
+
}
|
|
87
|
+
const { rootRouteNode, routeNodes: beforeRouteNodes } = getRouteNodesResult;
|
|
88
|
+
if (rootRouteNode === void 0) {
|
|
89
|
+
let errorMessage = `rootRouteNode must not be undefined. Make sure you've added your root route into the route-tree.`;
|
|
90
|
+
if (!config.virtualRouteConfig) {
|
|
91
|
+
errorMessage += `
|
|
92
|
+
Make sure that you add a "${routerGenerator.rootPathId}.tsx" file to your routes directory.
|
|
93
|
+
Add the file in: "${config.routesDirectory}/${routerGenerator.rootPathId}.tsx"`;
|
|
94
|
+
}
|
|
95
|
+
throw new Error(errorMessage);
|
|
96
|
+
}
|
|
97
|
+
const preRouteNodes = routerGenerator.multiSortBy(beforeRouteNodes, [
|
|
98
|
+
(d) => d.routePath === "/" ? -1 : 1,
|
|
99
|
+
(d) => {
|
|
100
|
+
var _a;
|
|
101
|
+
return (_a = d.routePath) == null ? void 0 : _a.split("/").length;
|
|
102
|
+
},
|
|
103
|
+
(d) => d.filePath.match(new RegExp(`[./]${config.indexToken}[.]`)) ? 1 : -1,
|
|
104
|
+
(d) => d.filePath.match(
|
|
105
|
+
/[./](component|errorComponent|pendingComponent|loader|lazy)[.]/
|
|
106
|
+
) ? 1 : -1,
|
|
107
|
+
(d) => d.filePath.match(new RegExp(`[./]${config.routeToken}[.]`)) ? -1 : 1,
|
|
108
|
+
(d) => {
|
|
109
|
+
var _a;
|
|
110
|
+
return ((_a = d.routePath) == null ? void 0 : _a.endsWith("/")) ? -1 : 1;
|
|
111
|
+
},
|
|
112
|
+
(d) => d.routePath
|
|
113
|
+
]).filter((d) => ![`/${routerGenerator.rootPathId}`].includes(d.routePath || ""));
|
|
114
|
+
const routeTree = [];
|
|
115
|
+
const routeNodes = [];
|
|
116
|
+
const handleRootNode = async (node) => {
|
|
117
|
+
if (!node) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const routeCode = fs.readFileSync(node.fullPath, "utf-8");
|
|
121
|
+
if (!routeCode) {
|
|
122
|
+
const _rootTemplate = ROUTE_TEMPLATE.rootRoute;
|
|
123
|
+
const replaced = await template.fillTemplate(config, _rootTemplate.template(), {
|
|
124
|
+
tsrImports: _rootTemplate.imports.tsrImports(),
|
|
125
|
+
tsrPath: routerGenerator.rootPathId,
|
|
126
|
+
tsrExportStart: _rootTemplate.imports.tsrExportStart(),
|
|
127
|
+
tsrExportEnd: _rootTemplate.imports.tsrExportEnd()
|
|
128
|
+
});
|
|
129
|
+
await routerGenerator.writeIfDifferent(
|
|
130
|
+
node.fullPath,
|
|
131
|
+
"",
|
|
132
|
+
// Empty string because the file doesn't exist yet
|
|
133
|
+
replaced,
|
|
134
|
+
{
|
|
135
|
+
beforeWrite: () => {
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
await handleRootNode(rootRouteNode);
|
|
142
|
+
const handleNode = async (node) => {
|
|
143
|
+
var _a;
|
|
144
|
+
routerGenerator.resetRegex(routeGroupPatternRegex);
|
|
145
|
+
let parentRoute = hasParentRoute(routeNodes, node, node.routePath);
|
|
146
|
+
if ((parentRoute == null ? void 0 : parentRoute.isVirtualParentRoute) && ((_a = parentRoute.children) == null ? void 0 : _a.length)) {
|
|
147
|
+
const possibleParentRoute = hasParentRoute(
|
|
148
|
+
parentRoute.children,
|
|
149
|
+
node,
|
|
150
|
+
node.routePath
|
|
151
|
+
);
|
|
152
|
+
if (possibleParentRoute) {
|
|
153
|
+
parentRoute = possibleParentRoute;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (parentRoute) node.parent = parentRoute;
|
|
157
|
+
node.path = determineNodePath(node);
|
|
158
|
+
const trimmedPath = routerGenerator.trimPathLeft(node.path ?? "");
|
|
159
|
+
const split = trimmedPath.split("/");
|
|
160
|
+
const lastRouteSegment = split[split.length - 1] ?? trimmedPath;
|
|
161
|
+
node.isNonPath = lastRouteSegment.startsWith("_") || routeGroupPatternRegex.test(lastRouteSegment);
|
|
162
|
+
node.cleanedPath = removeGroups(
|
|
163
|
+
routerGenerator.removeUnderscores(removeLayoutSegments(node.path)) ?? ""
|
|
164
|
+
);
|
|
165
|
+
const routeCode = fs.readFileSync(node.fullPath, "utf-8");
|
|
166
|
+
if (!node.isVirtualParentRoute && !node.isVirtual) ;
|
|
167
|
+
const cleanedPathIsEmpty = (node.cleanedPath || "").length === 0;
|
|
168
|
+
const nonPathRoute = node._fsRouteType === "pathless_layout" && node.isNonPath;
|
|
169
|
+
node.isVirtualParentRequired = node._fsRouteType === "pathless_layout" || nonPathRoute ? !cleanedPathIsEmpty : false;
|
|
170
|
+
if (!node.isVirtual && node.isVirtualParentRequired) {
|
|
171
|
+
const parentRoutePath = removeLastSegmentFromPath(node.routePath) || "/";
|
|
172
|
+
const parentVariableName = routerGenerator.routePathToVariable(parentRoutePath);
|
|
173
|
+
const anchorRoute = routeNodes.find(
|
|
174
|
+
(d) => d.routePath === parentRoutePath
|
|
175
|
+
);
|
|
176
|
+
if (!anchorRoute) {
|
|
177
|
+
const parentNode = {
|
|
178
|
+
...node,
|
|
179
|
+
path: removeLastSegmentFromPath(node.path) || "/",
|
|
180
|
+
filePath: removeLastSegmentFromPath(node.filePath) || "/",
|
|
181
|
+
fullPath: removeLastSegmentFromPath(node.fullPath) || "/",
|
|
182
|
+
routePath: parentRoutePath,
|
|
183
|
+
variableName: parentVariableName,
|
|
184
|
+
isVirtual: true,
|
|
185
|
+
_fsRouteType: "layout",
|
|
186
|
+
// layout since this route will wrap other routes
|
|
187
|
+
isVirtualParentRoute: true,
|
|
188
|
+
isVirtualParentRequired: false
|
|
189
|
+
};
|
|
190
|
+
parentNode.children = parentNode.children ?? [];
|
|
191
|
+
parentNode.children.push(node);
|
|
192
|
+
node.parent = parentNode;
|
|
193
|
+
if (node._fsRouteType === "pathless_layout") {
|
|
194
|
+
node.path = determineNodePath(node);
|
|
195
|
+
}
|
|
196
|
+
await handleNode(parentNode);
|
|
197
|
+
} else {
|
|
198
|
+
anchorRoute.children = anchorRoute.children ?? [];
|
|
199
|
+
anchorRoute.children.push(node);
|
|
200
|
+
node.parent = anchorRoute;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (!routeCode.split("\n").some((line) => line.trim().startsWith("export const ServerRoute"))) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (node.parent) {
|
|
207
|
+
if (!node.isVirtualParentRequired) {
|
|
208
|
+
node.parent.children = node.parent.children ?? [];
|
|
209
|
+
node.parent.children.push(node);
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
routeTree.push(node);
|
|
213
|
+
}
|
|
214
|
+
routeNodes.push(node);
|
|
215
|
+
};
|
|
216
|
+
for (const node of preRouteNodes) {
|
|
217
|
+
await handleNode(node);
|
|
218
|
+
}
|
|
219
|
+
checkRouteFullPathUniqueness(routeNodes, config);
|
|
220
|
+
function buildRouteTreeConfig(nodes, depth = 1) {
|
|
221
|
+
const children = nodes.map((node) => {
|
|
222
|
+
var _a, _b;
|
|
223
|
+
if (node._fsRouteType === "__root") {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (node._fsRouteType === "pathless_layout" && !((_a = node.children) == null ? void 0 : _a.length)) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const route = `${node.variableName}Route`;
|
|
230
|
+
if ((_b = node.children) == null ? void 0 : _b.length) {
|
|
231
|
+
const childConfigs = buildRouteTreeConfig(node.children, depth + 1);
|
|
232
|
+
const childrenDeclaration = `interface ${route}Children {
|
|
233
|
+
${node.children.map((child) => `${child.variableName}Route: typeof ${getResolvedRouteNodeVariableName(child)}`).join(",")}
|
|
234
|
+
}`;
|
|
235
|
+
const children2 = `const ${route}Children: ${route}Children = {
|
|
236
|
+
${node.children.map((child) => `${child.variableName}Route: ${getResolvedRouteNodeVariableName(child)}`).join(",")}
|
|
237
|
+
}`;
|
|
238
|
+
const routeWithChildren = `const ${route}WithChildren = ${route}._addFileChildren(${route}Children)`;
|
|
239
|
+
return [
|
|
240
|
+
childConfigs,
|
|
241
|
+
childrenDeclaration,
|
|
242
|
+
children2,
|
|
243
|
+
routeWithChildren
|
|
244
|
+
].join("\n\n");
|
|
245
|
+
}
|
|
246
|
+
return void 0;
|
|
247
|
+
});
|
|
248
|
+
return children.filter(Boolean).join("\n\n");
|
|
249
|
+
}
|
|
250
|
+
const routeConfigChildrenText = buildRouteTreeConfig(routeTree);
|
|
251
|
+
const sortedRouteNodes = routerGenerator.multiSortBy(routeNodes, [
|
|
252
|
+
(d) => {
|
|
253
|
+
var _a;
|
|
254
|
+
return ((_a = d.routePath) == null ? void 0 : _a.includes(`/${routerGenerator.rootPathId}`)) ? -1 : 1;
|
|
255
|
+
},
|
|
256
|
+
(d) => {
|
|
257
|
+
var _a;
|
|
258
|
+
return (_a = d.routePath) == null ? void 0 : _a.split("/").length;
|
|
259
|
+
},
|
|
260
|
+
(d) => {
|
|
261
|
+
var _a;
|
|
262
|
+
return ((_a = d.routePath) == null ? void 0 : _a.endsWith(config.indexToken)) ? -1 : 1;
|
|
263
|
+
},
|
|
264
|
+
(d) => d
|
|
265
|
+
]);
|
|
266
|
+
const imports = Object.entries({
|
|
267
|
+
createFileRoute: sortedRouteNodes.some((d) => d.isVirtual)
|
|
268
|
+
}).filter((d) => d[1]).map((d) => d[0]);
|
|
269
|
+
const virtualRouteNodes = sortedRouteNodes.filter((d) => d.isVirtual);
|
|
270
|
+
function getImportPath(node) {
|
|
271
|
+
return routerGenerator.replaceBackslash(
|
|
272
|
+
routerGenerator.removeExt(
|
|
273
|
+
path.relative(
|
|
274
|
+
path.dirname(generatedServerRouteTreePath),
|
|
275
|
+
path.resolve(config.routesDirectory, node.filePath)
|
|
276
|
+
)
|
|
277
|
+
)
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
const rootRouteExists = fs.existsSync(rootRouteNode.fullPath);
|
|
281
|
+
const rootRouteCode = rootRouteExists ? fs.readFileSync(rootRouteNode.fullPath, "utf-8") : "";
|
|
282
|
+
const hasServerRootRoute = rootRouteExists && rootRouteCode.includes("export const ServerRoute");
|
|
283
|
+
const routeImports = [
|
|
284
|
+
...config.routeTreeFileHeader,
|
|
285
|
+
`// This file was automatically generated by TanStack Router.
|
|
286
|
+
// You should NOT make any changes in this file as it will be overwritten.
|
|
287
|
+
// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified.`,
|
|
288
|
+
imports.length ? `import { ${imports.join(", ")} } from '${ROUTE_TEMPLATE.fullPkg}'
|
|
289
|
+
` : "",
|
|
290
|
+
"// Import Routes",
|
|
291
|
+
[
|
|
292
|
+
`import type { FileRoutesByPath, CreateServerFileRoute } from '${ROUTE_TEMPLATE.fullPkg}'`,
|
|
293
|
+
`import { createServerRoute, createServerFileRoute } from '${ROUTE_TEMPLATE.fullPkg}'`,
|
|
294
|
+
hasServerRootRoute ? `import { ServerRoute as rootRouteImport } from './${getImportPath(rootRouteNode)}'` : "",
|
|
295
|
+
...sortedRouteNodes.filter((d) => !d.isVirtual).map((node) => {
|
|
296
|
+
return `import { ServerRoute as ${node.variableName}RouteImport } from './${getImportPath(node)}'`;
|
|
297
|
+
})
|
|
298
|
+
].join("\n"),
|
|
299
|
+
virtualRouteNodes.length ? "// Create Virtual Routes" : "",
|
|
300
|
+
virtualRouteNodes.map((node) => {
|
|
301
|
+
return `const ${node.variableName}RouteImport = createFileRoute('${node.routePath}')()`;
|
|
302
|
+
}).join("\n"),
|
|
303
|
+
"// Create/Update Routes",
|
|
304
|
+
!hasServerRootRoute ? `
|
|
305
|
+
const rootRoute = createServerRoute()
|
|
306
|
+
` : "",
|
|
307
|
+
sortedRouteNodes.map((node) => {
|
|
308
|
+
var _a;
|
|
309
|
+
return [
|
|
310
|
+
[
|
|
311
|
+
`const ${node.variableName}Route = ${node.variableName}RouteImport.update({
|
|
312
|
+
${[
|
|
313
|
+
`id: '${node.path}'`,
|
|
314
|
+
!node.isNonPath ? `path: '${node.cleanedPath}'` : void 0,
|
|
315
|
+
`getParentRoute: () => ${((_a = node.parent) == null ? void 0 : _a.variableName) ?? "root"}Route`
|
|
316
|
+
].filter(Boolean).join(",")}
|
|
317
|
+
} as any)`
|
|
318
|
+
].join("")
|
|
319
|
+
].join("\n\n");
|
|
320
|
+
}).join("\n\n"),
|
|
321
|
+
"",
|
|
322
|
+
"// Populate the FileRoutesByPath interface",
|
|
323
|
+
`declare module '${ROUTE_TEMPLATE.fullPkg}' {
|
|
324
|
+
interface FileRoutesByPath {
|
|
325
|
+
${routeNodes.map((routeNode) => {
|
|
326
|
+
var _a, _b;
|
|
327
|
+
const filePathId = routeNode.routePath;
|
|
328
|
+
return `'${filePathId}': {
|
|
329
|
+
id: '${filePathId}'
|
|
330
|
+
path: '${inferPath(routeNode)}'
|
|
331
|
+
fullPath: '${inferFullPath(routeNode)}'
|
|
332
|
+
preLoaderRoute: typeof ${routeNode.variableName}RouteImport
|
|
333
|
+
parentRoute: typeof ${routeNode.isVirtualParentRequired ? `${(_a = routeNode.parent) == null ? void 0 : _a.variableName}Route` : ((_b = routeNode.parent) == null ? void 0 : _b.variableName) ? `${routeNode.parent.variableName}RouteImport` : "rootRoute"}
|
|
334
|
+
}`;
|
|
335
|
+
}).join("\n")}
|
|
336
|
+
}
|
|
337
|
+
}`,
|
|
338
|
+
`// Add type-safety to the createFileRoute function across the route tree`,
|
|
339
|
+
routeNodes.map((routeNode) => {
|
|
340
|
+
var _a;
|
|
341
|
+
return `declare module './${getImportPath(routeNode)}' {
|
|
342
|
+
const createServerFileRoute: CreateServerFileRoute<
|
|
343
|
+
FileRoutesByPath['${routeNode.routePath}']['parentRoute'],
|
|
344
|
+
FileRoutesByPath['${routeNode.routePath}']['id'],
|
|
345
|
+
FileRoutesByPath['${routeNode.routePath}']['path'],
|
|
346
|
+
FileRoutesByPath['${routeNode.routePath}']['fullPath'],
|
|
347
|
+
${((_a = routeNode.children) == null ? void 0 : _a.length) ? `${routeNode.variableName}RouteChildren` : "unknown"}
|
|
348
|
+
>
|
|
349
|
+
}`;
|
|
350
|
+
}).join("\n"),
|
|
351
|
+
"// Create and export the route tree",
|
|
352
|
+
routeConfigChildrenText,
|
|
353
|
+
`export interface FileRoutesByFullPath {
|
|
354
|
+
${[...createRouteNodesByFullPath(routeNodes).entries()].map(
|
|
355
|
+
([fullPath, routeNode]) => {
|
|
356
|
+
return `'${fullPath}': typeof ${getResolvedRouteNodeVariableName(routeNode)}`;
|
|
357
|
+
}
|
|
358
|
+
)}
|
|
359
|
+
}`,
|
|
360
|
+
`export interface FileRoutesByTo {
|
|
361
|
+
${[...createRouteNodesByTo(routeNodes).entries()].map(([to, routeNode]) => {
|
|
362
|
+
return `'${to}': typeof ${getResolvedRouteNodeVariableName(routeNode)}`;
|
|
363
|
+
})}
|
|
364
|
+
}`,
|
|
365
|
+
`export interface FileRoutesById {
|
|
366
|
+
'${routerCore.rootRouteId}': typeof rootRoute,
|
|
367
|
+
${[...createRouteNodesById(routeNodes).entries()].map(([id, routeNode]) => {
|
|
368
|
+
return `'${id}': typeof ${getResolvedRouteNodeVariableName(routeNode)}`;
|
|
369
|
+
})}
|
|
370
|
+
}`,
|
|
371
|
+
`export interface FileRouteTypes {
|
|
372
|
+
fileRoutesByFullPath: FileRoutesByFullPath
|
|
373
|
+
fullPaths: ${routeNodes.length > 0 ? [...createRouteNodesByFullPath(routeNodes).keys()].map((fullPath) => `'${fullPath}'`).join("|") : "never"}
|
|
374
|
+
fileRoutesByTo: FileRoutesByTo
|
|
375
|
+
to: ${routeNodes.length > 0 ? [...createRouteNodesByTo(routeNodes).keys()].map((to) => `'${to}'`).join("|") : "never"}
|
|
376
|
+
id: ${[`'${routerCore.rootRouteId}'`, ...[...createRouteNodesById(routeNodes).keys()].map((id) => `'${id}'`)].join("|")}
|
|
377
|
+
fileRoutesById: FileRoutesById
|
|
378
|
+
}`,
|
|
379
|
+
`export interface RootRouteChildren {
|
|
380
|
+
${routeTree.map((child) => `${child.variableName}Route: typeof ${getResolvedRouteNodeVariableName(child)}`).join(",")}
|
|
381
|
+
}`,
|
|
382
|
+
`const rootRouteChildren: RootRouteChildren = {
|
|
383
|
+
${routeTree.map((child) => `${child.variableName}Route: ${getResolvedRouteNodeVariableName(child)}`).join(",")}
|
|
384
|
+
}`,
|
|
385
|
+
`export const routeTree = rootRoute._addFileChildren(rootRouteChildren)._addFileTypes<FileRouteTypes>()`
|
|
386
|
+
].filter(Boolean).join("\n\n");
|
|
387
|
+
const createRouteManifest = () => {
|
|
388
|
+
const routesManifest = {
|
|
389
|
+
[routerCore.rootRouteId]: {
|
|
390
|
+
filePath: rootRouteNode.filePath,
|
|
391
|
+
children: routeTree.map((d) => d.routePath)
|
|
392
|
+
},
|
|
393
|
+
...Object.fromEntries(
|
|
394
|
+
routeNodes.map((d) => {
|
|
395
|
+
var _a, _b;
|
|
396
|
+
const filePathId = d.routePath;
|
|
397
|
+
return [
|
|
398
|
+
filePathId,
|
|
399
|
+
{
|
|
400
|
+
filePath: d.filePath,
|
|
401
|
+
parent: ((_a = d.parent) == null ? void 0 : _a.routePath) ? d.parent.routePath : void 0,
|
|
402
|
+
children: (_b = d.children) == null ? void 0 : _b.map((childRoute) => childRoute.routePath)
|
|
403
|
+
}
|
|
404
|
+
];
|
|
405
|
+
})
|
|
406
|
+
)
|
|
407
|
+
};
|
|
408
|
+
return JSON.stringify(
|
|
409
|
+
{
|
|
410
|
+
routes: routesManifest
|
|
411
|
+
},
|
|
412
|
+
null,
|
|
413
|
+
2
|
|
414
|
+
);
|
|
415
|
+
};
|
|
416
|
+
const includeManifest = ["react", "solid"];
|
|
417
|
+
const routeConfigFileContent = !includeManifest.includes(config.target) ? routeImports : [
|
|
418
|
+
routeImports,
|
|
419
|
+
"\n",
|
|
420
|
+
"/* ROUTE_MANIFEST_START",
|
|
421
|
+
createRouteManifest(),
|
|
422
|
+
"ROUTE_MANIFEST_END */"
|
|
423
|
+
].join("\n");
|
|
424
|
+
if (!checkLatest()) return;
|
|
425
|
+
const existingRouteTreeContent = await fsp.readFile(path.resolve(generatedServerRouteTreePath), "utf-8").catch((err) => {
|
|
426
|
+
if (err.code === "ENOENT") {
|
|
427
|
+
return "";
|
|
428
|
+
}
|
|
429
|
+
throw err;
|
|
430
|
+
});
|
|
431
|
+
if (!checkLatest()) return;
|
|
432
|
+
await fsp.mkdir(path.dirname(path.resolve(generatedServerRouteTreePath)), {
|
|
433
|
+
recursive: true
|
|
434
|
+
});
|
|
435
|
+
if (!checkLatest()) return;
|
|
436
|
+
const routeTreeWriteResult = await routerGenerator.writeIfDifferent(
|
|
437
|
+
path.resolve(generatedServerRouteTreePath),
|
|
438
|
+
await routerGenerator.format(existingRouteTreeContent, config),
|
|
439
|
+
await routerGenerator.format(routeConfigFileContent, config),
|
|
440
|
+
{
|
|
441
|
+
beforeWrite: () => {
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
);
|
|
445
|
+
const startDeclarationFilePath = path.join(
|
|
446
|
+
path.resolve(root, config.srcDirectory),
|
|
447
|
+
"tanstack-start.d.ts"
|
|
448
|
+
);
|
|
449
|
+
const serverRoutesRelativePath = routerGenerator.removeExt(
|
|
450
|
+
path.relative(
|
|
451
|
+
path.dirname(startDeclarationFilePath),
|
|
452
|
+
generatedServerRouteTreePath
|
|
453
|
+
)
|
|
454
|
+
);
|
|
455
|
+
const startDeclarationFileContent = buildStartDeclarationFile({
|
|
456
|
+
serverRoutesRelativePath
|
|
457
|
+
});
|
|
458
|
+
if (!fs.existsSync(startDeclarationFilePath)) {
|
|
459
|
+
await routerGenerator.writeIfDifferent(
|
|
460
|
+
startDeclarationFilePath,
|
|
461
|
+
"",
|
|
462
|
+
startDeclarationFileContent,
|
|
463
|
+
{
|
|
464
|
+
beforeWrite: () => {
|
|
465
|
+
logger.log(`🟡 Creating tanstack-start.d.ts`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
);
|
|
469
|
+
} else {
|
|
470
|
+
const existingDeclarationFileContent = await fsp.readFile(startDeclarationFilePath, "utf-8").catch((err) => {
|
|
471
|
+
if (err.code === "ENOENT") {
|
|
472
|
+
return "";
|
|
473
|
+
}
|
|
474
|
+
throw err;
|
|
475
|
+
});
|
|
476
|
+
await routerGenerator.writeIfDifferent(
|
|
477
|
+
startDeclarationFilePath,
|
|
478
|
+
existingDeclarationFileContent,
|
|
479
|
+
startDeclarationFileContent,
|
|
480
|
+
{
|
|
481
|
+
beforeWrite: () => {
|
|
482
|
+
logger.log(`🟡 Updating tanstack-start.d.ts`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
if (routeTreeWriteResult && !checkLatest()) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
function buildStartDeclarationFile({
|
|
492
|
+
serverRoutesRelativePath
|
|
493
|
+
}) {
|
|
494
|
+
return [`import '${serverRoutesRelativePath}'`].join("\n") + "\n";
|
|
495
|
+
}
|
|
496
|
+
function removeGroups(s) {
|
|
497
|
+
return s.replace(possiblyNestedRouteGroupPatternRegex, "");
|
|
498
|
+
}
|
|
499
|
+
function determineNodePath(node) {
|
|
500
|
+
var _a;
|
|
501
|
+
return node.path = node.parent ? ((_a = node.routePath) == null ? void 0 : _a.replace(node.parent.routePath ?? "", "")) || "/" : node.routePath;
|
|
502
|
+
}
|
|
503
|
+
function removeLastSegmentFromPath(routePath = "/") {
|
|
504
|
+
const segments = routePath.split("/");
|
|
505
|
+
segments.pop();
|
|
506
|
+
return segments.join("/");
|
|
507
|
+
}
|
|
508
|
+
function removeLayoutSegments(routePath = "/") {
|
|
509
|
+
const segments = routePath.split("/");
|
|
510
|
+
const newSegments = segments.filter((segment) => !segment.startsWith("_"));
|
|
511
|
+
return newSegments.join("/");
|
|
512
|
+
}
|
|
513
|
+
function hasParentRoute(routes, node, routePathToCheck) {
|
|
514
|
+
if (!routePathToCheck || routePathToCheck === "/") {
|
|
515
|
+
return null;
|
|
516
|
+
}
|
|
517
|
+
const sortedNodes = routerGenerator.multiSortBy(routes, [
|
|
518
|
+
(d) => d.routePath.length * -1,
|
|
519
|
+
(d) => d.variableName
|
|
520
|
+
]).filter((d) => d.routePath !== `/${routerGenerator.rootPathId}`);
|
|
521
|
+
for (const route of sortedNodes) {
|
|
522
|
+
if (route.routePath === "/") continue;
|
|
523
|
+
if (routePathToCheck.startsWith(`${route.routePath}/`) && route.routePath !== routePathToCheck) {
|
|
524
|
+
return route;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
const segments = routePathToCheck.split("/");
|
|
528
|
+
segments.pop();
|
|
529
|
+
const parentRoutePath = segments.join("/");
|
|
530
|
+
return hasParentRoute(routes, node, parentRoutePath);
|
|
531
|
+
}
|
|
532
|
+
const getResolvedRouteNodeVariableName = (routeNode) => {
|
|
533
|
+
var _a;
|
|
534
|
+
return ((_a = routeNode.children) == null ? void 0 : _a.length) ? `${routeNode.variableName}RouteWithChildren` : `${routeNode.variableName}Route`;
|
|
535
|
+
};
|
|
536
|
+
const createRouteNodesByFullPath = (routeNodes) => {
|
|
537
|
+
return new Map(
|
|
538
|
+
routeNodes.map((routeNode) => [inferFullPath(routeNode), routeNode])
|
|
539
|
+
);
|
|
540
|
+
};
|
|
541
|
+
const createRouteNodesByTo = (routeNodes) => {
|
|
542
|
+
return new Map(
|
|
543
|
+
dedupeBranchesAndIndexRoutes(routeNodes).map((routeNode) => [
|
|
544
|
+
inferTo(routeNode),
|
|
545
|
+
routeNode
|
|
546
|
+
])
|
|
547
|
+
);
|
|
548
|
+
};
|
|
549
|
+
const createRouteNodesById = (routeNodes) => {
|
|
550
|
+
return new Map(
|
|
551
|
+
routeNodes.map((routeNode) => {
|
|
552
|
+
const id = routeNode.routePath ?? "";
|
|
553
|
+
return [id, routeNode];
|
|
554
|
+
})
|
|
555
|
+
);
|
|
556
|
+
};
|
|
557
|
+
const inferFullPath = (routeNode) => {
|
|
558
|
+
const fullPath = removeGroups(
|
|
559
|
+
routerGenerator.removeUnderscores(removeLayoutSegments(routeNode.routePath)) ?? ""
|
|
560
|
+
);
|
|
561
|
+
return routeNode.cleanedPath === "/" ? fullPath : fullPath.replace(/\/$/, "");
|
|
562
|
+
};
|
|
563
|
+
const inferPath = (routeNode) => {
|
|
564
|
+
var _a;
|
|
565
|
+
return routeNode.cleanedPath === "/" ? routeNode.cleanedPath : ((_a = routeNode.cleanedPath) == null ? void 0 : _a.replace(/\/$/, "")) ?? "";
|
|
566
|
+
};
|
|
567
|
+
const inferTo = (routeNode) => {
|
|
568
|
+
const fullPath = inferFullPath(routeNode);
|
|
569
|
+
if (fullPath === "/") return fullPath;
|
|
570
|
+
return fullPath.replace(/\/$/, "");
|
|
571
|
+
};
|
|
572
|
+
const dedupeBranchesAndIndexRoutes = (routes) => {
|
|
573
|
+
return routes.filter((route) => {
|
|
574
|
+
var _a;
|
|
575
|
+
if ((_a = route.children) == null ? void 0 : _a.find((child) => child.cleanedPath === "/")) return false;
|
|
576
|
+
return true;
|
|
577
|
+
});
|
|
578
|
+
};
|
|
579
|
+
function checkUnique(routes, key) {
|
|
580
|
+
const keys = routes.map((d) => d[key]);
|
|
581
|
+
const uniqueKeys = new Set(keys);
|
|
582
|
+
if (keys.length !== uniqueKeys.size) {
|
|
583
|
+
const duplicateKeys = keys.filter((d, i) => keys.indexOf(d) !== i);
|
|
584
|
+
const conflictingFiles = routes.filter(
|
|
585
|
+
(d) => duplicateKeys.includes(d[key])
|
|
586
|
+
);
|
|
587
|
+
return conflictingFiles;
|
|
588
|
+
}
|
|
589
|
+
return void 0;
|
|
590
|
+
}
|
|
591
|
+
function checkRouteFullPathUniqueness(_routes, config) {
|
|
592
|
+
const routes = _routes.map((d) => {
|
|
593
|
+
const inferredFullPath = inferFullPath(d);
|
|
594
|
+
return { ...d, inferredFullPath };
|
|
595
|
+
});
|
|
596
|
+
const conflictingFiles = checkUnique(routes, "inferredFullPath");
|
|
597
|
+
if (conflictingFiles !== void 0) {
|
|
598
|
+
const errorMessage = `Conflicting configuration paths were found for the following route${conflictingFiles.length > 1 ? "s" : ""}: ${conflictingFiles.map((p) => `"${p.inferredFullPath}"`).join(", ")}.
|
|
599
|
+
Please ensure each Server Route has a unique full path.
|
|
600
|
+
Conflicting files:
|
|
601
|
+
${conflictingFiles.map((d) => path.resolve(config.routesDirectory, d.filePath)).join("\n ")}
|
|
602
|
+
`;
|
|
603
|
+
console.error(errorMessage);
|
|
604
|
+
process.exit(1);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
exports.TanStackStartServerRoutesVite = TanStackStartServerRoutesVite;
|
|
608
|
+
//# sourceMappingURL=plugin.cjs.map
|