@torpor/build 0.2.0 → 0.3.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/CHANGELOG.md +31 -0
- package/dist/RouteType-g8Ce5gOd.js +107 -0
- package/dist/RouteType-g8Ce5gOd.js.map +1 -0
- package/dist/Server-CSpY_-Ba.d.ts +78 -0
- package/dist/Server-CSpY_-Ba.d.ts.map +1 -0
- package/dist/ServerEvent-2-CnAy1W.js +74 -0
- package/dist/ServerEvent-2-CnAy1W.js.map +1 -0
- package/dist/Site-D4x1l0p5.d.ts +67 -0
- package/dist/Site-D4x1l0p5.d.ts.map +1 -0
- package/dist/_page-CsdPubw4.js +33 -0
- package/dist/_page-CsdPubw4.js.map +1 -0
- package/dist/bin/index.d.ts +1 -1
- package/dist/bin/index.js +14 -19
- package/dist/bin/index.js.map +1 -1
- package/dist/connectMiddleware-DbLj0St1.js +252 -0
- package/dist/connectMiddleware-DbLj0St1.js.map +1 -0
- package/dist/index.d.ts +188 -179
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +109 -145
- package/dist/index.js.map +1 -1
- package/dist/nav.d.ts +14 -0
- package/dist/nav.d.ts.map +1 -0
- package/dist/nav.js +184 -0
- package/dist/nav.js.map +1 -0
- package/dist/pathToRegex-CTgx7hQP.js +11 -0
- package/dist/pathToRegex-CTgx7hQP.js.map +1 -0
- package/dist/response-D-XpIuMH.js +307 -0
- package/dist/response-D-XpIuMH.js.map +1 -0
- package/dist/response.d.ts +336 -151
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +4 -37
- package/dist/run-CNdaKSPR.js +331 -0
- package/dist/run-CNdaKSPR.js.map +1 -0
- package/dist/run.d.ts +11 -8
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +8 -18
- package/dist/seeOther-BmehiI4C.js +110 -0
- package/dist/seeOther-BmehiI4C.js.map +1 -0
- package/dist/server.d.ts +12 -11
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +5 -18
- package/dist/state.d.ts +14 -10
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +3 -7
- package/dist/test.d.ts +12 -11
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +152 -225
- package/dist/test.js.map +1 -1
- package/package.json +25 -29
- package/src/bin/index.ts +9 -11
- package/src/index.ts +3 -0
- package/src/nav/load.ts +9 -0
- package/src/nav/loadData.ts +121 -0
- package/src/nav/navigate.ts +154 -0
- package/src/nav/reload.ts +9 -0
- package/src/nav.ts +4 -0
- package/src/response/badRequest.ts +9 -4
- package/src/response/created.ts +10 -3
- package/src/response/forbidden.ts +11 -4
- package/src/response/found.ts +30 -0
- package/src/response/methodNotAllowed.ts +8 -4
- package/src/response/movedPermanently.ts +25 -0
- package/src/response/notFound.ts +14 -7
- package/src/response/notModified.ts +24 -4
- package/src/response/ok.ts +19 -10
- package/src/response/permanentRedirect.ts +30 -0
- package/src/response/seeOther.ts +9 -3
- package/src/response/serverError.ts +14 -2
- package/src/response/temporaryRedirect.ts +31 -0
- package/src/response/unauthorized.ts +11 -4
- package/src/response/unprocessable.ts +8 -2
- package/src/response.ts +8 -4
- package/src/{site → run}/prepareTemplate.ts +1 -1
- package/src/{site → run}/run.ts +1 -1
- package/src/{site → run}/runBuild.ts +2 -2
- package/src/{site → run}/runDev.ts +2 -2
- package/src/{site → run}/runPreview.ts +1 -1
- package/src/run.ts +4 -4
- package/src/server/Routerx.ts +3 -3
- package/src/server/Server.ts +3 -3
- package/src/server/connect/connectMiddleware.ts +2 -1
- package/src/server/connect/nodeMessageToNodeResponse.ts +2 -2
- package/src/site/Router.ts +3 -3
- package/src/site/clientEntry.ts +11 -269
- package/src/site/defaultAdapter.ts +1 -1
- package/src/site/serverEntry.ts +67 -44
- package/src/state/$page.ts +3 -12
- package/src/state/client.ts +6 -0
- package/src/state/internal.ts +45 -0
- package/src/test/runTest.ts +1 -1
- package/src/types/Adapter.ts +2 -4
- package/src/types/ClientState.ts +8 -0
- package/src/types/InternalState.ts +7 -0
- package/src/types/LayoutHandler.ts +2 -4
- package/src/types/LayoutPath.ts +10 -0
- package/src/types/ManifestRoute.ts +2 -4
- package/src/types/PageEndPoint.ts +2 -4
- package/src/types/PageLoadEvent.ts +2 -4
- package/src/types/PageServerEndPoint.ts +2 -4
- package/src/types/PageState.ts +8 -0
- package/src/types/RouteHandler.ts +2 -4
- package/src/types/RouteLayoutHandler.ts +2 -4
- package/src/types/RouteMatchResult.ts +2 -4
- package/src/types/ServerHook.ts +3 -7
- package/src/types/ServerLoadEvent.ts +2 -4
- package/src/{site → utils}/pathToRegex.ts +1 -1
- package/{tsup.config.ts → tsdown.config.ts} +3 -4
- package/dist/Server-CNZWh5ca.d.cts +0 -71
- package/dist/Server-CNZWh5ca.d.ts +0 -71
- package/dist/Site-C2020GV_.d.ts +0 -63
- package/dist/Site-DQojGpE-.d.cts +0 -63
- package/dist/bin/index.cjs +0 -778
- package/dist/bin/index.cjs.map +0 -1
- package/dist/bin/index.d.cts +0 -1
- package/dist/chunk-565UOT3F.js +0 -18
- package/dist/chunk-565UOT3F.js.map +0 -1
- package/dist/chunk-B2UNTYN5.js +0 -68
- package/dist/chunk-B2UNTYN5.js.map +0 -1
- package/dist/chunk-CZHJQCPI.js +0 -286
- package/dist/chunk-CZHJQCPI.js.map +0 -1
- package/dist/chunk-PTVHEJZ5.js +0 -152
- package/dist/chunk-PTVHEJZ5.js.map +0 -1
- package/dist/chunk-SJVHHGVW.js +0 -13
- package/dist/chunk-SJVHHGVW.js.map +0 -1
- package/dist/chunk-TDOXEXG6.js +0 -85
- package/dist/chunk-TDOXEXG6.js.map +0 -1
- package/dist/chunk-WVXAN4IV.js +0 -50
- package/dist/chunk-WVXAN4IV.js.map +0 -1
- package/dist/chunk-YRBEYKQM.js +0 -372
- package/dist/chunk-YRBEYKQM.js.map +0 -1
- package/dist/index.cjs +0 -322
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -199
- package/dist/metafile-cjs.json +0 -1
- package/dist/metafile-esm.json +0 -1
- package/dist/response.cjs +0 -150
- package/dist/response.cjs.map +0 -1
- package/dist/response.d.cts +0 -166
- package/dist/response.js.map +0 -1
- package/dist/run.cjs +0 -787
- package/dist/run.cjs.map +0 -1
- package/dist/run.d.cts +0 -14
- package/dist/run.js.map +0 -1
- package/dist/server.cjs +0 -398
- package/dist/server.cjs.map +0 -1
- package/dist/server.d.cts +0 -17
- package/dist/server.js.map +0 -1
- package/dist/state.cjs +0 -44
- package/dist/state.cjs.map +0 -1
- package/dist/state.d.cts +0 -11
- package/dist/state.js.map +0 -1
- package/dist/test.cjs +0 -538
- package/dist/test.cjs.map +0 -1
- package/dist/test.d.cts +0 -15
- package/src/response/moved.ts +0 -16
- package/src/response/redirect.ts +0 -17
- package/src/state/Page.ts +0 -8
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import { serverError } from "./response-D-XpIuMH.js";
|
|
2
|
+
import { Server, connectMiddleware } from "./connectMiddleware-DbLj0St1.js";
|
|
3
|
+
import fs, { existsSync, promises, readFileSync } from "node:fs";
|
|
4
|
+
import fpath from "node:path";
|
|
5
|
+
import { build, createServer, defineConfig, transformWithEsbuild } from "vite";
|
|
6
|
+
import torpor from "@torpor/unplugin/vite";
|
|
7
|
+
import estorpor from "@torpor/unplugin/esbuild";
|
|
8
|
+
import { configDotenv } from "dotenv";
|
|
9
|
+
|
|
10
|
+
//#region src/site/manifest.ts
|
|
11
|
+
const moduleId = "@torpor/build/manifest";
|
|
12
|
+
/**
|
|
13
|
+
* A Vite plugin that provides information about the app to both clientEntry and
|
|
14
|
+
* serverEntry.
|
|
15
|
+
*/
|
|
16
|
+
function manifest(site, server = false) {
|
|
17
|
+
return {
|
|
18
|
+
name: "torpor-manifest",
|
|
19
|
+
resolveId: {
|
|
20
|
+
order: "pre",
|
|
21
|
+
handler(id) {
|
|
22
|
+
if (id === moduleId) return id;
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
load(id, viteOptions) {
|
|
26
|
+
if (id === moduleId) {
|
|
27
|
+
let serverRequest = server && !!viteOptions?.ssr;
|
|
28
|
+
return `
|
|
29
|
+
${!serverRequest ? "const load = { default: { load: true } };" : ""}
|
|
30
|
+
export default {
|
|
31
|
+
routes: [
|
|
32
|
+
${site.routes.map((r) => {
|
|
33
|
+
if (serverRequest || !/server\.(ts|js)$/.test(r.file)) return `{ path: "${r.path}", type: ${r.type}, endPoint: ${serverRequest || !/server\.(ts|js)$/.test(r.file) ? `() => import(/* @vite-ignore */ "${fpath.join(site.root, r.file)}")` : "undefined"} }`;
|
|
34
|
+
else {
|
|
35
|
+
const haveLoad = readFileSync(fpath.join(site.root, r.file)).includes("load:");
|
|
36
|
+
return `{ path: "${r.path}", type: ${r.type}, endPoint: ${haveLoad ? "() => load" : "undefined"} }`;
|
|
37
|
+
}
|
|
38
|
+
}).join(",\n ")}
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/run/runBuild.ts
|
|
49
|
+
async function runBuild(site) {
|
|
50
|
+
const distFolder = fpath.resolve(site.root, "dist");
|
|
51
|
+
if (existsSync(distFolder)) await promises.rm(distFolder, { recursive: true });
|
|
52
|
+
const clientFolder = fpath.join(distFolder, "client");
|
|
53
|
+
const serverFolder = fpath.join(distFolder, "server");
|
|
54
|
+
if (site.adapter.prebuild) await site.adapter.prebuild(site);
|
|
55
|
+
let siteHtml = fpath.resolve(site.root, "src/site.html");
|
|
56
|
+
const siteFolder = fpath.resolve(site.root, "./node_modules/@torpor/build/src/site/");
|
|
57
|
+
let clientScript = fpath.join(siteFolder, "clientEntry.ts");
|
|
58
|
+
let serverScript = fpath.join(siteFolder, "serverEntry.ts");
|
|
59
|
+
const clientConfig = structuredClone(site.viteConfig ?? {});
|
|
60
|
+
clientConfig.plugins = [
|
|
61
|
+
manifest(site),
|
|
62
|
+
torpor(),
|
|
63
|
+
...site.plugins
|
|
64
|
+
];
|
|
65
|
+
clientConfig.build ??= {};
|
|
66
|
+
clientConfig.build.outDir = clientFolder;
|
|
67
|
+
clientConfig.build.rollupOptions ??= {};
|
|
68
|
+
clientConfig.build.rollupOptions.input = [
|
|
69
|
+
siteHtml,
|
|
70
|
+
clientScript,
|
|
71
|
+
...site.routes.filter((r) => !/server\.(ts|js)$/.test(r.file)).map((r) => fpath.resolve(site.root, r.file)),
|
|
72
|
+
...site.inputs.filter((f) => !/server\.(ts|js)$/.test(f))
|
|
73
|
+
];
|
|
74
|
+
clientConfig.build.ssrManifest = true;
|
|
75
|
+
await build(defineConfig(clientConfig));
|
|
76
|
+
const serverConfig = structuredClone(site.viteConfig ?? {});
|
|
77
|
+
serverConfig.plugins = [
|
|
78
|
+
manifest(site, true),
|
|
79
|
+
torpor(),
|
|
80
|
+
...site.plugins
|
|
81
|
+
];
|
|
82
|
+
serverConfig.build ??= {};
|
|
83
|
+
serverConfig.build.outDir = serverFolder;
|
|
84
|
+
serverConfig.build.rollupOptions ??= {};
|
|
85
|
+
serverConfig.build.rollupOptions.input = [
|
|
86
|
+
serverScript,
|
|
87
|
+
...site.routes.map((r) => fpath.resolve(site.root, r.file)),
|
|
88
|
+
...site.inputs
|
|
89
|
+
];
|
|
90
|
+
serverConfig.build.ssr = serverScript;
|
|
91
|
+
await build(defineConfig(serverConfig));
|
|
92
|
+
siteHtml = fpath.join(clientFolder, "site.html");
|
|
93
|
+
await promises.rename(fpath.join(clientFolder, "src", "site.html"), siteHtml);
|
|
94
|
+
await promises.rm(fpath.join(clientFolder, "src"), { recursive: true });
|
|
95
|
+
if (site.adapter.postbuild) await site.adapter.postbuild(site);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/run/prepareTemplate.ts
|
|
100
|
+
function prepareTemplate(template, clientScript) {
|
|
101
|
+
let result = template;
|
|
102
|
+
let headStart = result.indexOf("</head>");
|
|
103
|
+
if (headStart === -1) throw new Error(`Couldn't find <head> end tag`);
|
|
104
|
+
result = result.substring(0, headStart) + "%COMPONENT_HEAD%" + result.substring(headStart);
|
|
105
|
+
let bodyStart = regexIndexOf(result, /<div\s+id=("app"|'app'|app)\s+/);
|
|
106
|
+
bodyStart = result.indexOf(">", bodyStart) + 1;
|
|
107
|
+
let bodyEnd = result.indexOf("</div>", bodyStart);
|
|
108
|
+
if (bodyStart === -1 || bodyEnd === -1) throw new Error(`Couldn't find <div id="app"></div>`);
|
|
109
|
+
result = result.substring(0, bodyStart) + "%COMPONENT_BODY%" + result.substring(bodyEnd);
|
|
110
|
+
result += `<script type="module" src="${clientScript}"><\/script>`;
|
|
111
|
+
return result;
|
|
112
|
+
}
|
|
113
|
+
function regexIndexOf(string, regex, position) {
|
|
114
|
+
var indexOf = string.substring(position || 0).search(regex);
|
|
115
|
+
return indexOf >= 0 ? indexOf + (position || 0) : indexOf;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/run/runDev.ts
|
|
120
|
+
async function runDev(site) {
|
|
121
|
+
const server = new Server();
|
|
122
|
+
const config = structuredClone(site.viteConfig ?? {});
|
|
123
|
+
config.server = { middlewareMode: true };
|
|
124
|
+
config.appType = "custom";
|
|
125
|
+
config.plugins = [
|
|
126
|
+
manifest(site, true),
|
|
127
|
+
torpor(),
|
|
128
|
+
...site.plugins
|
|
129
|
+
];
|
|
130
|
+
config.optimizeDeps ??= {};
|
|
131
|
+
config.optimizeDeps.extensions ??= [];
|
|
132
|
+
config.optimizeDeps.extensions.push(".torp");
|
|
133
|
+
config.optimizeDeps.esbuildOptions ??= {};
|
|
134
|
+
config.optimizeDeps.esbuildOptions.plugins ??= [];
|
|
135
|
+
config.optimizeDeps.esbuildOptions.plugins.push(estorpor());
|
|
136
|
+
const vite = await createServer(config);
|
|
137
|
+
const templateFile = fpath.resolve(site.root, "src/site.html");
|
|
138
|
+
let template = await promises.readFile(templateFile, "utf-8");
|
|
139
|
+
template = await vite.transformIndexHtml("", template);
|
|
140
|
+
const siteFolder = fpath.resolve(site.root, "./node_modules/@torpor/build/src/site/");
|
|
141
|
+
let clientScript = fpath.join(siteFolder, "clientEntry.ts");
|
|
142
|
+
let serverScript = fpath.join(siteFolder, "serverEntry.ts");
|
|
143
|
+
template = prepareTemplate(template, clientScript);
|
|
144
|
+
server.use(connectMiddleware(vite.middlewares));
|
|
145
|
+
server.add("*", async (ev) => {
|
|
146
|
+
try {
|
|
147
|
+
const { load } = await vite.ssrLoadModule(serverScript);
|
|
148
|
+
return await load(ev, template);
|
|
149
|
+
} catch (e) {
|
|
150
|
+
vite.ssrFixStacktrace(e);
|
|
151
|
+
return serverError(e);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
configDotenv();
|
|
155
|
+
process.env.PROTOCOL ??= "http:";
|
|
156
|
+
process.env.HOST ??= "localhost";
|
|
157
|
+
process.env.PORT ??= "7059";
|
|
158
|
+
site.adapter.serve(server, site);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
//#endregion
|
|
162
|
+
//#region src/server/contentType.ts
|
|
163
|
+
function contentType(extension) {
|
|
164
|
+
return types[extension] || "application/octet-stream";
|
|
165
|
+
}
|
|
166
|
+
const types = {
|
|
167
|
+
".aac": "audio/aac",
|
|
168
|
+
".abw": "application/x-abiword",
|
|
169
|
+
".apng": "image/apng",
|
|
170
|
+
".arc": "application/x-freearc",
|
|
171
|
+
".avif": "image/avif",
|
|
172
|
+
".avi": "video/x-msvideo",
|
|
173
|
+
".azw": "application/vnd.amazon.ebook",
|
|
174
|
+
".bin": "application/octet-stream",
|
|
175
|
+
".bmp": "image/bmp",
|
|
176
|
+
".bz": "application/x-bzip",
|
|
177
|
+
".bz2": "application/x-bzip2",
|
|
178
|
+
".cda": "application/x-cdf",
|
|
179
|
+
".csh": "application/x-csh",
|
|
180
|
+
".css": "text/css",
|
|
181
|
+
".csv": "text/csv",
|
|
182
|
+
".doc": "application/msword",
|
|
183
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
184
|
+
".eot": "application/vnd.ms-fontobject",
|
|
185
|
+
".epub": "application/epub+zip",
|
|
186
|
+
".gz": "application/gzip",
|
|
187
|
+
".gif": "image/gif",
|
|
188
|
+
".htm": "text/html",
|
|
189
|
+
".html": "text/html",
|
|
190
|
+
".ico": "image/vnd.microsoft.icon",
|
|
191
|
+
".ics": "text/calendar",
|
|
192
|
+
".jar": "application/java-archive",
|
|
193
|
+
".jpeg": "image/jpeg",
|
|
194
|
+
".jpg": "image/jpeg",
|
|
195
|
+
".js": "text/javascript",
|
|
196
|
+
".json": "application/json",
|
|
197
|
+
".jsonld": "application/ld+json",
|
|
198
|
+
".mid": "audio/midi",
|
|
199
|
+
".midi": "audio/midi",
|
|
200
|
+
".mjs": "text/javascript",
|
|
201
|
+
".mp3": "audio/mpeg",
|
|
202
|
+
".mp4": "video/mp4",
|
|
203
|
+
".mpeg": "video/mpeg",
|
|
204
|
+
".mpkg": "application/vnd.apple.installer+xml",
|
|
205
|
+
".odp": "application/vnd.oasis.opendocument.presentation",
|
|
206
|
+
".ods": "application/vnd.oasis.opendocument.spreadsheet",
|
|
207
|
+
".odt": "application/vnd.oasis.opendocument.text",
|
|
208
|
+
".oga": "audio/ogg",
|
|
209
|
+
".ogv": "video/ogg",
|
|
210
|
+
".ogx": "application/ogg",
|
|
211
|
+
".opus": "audio/ogg",
|
|
212
|
+
".otf": "font/otf",
|
|
213
|
+
".png": "image/png",
|
|
214
|
+
".pdf": "application/pdf",
|
|
215
|
+
".php": "application/x-httpd-php",
|
|
216
|
+
".ppt": "application/vnd.ms-powerpoint",
|
|
217
|
+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
218
|
+
".rar": "application/vnd.rar",
|
|
219
|
+
".rtf": "application/rtf",
|
|
220
|
+
".sh": "application/x-sh",
|
|
221
|
+
".svg": "image/svg+xml",
|
|
222
|
+
".tar": "application/x-tar",
|
|
223
|
+
".tif": "image/tiff",
|
|
224
|
+
".tiff": "image/tiff",
|
|
225
|
+
".ts": "video/mp2t",
|
|
226
|
+
".ttf": "font/ttf",
|
|
227
|
+
".txt": "text/plain",
|
|
228
|
+
".vsd": "application/vnd.visio",
|
|
229
|
+
".wav": "audio/wav",
|
|
230
|
+
".weba": "audio/webm",
|
|
231
|
+
".webm": "video/webm",
|
|
232
|
+
".webp": "image/webp",
|
|
233
|
+
".woff": "font/woff",
|
|
234
|
+
".woff2": "font/woff2",
|
|
235
|
+
".xhtml": "application/xhtml+xml",
|
|
236
|
+
".xls": "application/vnd.ms-excel",
|
|
237
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
238
|
+
".xml": "application/xml",
|
|
239
|
+
".xul": "application/vnd.mozilla.xul+xml",
|
|
240
|
+
".zip": "application/zip",
|
|
241
|
+
".3gp": "video/3gpp",
|
|
242
|
+
".3g2": "video/3gpp2",
|
|
243
|
+
".7z": "application/x-7z-compressed"
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/run/runPreview.ts
|
|
248
|
+
async function runPreview(site) {
|
|
249
|
+
await runBuild(site);
|
|
250
|
+
const distFolder = fpath.resolve(site.root, "dist");
|
|
251
|
+
const clientFolder = fpath.join(distFolder, "client");
|
|
252
|
+
const serverFolder = fpath.join(distFolder, "server");
|
|
253
|
+
let clientScript = (await promises.readdir(fpath.join(clientFolder, "assets"))).find((f) => f.startsWith("clientEntry-"));
|
|
254
|
+
if (!clientScript) throw new Error("clientEntry.js not found");
|
|
255
|
+
clientScript = `/assets/${clientScript}`;
|
|
256
|
+
let siteHtml = fpath.join(clientFolder, "site.html");
|
|
257
|
+
let template = await promises.readFile(siteHtml, "utf-8");
|
|
258
|
+
template = prepareTemplate(template, clientScript);
|
|
259
|
+
const server = new Server();
|
|
260
|
+
server.add("/assets/*", async (ev) => {
|
|
261
|
+
try {
|
|
262
|
+
const url = new URL(ev.request.url);
|
|
263
|
+
const file = fpath.join(site.root, "dist", "client", url.pathname);
|
|
264
|
+
if (existsSync(file)) {
|
|
265
|
+
const body = bufferToArrayBuffer(await promises.readFile(file));
|
|
266
|
+
return new Response(body, {
|
|
267
|
+
status: 200,
|
|
268
|
+
headers: { "Content-Type": contentType(fpath.extname(file)) }
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
} catch (e) {
|
|
272
|
+
return serverError(e);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
const serverScript = fpath.resolve(serverFolder, "serverEntry.js");
|
|
276
|
+
server.add("*", async (ev) => {
|
|
277
|
+
try {
|
|
278
|
+
const { load } = await import(serverScript);
|
|
279
|
+
return await load(ev, template);
|
|
280
|
+
} catch (e) {
|
|
281
|
+
return serverError(e);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
configDotenv();
|
|
285
|
+
process.env.PROTOCOL ??= "http:";
|
|
286
|
+
process.env.HOST ??= "localhost";
|
|
287
|
+
process.env.PORT ??= "7059";
|
|
288
|
+
site.adapter.serve(server, site);
|
|
289
|
+
}
|
|
290
|
+
function bufferToArrayBuffer(buffer) {
|
|
291
|
+
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/run/run.ts
|
|
296
|
+
async function run(folder, mode) {
|
|
297
|
+
let configFile = "";
|
|
298
|
+
let deleteConfigFile = false;
|
|
299
|
+
const jsConfigFile = fpath.join(folder, "site.config.js");
|
|
300
|
+
const tsConfigFile = fpath.join(folder, "site.config.ts");
|
|
301
|
+
if (fs.existsSync(jsConfigFile)) configFile = jsConfigFile;
|
|
302
|
+
else if (fs.existsSync(tsConfigFile)) {
|
|
303
|
+
configFile = fpath.join(folder, "site.config.temp.js");
|
|
304
|
+
let source = fs.readFileSync(tsConfigFile, "utf-8");
|
|
305
|
+
let transformed = await transformWithEsbuild(source, tsConfigFile, { loader: "ts" });
|
|
306
|
+
fs.writeFileSync(configFile, transformed.code);
|
|
307
|
+
deleteConfigFile = true;
|
|
308
|
+
} else throw new Error("site.config file not found");
|
|
309
|
+
const site = (await import(configFile)).default;
|
|
310
|
+
if (!site || !site.root || !site.routes) throw new Error("Invalid site in config file");
|
|
311
|
+
if (deleteConfigFile) fs.rmSync(configFile);
|
|
312
|
+
switch (mode) {
|
|
313
|
+
case "dev":
|
|
314
|
+
await runDev(site);
|
|
315
|
+
break;
|
|
316
|
+
case "build":
|
|
317
|
+
await runBuild(site);
|
|
318
|
+
break;
|
|
319
|
+
case "preview":
|
|
320
|
+
await runPreview(site);
|
|
321
|
+
break;
|
|
322
|
+
default:
|
|
323
|
+
console.log("No mode passed, running in dev");
|
|
324
|
+
await runDev(site);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
//#endregion
|
|
330
|
+
export { run, runBuild, runDev, runPreview };
|
|
331
|
+
//# sourceMappingURL=run-CNdaKSPR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-CNdaKSPR.js","names":["path","path","fs","createViteServer","path","fs","e: any","types: Record<string, string>","path","fs","e: any","path"],"sources":["../src/site/manifest.ts","../src/run/runBuild.ts","../src/run/prepareTemplate.ts","../src/run/runDev.ts","../src/server/contentType.ts","../src/run/runPreview.ts","../src/run/run.ts"],"sourcesContent":["import { readFileSync } from \"node:fs\";\nimport path from \"node:path\";\nimport { Plugin } from \"vite\";\nimport Site from \"./Site\";\n\nconst moduleId = \"@torpor/build/manifest\";\n\n/**\n * A Vite plugin that provides information about the app to both clientEntry and\n * serverEntry.\n */\nexport default function manifest(site: Site, server = false): Plugin {\n\treturn {\n\t\tname: \"torpor-manifest\",\n\t\tresolveId: {\n\t\t\torder: \"pre\",\n\t\t\thandler(id) {\n\t\t\t\tif (id === moduleId) {\n\t\t\t\t\treturn id;\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\t// @ts-ignore\n\t\tload(id, viteOptions) {\n\t\t\tif (id === moduleId) {\n\t\t\t\tlet serverRequest = server && !!viteOptions?.ssr;\n\t\t\t\treturn `\n${!serverRequest ? \"const load = { default: { load: true } };\" : \"\"}\nexport default {\n routes: [\n ${site.routes\n\t\t\t.map((r) => {\n\t\t\t\tif (serverRequest || !/server\\.(ts|js)$/.test(r.file)) {\n\t\t\t\t\treturn `{ path: \"${r.path}\", type: ${r.type}, endPoint: ${serverRequest || !/server\\.(ts|js)$/.test(r.file) ? `() => import(/* @vite-ignore */ \"${path.join(site.root, r.file)}\")` : \"undefined\"} }`;\n\t\t\t\t} else {\n\t\t\t\t\t// On the client, for a server route, we need to check\n\t\t\t\t\t// whether there's a load function and set a dummy endPoint\n\t\t\t\t\t// if so. The correct endpoint will be hit in clientEntry\n\t\t\t\t\t// HACK: Should do this better than checking for `load:` in source...\n\t\t\t\t\tconst haveLoad = readFileSync(path.join(site.root, r.file)).includes(\"load:\");\n\t\t\t\t\treturn `{ path: \"${r.path}\", type: ${r.type}, endPoint: ${haveLoad ? \"() => load\" : \"undefined\"} }`;\n\t\t\t\t}\n\t\t\t})\n\t\t\t.join(\",\\n \")}\n ],\n};\n`;\n\t\t\t}\n\t\t},\n\t};\n}\n","import torpor from \"@torpor/unplugin/vite\";\nimport { existsSync, promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { build, defineConfig } from \"vite\";\nimport Site from \"../site/Site\";\nimport manifest from \"../site/manifest.ts\";\n\n// TODO: Don't cache index.html in dev?\n// TODO: Multiple hook.server locations\n// TODO: Don't reload layouts during client routing\n// TODO: Call the correct +page and +server routes when in the same folder\n\nexport default async function runBuild(site: Site): Promise<void> {\n\t// Delete the dist folder if it exists\n\tconst distFolder = path.resolve(site.root, \"dist\");\n\tif (existsSync(distFolder)) {\n\t\tawait fs.rm(distFolder, { recursive: true });\n\t}\n\tconst clientFolder = path.join(distFolder, \"client\");\n\tconst serverFolder = path.join(distFolder, \"server\");\n\n\t// HOOK: Prebuild\n\tif (site.adapter.prebuild) {\n\t\tawait site.adapter.prebuild(site);\n\t}\n\n\t// TODO: From a setting\n\tlet siteHtml = path.resolve(site.root, \"src/site.html\");\n\n\t// TODO: Is this going to be the correct path after installing from npm?\n\tconst siteFolder = path.resolve(site.root, \"./node_modules/@torpor/build/src/site/\");\n\tlet clientScript = path.join(siteFolder, \"clientEntry.ts\");\n\tlet serverScript = path.join(siteFolder, \"serverEntry.ts\");\n\n\t// Build the client assets, including site.html and the route files\n\t// EXCLUDING anything with `server.js` in the name\n\tconst clientConfig = structuredClone(site.viteConfig ?? {});\n\tclientConfig.plugins = [manifest(site), torpor(), ...site.plugins];\n\tclientConfig.build ??= {};\n\tclientConfig.build.outDir = clientFolder;\n\tclientConfig.build.rollupOptions ??= {};\n\tclientConfig.build.rollupOptions.input = [\n\t\tsiteHtml,\n\t\tclientScript,\n\t\t...site.routes\n\t\t\t.filter((r) => !/server\\.(ts|js)$/.test(r.file))\n\t\t\t.map((r) => path.resolve(site.root, r.file)),\n\t\t...site.inputs.filter((f) => !/server\\.(ts|js)$/.test(f)),\n\t];\n\tclientConfig.build.ssrManifest = true;\n\tawait build(defineConfig(clientConfig));\n\n\t// Build the server assets, including the server entry script and the route\n\t// files\n\tconst serverConfig = structuredClone(site.viteConfig ?? {});\n\tserverConfig.plugins = [manifest(site, true), torpor(), ...site.plugins];\n\tserverConfig.build ??= {};\n\tserverConfig.build.outDir = serverFolder;\n\tserverConfig.build.rollupOptions ??= {};\n\tserverConfig.build.rollupOptions.input = [\n\t\tserverScript,\n\t\t...site.routes.map((r) => path.resolve(site.root, r.file)),\n\t\t...site.inputs,\n\t];\n\tserverConfig.build.ssr = serverScript;\n\tawait build(defineConfig(serverConfig));\n\n\t// Move the site.html file into /client\n\t// HACK: Should do this in Rollup if possible?\n\tsiteHtml = path.join(clientFolder, \"site.html\");\n\tawait fs.rename(path.join(clientFolder, \"src\", \"site.html\"), siteHtml);\n\tawait fs.rm(path.join(clientFolder, \"src\"), { recursive: true });\n\n\t// HOOK: Postbuild\n\tif (site.adapter.postbuild) {\n\t\tawait site.adapter.postbuild(site);\n\t}\n}\n","export default function prepareTemplate(template: string, clientScript: string): string {\n\tlet result = template;\n\n\t// Put %COMPONENT_HEAD% before </head>\n\t// This is where styles will go\n\tlet headStart = result.indexOf(\"</head>\");\n\tif (headStart === -1) {\n\t\tthrow new Error(`Couldn't find <head> end tag`);\n\t}\n\tresult = result.substring(0, headStart) + \"%COMPONENT_HEAD%\" + result.substring(headStart);\n\n\t// Put %COMPONENT_BODY% inside <div id=\"app\"></div>\n\t// This is where the component's HTML will go\n\tlet bodyStart = regexIndexOf(result, /<div\\s+id=(\"app\"|'app'|app)\\s+/);\n\tbodyStart = result.indexOf(\">\", bodyStart) + 1;\n\tlet bodyEnd = result.indexOf(\"</div>\", bodyStart);\n\tif (bodyStart === -1 || bodyEnd === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tresult = result.substring(0, bodyStart) + \"%COMPONENT_BODY%\" + result.substring(bodyEnd);\n\n\t// Add the clientEntry script at the very end, for hydrating and navigating\n\tresult += `<script type=\"module\" src=\"${clientScript}\"></script>`;\n\n\treturn result;\n}\n\n// From https://stackoverflow.com/a/274094\nfunction regexIndexOf(string: string, regex: RegExp, position?: number) {\n\tvar indexOf = string.substring(position || 0).search(regex);\n\treturn indexOf >= 0 ? indexOf + (position || 0) : indexOf;\n}\n","import estorpor from \"@torpor/unplugin/esbuild\";\nimport torpor from \"@torpor/unplugin/vite\";\nimport { configDotenv } from \"dotenv\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { createServer as createViteServer } from \"vite\";\nimport { serverError } from \"../response.ts\";\nimport Server from \"../server/Server.ts\";\nimport connectMiddleware from \"../server/connect/connectMiddleware.ts\";\nimport Site from \"../site/Site.ts\";\nimport manifest from \"../site/manifest.ts\";\nimport prepareTemplate from \"./prepareTemplate.ts\";\n\nexport default async function runDev(site: Site): Promise<void> {\n\tconst server = new Server();\n\n\t// Create the Vite server in middleware mode and configure the app type as\n\t// \"custom\", disabling Vite's own HTML serving logic so the parent server\n\t// can take control\n\tconst config = structuredClone(site.viteConfig ?? {});\n\tconfig.server = { middlewareMode: true };\n\tconfig.appType = \"custom\";\n\tconfig.plugins = [manifest(site, true), torpor(), ...site.plugins];\n\n\t// HACK: To be able to import `.torp` files from barrel files in\n\t// node_modules, we need to add their libraries to `ssr.noExternal` in\n\t// site.config.ts, and let esbuild know how to compile them here\n\tconfig.optimizeDeps ??= {};\n\tconfig.optimizeDeps.extensions ??= [];\n\tconfig.optimizeDeps.extensions.push(\".torp\");\n\tconfig.optimizeDeps.esbuildOptions ??= {};\n\tconfig.optimizeDeps.esbuildOptions.plugins ??= [];\n\tconfig.optimizeDeps.esbuildOptions.plugins.push(estorpor());\n\n\tconst vite = await createViteServer(config);\n\n\t// Read site.html\n\t// It's called site.html because @torpor/build builds the site (html, routes\n\t// etc) while the user builds the app (components etc)\n\tconst templateFile = path.resolve(site.root, \"src/site.html\");\n\tlet template = await fs.readFile(templateFile, \"utf-8\");\n\n\t// Apply Vite HTML transforms. This injects the Vite HMR client, and also\n\t// applies HTML transforms from Vite plugins. Note that we only support a\n\t// universal transform, not individual transforms for each route\n\ttemplate = await vite.transformIndexHtml(\"\", template);\n\n\t// TODO: Is this going to be the correct path after installing from npm?\n\tconst siteFolder = path.resolve(site.root, \"./node_modules/@torpor/build/src/site/\");\n\tlet clientScript = path.join(siteFolder, \"clientEntry.ts\");\n\tlet serverScript = path.join(siteFolder, \"serverEntry.ts\");\n\n\t// Prepare site.html so that we can just splice components into it\n\ttemplate = prepareTemplate(template, clientScript);\n\n\t// Use Vite's Connect instance as middleware. We need to wrap it with\n\t// createMiddlewareHandler that converts Connect middleware to\n\t// Request/Response web handlers\n\tserver.use(connectMiddleware(vite.middlewares));\n\n\t// Every request (GET, POST, etc) goes through loadEndPoint\n\tserver.add(\"*\", async (ev) => {\n\t\ttry {\n\t\t\t// Load the server entry. ssrLoadModule automatically transforms ESM\n\t\t\t// source code to be usable in Node.js. No bundling is required, and\n\t\t\t// it provides efficient invalidation similar to HMR\n\t\t\tconst { load } = await vite.ssrLoadModule(serverScript);\n\n\t\t\t// Render the app HTML (or fetch server data etc) via serverEntry's\n\t\t\t// exported `load` function\n\t\t\treturn await load(ev, template);\n\t\t} catch (e: any) {\n\t\t\t// If an error is caught, let Vite fix the stack trace so it maps\n\t\t\t// back to your actual source code\n\t\t\tvite.ssrFixStacktrace(e);\n\t\t\treturn serverError(e);\n\t\t}\n\t});\n\n\t// Load environment variables from a `.env` file, with defaults if not set\n\tconfigDotenv();\n\n\tprocess.env.PROTOCOL ??= \"http:\";\n\tprocess.env.HOST ??= \"localhost\";\n\tprocess.env.PORT ??= \"7059\";\n\n\t// Serve the site\n\tsite.adapter.serve(server, site);\n}\n","// From https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types\n\nexport default function contentType(extension: string): string {\n\treturn types[extension] || \"application/octet-stream\";\n}\n\nconst types: Record<string, string> = {\n\t\".aac\": \"audio/aac\",\n\t\".abw\": \"application/x-abiword\",\n\t\".apng\": \"image/apng\",\n\t\".arc\": \"application/x-freearc\",\n\t\".avif\": \"image/avif\",\n\t\".avi\": \"video/x-msvideo\",\n\t\".azw\": \"application/vnd.amazon.ebook\",\n\t\".bin\": \"application/octet-stream\",\n\t\".bmp\": \"image/bmp\",\n\t\".bz\": \"application/x-bzip\",\n\t\".bz2\": \"application/x-bzip2\",\n\t\".cda\": \"application/x-cdf\",\n\t\".csh\": \"application/x-csh\",\n\t\".css\": \"text/css\",\n\t\".csv\": \"text/csv\",\n\t\".doc\": \"application/msword\",\n\t\".docx\": \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\",\n\t\".eot\": \"application/vnd.ms-fontobject\",\n\t\".epub\": \"application/epub+zip\",\n\t\".gz\": \"application/gzip\",\n\t\".gif\": \"image/gif\",\n\t\".htm\": \"text/html\",\n\t\".html\": \"text/html\",\n\t\".ico\": \"image/vnd.microsoft.icon\",\n\t\".ics\": \"text/calendar\",\n\t\".jar\": \"application/java-archive\",\n\t\".jpeg\": \"image/jpeg\",\n\t\".jpg\": \"image/jpeg\",\n\t\".js\": \"text/javascript\",\n\t\".json\": \"application/json\",\n\t\".jsonld\": \"application/ld+json\",\n\t\".mid\": \"audio/midi\",\n\t\".midi\": \"audio/midi\",\n\t\".mjs\": \"text/javascript\",\n\t\".mp3\": \"audio/mpeg\",\n\t\".mp4\": \"video/mp4\",\n\t\".mpeg\": \"video/mpeg\",\n\t\".mpkg\": \"application/vnd.apple.installer+xml\",\n\t\".odp\": \"application/vnd.oasis.opendocument.presentation\",\n\t\".ods\": \"application/vnd.oasis.opendocument.spreadsheet\",\n\t\".odt\": \"application/vnd.oasis.opendocument.text\",\n\t\".oga\": \"audio/ogg\",\n\t\".ogv\": \"video/ogg\",\n\t\".ogx\": \"application/ogg\",\n\t\".opus\": \"audio/ogg\",\n\t\".otf\": \"font/otf\",\n\t\".png\": \"image/png\",\n\t\".pdf\": \"application/pdf\",\n\t\".php\": \"application/x-httpd-php\",\n\t\".ppt\": \"application/vnd.ms-powerpoint\",\n\t\".pptx\": \"application/vnd.openxmlformats-officedocument.presentationml.presentation\",\n\t\".rar\": \"application/vnd.rar\",\n\t\".rtf\": \"application/rtf\",\n\t\".sh\": \"application/x-sh\",\n\t\".svg\": \"image/svg+xml\",\n\t\".tar\": \"application/x-tar\",\n\t\".tif\": \"image/tiff\",\n\t\".tiff\": \"image/tiff\",\n\t\".ts\": \"video/mp2t\",\n\t\".ttf\": \"font/ttf\",\n\t\".txt\": \"text/plain\",\n\t\".vsd\": \"application/vnd.visio\",\n\t\".wav\": \"audio/wav\",\n\t\".weba\": \"audio/webm\",\n\t\".webm\": \"video/webm\",\n\t\".webp\": \"image/webp\",\n\t\".woff\": \"font/woff\",\n\t\".woff2\": \"font/woff2\",\n\t\".xhtml\": \"application/xhtml+xml\",\n\t\".xls\": \"application/vnd.ms-excel\",\n\t\".xlsx\": \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\",\n\t\".xml\": \"application/xml\",\n\t\".xul\": \"application/vnd.mozilla.xul+xml\",\n\t\".zip\": \"application/zip\",\n\t\".3gp\": \"video/3gpp\",\n\t\".3g2\": \"video/3gpp2\",\n\t\".7z\": \"application/x-7z-compressed\",\n};\n","import { configDotenv } from \"dotenv\";\nimport { existsSync, promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { serverError } from \"../response.ts\";\nimport Server from \"../server/Server.ts\";\nimport contentType from \"../server/contentType.ts\";\nimport Site from \"../site/Site\";\nimport prepareTemplate from \"./prepareTemplate.ts\";\nimport runBuild from \"./runBuild\";\n\nexport default async function runPreview(site: Site): Promise<void> {\n\t// Build the site\n\tawait runBuild(site);\n\n\tconst distFolder = path.resolve(site.root, \"dist\");\n\tconst clientFolder = path.join(distFolder, \"client\");\n\tconst serverFolder = path.join(distFolder, \"server\");\n\n\t// Find the client script file in /assets\n\tlet clientScript = (await fs.readdir(path.join(clientFolder, \"assets\"))).find((f) =>\n\t\tf.startsWith(\"clientEntry-\"),\n\t);\n\tif (!clientScript) {\n\t\tthrow new Error(\"clientEntry.js not found\");\n\t}\n\tclientScript = `/assets/${clientScript}`;\n\n\t// Read site.html\n\t// It's called site.html because @torpor/build builds the site (html, routes\n\t// etc) while the user builds the app (components etc)\n\tlet siteHtml = path.join(clientFolder, \"site.html\");\n\tlet template = await fs.readFile(siteHtml, \"utf-8\");\n\n\t// Prepare site.html so that we can just splice components into it\n\ttemplate = prepareTemplate(template, clientScript);\n\n\t// Configure the server\n\tconst server = new Server();\n\n\t// Serve dist/client/assets statically\n\tserver.add(\"/assets/*\", async (ev) => {\n\t\ttry {\n\t\t\tconst url = new URL(ev.request.url);\n\t\t\tconst file = path.join(site.root, \"dist\", \"client\", url.pathname);\n\t\t\tif (existsSync(file)) {\n\t\t\t\t// TODO: Stream the data?\n\t\t\t\tconst body = bufferToArrayBuffer(await fs.readFile(file));\n\t\t\t\treturn new Response(body, {\n\t\t\t\t\tstatus: 200,\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"Content-Type\": contentType(path.extname(file)),\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t}\n\t\t} catch (e: any) {\n\t\t\treturn serverError(e);\n\t\t}\n\t});\n\n\t// Every request (GET, POST, etc) goes through loadEndPoint\n\tconst serverScript = path.resolve(serverFolder, \"serverEntry.js\");\n\tserver.add(\"*\", async (ev) => {\n\t\ttry {\n\t\t\t// Load the server entry\n\t\t\tconst { load } = await import(serverScript);\n\n\t\t\t// Render the app HTML (or fetch server data etc) via serverEntry's\n\t\t\t// exported `load` function\n\t\t\treturn await load(ev, template);\n\t\t} catch (e: any) {\n\t\t\treturn serverError(e);\n\t\t}\n\t});\n\n\t// Load environment variables from a `.env` file, with defaults if not set\n\tconfigDotenv();\n\n\tprocess.env.PROTOCOL ??= \"http:\";\n\tprocess.env.HOST ??= \"localhost\";\n\tprocess.env.PORT ??= \"7059\";\n\n\t// Serve the site\n\tsite.adapter.serve(server, site);\n}\n\n// From https://stackoverflow.com/a/79345620\n// No idea why it is needed\nfunction bufferToArrayBuffer(buffer: Buffer): ArrayBuffer {\n\treturn buffer.buffer.slice(\n\t\tbuffer.byteOffset,\n\t\tbuffer.byteOffset + buffer.byteLength,\n\t) as ArrayBuffer;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { transformWithEsbuild } from \"vite\";\nimport Site from \"../site/Site\";\nimport runBuild from \"./runBuild\";\nimport runDev from \"./runDev\";\nimport runPreview from \"./runPreview\";\n\nexport default async function run(\n\tfolder: string,\n\tmode: \"dev\" | \"build\" | \"preview\",\n): Promise<void> {\n\t// Look for and load a site.config.js/ts file in the working directory\n\tlet configFile = \"\";\n\tlet deleteConfigFile = false;\n\tconst jsConfigFile = path.join(folder, \"site.config.js\");\n\tconst tsConfigFile = path.join(folder, \"site.config.ts\");\n\tif (fs.existsSync(jsConfigFile)) {\n\t\t// If it's JS, we can just import it straight up\n\t\tconfigFile = jsConfigFile;\n\t} else if (fs.existsSync(tsConfigFile)) {\n\t\t// If it's TS, we need to convert it to JS\n\t\t// TODO: Put the generated file somewhere better (dist?)\n\t\tconfigFile = path.join(folder, \"site.config.temp.js\");\n\t\tlet source = fs.readFileSync(tsConfigFile, \"utf-8\");\n\t\tlet transformed = await transformWithEsbuild(source, tsConfigFile, {\n\t\t\tloader: \"ts\",\n\t\t});\n\t\tfs.writeFileSync(configFile, transformed.code);\n\t\tdeleteConfigFile = true;\n\t} else {\n\t\tthrow new Error(\"site.config file not found\");\n\t}\n\n\tconst site = (await import(configFile)).default as Site;\n\tif (!site || !site.root || !site.routes) {\n\t\tthrow new Error(\"Invalid site in config file\");\n\t}\n\tif (deleteConfigFile) {\n\t\tfs.rmSync(configFile);\n\t}\n\n\tswitch (mode) {\n\t\tcase \"dev\": {\n\t\t\tawait runDev(site);\n\t\t\tbreak;\n\t\t}\n\t\tcase \"build\": {\n\t\t\tawait runBuild(site);\n\t\t\tbreak;\n\t\t}\n\t\tcase \"preview\": {\n\t\t\tawait runPreview(site);\n\t\t\tbreak;\n\t\t}\n\t\tdefault: {\n\t\t\tconsole.log(\"No mode passed, running in dev\");\n\t\t\tawait runDev(site);\n\t\t\tbreak;\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;AAKA,MAAM,WAAW;;;;;AAMjB,SAAwB,SAAS,MAAY,SAAS,OAAe;AACpE,QAAO;EACN,MAAM;EACN,WAAW;GACV,OAAO;GACP,QAAQ,IAAI;AACX,QAAI,OAAO,SACV,QAAO;;GAGT;EAED,KAAK,IAAI,aAAa;AACrB,OAAI,OAAO,UAAU;IACpB,IAAI,gBAAgB,UAAU,CAAC,CAAC,aAAa;AAC7C,WAAO;EACT,CAAC,gBAAgB,8CAA8C,GAAG;;;MAG9D,KAAK,OACP,KAAK,MAAM;AACX,SAAI,iBAAiB,CAAC,mBAAmB,KAAK,EAAE,KAAK,C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} from "./Site-D4x1l0p5.js";
|
|
5
3
|
|
|
4
|
+
//#region src/run/run.d.ts
|
|
6
5
|
declare function run(folder: string, mode: "dev" | "build" | "preview"): Promise<void>;
|
|
7
|
-
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/run/runBuild.d.ts
|
|
8
8
|
declare function runBuild(site: Site): Promise<void>;
|
|
9
|
-
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/run/runDev.d.ts
|
|
10
11
|
declare function runDev(site: Site): Promise<void>;
|
|
11
|
-
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/run/runPreview.d.ts
|
|
12
14
|
declare function runPreview(site: Site): Promise<void>;
|
|
13
|
-
|
|
15
|
+
//#endregion
|
|
14
16
|
export { run, runBuild, runDev, runPreview };
|
|
17
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","names":[],"sources":["../src/run/run.ts","../src/run/runBuild.ts","../src/run/runDev.ts","../src/run/runPreview.ts"],"sourcesContent":[],"mappings":";;;;iBAQ8B,GAAA,qDAG3B;;;iBCC2B,QAAA,OAAe,OAAO;;;iBCCtB,MAAA,OAAa,OAAO;;;iBCHpB,UAAA,OAAiB,OAAO"}
|
package/dist/run.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import "./chunk-WVXAN4IV.js";
|
|
10
|
-
import "./chunk-TDOXEXG6.js";
|
|
11
|
-
import "./chunk-SJVHHGVW.js";
|
|
12
|
-
export {
|
|
13
|
-
run,
|
|
14
|
-
runBuild,
|
|
15
|
-
runDev,
|
|
16
|
-
runPreview
|
|
17
|
-
};
|
|
18
|
-
//# sourceMappingURL=run.js.map
|
|
1
|
+
import "./pathToRegex-CTgx7hQP.js";
|
|
2
|
+
import "./seeOther-BmehiI4C.js";
|
|
3
|
+
import "./response-D-XpIuMH.js";
|
|
4
|
+
import { run, runBuild, runDev, runPreview } from "./run-CNdaKSPR.js";
|
|
5
|
+
import "./ServerEvent-2-CnAy1W.js";
|
|
6
|
+
import "./connectMiddleware-DbLj0St1.js";
|
|
7
|
+
|
|
8
|
+
export { run, runBuild, runDev, runPreview };
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
//#region src/response/response.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a response with the supplied status code and optional body.
|
|
4
|
+
*
|
|
5
|
+
* If the body is an object, it will be converted to JSON and the content-type
|
|
6
|
+
* header set to "application/json". Otherwise, if the body is a string, the
|
|
7
|
+
* content-type header will be set to "text/plain".
|
|
8
|
+
*/
|
|
9
|
+
function response(status, body) {
|
|
10
|
+
let headers = void 0;
|
|
11
|
+
if (body) if (typeof body === "object") {
|
|
12
|
+
headers = { "content-type": "application/json" };
|
|
13
|
+
body = JSON.stringify(body);
|
|
14
|
+
} else headers = { "content-type": "text/plain" };
|
|
15
|
+
return new Response(body, {
|
|
16
|
+
status,
|
|
17
|
+
headers
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/response/transfer.ts
|
|
23
|
+
/**
|
|
24
|
+
* Creates a redirect response with the supplied status code.
|
|
25
|
+
*/
|
|
26
|
+
function transfer(status, location) {
|
|
27
|
+
return new Response(null, {
|
|
28
|
+
status,
|
|
29
|
+
headers: { location }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/response/notFound.ts
|
|
35
|
+
/**
|
|
36
|
+
* 404 Not Found
|
|
37
|
+
*
|
|
38
|
+
* The HTTP 404 Not Found client error response status code indicates that the
|
|
39
|
+
* server cannot find the requested resource. Links that lead to a 404 page are
|
|
40
|
+
* often called broken or dead links and can be subject to link rot.
|
|
41
|
+
*
|
|
42
|
+
* A 404 status code only indicates that the resource is missing without
|
|
43
|
+
* indicating if this is temporary or permanent. If a resource is permanently
|
|
44
|
+
* removed, servers should send the 410 Gone status instead.
|
|
45
|
+
*
|
|
46
|
+
* 404 errors on a website can lead to a poor user experience for your visitors,
|
|
47
|
+
* so the number of broken links (internal and external) should be minimized to
|
|
48
|
+
* prevent frustration for readers. Common causes of 404 responses are mistyped
|
|
49
|
+
* URLs or pages that are moved or deleted without redirection. For more
|
|
50
|
+
* information, see the Redirections in HTTP guide.
|
|
51
|
+
*
|
|
52
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/404
|
|
53
|
+
*/
|
|
54
|
+
function notFound(body) {
|
|
55
|
+
return response(404, body ?? "Not found");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/response/ok.ts
|
|
60
|
+
/**
|
|
61
|
+
* 200 OK
|
|
62
|
+
*
|
|
63
|
+
* The HTTP 200 OK successful response status code indicates that a request has
|
|
64
|
+
* succeeded. A 200 OK response is cacheable by default.
|
|
65
|
+
*
|
|
66
|
+
* A 200 OK response has a different meaning and format depending on the HTTP
|
|
67
|
+
* request method. Here's how they vary for different methods:
|
|
68
|
+
*
|
|
69
|
+
* - GET: A resource was retrieved by the server and included in the response
|
|
70
|
+
* body.
|
|
71
|
+
* - POST: An action succeeded; the response has a message body describing the
|
|
72
|
+
* result.
|
|
73
|
+
* - HEAD: Identical to GET, except there is no message body.
|
|
74
|
+
* - TRACE: The response has a message body containing the request as received
|
|
75
|
+
* by the server.
|
|
76
|
+
*
|
|
77
|
+
* Although possible, successful PUT or DELETE requests often do not result in a
|
|
78
|
+
* 200 OK response. It is more common to see 201 Created if the resource is
|
|
79
|
+
* uploaded or created for the first time, or 204 No Content upon successful
|
|
80
|
+
* deletion of a resource.
|
|
81
|
+
*
|
|
82
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/200
|
|
83
|
+
*/
|
|
84
|
+
function ok(body) {
|
|
85
|
+
return response(200, body);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/response/seeOther.ts
|
|
90
|
+
/**
|
|
91
|
+
* 303 See Other
|
|
92
|
+
*
|
|
93
|
+
* The HTTP 303 See Other redirection response status code indicates that the
|
|
94
|
+
* browser should redirect to the URL in the Location header instead of
|
|
95
|
+
* rendering the requested resource.
|
|
96
|
+
*
|
|
97
|
+
* This response code is often sent back as a result of PUT or POST methods so
|
|
98
|
+
* the client may retrieve a confirmation, or view a representation of a
|
|
99
|
+
* real-world object (see HTTP range-14). The method to retrieve the redirected
|
|
100
|
+
* resource is always GET.
|
|
101
|
+
*
|
|
102
|
+
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303
|
|
103
|
+
*/
|
|
104
|
+
function seeOther(location) {
|
|
105
|
+
return transfer(303, location);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//#endregion
|
|
109
|
+
export { notFound, ok, response, seeOther, transfer };
|
|
110
|
+
//# sourceMappingURL=seeOther-BmehiI4C.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seeOther-BmehiI4C.js","names":["headers: Record<string, string> | undefined"],"sources":["../src/response/response.ts","../src/response/transfer.ts","../src/response/notFound.ts","../src/response/ok.ts","../src/response/seeOther.ts"],"sourcesContent":["/**\n * Creates a response with the supplied status code and optional body.\n *\n * If the body is an object, it will be converted to JSON and the content-type\n * header set to \"application/json\". Otherwise, if the body is a string, the\n * content-type header will be set to \"text/plain\".\n */\nexport default function response(status: number, body?: object | string): Response {\n\tlet headers: Record<string, string> | undefined = undefined;\n\tif (body) {\n\t\tif (typeof body === \"object\") {\n\t\t\theaders = { \"content-type\": \"application/json\" };\n\t\t\tbody = JSON.stringify(body);\n\t\t} else {\n\t\t\theaders = { \"content-type\": \"text/plain\" };\n\t\t}\n\t}\n\treturn new Response(body, {\n\t\tstatus,\n\t\theaders,\n\t});\n}\n","/**\n * Creates a redirect response with the supplied status code.\n */\nexport default function transfer(\n\tstatus: 300 | 301 | 302 | 303 | 304 | 307 | 308,\n\tlocation: string,\n): Response {\n\treturn new Response(null, {\n\t\tstatus,\n\t\theaders: {\n\t\t\tlocation,\n\t\t},\n\t});\n}\n","import response from \"./response\";\n\n/**\n * 404 Not Found\n *\n * The HTTP 404 Not Found client error response status code indicates that the\n * server cannot find the requested resource. Links that lead to a 404 page are\n * often called broken or dead links and can be subject to link rot.\n *\n * A 404 status code only indicates that the resource is missing without\n * indicating if this is temporary or permanent. If a resource is permanently\n * removed, servers should send the 410 Gone status instead.\n *\n * 404 errors on a website can lead to a poor user experience for your visitors,\n * so the number of broken links (internal and external) should be minimized to\n * prevent frustration for readers. Common causes of 404 responses are mistyped\n * URLs or pages that are moved or deleted without redirection. For more\n * information, see the Redirections in HTTP guide.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/404\n */\nexport default function notFound(body?: object | string): Response {\n\treturn response(404, body ?? \"Not found\");\n}\n","import response from \"./response\";\n\n/**\n * 200 OK\n *\n * The HTTP 200 OK successful response status code indicates that a request has\n * succeeded. A 200 OK response is cacheable by default.\n *\n * A 200 OK response has a different meaning and format depending on the HTTP\n * request method. Here's how they vary for different methods:\n *\n * - GET: A resource was retrieved by the server and included in the response\n * body.\n * - POST: An action succeeded; the response has a message body describing the\n * result.\n * - HEAD: Identical to GET, except there is no message body.\n * - TRACE: The response has a message body containing the request as received\n * by the server.\n *\n * Although possible, successful PUT or DELETE requests often do not result in a\n * 200 OK response. It is more common to see 201 Created if the resource is\n * uploaded or created for the first time, or 204 No Content upon successful\n * deletion of a resource.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/200\n */\nexport default function ok(body?: object | string): Response {\n\treturn response(200, body);\n}\n","import transfer from \"./transfer\";\n\n/**\n * 303 See Other\n *\n * The HTTP 303 See Other redirection response status code indicates that the\n * browser should redirect to the URL in the Location header instead of\n * rendering the requested resource.\n *\n * This response code is often sent back as a result of PUT or POST methods so\n * the client may retrieve a confirmation, or view a representation of a\n * real-world object (see HTTP range-14). The method to retrieve the redirected\n * resource is always GET.\n *\n * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/303\n */\nexport default function seeOther(location: string): Response {\n\treturn transfer(303, location);\n}\n"],"mappings":";;;;;;;;AAOA,SAAwB,SAAS,QAAgB,MAAkC;CAClF,IAAIA,UAA8C;AAClD,KAAI,KACH,KAAI,OAAO,SAAS,UAAU;AAC7B,YAAU,EAAE,gBAAgB,oBAAoB;AAChD,SAAO,KAAK,UAAU,KAAK;OAE3B,WAAU,EAAE,gBAAgB,cAAc;AAG5C,QAAO,IAAI,SAAS,MAAM;EACzB;EACA;EACA,CAAC;;;;;;;;ACjBH,SAAwB,SACvB,QACA,UACW;AACX,QAAO,IAAI,SAAS,MAAM;EACzB;EACA,SAAS,EACR,UACA;EACD,CAAC;;;;;;;;;;;;;;;;;;;;;;;;ACSH,SAAwB,SAAS,MAAkC;AAClE,QAAO,SAAS,KAAK,QAAQ,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACI1C,SAAwB,GAAG,MAAkC;AAC5D,QAAO,SAAS,KAAK,KAAK;;;;;;;;;;;;;;;;;;;ACX3B,SAAwB,SAAS,UAA4B;AAC5D,QAAO,SAAS,KAAK,SAAS"}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { CookieHelper, HeaderHelper, MiddlewareFunction, Server, ServerEvent } from "./Server-CSpY_-Ba.js";
|
|
2
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
3
|
+
|
|
4
|
+
//#region src/server/connect/connectMiddleware.d.ts
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
* Converts a Connect-style middleware to a web-compatible request handler.
|
|
8
|
+
*
|
|
9
|
+
* @param handler - The Connect-style middleware function to be converted.
|
|
10
|
+
* @returns A function that handles web requests and returns a Response or undefined.
|
|
11
|
+
*/
|
|
12
12
|
declare function connectMiddleware(handler: ConnectMiddleware | ConnectMiddlewareBoolean): MiddlewareFunction;
|
|
13
13
|
type NextFunction = (err?: unknown) => void;
|
|
14
14
|
type ConnectMiddleware<PlatformRequest extends IncomingMessage = IncomingMessage, PlatformResponse extends ServerResponse = ServerResponse> = (req: PlatformRequest, res: PlatformResponse, next: NextFunction) => void | Promise<void>;
|
|
15
15
|
type ConnectMiddlewareBoolean<PlatformRequest extends IncomingMessage = IncomingMessage, PlatformResponse extends ServerResponse = ServerResponse> = (req: PlatformRequest, res: PlatformResponse, next: NextFunction) => boolean | Promise<boolean>;
|
|
16
|
-
|
|
17
|
-
export { connectMiddleware };
|
|
16
|
+
//#endregion
|
|
17
|
+
export { CookieHelper, HeaderHelper, Server, ServerEvent, connectMiddleware };
|
|
18
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","names":[],"sources":["../src/server/connect/connectMiddleware.ts"],"sourcesContent":[],"mappings":";;;;;;;AACoC;;;;AAiBjC,iBAFqB,iBAAA,CAErB,OAAA,EADO,iBACP,GAD2B,wBAC3B,CAAA,EAAA,kBAAA;KAsDE,YAAA,GAtDF,CAAA,GAAA,CAAA,EAAA,OAAA,EAAA,GAAA,IAAA;AAAA,KAuDE,iBADA,CAAA,wBAEoB,eADpB,GACsC,eADtC,EAAA,yBAEqB,cAFrB,GAEsC,cAFtC,QACoB,EAEf,eAFe,EAAA,GAAA,EAEO,gBAFP,EAAA,IAAA,EAE+B,YAF/B,EAAA,GAAA,IAAA,GAEuD,OAFvD,CAAA,IAAA,CAAA;KAGpB,wBAHsC,yBAIlB,eAHC,GAGiB,eAHjB,2BAIA,cAJiB,GAIA,cAJA,QACjC,EAIA,eAJA,EAAA,GAAA,EAIsB,gBAJtB,EAAA,IAAA,EAI8C,YAJ9C,EAAA,GAAA,OAAA,GAIyE,OAJzE,CAAA,OAAA,CAAA"}
|
package/dist/server.js
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
CookieHelper,
|
|
7
|
-
HeaderHelper,
|
|
8
|
-
ServerEvent
|
|
9
|
-
} from "./chunk-TDOXEXG6.js";
|
|
10
|
-
import "./chunk-SJVHHGVW.js";
|
|
11
|
-
export {
|
|
12
|
-
CookieHelper,
|
|
13
|
-
HeaderHelper,
|
|
14
|
-
Server,
|
|
15
|
-
ServerEvent,
|
|
16
|
-
connectMiddleware
|
|
17
|
-
};
|
|
18
|
-
//# sourceMappingURL=server.js.map
|
|
1
|
+
import "./pathToRegex-CTgx7hQP.js";
|
|
2
|
+
import { CookieHelper, HeaderHelper, ServerEvent } from "./ServerEvent-2-CnAy1W.js";
|
|
3
|
+
import { Server, connectMiddleware } from "./connectMiddleware-DbLj0St1.js";
|
|
4
|
+
|
|
5
|
+
export { CookieHelper, HeaderHelper, Server, ServerEvent, connectMiddleware };
|