@torpor/build 1.5.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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-Blz32JRD.d.mts → Site-DW-gKnPv.d.mts} +22 -1
- package/dist/{Site-Blz32JRD.d.mts.map → Site-DW-gKnPv.d.mts.map} +1 -1
- package/dist/bin/index.js +1 -1
- 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 +51 -0
- package/dist/index.mjs.map +1 -1
- package/dist/nodeDev.d.mts +1 -1
- 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/run.d.mts +8 -2
- package/dist/run.d.mts.map +1 -1
- package/dist/run.mjs +1 -1
- package/dist/{runOpenApi-tnflDjZV.mjs → runOpenApi-DTUtB-XC.mjs} +150 -45
- package/dist/runOpenApi-DTUtB-XC.mjs.map +1 -0
- package/dist/test.d.mts +1 -1
- package/package.json +6 -6
- package/dist/runOpenApi-tnflDjZV.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pathTrie-D4Ax2Mu9.mjs","names":[
|
|
1
|
+
{"version":3,"file":"pathTrie-D4Ax2Mu9.mjs","names":[],"sources":["../src/utils/pathTrie.ts"],"sourcesContent":["/**\n * A simple trie router for path patterns.\n *\n * Pattern segments:\n * - `posts` — static segment\n * - `[id]` — matches one non-empty path segment, captured as `params.id`\n * - `[...path]` — matches the rest of the path (at least one segment),\n * captured as `params.path`\n * - `*` — matches the rest of the path (possibly nothing), not captured\n * - `*.css` — a segment with an embedded `*` matches one path segment\n *\n * `*` and `[...]` must be the last segment of a pattern. Matching priority at\n * each level is static, then param, then glob, then rest. Trailing slashes are\n * ignored, so `/posts` and `/posts/` match the same route.\n */\nexport default class PathTrie<T> {\n\t#root = new TrieNode<T>();\n\n\t/**\n\t * Registers a value at a path pattern. The first registration of an exact\n\t * pattern wins, matching the previous regex-scanning router.\n\t */\n\tinsert(pattern: string, value: T): void {\n\t\tlet node = this.#root;\n\t\tconst segments = splitPath(pattern);\n\t\tfor (let i = 0; i < segments.length; i++) {\n\t\t\tconst segment = segments[i];\n\t\t\tif (segment === \"*\") {\n\t\t\t\tif (i !== segments.length - 1) {\n\t\t\t\t\tthrow new Error(`The '*' wildcard must be the last segment of a path: ${pattern}`);\n\t\t\t\t}\n\t\t\t\tnode.rest ??= { name: undefined, allowEmpty: true, child: new TrieNode() };\n\t\t\t\tnode = node.rest.child;\n\t\t\t} else if (segment.startsWith(\"[...\") && segment.endsWith(\"]\")) {\n\t\t\t\tif (i !== segments.length - 1) {\n\t\t\t\t\tthrow new Error(`The '[...x]' splat must be the last segment of a path: ${pattern}`);\n\t\t\t\t}\n\t\t\t\tnode.rest ??= {\n\t\t\t\t\tname: segment.slice(4, -1),\n\t\t\t\t\tallowEmpty: false,\n\t\t\t\t\tchild: new TrieNode(),\n\t\t\t\t};\n\t\t\t\tnode = node.rest.child;\n\t\t\t} else if (segment.startsWith(\"[\") && segment.endsWith(\"]\")) {\n\t\t\t\tnode.param ??= { name: segment.slice(1, -1), child: new TrieNode() };\n\t\t\t\tnode = node.param.child;\n\t\t\t} else if (segment.includes(\"*\")) {\n\t\t\t\tnode.glob ??= { regex: globToRegex(segment), child: new TrieNode() };\n\t\t\t\tnode = node.glob.child;\n\t\t\t} else {\n\t\t\t\tnode.static ??= new Map();\n\t\t\t\tlet child = node.static.get(segment);\n\t\t\t\tif (!child) {\n\t\t\t\t\tchild = new TrieNode();\n\t\t\t\t\tnode.static.set(segment, child);\n\t\t\t\t}\n\t\t\t\tnode = child;\n\t\t\t}\n\t\t}\n\t\tnode.value ??= value;\n\t}\n\n\t/**\n\t * Matches a request path against the trie. Returns the registered value\n\t * and any captured params, or undefined when nothing matches.\n\t */\n\tmatch(path: string): PathMatch<T> | undefined {\n\t\t// Trailing slashes are optional: `/posts/` matches `/posts`. Whether\n\t\t// there WAS a trailing separator still matters for a trailing bare\n\t\t// `*`, which requires one (e.g. `/assets/*` matches `/assets/` but\n\t\t// not `/assets`)\n\t\tlet trailingSlash = path === \"/\";\n\t\twhile (path.length > 1 && path.endsWith(\"/\")) {\n\t\t\tpath = path.slice(0, -1);\n\t\t\ttrailingSlash = true;\n\t\t}\n\t\tconst segments = path === \"/\" ? [] : splitPath(path);\n\t\treturn this.#matchNode(this.#root, segments, 0, {}, trailingSlash);\n\t}\n\n\t#matchNode(\n\t\tnode: TrieNode<T>,\n\t\tsegments: string[],\n\t\ti: number,\n\t\tparams: Record<string, string>,\n\t\ttrailingSlash: boolean,\n\t): PathMatch<T> | undefined {\n\t\tif (i === segments.length) {\n\t\t\tif (node.value !== undefined) {\n\t\t\t\treturn { value: node.value, params: orUndefined(params) };\n\t\t\t}\n\t\t\t// A trailing bare `*` also matches nothing, when the path ended\n\t\t\t// with its separator (e.g. `/assets/*` matches `/assets/`)\n\t\t\tconst rest = node.rest;\n\t\t\tif (rest?.allowEmpty && trailingSlash && rest.child.value !== undefined) {\n\t\t\t\treturn { value: rest.child.value, params: orUndefined(params) };\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst segment = segments[i];\n\n\t\t// Static segments win over params\n\t\tconst stat = node.static?.get(segment);\n\t\tif (stat) {\n\t\t\tconst match = this.#matchNode(stat, segments, i + 1, params, trailingSlash);\n\t\t\tif (match) return match;\n\t\t}\n\n\t\tif (node.param && segment !== \"\") {\n\t\t\tconst match = this.#matchNode(\n\t\t\t\tnode.param.child,\n\t\t\t\tsegments,\n\t\t\t\ti + 1,\n\t\t\t\t{\n\t\t\t\t\t...params,\n\t\t\t\t\t[node.param.name]: segment,\n\t\t\t\t},\n\t\t\t\ttrailingSlash,\n\t\t\t);\n\t\t\tif (match) return match;\n\t\t}\n\n\t\tif (node.glob?.regex.test(segment)) {\n\t\t\tconst glob = node.glob;\n\t\t\tconst match = this.#matchNode(glob.child, segments, i + 1, params, trailingSlash);\n\t\t\tif (match) return match;\n\t\t}\n\n\t\t// The rest matcher consumes all remaining segments\n\t\tconst rest = node.rest;\n\t\tif (rest?.child.value !== undefined) {\n\t\t\t// Only build the captured value (and a new params object) when the\n\t\t\t// pattern actually names it — a bare `*` catch-all shouldn't pay\n\t\t\t// for a slice+join on every request\n\t\t\tif (rest.name !== undefined) {\n\t\t\t\treturn {\n\t\t\t\t\tvalue: rest.child.value,\n\t\t\t\t\tparams: orUndefined({ ...params, [rest.name]: segments.slice(i).join(\"/\") }),\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn { value: rest.child.value, params: orUndefined(params) };\n\t\t}\n\n\t\treturn undefined;\n\t}\n}\n\nexport interface PathMatch<T> {\n\tvalue: T;\n\tparams: Record<string, string> | undefined;\n}\n\nclass TrieNode<T> {\n\tvalue: T | undefined = undefined;\n\tstatic: Map<string, TrieNode<T>> | undefined;\n\tparam: { name: string; child: TrieNode<T> } | undefined;\n\tglob: { regex: RegExp; child: TrieNode<T> } | undefined;\n\trest: { name: string | undefined; allowEmpty: boolean; child: TrieNode<T> } | undefined;\n}\n\n/**\n * Splits a path or pattern into segments, dropping the leading empty segment\n * and any trailing empty (from a trailing slash). `/posts/5` becomes\n * `[\"posts\", \"5\"]` and `/` becomes `[]`.\n */\nfunction splitPath(path: string): string[] {\n\tconst segments = path.split(\"/\");\n\tif (segments[0] === \"\") segments.shift();\n\tif (segments.length && segments[segments.length - 1] === \"\") segments.pop();\n\treturn segments;\n}\n\n/**\n * Compiles a pattern segment with an embedded `*` (e.g. `*.css`) into a\n * regex that matches a single path segment.\n */\nfunction globToRegex(segment: string): RegExp {\n\tconst source = segment\n\t\t.split(\"*\")\n\t\t.map((part) => part.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\"))\n\t\t.join(\".*\");\n\treturn new RegExp(`^${source}$`);\n}\n\nfunction orUndefined(params: Record<string, string>): Record<string, string> | undefined {\n\treturn Object.keys(params).length ? params : undefined;\n}\n"],"mappings":"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"}
|
package/dist/run.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as Site } from "./Site-
|
|
1
|
+
import { t as Site } from "./Site-DW-gKnPv.mjs";
|
|
2
2
|
import { ViteDevServer } from "vite";
|
|
3
3
|
//#region src/run/run.d.ts
|
|
4
4
|
declare function run(folder: string, mode: "dev" | "build" | "preview"): Promise<void>;
|
|
@@ -7,7 +7,13 @@ declare function run(folder: string, mode: "dev" | "build" | "preview"): Promise
|
|
|
7
7
|
declare function runBuild(site: Site): Promise<void>;
|
|
8
8
|
//#endregion
|
|
9
9
|
//#region src/run/runDev.d.ts
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* @param configVite The Vite server used to load the site config (see
|
|
12
|
+
* `loadSite`). Its module graph tells the dev server which files the config
|
|
13
|
+
* imports, so `addRoute` calls in those files (e.g. a `routes.ts`) trigger a
|
|
14
|
+
* route reload too. Without it, only file-based routes are reloaded.
|
|
15
|
+
*/
|
|
16
|
+
declare function runDev(site: Site, configVite?: ViteDevServer): Promise<void>;
|
|
11
17
|
//#endregion
|
|
12
18
|
//#region src/run/runOpenApi.d.ts
|
|
13
19
|
/**
|
package/dist/run.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.mts","names":[],"sources":["../src/run/run.ts","../src/run/runBuild.ts","../src/run/runDev.ts","../src/run/runOpenApi.ts","../src/run/runPreview.ts"],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"run.d.mts","names":[],"sources":["../src/run/run.ts","../src/run/runBuild.ts","../src/run/runDev.ts","../src/run/runOpenApi.ts","../src/run/runPreview.ts"],"mappings":";;;iBAO8B,IAC7B,gBACA,oCACE;;;iBCQ2B,SAAS,MAAM,OAAO;;;;;;;;;iBCmBtB,OAAO,MAAM,MAAM,aAAa,gBAAgB;;;;;;;;iBCvBhD,WAC7B,MAAM,MACN,MAAM,eACN,mBACE;;;iBCR2B,WAAW,MAAM,OAAO"}
|
package/dist/run.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as runDev, n as run, o as runBuild, r as runPreview, t as runOpenApi } from "./runOpenApi-DTUtB-XC.mjs";
|
|
2
2
|
export { run, runBuild, runDev, runOpenApi, runPreview };
|
|
@@ -8,9 +8,9 @@ import { t as Server } from "./Server-C0PGzSQO.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
16
|
import { createHash } from "node:crypto";
|
|
@@ -507,7 +507,8 @@ function walk(node) {
|
|
|
507
507
|
}
|
|
508
508
|
//#endregion
|
|
509
509
|
//#region src/site/manifest.ts
|
|
510
|
-
const
|
|
510
|
+
const MANIFEST_MODULE_ID = "@torpor/build/manifest";
|
|
511
|
+
const moduleId = MANIFEST_MODULE_ID;
|
|
511
512
|
const LOAD_EXPORT_RE = /[{,]\s*load\s*[:(]|\bexport\s+(?:async\s+)?(?:function|const|let|var)\s+load\b|\bexport\s*\{[^}]*\bload\b/;
|
|
512
513
|
function hasLoadExport(source) {
|
|
513
514
|
return LOAD_EXPORT_RE.test(source);
|
|
@@ -1130,6 +1131,78 @@ function fingerprintDir(dir, root, hash) {
|
|
|
1130
1131
|
}
|
|
1131
1132
|
}
|
|
1132
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
|
|
1133
1206
|
//#region src/run/staleTorpCopies.ts
|
|
1134
1207
|
/**
|
|
1135
1208
|
* Workspace packages that ship copies of their .torp components (e.g.
|
|
@@ -1201,7 +1274,13 @@ function packageLabel(dir, workspaceRoot) {
|
|
|
1201
1274
|
}
|
|
1202
1275
|
//#endregion
|
|
1203
1276
|
//#region src/run/runDev.ts
|
|
1204
|
-
|
|
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) {
|
|
1205
1284
|
const config = structuredClone(site.viteConfig ?? {});
|
|
1206
1285
|
config.appType = "custom";
|
|
1207
1286
|
config.server ??= {};
|
|
@@ -1243,7 +1322,7 @@ async function runDev(site) {
|
|
|
1243
1322
|
const vite = await createServer(config);
|
|
1244
1323
|
await warmServerEntry(vite, site, sourceMode);
|
|
1245
1324
|
await vite.listen();
|
|
1246
|
-
|
|
1325
|
+
watchRoutes(site, vite, configVite);
|
|
1247
1326
|
const listeningUrl = vite.resolvedUrls?.local?.[0] ?? connectingUrl;
|
|
1248
1327
|
console.log(`Listening on ${listeningUrl}\n`);
|
|
1249
1328
|
}
|
|
@@ -1264,12 +1343,20 @@ async function warmServerEntry(vite, site, sourceMode) {
|
|
|
1264
1343
|
* Reports route type issues at startup, then re-checks route files and
|
|
1265
1344
|
* `makeApi` calls as they change so annotation problems surface during
|
|
1266
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.
|
|
1267
1353
|
*/
|
|
1268
|
-
function
|
|
1354
|
+
function watchRoutes(site, vite, configVite) {
|
|
1269
1355
|
reportRouteIssues(checkRoutes(site));
|
|
1270
1356
|
reportRouteIssues(checkApiCalls(site));
|
|
1271
1357
|
reportRouteIssues(checkLayoutSlots(site));
|
|
1272
|
-
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();
|
|
1273
1360
|
const apiCheck = createApiCallCheck(site);
|
|
1274
1361
|
const reported = /* @__PURE__ */ new Map();
|
|
1275
1362
|
const report = (file, issues) => {
|
|
@@ -1293,8 +1380,59 @@ function watchRouteTypes(site, vite) {
|
|
|
1293
1380
|
}
|
|
1294
1381
|
report(file, checkApiCallSource(source, file, apiCheck));
|
|
1295
1382
|
};
|
|
1296
|
-
|
|
1297
|
-
|
|
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
|
+
}
|
|
1298
1436
|
}
|
|
1299
1437
|
function normalizePlugins(plugins) {
|
|
1300
1438
|
if (!plugins) return [];
|
|
@@ -1441,45 +1579,12 @@ async function runPreview(site) {
|
|
|
1441
1579
|
}
|
|
1442
1580
|
//#endregion
|
|
1443
1581
|
//#region src/run/run.ts
|
|
1444
|
-
/**
|
|
1445
|
-
* Looks for and loads a site.config.js/ts file in the working directory,
|
|
1446
|
-
* running the site's plugins. The Vite server used to load the config is
|
|
1447
|
-
* returned as well, so that (for example) route files can be loaded through
|
|
1448
|
-
* it; callers are responsible for closing it.
|
|
1449
|
-
*/
|
|
1450
|
-
async function loadSite(folder) {
|
|
1451
|
-
const jsConfigFile = fpath.join(folder, "site.config.js");
|
|
1452
|
-
const tsConfigFile = fpath.join(folder, "site.config.ts");
|
|
1453
|
-
let configFile = "";
|
|
1454
|
-
if (fs.existsSync(jsConfigFile)) configFile = jsConfigFile;
|
|
1455
|
-
else if (fs.existsSync(tsConfigFile)) configFile = tsConfigFile;
|
|
1456
|
-
else throw new Error("site.config file not found");
|
|
1457
|
-
const vite = await createServer({
|
|
1458
|
-
server: { middlewareMode: true },
|
|
1459
|
-
appType: "custom",
|
|
1460
|
-
resolve: { tsconfigPaths: true },
|
|
1461
|
-
optimizeDeps: { noDiscovery: true }
|
|
1462
|
-
});
|
|
1463
|
-
try {
|
|
1464
|
-
const site = (await vite.ssrLoadModule(configFile)).default;
|
|
1465
|
-
site.configFile = configFile;
|
|
1466
|
-
if (!site || !site.root || !site.routes) throw new Error("Invalid site in config file");
|
|
1467
|
-
for (const plugin of site.plugins) await plugin(site);
|
|
1468
|
-
return {
|
|
1469
|
-
site,
|
|
1470
|
-
vite
|
|
1471
|
-
};
|
|
1472
|
-
} catch (error) {
|
|
1473
|
-
await vite.close();
|
|
1474
|
-
throw error;
|
|
1475
|
-
}
|
|
1476
|
-
}
|
|
1477
1582
|
async function run(folder, mode) {
|
|
1478
1583
|
const { site, vite } = await loadSite(folder);
|
|
1479
1584
|
try {
|
|
1480
1585
|
switch (mode) {
|
|
1481
1586
|
case "dev":
|
|
1482
|
-
await runDev(site);
|
|
1587
|
+
await runDev(site, vite);
|
|
1483
1588
|
break;
|
|
1484
1589
|
case "build":
|
|
1485
1590
|
await runBuild(site);
|
|
@@ -1489,7 +1594,7 @@ async function run(folder, mode) {
|
|
|
1489
1594
|
break;
|
|
1490
1595
|
default:
|
|
1491
1596
|
console.log("No mode passed, running in dev");
|
|
1492
|
-
await runDev(site);
|
|
1597
|
+
await runDev(site, vite);
|
|
1493
1598
|
}
|
|
1494
1599
|
} finally {
|
|
1495
1600
|
await vite.close();
|
|
@@ -1527,6 +1632,6 @@ async function runOpenApi(site, vite, outFile = "openapi.json") {
|
|
|
1527
1632
|
console.log(`OpenAPI document written to ${outFile} (${Object.keys(doc.paths ?? {}).length} paths)`);
|
|
1528
1633
|
}
|
|
1529
1634
|
//#endregion
|
|
1530
|
-
export {
|
|
1635
|
+
export { loadSite as a, runDev as i, run as n, runBuild as o, runPreview as r, runOpenApi as t };
|
|
1531
1636
|
|
|
1532
|
-
//# sourceMappingURL=runOpenApi-
|
|
1637
|
+
//# sourceMappingURL=runOpenApi-DTUtB-XC.mjs.map
|