@thexjs/core 1.3.0 → 1.3.2
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/adapter.d.ts +8 -0
- package/dist/adapter.js +44 -16
- package/dist/chunk-MYYP67KQ.js +683 -0
- package/dist/index.js +56 -477
- package/package.json +1 -1
- package/dist/chunk-O6SIM7EF.js +0 -221
package/dist/chunk-O6SIM7EF.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
// src/router.ts
|
|
2
|
-
import { readdirSync, statSync, writeFileSync } from "fs";
|
|
3
|
-
import { join, relative, sep } from "path";
|
|
4
|
-
var ROUTE_FILE = /\.(tsx|ts)$/;
|
|
5
|
-
function scanPages(rootDir) {
|
|
6
|
-
return scanRoutes(rootDir).map((r) => ({ ...r, isApi: false }));
|
|
7
|
-
}
|
|
8
|
-
function scanApiDir(rootDir) {
|
|
9
|
-
return scanRoutes(rootDir).map((r) => ({
|
|
10
|
-
...r,
|
|
11
|
-
routePath: r.routePath === "/" ? "/api" : `/api${r.routePath}`,
|
|
12
|
-
isApi: true
|
|
13
|
-
}));
|
|
14
|
-
}
|
|
15
|
-
function scanRoutes(rootDir) {
|
|
16
|
-
try {
|
|
17
|
-
statSync(rootDir);
|
|
18
|
-
} catch {
|
|
19
|
-
return [];
|
|
20
|
-
}
|
|
21
|
-
const entries = [];
|
|
22
|
-
function walk(dir) {
|
|
23
|
-
for (const name of readdirSync(dir)) {
|
|
24
|
-
if (name.startsWith("_") || name.startsWith(".")) continue;
|
|
25
|
-
const full = join(dir, name);
|
|
26
|
-
const stat = statSync(full);
|
|
27
|
-
if (stat.isDirectory()) {
|
|
28
|
-
walk(full);
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
if (!ROUTE_FILE.test(name)) continue;
|
|
32
|
-
const rel = relative(rootDir, full).replace(ROUTE_FILE, "");
|
|
33
|
-
entries.push(toRouteEntry(rel, full));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
walk(rootDir);
|
|
37
|
-
return entries;
|
|
38
|
-
}
|
|
39
|
-
function toRouteEntry(relPath, filePath) {
|
|
40
|
-
const segments = relPath.split(sep);
|
|
41
|
-
const paramNames = [];
|
|
42
|
-
const isApi = segments[0] === "api";
|
|
43
|
-
const routeSegments = segments.map((segment) => {
|
|
44
|
-
if (segment === "index") return "";
|
|
45
|
-
if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
46
|
-
const name = segment.slice(4, -1);
|
|
47
|
-
paramNames.push(name);
|
|
48
|
-
return "*";
|
|
49
|
-
}
|
|
50
|
-
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
51
|
-
const name = segment.slice(1, -1);
|
|
52
|
-
paramNames.push(name);
|
|
53
|
-
return `:${name}`;
|
|
54
|
-
}
|
|
55
|
-
return segment;
|
|
56
|
-
});
|
|
57
|
-
const joined = routeSegments.filter((s) => s.length > 0).join("/");
|
|
58
|
-
const routePath = joined.length === 0 ? "/" : `/${joined}`;
|
|
59
|
-
return { filePath, routePath, paramNames, isApi };
|
|
60
|
-
}
|
|
61
|
-
function scanLayouts(rootDir) {
|
|
62
|
-
const entries = [];
|
|
63
|
-
function walk(dir) {
|
|
64
|
-
for (const name of readdirSync(dir)) {
|
|
65
|
-
if (name.startsWith(".")) continue;
|
|
66
|
-
const full = join(dir, name);
|
|
67
|
-
const stat = statSync(full);
|
|
68
|
-
if (stat.isDirectory()) {
|
|
69
|
-
walk(full);
|
|
70
|
-
} else if (name === "_layout.tsx" || name === "_layout.ts") {
|
|
71
|
-
entries.push({ filePath: full, dirPath: dir });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
walk(rootDir);
|
|
76
|
-
return entries;
|
|
77
|
-
}
|
|
78
|
-
function scanLayoutsDir(layoutsDir) {
|
|
79
|
-
const entries = [];
|
|
80
|
-
try {
|
|
81
|
-
if (!statSync(layoutsDir).isDirectory()) return entries;
|
|
82
|
-
} catch {
|
|
83
|
-
return entries;
|
|
84
|
-
}
|
|
85
|
-
for (const name of readdirSync(layoutsDir)) {
|
|
86
|
-
if (name.startsWith(".")) continue;
|
|
87
|
-
const full = join(layoutsDir, name);
|
|
88
|
-
const stat = statSync(full);
|
|
89
|
-
if (stat.isFile() && (name.endsWith(".tsx") || name.endsWith(".ts"))) {
|
|
90
|
-
entries.push({ filePath: full, dirPath: layoutsDir });
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return entries;
|
|
94
|
-
}
|
|
95
|
-
function findLayoutChain(routeFilePath, layouts, routesDir) {
|
|
96
|
-
const routeDir = join(routeFilePath, "..");
|
|
97
|
-
const chain = [];
|
|
98
|
-
let current = routeDir;
|
|
99
|
-
while (current.startsWith(routesDir)) {
|
|
100
|
-
const layout = layouts.find((l) => l.dirPath === current);
|
|
101
|
-
if (layout) chain.unshift(layout);
|
|
102
|
-
const parent = join(current, "..");
|
|
103
|
-
if (parent === current) break;
|
|
104
|
-
current = parent;
|
|
105
|
-
}
|
|
106
|
-
return chain;
|
|
107
|
-
}
|
|
108
|
-
var NOT_FOUND_CANDIDATES = ["_404.tsx", "_404.ts"];
|
|
109
|
-
function scanNotFound(rootDir) {
|
|
110
|
-
for (const name of NOT_FOUND_CANDIDATES) {
|
|
111
|
-
const full = join(rootDir, name);
|
|
112
|
-
try {
|
|
113
|
-
statSync(full);
|
|
114
|
-
return { filePath: full };
|
|
115
|
-
} catch {
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
function scanMiddleware(rootDir) {
|
|
121
|
-
const entries = [];
|
|
122
|
-
function walk(dir) {
|
|
123
|
-
for (const name of readdirSync(dir)) {
|
|
124
|
-
if (name.startsWith(".")) continue;
|
|
125
|
-
const full = join(dir, name);
|
|
126
|
-
const stat = statSync(full);
|
|
127
|
-
if (stat.isDirectory()) {
|
|
128
|
-
walk(full);
|
|
129
|
-
} else if (name === "_middleware.ts" || name === "_middleware.tsx") {
|
|
130
|
-
entries.push({ filePath: full, dirPath: dir });
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
walk(rootDir);
|
|
135
|
-
return entries;
|
|
136
|
-
}
|
|
137
|
-
function findMiddlewareChain(routeFilePath, middleware, routesDir) {
|
|
138
|
-
const routeDir = join(routeFilePath, "..");
|
|
139
|
-
const chain = [];
|
|
140
|
-
let current = routeDir;
|
|
141
|
-
while (current.startsWith(routesDir)) {
|
|
142
|
-
const mw = middleware.find((m) => m.dirPath === current);
|
|
143
|
-
if (mw) chain.unshift(mw);
|
|
144
|
-
const parent = join(current, "..");
|
|
145
|
-
if (parent === current) break;
|
|
146
|
-
current = parent;
|
|
147
|
-
}
|
|
148
|
-
return chain;
|
|
149
|
-
}
|
|
150
|
-
function routePatternToRegex(routePath) {
|
|
151
|
-
const regexStr = routePath.replace(/\//g, "\\/").replace(/:(\w+)/g, "([^/]+)").replace(/\*/g, "(.+)");
|
|
152
|
-
return new RegExp(`^${regexStr}$`);
|
|
153
|
-
}
|
|
154
|
-
function extractParams(routePath, paramNames, url) {
|
|
155
|
-
const regex = routePatternToRegex(routePath);
|
|
156
|
-
const match = url.match(regex);
|
|
157
|
-
if (!match) return null;
|
|
158
|
-
const params = {};
|
|
159
|
-
let idx = 1;
|
|
160
|
-
for (const name of paramNames) {
|
|
161
|
-
const value = match[idx];
|
|
162
|
-
if (value !== void 0) {
|
|
163
|
-
params[name] = safeDecodeURIComponent(value);
|
|
164
|
-
}
|
|
165
|
-
idx++;
|
|
166
|
-
}
|
|
167
|
-
return params;
|
|
168
|
-
}
|
|
169
|
-
function safeDecodeURIComponent(value) {
|
|
170
|
-
try {
|
|
171
|
-
return decodeURIComponent(value);
|
|
172
|
-
} catch {
|
|
173
|
-
return value;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
function generateManifestSource(routes) {
|
|
177
|
-
const routeEntries = routes.map((r) => {
|
|
178
|
-
const paramsType = r.paramNames.length === 0 ? "Record<string, never>" : `{ ${r.paramNames.map((n) => `${n}: string`).join("; ")} }`;
|
|
179
|
-
return ` "${r.routePath}": ${paramsType};`;
|
|
180
|
-
});
|
|
181
|
-
return `// Auto-generated by @thexjs/core -- do not edit
|
|
182
|
-
export type RouteMap = {
|
|
183
|
-
${routeEntries.join("\n")}
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
export function href<T extends keyof RouteMap & string>(
|
|
187
|
-
path: T,
|
|
188
|
-
...[params]: RouteMap[T] extends Record<string, never> ? [] : [RouteMap[T]]
|
|
189
|
-
): string {
|
|
190
|
-
if (!params) return path;
|
|
191
|
-
let result: string = path;
|
|
192
|
-
for (const [key, value] of Object.entries(params)) {
|
|
193
|
-
result = result.replace(new RegExp(\`:\${key}(?=/|$)\`), encodeURIComponent(String(value)));
|
|
194
|
-
}
|
|
195
|
-
result = result.replace(/\\*+$/, "");
|
|
196
|
-
return result;
|
|
197
|
-
}
|
|
198
|
-
`;
|
|
199
|
-
}
|
|
200
|
-
function writeManifest(routes, routesDir) {
|
|
201
|
-
const source = generateManifestSource(routes);
|
|
202
|
-
const manifestPath = join(routesDir, "..", "x-routes.ts");
|
|
203
|
-
writeFileSync(manifestPath, source, "utf-8");
|
|
204
|
-
return manifestPath;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export {
|
|
208
|
-
scanPages,
|
|
209
|
-
scanApiDir,
|
|
210
|
-
scanRoutes,
|
|
211
|
-
scanLayouts,
|
|
212
|
-
scanLayoutsDir,
|
|
213
|
-
findLayoutChain,
|
|
214
|
-
scanNotFound,
|
|
215
|
-
scanMiddleware,
|
|
216
|
-
findMiddlewareChain,
|
|
217
|
-
routePatternToRegex,
|
|
218
|
-
extractParams,
|
|
219
|
-
generateManifestSource,
|
|
220
|
-
writeManifest
|
|
221
|
-
};
|