@torpor/build 0.1.25 → 0.1.26
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/CHANGELOG.md +10 -0
- package/dist/{Site-DgXPSQoO.d.ts → Site-4tGiw2-I.d.ts} +23 -2
- package/dist/{Site-BeCLcGj_.d.cts → Site-BXX-S541.d.cts} +23 -2
- package/dist/{chunk-UN2WQH2T.js → chunk-PTVHEJZ5.js} +1 -1
- package/dist/{chunk-UN2WQH2T.js.map → chunk-PTVHEJZ5.js.map} +1 -1
- package/dist/index.cjs +61 -26
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +45 -10
- package/dist/index.js.map +1 -1
- package/dist/metafile-cjs.json +1 -1
- package/dist/metafile-esm.json +1 -1
- package/dist/run.d.cts +1 -1
- package/dist/run.d.ts +1 -1
- package/dist/test.cjs.map +1 -1
- package/dist/test.d.cts +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/package.json +3 -3
- package/src/site/Site.ts +79 -20
- package/src/types/RouteType.ts +11 -0
package/src/site/Site.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { promises as fs } from "node:fs";
|
|
2
|
-
import
|
|
2
|
+
import fpath from "node:path";
|
|
3
3
|
import { type Plugin, type UserConfig } from "vite";
|
|
4
4
|
import tsconfigPaths from "vite-tsconfig-paths";
|
|
5
5
|
import type Adapter from "../types/Adapter";
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
LAYOUT_SERVER_ROUTE,
|
|
12
12
|
PAGE_ROUTE,
|
|
13
13
|
PAGE_SERVER_ROUTE,
|
|
14
|
+
RouteType,
|
|
14
15
|
SERVER_ROUTE,
|
|
15
16
|
} from "../types/RouteType";
|
|
16
17
|
import defaultAdapter from "./defaultAdapter";
|
|
@@ -26,7 +27,7 @@ import defaultAdapter from "./defaultAdapter";
|
|
|
26
27
|
*/
|
|
27
28
|
export default class Site {
|
|
28
29
|
root: string;
|
|
29
|
-
routes: { path: string; file: string; type:
|
|
30
|
+
routes: { path: string; file: string; type: RouteType }[] = [];
|
|
30
31
|
// Is default plugins a bad idea?
|
|
31
32
|
// HACK: Set loose because otherwise it uses a list of allowed extensions
|
|
32
33
|
// TODO: Should probably just do this ourselves and let the user pass in
|
|
@@ -52,38 +53,65 @@ export default class Site {
|
|
|
52
53
|
this.root = process.cwd();
|
|
53
54
|
}
|
|
54
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Adds a folder of routes, based on the names of the files in the folder
|
|
58
|
+
* @param folder The folder containing the routes
|
|
59
|
+
*/
|
|
55
60
|
async addRouteFolder(folder: string): Promise<void> {
|
|
56
|
-
const routeFolder =
|
|
61
|
+
const routeFolder = fpath.join(this.root, folder);
|
|
57
62
|
const routeFiles = await fs.readdir(routeFolder, { recursive: true });
|
|
58
|
-
|
|
59
63
|
for (let file of routeFiles) {
|
|
60
64
|
// The file must start with `+` or `_` and end with `.js` or `.ts`
|
|
61
|
-
if (/^(\+|_).+(\.js|\.ts)$/.test(
|
|
62
|
-
let routePath = file
|
|
63
|
-
.replace(/^\//, "")
|
|
64
|
-
.replace(/(\.ts|\.js)$/, "")
|
|
65
|
-
.replace(/_hook.server$/, "_hook/~server")
|
|
66
|
-
.replace(/_layout.server$/, "_layout/~server")
|
|
67
|
-
.replace(/\+page.server$/, "~server")
|
|
68
|
-
.replace(/\+server$/, "")
|
|
69
|
-
.replace(/\+page$/, "")
|
|
70
|
-
.replace(/\/$/, "");
|
|
71
|
-
routePath = routePath.length > 0 ? `/${routePath}` : "/";
|
|
65
|
+
if (/^(\+|_).+(\.js|\.ts)$/.test(fpath.basename(file))) {
|
|
66
|
+
let routePath = this.#routePath(file);
|
|
72
67
|
let type = this.#routeType(file);
|
|
73
|
-
file =
|
|
68
|
+
file = fpath.relative(this.root, fpath.resolve(routeFolder, file));
|
|
74
69
|
this.routes.push({ path: routePath, file, type });
|
|
75
70
|
}
|
|
76
71
|
}
|
|
77
72
|
this.#sortRoutes();
|
|
78
73
|
}
|
|
79
74
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Adds a file route, which can have a standard name (e.g. +page.ts) or any
|
|
77
|
+
* name if you pass the type
|
|
78
|
+
* @param path The route path
|
|
79
|
+
* @param file The file
|
|
80
|
+
* @param type The file type (if the file name is non-standard)
|
|
81
|
+
*/
|
|
82
|
+
addRouteFile(
|
|
83
|
+
path: string,
|
|
84
|
+
file: string,
|
|
85
|
+
type?:
|
|
86
|
+
| "+page"
|
|
87
|
+
| "+page.server"
|
|
88
|
+
| "+server"
|
|
89
|
+
| "_layout"
|
|
90
|
+
| "_layout.server"
|
|
91
|
+
| "_hook"
|
|
92
|
+
| "_hook.server"
|
|
93
|
+
| "_error",
|
|
94
|
+
): void {
|
|
95
|
+
let routePath = this.#routePath(fpath.join(path, type ?? fpath.basename(file)));
|
|
96
|
+
let routeType = type ? this.#routeTypeFromString(type) : this.#routeType(file);
|
|
97
|
+
this.routes.push({ path: routePath, file, type: routeType });
|
|
83
98
|
this.#sortRoutes();
|
|
84
99
|
}
|
|
85
100
|
|
|
86
|
-
#
|
|
101
|
+
#routePath(file: string): string {
|
|
102
|
+
let routePath = file
|
|
103
|
+
.replace(/^\//, "")
|
|
104
|
+
.replace(/(\.ts|\.js)$/, "")
|
|
105
|
+
.replace(/_hook.server$/, "_hook/~server")
|
|
106
|
+
.replace(/_layout.server$/, "_layout/~server")
|
|
107
|
+
.replace(/\+page.server$/, "~server")
|
|
108
|
+
.replace(/\+server$/, "")
|
|
109
|
+
.replace(/\+page$/, "")
|
|
110
|
+
.replace(/\/$/, "");
|
|
111
|
+
return routePath.length > 0 ? `/${routePath}` : "/";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
#routeType(file: string): RouteType {
|
|
87
115
|
let routePath = file.replace(/^\//, "").replace(/(\.ts|\.js)$/, "");
|
|
88
116
|
if (routePath.endsWith("+page")) {
|
|
89
117
|
return PAGE_ROUTE;
|
|
@@ -105,6 +133,37 @@ export default class Site {
|
|
|
105
133
|
return -1;
|
|
106
134
|
}
|
|
107
135
|
|
|
136
|
+
#routeTypeFromString(
|
|
137
|
+
type:
|
|
138
|
+
| "+page"
|
|
139
|
+
| "+page.server"
|
|
140
|
+
| "+server"
|
|
141
|
+
| "_layout"
|
|
142
|
+
| "_layout.server"
|
|
143
|
+
| "_hook"
|
|
144
|
+
| "_hook.server"
|
|
145
|
+
| "_error",
|
|
146
|
+
) {
|
|
147
|
+
switch (type) {
|
|
148
|
+
case "+page":
|
|
149
|
+
return PAGE_ROUTE;
|
|
150
|
+
case "+page.server":
|
|
151
|
+
return PAGE_SERVER_ROUTE;
|
|
152
|
+
case "+server":
|
|
153
|
+
return SERVER_ROUTE;
|
|
154
|
+
case "_layout":
|
|
155
|
+
return LAYOUT_ROUTE;
|
|
156
|
+
case "_layout.server":
|
|
157
|
+
return LAYOUT_SERVER_ROUTE;
|
|
158
|
+
case "_hook":
|
|
159
|
+
return HOOK_ROUTE;
|
|
160
|
+
case "_hook.server":
|
|
161
|
+
return HOOK_SERVER_ROUTE;
|
|
162
|
+
case "_error":
|
|
163
|
+
return ERROR_ROUTE;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
108
167
|
// TODO: You could make your site in a single file, like e.g. Hono...
|
|
109
168
|
//addPage(path: string, endPoint: PageEndPoint, serverEndPoint?: PageServerEndPoint) {}
|
|
110
169
|
//addEndPoint(path: string, endPoint: ServerEndPoint) {}
|
package/src/types/RouteType.ts
CHANGED
|
@@ -6,3 +6,14 @@ export const LAYOUT_SERVER_ROUTE = 5;
|
|
|
6
6
|
export const HOOK_ROUTE = 6;
|
|
7
7
|
export const HOOK_SERVER_ROUTE = 7;
|
|
8
8
|
export const ERROR_ROUTE = 8;
|
|
9
|
+
|
|
10
|
+
export type RouteType =
|
|
11
|
+
| typeof PAGE_ROUTE
|
|
12
|
+
| typeof PAGE_SERVER_ROUTE
|
|
13
|
+
| typeof SERVER_ROUTE
|
|
14
|
+
| typeof LAYOUT_ROUTE
|
|
15
|
+
| typeof LAYOUT_SERVER_ROUTE
|
|
16
|
+
| typeof HOOK_ROUTE
|
|
17
|
+
| typeof HOOK_SERVER_ROUTE
|
|
18
|
+
| typeof ERROR_ROUTE
|
|
19
|
+
| -1;
|