@torpor/build 1.4.6 → 1.6.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/README.md +3 -2
- package/dist/Router-C4ADUfhv.mjs.map +1 -1
- package/dist/Server-C0PGzSQO.mjs.map +1 -1
- package/dist/ServerEvent-BY4f3RVF.mjs.map +1 -1
- package/dist/{Site-CQ29Opvm.d.mts → Site-DW-gKnPv.d.mts} +25 -3
- package/dist/{Site-CQ29Opvm.d.mts.map → Site-DW-gKnPv.d.mts.map} +1 -1
- package/dist/bin/index.js +1 -1
- package/dist/clientEntry.mjs +4 -4
- package/dist/clientEntry.mjs.map +1 -1
- package/dist/dataRequest-JosDB_ig.mjs +50 -0
- package/dist/dataRequest-JosDB_ig.mjs.map +1 -0
- package/dist/dev.mjs +1 -1
- package/dist/devPlugin-V689J6l6.mjs +144 -0
- package/dist/devPlugin-V689J6l6.mjs.map +1 -0
- package/dist/env.d.mts +2 -3
- package/dist/env.d.mts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +54 -2
- package/dist/index.mjs.map +1 -1
- package/dist/load-LWQoFWh-.mjs +349 -0
- package/dist/load-LWQoFWh-.mjs.map +1 -0
- package/dist/nav.mjs +1 -1
- package/dist/nodeDev.d.mts +16 -0
- package/dist/nodeDev.d.mts.map +1 -0
- package/dist/nodeDev.mjs +2 -0
- package/dist/openapi.d.mts +4 -4
- package/dist/openapi.d.mts.map +1 -1
- package/dist/pathTrie-D4Ax2Mu9.mjs.map +1 -1
- package/dist/{response-CuNPcxsY.mjs → response-sth6FQsj.mjs} +33 -3
- package/dist/response-sth6FQsj.mjs.map +1 -0
- package/dist/response.mjs +2 -2
- package/dist/run.d.mts +8 -2
- package/dist/run.d.mts.map +1 -1
- package/dist/run.mjs +1 -1
- package/dist/{runOpenApi-CbbrPi4E.mjs → runOpenApi-DTUtB-XC.mjs} +159 -187
- package/dist/runOpenApi-DTUtB-XC.mjs.map +1 -0
- package/dist/serverEntry-fj0XfTs4.d.mts.map +1 -1
- package/dist/{serverEntry-D3uwudkX.mjs → serverEntry-o6kk_-Wo.mjs} +2 -2
- package/dist/{serverEntry-D3uwudkX.mjs.map → serverEntry-o6kk_-Wo.mjs.map} +1 -1
- package/dist/serverEntry.mjs +1 -1
- package/dist/{serverHandlers-C8qzOSX-.mjs → serverHandlers-F1gJ1lrj.mjs} +194 -30
- package/dist/serverHandlers-F1gJ1lrj.mjs.map +1 -0
- package/dist/test.d.mts +1 -1
- package/dist/test.mjs +1 -1
- package/dist/{unprocessable-D6RcYLh6.mjs → unprocessable-CoMN2zFa.mjs} +2 -32
- package/dist/unprocessable-CoMN2zFa.mjs.map +1 -0
- package/package.json +11 -6
- package/dist/load-Uyj9I60p.mjs +0 -246
- package/dist/load-Uyj9I60p.mjs.map +0 -1
- package/dist/response-CuNPcxsY.mjs.map +0 -1
- package/dist/runOpenApi-CbbrPi4E.mjs.map +0 -1
- package/dist/serverHandlers-C8qzOSX-.mjs.map +0 -1
- package/dist/unprocessable-D6RcYLh6.mjs.map +0 -1
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
import { i as normalizeOrigin, r as normalizeBasePath } from "./basePath-DQnbHc7o.mjs";
|
|
2
2
|
import { i as buildOpenApiDocument, t as OPEN_API_STATE_KEY } from "./plugin-D8Www11B.mjs";
|
|
3
|
-
import { r as serverError } from "./response-
|
|
3
|
+
import { r as serverError } from "./response-sth6FQsj.mjs";
|
|
4
|
+
import { i as siteEntryPaths, n as prepareTemplate, r as detectSourceMode, t as devPlugin } from "./devPlugin-V689J6l6.mjs";
|
|
4
5
|
import { t as ServerEvent } from "./ServerEvent-BY4f3RVF.mjs";
|
|
5
6
|
import { n as sitemapXml } from "./sitemap-4imxJVrG.mjs";
|
|
6
7
|
import { t as Server } from "./Server-C0PGzSQO.mjs";
|
|
7
|
-
import { t as flattenHeaders } from "./flattenHeaders-DM5qsSqO.mjs";
|
|
8
8
|
import { createRequire, isBuiltin } from "node:module";
|
|
9
9
|
import fs, { existsSync, promises, readFileSync, readdirSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
10
10
|
import fpath from "node:path";
|
|
11
|
-
import { build, createServer, defineConfig } from "vite";
|
|
12
11
|
import torpor from "@torpor/unplugin/vite";
|
|
13
12
|
import { configDotenv } from "dotenv";
|
|
13
|
+
import { build, createServer, defineConfig } from "vite";
|
|
14
14
|
import { parse } from "@torpor/view/compile";
|
|
15
15
|
import { pathToFileURL } from "node:url";
|
|
16
|
-
import { Readable } from "node:stream";
|
|
17
16
|
import { createHash } from "node:crypto";
|
|
18
17
|
import { writeFile } from "node:fs/promises";
|
|
19
18
|
//#region src/utils/tsconfigAliases.ts
|
|
@@ -508,7 +507,8 @@ function walk(node) {
|
|
|
508
507
|
}
|
|
509
508
|
//#endregion
|
|
510
509
|
//#region src/site/manifest.ts
|
|
511
|
-
const
|
|
510
|
+
const MANIFEST_MODULE_ID = "@torpor/build/manifest";
|
|
511
|
+
const moduleId = MANIFEST_MODULE_ID;
|
|
512
512
|
const LOAD_EXPORT_RE = /[{,]\s*load\s*[:(]|\bexport\s+(?:async\s+)?(?:function|const|let|var)\s+load\b|\bexport\s*\{[^}]*\bload\b/;
|
|
513
513
|
function hasLoadExport(source) {
|
|
514
514
|
return LOAD_EXPORT_RE.test(source);
|
|
@@ -736,64 +736,6 @@ function resolveFromNodeModules(name, require) {
|
|
|
736
736
|
}
|
|
737
737
|
}
|
|
738
738
|
//#endregion
|
|
739
|
-
//#region src/utils/entryPaths.ts
|
|
740
|
-
/**
|
|
741
|
-
* Whether tb should run the framework from its TypeScript source instead of
|
|
742
|
-
* its compiled dist. This is a framework-development convenience: registry
|
|
743
|
-
* installs always run dist. Source mode is enabled explicitly via the
|
|
744
|
-
* TORPOR_SOURCE_DEV env var, or automatically when both `@torpor/build` and
|
|
745
|
-
* `@torpor/view` are symlinked into the app from outside node_modules (as
|
|
746
|
-
* workspace links and `link:` overrides do). Both packages must be linked,
|
|
747
|
-
* because source mode resolves their `torpor:source` export conditions, which
|
|
748
|
-
* point at src files that registry installs don't ship.
|
|
749
|
-
*/
|
|
750
|
-
function detectSourceMode(siteRoot) {
|
|
751
|
-
if (process.env.TORPOR_SOURCE_DEV) return true;
|
|
752
|
-
return ["@torpor/build", "@torpor/view"].every((pkg) => {
|
|
753
|
-
try {
|
|
754
|
-
const dist = fpath.resolve(siteRoot, "node_modules", pkg, "dist/index.mjs");
|
|
755
|
-
return !realpathSync(dist).includes("node_modules");
|
|
756
|
-
} catch {
|
|
757
|
-
return false;
|
|
758
|
-
}
|
|
759
|
-
});
|
|
760
|
-
}
|
|
761
|
-
/**
|
|
762
|
-
* The site entry files for the given mode: raw TypeScript sources when the
|
|
763
|
-
* framework is linked (Vite transforms them, and edits show up without a
|
|
764
|
-
* rebuild), compiled `.mjs` bundles for registry installs.
|
|
765
|
-
*/
|
|
766
|
-
function siteEntryPaths(siteRoot, sourceMode) {
|
|
767
|
-
const folder = fpath.resolve(siteRoot, "node_modules/@torpor/build", sourceMode ? "src/site" : "dist");
|
|
768
|
-
const ext = sourceMode ? ".ts" : ".mjs";
|
|
769
|
-
return {
|
|
770
|
-
serverEntry: fpath.join(folder, `serverEntry${ext}`),
|
|
771
|
-
clientEntry: fpath.join(folder, `clientEntry${ext}`),
|
|
772
|
-
clientDevEntry: fpath.join(folder, `clientEntryDev${ext}`)
|
|
773
|
-
};
|
|
774
|
-
}
|
|
775
|
-
//#endregion
|
|
776
|
-
//#region src/run/prepareTemplate.ts
|
|
777
|
-
function prepareTemplate(template, clientScript, clientDevScript) {
|
|
778
|
-
let result = template;
|
|
779
|
-
let headStart = result.indexOf("</head>");
|
|
780
|
-
if (headStart === -1) throw new Error(`Couldn't find <head> end tag`);
|
|
781
|
-
result = result.substring(0, headStart) + "%COMPONENT_HEAD%" + result.substring(headStart);
|
|
782
|
-
let divMatchStart = regexIndexOf(result, /<div\s+id=("app"|'app'|app)[\s>]/);
|
|
783
|
-
if (divMatchStart === -1) throw new Error(`Couldn't find <div id="app"></div>`);
|
|
784
|
-
let bodyStart = result.indexOf(">", divMatchStart) + 1;
|
|
785
|
-
let bodyEnd = result.indexOf("</div>", bodyStart);
|
|
786
|
-
if (bodyEnd === -1) throw new Error(`Couldn't find <div id="app"></div>`);
|
|
787
|
-
result = result.substring(0, bodyStart) + "%COMPONENT_BODY%" + result.substring(bodyEnd);
|
|
788
|
-
if (clientDevScript) result += `<script type="module" src="${clientDevScript}"><\/script>`;
|
|
789
|
-
result += `<script type="module" src="${clientScript}"><\/script>`;
|
|
790
|
-
return result;
|
|
791
|
-
}
|
|
792
|
-
function regexIndexOf(string, regex, position) {
|
|
793
|
-
var indexOf = string.substring(position || 0).search(regex);
|
|
794
|
-
return indexOf >= 0 ? indexOf + (position || 0) : indexOf;
|
|
795
|
-
}
|
|
796
|
-
//#endregion
|
|
797
739
|
//#region src/run/runPrerender.ts
|
|
798
740
|
/**
|
|
799
741
|
* Resolves which page paths should be prerendered, from the routes of a
|
|
@@ -886,12 +828,15 @@ async function runPrerender(site) {
|
|
|
886
828
|
await writeFileForPath(clientFolder, entry.path, await response.text());
|
|
887
829
|
written++;
|
|
888
830
|
}
|
|
889
|
-
if (routes.find((r) => r.type === 8) && template !== void 0) {
|
|
890
|
-
const url =
|
|
831
|
+
if (routes.find((r) => r.type === 8) && template !== void 0) for (const status of [404, 500]) {
|
|
832
|
+
const url = `http://torpor.build/_error?status=${status}`;
|
|
891
833
|
const serverEvent = new ServerEvent(new Request(url), void 0, new URL(url));
|
|
892
834
|
const response = await serverEntry.load(serverEvent, template);
|
|
893
|
-
if (isRedirect(response))
|
|
894
|
-
|
|
835
|
+
if (isRedirect(response)) {
|
|
836
|
+
skipped.push(`/_error (status ${response.status})`);
|
|
837
|
+
break;
|
|
838
|
+
}
|
|
839
|
+
if (response.status === status && response.headers.get("Content-Type")?.includes("text/html")) await promises.writeFile(fpath.join(clientFolder, `${status}.html`), await response.text());
|
|
895
840
|
else failures.push(`/_error (status ${await responseStatus(response)})`);
|
|
896
841
|
}
|
|
897
842
|
if (skipped.length) console.log(`Skipped prerendering ${skipped.length} route${skipped.length === 1 ? "" : "s"} (redirected):\n` + skipped.map((s) => ` - ${s}`).join("\n"));
|
|
@@ -1052,83 +997,6 @@ function asAliasArray$1(alias) {
|
|
|
1052
997
|
}));
|
|
1053
998
|
}
|
|
1054
999
|
//#endregion
|
|
1055
|
-
//#region src/run/devPlugin.ts
|
|
1056
|
-
/**
|
|
1057
|
-
* The default Node-runtime dev plugin. Reads `src/site.html`, sets up a torpor
|
|
1058
|
-
* `Server` whose catch-all route renders via `vite.ssrLoadModule`, and registers
|
|
1059
|
-
* a Connect middleware (after Vite's own middlewares) that bridges Node
|
|
1060
|
-
* req/res to the torpor Server's fetch handler.
|
|
1061
|
-
*
|
|
1062
|
-
* Adapters that need a different runtime (e.g. workerd) provide their own
|
|
1063
|
-
* `dev()` plugin instead.
|
|
1064
|
-
*/
|
|
1065
|
-
function devPlugin(site, sourceMode = false) {
|
|
1066
|
-
return {
|
|
1067
|
-
name: "torpor-dev",
|
|
1068
|
-
configureServer(vite) {
|
|
1069
|
-
const handlerPromise = createDevHandler(vite, site, sourceMode);
|
|
1070
|
-
return () => {
|
|
1071
|
-
vite.middlewares.use(async (req, res, next) => {
|
|
1072
|
-
try {
|
|
1073
|
-
await (await handlerPromise)(req, res);
|
|
1074
|
-
} catch (e) {
|
|
1075
|
-
next(e);
|
|
1076
|
-
}
|
|
1077
|
-
});
|
|
1078
|
-
};
|
|
1079
|
-
}
|
|
1080
|
-
};
|
|
1081
|
-
}
|
|
1082
|
-
async function createDevHandler(vite, site, sourceMode) {
|
|
1083
|
-
const entries = siteEntryPaths(site.root, sourceMode);
|
|
1084
|
-
const serverScript = entries.serverEntry;
|
|
1085
|
-
let template;
|
|
1086
|
-
const templateFile = fpath.resolve(site.root, "src/site.html");
|
|
1087
|
-
if (existsSync(templateFile)) {
|
|
1088
|
-
template = await promises.readFile(templateFile, "utf-8");
|
|
1089
|
-
template = await vite.transformIndexHtml("", template);
|
|
1090
|
-
template = prepareTemplate(template, entries.clientEntry, entries.clientDevEntry);
|
|
1091
|
-
}
|
|
1092
|
-
const server = new Server();
|
|
1093
|
-
server.add("*", async (ev) => {
|
|
1094
|
-
try {
|
|
1095
|
-
const { load } = await vite.ssrLoadModule(serverScript);
|
|
1096
|
-
return await load(ev, template);
|
|
1097
|
-
} catch (e) {
|
|
1098
|
-
vite.ssrFixStacktrace(e);
|
|
1099
|
-
return serverError(e);
|
|
1100
|
-
}
|
|
1101
|
-
});
|
|
1102
|
-
globalThis.adapter = { env: process.env };
|
|
1103
|
-
return async (req, res) => {
|
|
1104
|
-
const request = await nodeReqToRequest(req);
|
|
1105
|
-
await sendResponse(await server.fetch(request), res);
|
|
1106
|
-
};
|
|
1107
|
-
}
|
|
1108
|
-
async function nodeReqToRequest(req) {
|
|
1109
|
-
const url = req.url ?? "/";
|
|
1110
|
-
const host = req.headers.host ?? "localhost";
|
|
1111
|
-
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https" : "http";
|
|
1112
|
-
const init = {
|
|
1113
|
-
method: req.method,
|
|
1114
|
-
headers: flattenHeaders(req.headers)
|
|
1115
|
-
};
|
|
1116
|
-
if (req.method !== "GET" && req.method !== "HEAD") {
|
|
1117
|
-
init.body = Readable.toWeb(req);
|
|
1118
|
-
init.duplex = "half";
|
|
1119
|
-
}
|
|
1120
|
-
return new Request(`${protocol}://${host}${url}`, init);
|
|
1121
|
-
}
|
|
1122
|
-
async function sendResponse(response, res) {
|
|
1123
|
-
res.statusCode = response.status;
|
|
1124
|
-
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
1125
|
-
if (!response.body) {
|
|
1126
|
-
res.end();
|
|
1127
|
-
return;
|
|
1128
|
-
}
|
|
1129
|
-
Readable.fromWeb(response.body).pipe(res);
|
|
1130
|
-
}
|
|
1131
|
-
//#endregion
|
|
1132
1000
|
//#region src/run/depCache.ts
|
|
1133
1001
|
/**
|
|
1134
1002
|
* Vite's dep-optimization cache (node_modules/.vite) keys off the lockfile
|
|
@@ -1263,6 +1131,78 @@ function fingerprintDir(dir, root, hash) {
|
|
|
1263
1131
|
}
|
|
1264
1132
|
}
|
|
1265
1133
|
//#endregion
|
|
1134
|
+
//#region src/run/loadSite.ts
|
|
1135
|
+
/**
|
|
1136
|
+
* Finds the site.config.js/ts file in the given folder.
|
|
1137
|
+
*/
|
|
1138
|
+
function findConfigFile(folder) {
|
|
1139
|
+
const jsConfigFile = fpath.join(folder, "site.config.js");
|
|
1140
|
+
const tsConfigFile = fpath.join(folder, "site.config.ts");
|
|
1141
|
+
if (fs.existsSync(jsConfigFile)) return jsConfigFile;
|
|
1142
|
+
if (fs.existsSync(tsConfigFile)) return tsConfigFile;
|
|
1143
|
+
throw new Error("site.config file not found");
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Creates the temporary Vite server used to load the site config, so that
|
|
1147
|
+
* local TS imports (e.g. a routes.ts file) and path aliases are resolved
|
|
1148
|
+
* correctly. Kept separate from the app's dev server: the config is loaded
|
|
1149
|
+
* before that server exists.
|
|
1150
|
+
*/
|
|
1151
|
+
function createConfigVite() {
|
|
1152
|
+
return createServer({
|
|
1153
|
+
server: { middlewareMode: true },
|
|
1154
|
+
appType: "custom",
|
|
1155
|
+
resolve: { tsconfigPaths: true },
|
|
1156
|
+
optimizeDeps: { noDiscovery: true }
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* Loads (or, when the config module has been invalidated, re-loads) the site
|
|
1161
|
+
* config through an existing config Vite server and runs its plugins.
|
|
1162
|
+
*/
|
|
1163
|
+
async function loadSiteFromVite(vite, configFile) {
|
|
1164
|
+
const site = (await vite.ssrLoadModule(configFile)).default;
|
|
1165
|
+
site.configFile = configFile;
|
|
1166
|
+
if (!site || !site.root || !site.routes) throw new Error("Invalid site in config file");
|
|
1167
|
+
for (const plugin of site.plugins) await plugin(site);
|
|
1168
|
+
return site;
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Looks for and loads a site.config.js/ts file in the working directory,
|
|
1172
|
+
* running the site's plugins. The Vite server used to load the config is
|
|
1173
|
+
* returned as well, so that (for example) route files can be loaded through
|
|
1174
|
+
* it; callers are responsible for closing it.
|
|
1175
|
+
*/
|
|
1176
|
+
async function loadSite(folder) {
|
|
1177
|
+
const configFile = findConfigFile(folder);
|
|
1178
|
+
const vite = await createConfigVite();
|
|
1179
|
+
try {
|
|
1180
|
+
return {
|
|
1181
|
+
site: await loadSiteFromVite(vite, configFile),
|
|
1182
|
+
vite
|
|
1183
|
+
};
|
|
1184
|
+
} catch (error) {
|
|
1185
|
+
await vite.close();
|
|
1186
|
+
throw error;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* The site-root files the config depends on -- the config itself and any
|
|
1191
|
+
* modules it imports (e.g. a `routes.ts` that calls `addRoute`). The dev
|
|
1192
|
+
* server watches these so route changes defined in code are picked up, not
|
|
1193
|
+
* just edits to `site.config.ts` itself.
|
|
1194
|
+
*/
|
|
1195
|
+
function configDependencyFiles(vite, root) {
|
|
1196
|
+
const files = /* @__PURE__ */ new Set();
|
|
1197
|
+
const graph = vite.environments.ssr.moduleGraph;
|
|
1198
|
+
for (const mod of graph.idToModuleMap.values()) if (mod.file && isInside$1(root, mod.file)) files.add(mod.file);
|
|
1199
|
+
return files;
|
|
1200
|
+
}
|
|
1201
|
+
function isInside$1(dir, file) {
|
|
1202
|
+
const rel = fpath.relative(dir, file);
|
|
1203
|
+
return rel === "" || !rel.startsWith("..") && !fpath.isAbsolute(rel);
|
|
1204
|
+
}
|
|
1205
|
+
//#endregion
|
|
1266
1206
|
//#region src/run/staleTorpCopies.ts
|
|
1267
1207
|
/**
|
|
1268
1208
|
* Workspace packages that ship copies of their .torp components (e.g.
|
|
@@ -1334,7 +1274,13 @@ function packageLabel(dir, workspaceRoot) {
|
|
|
1334
1274
|
}
|
|
1335
1275
|
//#endregion
|
|
1336
1276
|
//#region src/run/runDev.ts
|
|
1337
|
-
|
|
1277
|
+
/**
|
|
1278
|
+
* @param configVite The Vite server used to load the site config (see
|
|
1279
|
+
* `loadSite`). Its module graph tells the dev server which files the config
|
|
1280
|
+
* imports, so `addRoute` calls in those files (e.g. a `routes.ts`) trigger a
|
|
1281
|
+
* route reload too. Without it, only file-based routes are reloaded.
|
|
1282
|
+
*/
|
|
1283
|
+
async function runDev(site, configVite) {
|
|
1338
1284
|
const config = structuredClone(site.viteConfig ?? {});
|
|
1339
1285
|
config.appType = "custom";
|
|
1340
1286
|
config.server ??= {};
|
|
@@ -1376,7 +1322,7 @@ async function runDev(site) {
|
|
|
1376
1322
|
const vite = await createServer(config);
|
|
1377
1323
|
await warmServerEntry(vite, site, sourceMode);
|
|
1378
1324
|
await vite.listen();
|
|
1379
|
-
|
|
1325
|
+
watchRoutes(site, vite, configVite);
|
|
1380
1326
|
const listeningUrl = vite.resolvedUrls?.local?.[0] ?? connectingUrl;
|
|
1381
1327
|
console.log(`Listening on ${listeningUrl}\n`);
|
|
1382
1328
|
}
|
|
@@ -1397,12 +1343,20 @@ async function warmServerEntry(vite, site, sourceMode) {
|
|
|
1397
1343
|
* Reports route type issues at startup, then re-checks route files and
|
|
1398
1344
|
* `makeApi` calls as they change so annotation problems surface during
|
|
1399
1345
|
* development without failing the dev server.
|
|
1346
|
+
*
|
|
1347
|
+
* Also watches the config's import graph and the registered route folders,
|
|
1348
|
+
* re-loading the site config when route definitions change (edits to
|
|
1349
|
+
* `site.config.ts` or a `routes.ts` it imports) or when route files are added
|
|
1350
|
+
* or removed. The re-loaded state is swapped onto the live site, the generated
|
|
1351
|
+
* manifest invalidated, and a full client reload triggered, so the running
|
|
1352
|
+
* server picks up the change without a restart.
|
|
1400
1353
|
*/
|
|
1401
|
-
function
|
|
1354
|
+
function watchRoutes(site, vite, configVite) {
|
|
1402
1355
|
reportRouteIssues(checkRoutes(site));
|
|
1403
1356
|
reportRouteIssues(checkApiCalls(site));
|
|
1404
1357
|
reportRouteIssues(checkLayoutSlots(site));
|
|
1405
|
-
const
|
|
1358
|
+
const buildRouteFiles = () => new Map(site.routes.filter((r) => r.file?.endsWith(".ts") || r.file?.endsWith(".torp") && r.type === 4).map((r) => [fpath.resolve(site.root, r.file), r]));
|
|
1359
|
+
let routeFiles = buildRouteFiles();
|
|
1406
1360
|
const apiCheck = createApiCallCheck(site);
|
|
1407
1361
|
const reported = /* @__PURE__ */ new Map();
|
|
1408
1362
|
const report = (file, issues) => {
|
|
@@ -1426,8 +1380,59 @@ function watchRouteTypes(site, vite) {
|
|
|
1426
1380
|
}
|
|
1427
1381
|
report(file, checkApiCallSource(source, file, apiCheck));
|
|
1428
1382
|
};
|
|
1429
|
-
|
|
1430
|
-
|
|
1383
|
+
let configDeps = configVite && site.configFile ? configDependencyFiles(configVite, site.root) : /* @__PURE__ */ new Set();
|
|
1384
|
+
const inRouteFolder = (file) => site.routeFolders.some((f) => isInside(fpath.resolve(site.root, f.folder), file));
|
|
1385
|
+
let pending = Promise.resolve();
|
|
1386
|
+
const reload = () => {
|
|
1387
|
+
if (!site.configFile) return;
|
|
1388
|
+
pending = pending.then(async () => {
|
|
1389
|
+
const { site: fresh, vite: freshVite } = await loadSite(site.root);
|
|
1390
|
+
try {
|
|
1391
|
+
configDeps = configDependencyFiles(freshVite, site.root);
|
|
1392
|
+
if (!site.applyRouteState(fresh)) return;
|
|
1393
|
+
} finally {
|
|
1394
|
+
await freshVite.close();
|
|
1395
|
+
}
|
|
1396
|
+
routeFiles = buildRouteFiles();
|
|
1397
|
+
reportRouteIssues(checkRoutes(site));
|
|
1398
|
+
reportRouteIssues(checkApiCalls(site));
|
|
1399
|
+
reportRouteIssues(checkLayoutSlots(site));
|
|
1400
|
+
invalidateManifest(vite);
|
|
1401
|
+
vite.ws.send({
|
|
1402
|
+
type: "full-reload",
|
|
1403
|
+
path: "*"
|
|
1404
|
+
});
|
|
1405
|
+
}).catch((e) => console.error(e));
|
|
1406
|
+
};
|
|
1407
|
+
vite.watcher.on("change", (file) => {
|
|
1408
|
+
recheck(file);
|
|
1409
|
+
if (configDeps.has(file)) reload();
|
|
1410
|
+
});
|
|
1411
|
+
vite.watcher.on("add", (file) => {
|
|
1412
|
+
recheck(file);
|
|
1413
|
+
if (configDeps.has(file) || inRouteFolder(file)) reload();
|
|
1414
|
+
});
|
|
1415
|
+
const onRemove = (file) => {
|
|
1416
|
+
if (configDeps.has(file) || inRouteFolder(file)) reload();
|
|
1417
|
+
};
|
|
1418
|
+
vite.watcher.on("unlink", onRemove);
|
|
1419
|
+
vite.watcher.on("addDir", onRemove);
|
|
1420
|
+
vite.watcher.on("unlinkDir", onRemove);
|
|
1421
|
+
}
|
|
1422
|
+
function isInside(dir, file) {
|
|
1423
|
+
const rel = fpath.relative(dir, file);
|
|
1424
|
+
return rel === "" || !rel.startsWith("..") && !fpath.isAbsolute(rel);
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Invalidates the generated `@torpor/build/manifest` module in every
|
|
1428
|
+
* environment, so the server entry re-runs with the rebuilt route table on the
|
|
1429
|
+
* next request and the client gets the new manifest on reload.
|
|
1430
|
+
*/
|
|
1431
|
+
function invalidateManifest(vite) {
|
|
1432
|
+
for (const env of Object.values(vite.environments)) {
|
|
1433
|
+
const mod = env.moduleGraph.getModuleById(MANIFEST_MODULE_ID);
|
|
1434
|
+
if (mod) env.moduleGraph.invalidateModule(mod);
|
|
1435
|
+
}
|
|
1431
1436
|
}
|
|
1432
1437
|
function normalizePlugins(plugins) {
|
|
1433
1438
|
if (!plugins) return [];
|
|
@@ -1574,45 +1579,12 @@ async function runPreview(site) {
|
|
|
1574
1579
|
}
|
|
1575
1580
|
//#endregion
|
|
1576
1581
|
//#region src/run/run.ts
|
|
1577
|
-
/**
|
|
1578
|
-
* Looks for and loads a site.config.js/ts file in the working directory,
|
|
1579
|
-
* running the site's plugins. The Vite server used to load the config is
|
|
1580
|
-
* returned as well, so that (for example) route files can be loaded through
|
|
1581
|
-
* it; callers are responsible for closing it.
|
|
1582
|
-
*/
|
|
1583
|
-
async function loadSite(folder) {
|
|
1584
|
-
const jsConfigFile = fpath.join(folder, "site.config.js");
|
|
1585
|
-
const tsConfigFile = fpath.join(folder, "site.config.ts");
|
|
1586
|
-
let configFile = "";
|
|
1587
|
-
if (fs.existsSync(jsConfigFile)) configFile = jsConfigFile;
|
|
1588
|
-
else if (fs.existsSync(tsConfigFile)) configFile = tsConfigFile;
|
|
1589
|
-
else throw new Error("site.config file not found");
|
|
1590
|
-
const vite = await createServer({
|
|
1591
|
-
server: { middlewareMode: true },
|
|
1592
|
-
appType: "custom",
|
|
1593
|
-
resolve: { tsconfigPaths: true },
|
|
1594
|
-
optimizeDeps: { noDiscovery: true }
|
|
1595
|
-
});
|
|
1596
|
-
try {
|
|
1597
|
-
const site = (await vite.ssrLoadModule(configFile)).default;
|
|
1598
|
-
site.configFile = configFile;
|
|
1599
|
-
if (!site || !site.root || !site.routes) throw new Error("Invalid site in config file");
|
|
1600
|
-
for (const plugin of site.plugins) await plugin(site);
|
|
1601
|
-
return {
|
|
1602
|
-
site,
|
|
1603
|
-
vite
|
|
1604
|
-
};
|
|
1605
|
-
} catch (error) {
|
|
1606
|
-
await vite.close();
|
|
1607
|
-
throw error;
|
|
1608
|
-
}
|
|
1609
|
-
}
|
|
1610
1582
|
async function run(folder, mode) {
|
|
1611
1583
|
const { site, vite } = await loadSite(folder);
|
|
1612
1584
|
try {
|
|
1613
1585
|
switch (mode) {
|
|
1614
1586
|
case "dev":
|
|
1615
|
-
await runDev(site);
|
|
1587
|
+
await runDev(site, vite);
|
|
1616
1588
|
break;
|
|
1617
1589
|
case "build":
|
|
1618
1590
|
await runBuild(site);
|
|
@@ -1622,7 +1594,7 @@ async function run(folder, mode) {
|
|
|
1622
1594
|
break;
|
|
1623
1595
|
default:
|
|
1624
1596
|
console.log("No mode passed, running in dev");
|
|
1625
|
-
await runDev(site);
|
|
1597
|
+
await runDev(site, vite);
|
|
1626
1598
|
}
|
|
1627
1599
|
} finally {
|
|
1628
1600
|
await vite.close();
|
|
@@ -1660,6 +1632,6 @@ async function runOpenApi(site, vite, outFile = "openapi.json") {
|
|
|
1660
1632
|
console.log(`OpenAPI document written to ${outFile} (${Object.keys(doc.paths ?? {}).length} paths)`);
|
|
1661
1633
|
}
|
|
1662
1634
|
//#endregion
|
|
1663
|
-
export {
|
|
1635
|
+
export { loadSite as a, runDev as i, run as n, runBuild as o, runPreview as r, runOpenApi as t };
|
|
1664
1636
|
|
|
1665
|
-
//# sourceMappingURL=runOpenApi-
|
|
1637
|
+
//# sourceMappingURL=runOpenApi-DTUtB-XC.mjs.map
|