@fluixi/start 0.1.0-alpha.73 → 0.1.0-alpha.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter.cjs +748 -0
- package/dist/adapter.mjs +717 -0
- package/dist/adapters/entry.cjs +122 -0
- package/dist/adapters/entry.mjs +95 -0
- package/dist/adapters/platforms.cjs +311 -0
- package/dist/adapters/platforms.mjs +284 -0
- package/dist/api-RH6E5UTH.mjs +114 -0
- package/dist/api.cjs +145 -0
- package/dist/api.mjs +116 -0
- package/dist/commands/api-23MJFSYL.mjs +17 -0
- package/dist/commands/api-4IYNQRKG.mjs +16 -0
- package/dist/commands/api-RH6E5UTH.mjs +114 -0
- package/dist/commands/build.cjs +870 -0
- package/dist/commands/build.mjs +710 -0
- package/dist/commands/chunk-NS5GR2GX.mjs +115 -0
- package/dist/commands/chunk-OCPCK4ZJ.mjs +117 -0
- package/dist/commands/dev.cjs +663 -0
- package/dist/commands/dev.mjs +505 -0
- package/dist/commands/index.cjs +1138 -0
- package/dist/commands/index.mjs +975 -0
- package/dist/commands/prerender.cjs +402 -0
- package/dist/commands/prerender.mjs +375 -0
- package/dist/commands/start.cjs +461 -0
- package/dist/commands/start.mjs +426 -0
- package/dist/config.cjs +144 -0
- package/dist/config.mjs +108 -0
- package/dist/di.cjs +200 -0
- package/dist/di.mjs +169 -0
- package/dist/document.cjs +115 -0
- package/dist/document.mjs +86 -0
- package/dist/generated-api.cjs +43 -0
- package/dist/generated-api.mjs +18 -0
- package/dist/generated-server-fns.cjs +43 -0
- package/dist/generated-server-fns.mjs +18 -0
- package/dist/handler-core.cjs +211 -0
- package/dist/handler-core.mjs +185 -0
- package/dist/handler.cjs +181 -0
- package/dist/handler.mjs +150 -0
- package/dist/head.cjs +25 -0
- package/dist/head.mjs +4 -0
- package/dist/image.cjs +146 -0
- package/dist/image.mjs +120 -0
- package/dist/index.cjs +1036 -0
- package/dist/index.mjs +968 -0
- package/dist/interceptors.cjs +71 -0
- package/dist/interceptors.mjs +42 -0
- package/dist/internal.cjs +388 -0
- package/dist/internal.mjs +218 -0
- package/dist/middleware.cjs +58 -0
- package/dist/middleware.mjs +31 -0
- package/dist/preload.cjs +42 -0
- package/dist/preload.mjs +18 -0
- package/dist/router.cjs +25 -0
- package/dist/router.mjs +4 -0
- package/dist/server-fn-setup.cjs +46 -0
- package/dist/server-fn-setup.mjs +22 -0
- package/dist/server-fn.cjs +109 -0
- package/dist/server-fn.mjs +79 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/dist/ui.cjs +123 -0
- package/dist/ui.mjs +93 -0
- package/package.json +45 -25
|
@@ -0,0 +1,870 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// src/api.ts
|
|
34
|
+
var api_exports = {};
|
|
35
|
+
__export(api_exports, {
|
|
36
|
+
API_VIRTUAL_MODULE: () => API_VIRTUAL_MODULE,
|
|
37
|
+
apiRoutesVitePlugin: () => apiRoutesVitePlugin,
|
|
38
|
+
derivePattern: () => derivePattern,
|
|
39
|
+
handleApi: () => handleApi,
|
|
40
|
+
isApiPath: () => isApiPath,
|
|
41
|
+
matchApiRoutes: () => matchApiRoutes
|
|
42
|
+
});
|
|
43
|
+
function derivePattern(rel, prefix = "/api") {
|
|
44
|
+
const segs = rel.split("/").filter(Boolean);
|
|
45
|
+
if (segs[segs.length - 1] === "index") segs.pop();
|
|
46
|
+
const keys = [];
|
|
47
|
+
let rank = 0;
|
|
48
|
+
const parts = segs.map((seg) => {
|
|
49
|
+
const catchAll = seg.match(/^\[\.\.\.(.+)\]$/);
|
|
50
|
+
if (catchAll) {
|
|
51
|
+
keys.push(catchAll[1]);
|
|
52
|
+
return "(.+)";
|
|
53
|
+
}
|
|
54
|
+
const param = seg.match(/^\[(.+)\]$/);
|
|
55
|
+
if (param) {
|
|
56
|
+
keys.push(param[1]);
|
|
57
|
+
rank += 1;
|
|
58
|
+
return "([^/]+)";
|
|
59
|
+
}
|
|
60
|
+
rank += 10;
|
|
61
|
+
return seg.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
62
|
+
});
|
|
63
|
+
return { source: `^${prefix}${parts.length ? "/" + parts.join("/") : ""}/?$`, keys, rank };
|
|
64
|
+
}
|
|
65
|
+
function isApiPath(pathname, prefix = "/api") {
|
|
66
|
+
return pathname === prefix || pathname.startsWith(prefix + "/");
|
|
67
|
+
}
|
|
68
|
+
function matchApiRoutes(routes, pathname) {
|
|
69
|
+
for (const r of [...routes].sort((a, b) => b.rank - a.rank)) {
|
|
70
|
+
const m = new RegExp(r.source).exec(pathname);
|
|
71
|
+
if (!m) continue;
|
|
72
|
+
const params = {};
|
|
73
|
+
r.keys.forEach((k, i) => params[k] = decodeURIComponent(m[i + 1] ?? ""));
|
|
74
|
+
return { mod: r.mod, params };
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
async function handleApi(routes, request, prefix = "/api") {
|
|
79
|
+
const pathname = new URL(request.url).pathname;
|
|
80
|
+
const match = matchApiRoutes(routes, pathname);
|
|
81
|
+
if (!match) return new Response("Not Found", { status: 404 });
|
|
82
|
+
const fn = match.mod[request.method];
|
|
83
|
+
if (typeof fn !== "function") {
|
|
84
|
+
const allow = METHODS.filter((m) => typeof match.mod[m] === "function").join(", ");
|
|
85
|
+
return new Response("Method Not Allowed", { status: 405, headers: allow ? { allow } : void 0 });
|
|
86
|
+
}
|
|
87
|
+
const out = await fn(request, { params: match.params });
|
|
88
|
+
if (out instanceof Response) return out;
|
|
89
|
+
return new Response(JSON.stringify(out ?? null), { headers: { "content-type": "application/json; charset=utf-8" } });
|
|
90
|
+
}
|
|
91
|
+
function scanFiles(dir, base = dir) {
|
|
92
|
+
let out = [];
|
|
93
|
+
let entries = [];
|
|
94
|
+
try {
|
|
95
|
+
entries = (0, import_node_fs.readdirSync)(dir);
|
|
96
|
+
} catch {
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
for (const name of entries) {
|
|
100
|
+
const full = (0, import_node_path2.join)(dir, name);
|
|
101
|
+
if ((0, import_node_fs.statSync)(full).isDirectory()) out = out.concat(scanFiles(full, base));
|
|
102
|
+
else if (/\.(ts|tsx|js|mjs)$/.test(name) && !/\.(spec|test)\./.test(name)) out.push(full);
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
function apiRoutesVitePlugin(opts) {
|
|
107
|
+
let root = process.cwd();
|
|
108
|
+
const prefix = opts.prefix ?? "/api";
|
|
109
|
+
const id = "\0" + API_VMOD;
|
|
110
|
+
return {
|
|
111
|
+
name: "fluixi-api",
|
|
112
|
+
// Must beat node resolution: '@fluixi/start/api-routes' is a real file in this package,
|
|
113
|
+
// so without `pre` Vite resolves the typed stub and the generated table never loads —
|
|
114
|
+
// the app builds and every API route 404s.
|
|
115
|
+
enforce: "pre",
|
|
116
|
+
configResolved(config) {
|
|
117
|
+
root = config.root || root;
|
|
118
|
+
},
|
|
119
|
+
resolveId(source) {
|
|
120
|
+
return source === API_VMOD || source === "@fluixi/start/api-routes" ? id : null;
|
|
121
|
+
},
|
|
122
|
+
load(source) {
|
|
123
|
+
if (source !== id) return null;
|
|
124
|
+
const dir = (0, import_node_path2.resolve)(root, opts.dir);
|
|
125
|
+
const files = scanFiles(dir);
|
|
126
|
+
const imports = [];
|
|
127
|
+
const entries = [];
|
|
128
|
+
files.forEach((file, i) => {
|
|
129
|
+
const rel = (0, import_node_path2.relative)(dir, file).replace(/\.(ts|tsx|js|mjs)$/, "").split(/[\\/]/).join("/");
|
|
130
|
+
const { source: src, keys, rank } = derivePattern(rel, prefix);
|
|
131
|
+
imports.push(`import * as _${i} from ${JSON.stringify(file)};`);
|
|
132
|
+
entries.push(`{ source: ${JSON.stringify(src)}, keys: ${JSON.stringify(keys)}, rank: ${rank}, mod: _${i} }`);
|
|
133
|
+
});
|
|
134
|
+
return `import { isApiPath, handleApi } from '@fluixi/start/api';
|
|
135
|
+
${imports.join("\n")}
|
|
136
|
+
const routes = [${entries.join(",")}];
|
|
137
|
+
export const isApiRequest = (request) => isApiPath(new URL(request.url).pathname, ${JSON.stringify(prefix)});
|
|
138
|
+
export const handleApiRequest = (request) => handleApi(routes, request, ${JSON.stringify(prefix)});
|
|
139
|
+
`;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
var import_node_fs, import_node_path2, API_VMOD, METHODS, API_VIRTUAL_MODULE;
|
|
144
|
+
var init_api = __esm({
|
|
145
|
+
"src/api.ts"() {
|
|
146
|
+
"use strict";
|
|
147
|
+
import_node_fs = require("node:fs");
|
|
148
|
+
import_node_path2 = require("node:path");
|
|
149
|
+
API_VMOD = "virtual:fluixi-api";
|
|
150
|
+
METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"];
|
|
151
|
+
API_VIRTUAL_MODULE = API_VMOD;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// src/commands/build.ts
|
|
156
|
+
var build_exports = {};
|
|
157
|
+
__export(build_exports, {
|
|
158
|
+
build: () => build
|
|
159
|
+
});
|
|
160
|
+
module.exports = __toCommonJS(build_exports);
|
|
161
|
+
var import_node_fs4 = require("node:fs");
|
|
162
|
+
var import_promises3 = require("node:fs/promises");
|
|
163
|
+
var import_node_path6 = require("node:path");
|
|
164
|
+
|
|
165
|
+
// src/config.ts
|
|
166
|
+
var import_node_path = require("node:path");
|
|
167
|
+
function resolveConfig(config = {}) {
|
|
168
|
+
const root = (0, import_node_path.resolve)(config.root ?? process.cwd());
|
|
169
|
+
return {
|
|
170
|
+
root,
|
|
171
|
+
ssr: config.ssr ?? true,
|
|
172
|
+
port: config.port ?? 3e3,
|
|
173
|
+
serverEntry: config.serverEntry ?? "src/entry-server.ts",
|
|
174
|
+
clientEntry: config.clientEntry ?? "src/entry-client.ts",
|
|
175
|
+
template: config.template ?? "index.html",
|
|
176
|
+
mountId: config.mountId ?? "root",
|
|
177
|
+
routesDir: config.routesDir ?? "src/routes",
|
|
178
|
+
apiDir: config.apiDir ?? "src/api",
|
|
179
|
+
middleware: config.middleware ?? "src/middleware.ts",
|
|
180
|
+
ssrNoExternal: config.ssrNoExternal ?? [],
|
|
181
|
+
resolve: config.resolve ?? [],
|
|
182
|
+
adapter: config.adapter,
|
|
183
|
+
i18n: resolveI18n(config.i18n),
|
|
184
|
+
prerender: resolvePrerender(config.prerender)
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function resolvePrerender(prerender2) {
|
|
188
|
+
if (!prerender2) return false;
|
|
189
|
+
const opts = typeof prerender2 === "object" ? prerender2 : {};
|
|
190
|
+
return {
|
|
191
|
+
routes: opts.routes?.length ? opts.routes : ["/"],
|
|
192
|
+
crawl: opts.crawl ?? true
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function resolveI18n(i18n) {
|
|
196
|
+
if (i18n === false) return false;
|
|
197
|
+
const opts = typeof i18n === "object" ? i18n : {};
|
|
198
|
+
return {
|
|
199
|
+
dir: opts.dir ?? "src/i18n",
|
|
200
|
+
defaultLocale: opts.defaultLocale,
|
|
201
|
+
locales: opts.locales
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/internal.ts
|
|
206
|
+
var import_node_fs2 = require("node:fs");
|
|
207
|
+
var import_node_path3 = require("node:path");
|
|
208
|
+
|
|
209
|
+
// src/document.ts
|
|
210
|
+
var import_head = require("@fluixi/head");
|
|
211
|
+
function injectApp(template, appHtml, mountId = "root") {
|
|
212
|
+
const mount = new RegExp(`<div id=["']${mountId}["']\\s*>\\s*</div>`);
|
|
213
|
+
if (mount.test(template)) {
|
|
214
|
+
return template.replace(mount, `<div id="${mountId}">${appHtml}</div>`);
|
|
215
|
+
}
|
|
216
|
+
if (template.includes("<!--ssr-outlet-->")) {
|
|
217
|
+
return template.replace("<!--ssr-outlet-->", appHtml);
|
|
218
|
+
}
|
|
219
|
+
return template.replace("</body>", `<div id="${mountId}">${appHtml}</div></body>`);
|
|
220
|
+
}
|
|
221
|
+
function splitTemplate(template, mountId = "root") {
|
|
222
|
+
const mount = new RegExp(`<div id=["']${mountId}["']\\s*>\\s*</div>`);
|
|
223
|
+
const m = template.match(mount);
|
|
224
|
+
if (m && m.index !== void 0) {
|
|
225
|
+
return {
|
|
226
|
+
head: template.slice(0, m.index) + `<div id="${mountId}">`,
|
|
227
|
+
tail: `</div>` + template.slice(m.index + m[0].length)
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const outlet = template.indexOf("<!--ssr-outlet-->");
|
|
231
|
+
if (outlet !== -1) {
|
|
232
|
+
return {
|
|
233
|
+
head: template.slice(0, outlet),
|
|
234
|
+
tail: template.slice(outlet + "<!--ssr-outlet-->".length)
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const body = template.indexOf("</body>");
|
|
238
|
+
if (body !== -1) {
|
|
239
|
+
return {
|
|
240
|
+
head: template.slice(0, body) + `<div id="${mountId}">`,
|
|
241
|
+
tail: `</div>` + template.slice(body)
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
return { head: template, tail: "" };
|
|
245
|
+
}
|
|
246
|
+
function injectPreload(template, files) {
|
|
247
|
+
const tags = files.filter((file) => !template.includes(file)).map(
|
|
248
|
+
(file) => file.endsWith(".css") ? `<link rel="stylesheet" href="${file}">` : (
|
|
249
|
+
// `modulepreload` also fetches the chunk's own static imports.
|
|
250
|
+
`<link rel="modulepreload" href="${file}">`
|
|
251
|
+
)
|
|
252
|
+
);
|
|
253
|
+
if (tags.length === 0) return template;
|
|
254
|
+
const head = template.indexOf("</head>");
|
|
255
|
+
if (head === -1) return template;
|
|
256
|
+
return template.slice(0, head) + tags.join("") + template.slice(head);
|
|
257
|
+
}
|
|
258
|
+
function setHtmlAttr(html, name, value) {
|
|
259
|
+
const existing = new RegExp(`(<html\\b[^>]*?)\\s${name}="[^"]*"`, "i");
|
|
260
|
+
if (existing.test(html)) return html.replace(existing, `$1 ${name}="${value}"`);
|
|
261
|
+
return html.replace(/<html\b/i, `<html ${name}="${value}"`);
|
|
262
|
+
}
|
|
263
|
+
function injectAppAndHead(template, rendered, mountId = "root") {
|
|
264
|
+
const { head, body } = (0, import_head.extractHeadMarker)(rendered);
|
|
265
|
+
let html = injectApp(template, body, mountId);
|
|
266
|
+
if (head) {
|
|
267
|
+
if (head.headHtml) {
|
|
268
|
+
if (/<title[\s>]/i.test(head.headHtml)) html = html.replace(/<title>[\s\S]*?<\/title>/i, "");
|
|
269
|
+
html = html.replace("</head>", `${head.headHtml}</head>`);
|
|
270
|
+
}
|
|
271
|
+
for (const [k, v] of Object.entries(head.htmlAttrs)) html = setHtmlAttr(html, k, v);
|
|
272
|
+
}
|
|
273
|
+
return html;
|
|
274
|
+
}
|
|
275
|
+
function guardHandler(handler, onError) {
|
|
276
|
+
return async (request) => {
|
|
277
|
+
try {
|
|
278
|
+
return await handler(request);
|
|
279
|
+
} catch (e) {
|
|
280
|
+
onError?.(e);
|
|
281
|
+
return new Response(String(e?.stack || e), {
|
|
282
|
+
status: 500,
|
|
283
|
+
headers: { "content-type": "text/plain; charset=utf-8" }
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/internal.ts
|
|
290
|
+
var I18N_VMOD = "virtual:fluixi-i18n";
|
|
291
|
+
function i18nVitePlugin(opts) {
|
|
292
|
+
let root = process.cwd();
|
|
293
|
+
const id = "\0" + I18N_VMOD;
|
|
294
|
+
return {
|
|
295
|
+
name: "fluixi-i18n",
|
|
296
|
+
configResolved(config) {
|
|
297
|
+
root = config.root || root;
|
|
298
|
+
},
|
|
299
|
+
resolveId(source) {
|
|
300
|
+
return source === I18N_VMOD ? id : null;
|
|
301
|
+
},
|
|
302
|
+
load(source) {
|
|
303
|
+
if (source !== id) return null;
|
|
304
|
+
const dir = (0, import_node_path3.resolve)(root, opts.dir);
|
|
305
|
+
let files = [];
|
|
306
|
+
try {
|
|
307
|
+
files = (0, import_node_fs2.readdirSync)(dir).filter((f) => f.endsWith(".json"));
|
|
308
|
+
} catch {
|
|
309
|
+
}
|
|
310
|
+
const discovered = files.map((f) => f.replace(/\.json$/, "")).sort();
|
|
311
|
+
const locales = opts.locales?.length ? opts.locales : discovered;
|
|
312
|
+
const defaultLocale = opts.defaultLocale ?? locales[0] ?? "en";
|
|
313
|
+
const imports = locales.map((l, i) => `import _${i} from ${JSON.stringify((0, import_node_path3.resolve)(dir, l + ".json"))};`).join("\n");
|
|
314
|
+
const messages = `export const messages = {${locales.map((l, i) => `${JSON.stringify(l)}:_${i}`).join(",")}};`;
|
|
315
|
+
return `${imports}
|
|
316
|
+
export const locales = ${JSON.stringify(locales)};
|
|
317
|
+
export const defaultLocale = ${JSON.stringify(defaultLocale)};
|
|
318
|
+
${messages}
|
|
319
|
+
`;
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
async function loadServerEnv(root, mode) {
|
|
324
|
+
const { loadEnv } = await import("vite");
|
|
325
|
+
const env = loadEnv(mode, root, "");
|
|
326
|
+
for (const [k, v] of Object.entries(env)) {
|
|
327
|
+
if (process.env[k] === void 0) process.env[k] = v;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function i18nTypeBlock(root, i18n) {
|
|
331
|
+
const dir = (0, import_node_path3.resolve)(root, i18n.dir);
|
|
332
|
+
let discovered = [];
|
|
333
|
+
try {
|
|
334
|
+
discovered = (0, import_node_fs2.readdirSync)(dir).filter((f) => f.endsWith(".json")).map((f) => f.replace(/\.json$/, "")).sort();
|
|
335
|
+
} catch {
|
|
336
|
+
}
|
|
337
|
+
const locales = i18n.locales?.length ? i18n.locales : discovered;
|
|
338
|
+
const defaultLocale = i18n.defaultLocale ?? locales[0] ?? "en";
|
|
339
|
+
const typed = locales.length > 0 && locales.every((l) => discovered.includes(l));
|
|
340
|
+
if (!typed) {
|
|
341
|
+
return [
|
|
342
|
+
`declare module 'virtual:fluixi-i18n' {`,
|
|
343
|
+
` export const messages: Record<string, Record<string, unknown>>;`,
|
|
344
|
+
` export const locales: string[];`,
|
|
345
|
+
` export const defaultLocale: string;`,
|
|
346
|
+
`}`
|
|
347
|
+
];
|
|
348
|
+
}
|
|
349
|
+
const srcDir = (0, import_node_path3.resolve)(root, "src");
|
|
350
|
+
const toSpec = (l) => {
|
|
351
|
+
const rel = (0, import_node_path3.relative)(srcDir, (0, import_node_path3.resolve)(dir, l + ".json")).split(/[\\/]/).join("/");
|
|
352
|
+
return rel.startsWith(".") ? rel : "./" + rel;
|
|
353
|
+
};
|
|
354
|
+
const entries = locales.map((l) => ` ${JSON.stringify(l)}: (typeof import(${JSON.stringify(toSpec(l))}))['default'];`);
|
|
355
|
+
const tuple = locales.map((l) => JSON.stringify(l)).join(", ");
|
|
356
|
+
return [
|
|
357
|
+
`declare module 'virtual:fluixi-i18n' {`,
|
|
358
|
+
` export const messages: {`,
|
|
359
|
+
...entries,
|
|
360
|
+
` };`,
|
|
361
|
+
` export const locales: readonly [${tuple}];`,
|
|
362
|
+
` export const defaultLocale: ${JSON.stringify(defaultLocale)};`,
|
|
363
|
+
`}`
|
|
364
|
+
];
|
|
365
|
+
}
|
|
366
|
+
function startTypesPlugin(cfg) {
|
|
367
|
+
let root = process.cwd();
|
|
368
|
+
return {
|
|
369
|
+
name: "fluixi-start-types",
|
|
370
|
+
configResolved(config) {
|
|
371
|
+
root = config.root || root;
|
|
372
|
+
},
|
|
373
|
+
buildStart() {
|
|
374
|
+
const blocks = [];
|
|
375
|
+
if (cfg.i18n) blocks.push(...i18nTypeBlock(root, cfg.i18n));
|
|
376
|
+
if (blocks.length === 0) return;
|
|
377
|
+
const content = `// Auto-generated by @fluixi/start — do not edit.
|
|
378
|
+
${blocks.join("\n")}
|
|
379
|
+
`;
|
|
380
|
+
const file = (0, import_node_path3.resolve)(root, "src/fluixi-start-env.d.ts");
|
|
381
|
+
try {
|
|
382
|
+
if (!(0, import_node_fs2.existsSync)(file) || (0, import_node_fs2.readFileSync)(file, "utf8") !== content) (0, import_node_fs2.writeFileSync)(file, content);
|
|
383
|
+
} catch {
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
async function fluixiPlugins(cfg) {
|
|
389
|
+
const { fluixi, fluixiRoutesPlugin } = await import("@fluixi/core/plugins");
|
|
390
|
+
const { serverFunctionsVitePlugin } = await import("@fluixi/compiler/integrations");
|
|
391
|
+
const plugins = [
|
|
392
|
+
serverFunctionsVitePlugin(),
|
|
393
|
+
fluixiRoutesPlugin({ routesDir: cfg.routesDir, polyfills: false }),
|
|
394
|
+
// The app's component-resolution rules reach the compiler here; without this an SSR
|
|
395
|
+
// app has no way to say where an unimported tag comes from.
|
|
396
|
+
//
|
|
397
|
+
// `fluixi()` and not `createVitePlugin()`: the latter is the bare compiler integration,
|
|
398
|
+
// and the reactive graph — agent, source marks, /__fluixi/graph — is composed on top of
|
|
399
|
+
// it by the former. A start app got no graph at all until this changed.
|
|
400
|
+
fluixi({ resolve: cfg.resolve })
|
|
401
|
+
];
|
|
402
|
+
if (cfg.i18n) plugins.push(i18nVitePlugin(cfg.i18n));
|
|
403
|
+
const { apiRoutesVitePlugin: apiRoutesVitePlugin2 } = await Promise.resolve().then(() => (init_api(), api_exports));
|
|
404
|
+
plugins.push(apiRoutesVitePlugin2({ dir: cfg.apiDir }));
|
|
405
|
+
plugins.push(startTypesPlugin(cfg));
|
|
406
|
+
return plugins;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// src/adapter.ts
|
|
410
|
+
var import_promises = require("node:fs/promises");
|
|
411
|
+
var import_node_fs3 = require("node:fs");
|
|
412
|
+
var import_node_path4 = require("node:path");
|
|
413
|
+
var import_node_url = require("node:url");
|
|
414
|
+
var import_node_module = require("node:module");
|
|
415
|
+
|
|
416
|
+
// src/handler.ts
|
|
417
|
+
function streamDocument(head, body, tail) {
|
|
418
|
+
const enc = new TextEncoder();
|
|
419
|
+
const stream = new ReadableStream({
|
|
420
|
+
async start(controller) {
|
|
421
|
+
try {
|
|
422
|
+
controller.enqueue(enc.encode(head));
|
|
423
|
+
const reader = body.getReader();
|
|
424
|
+
for (; ; ) {
|
|
425
|
+
const { done: done2, value } = await reader.read();
|
|
426
|
+
if (done2) break;
|
|
427
|
+
controller.enqueue(value);
|
|
428
|
+
}
|
|
429
|
+
controller.enqueue(enc.encode(tail));
|
|
430
|
+
controller.close();
|
|
431
|
+
} catch (err) {
|
|
432
|
+
controller.error(err);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
return new Response(stream, {
|
|
437
|
+
status: 200,
|
|
438
|
+
headers: { "content-type": "text/html; charset=utf-8" }
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
function createRequestHandler(render) {
|
|
442
|
+
return async (request) => {
|
|
443
|
+
const url = new URL(request.url);
|
|
444
|
+
try {
|
|
445
|
+
const html = await render(url.pathname + url.search, request);
|
|
446
|
+
return new Response(html, {
|
|
447
|
+
status: 200,
|
|
448
|
+
headers: { "content-type": "text/html; charset=utf-8" }
|
|
449
|
+
});
|
|
450
|
+
} catch (e) {
|
|
451
|
+
return new Response(String(e?.stack || e), {
|
|
452
|
+
status: 500,
|
|
453
|
+
headers: { "content-type": "text/plain; charset=utf-8" }
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/middleware.ts
|
|
460
|
+
function normalizeMiddleware(mod) {
|
|
461
|
+
const def = mod?.default ?? mod;
|
|
462
|
+
if (def == null) return [];
|
|
463
|
+
if (Array.isArray(def)) return def.filter((m) => typeof m === "function");
|
|
464
|
+
return typeof def === "function" ? [def] : [];
|
|
465
|
+
}
|
|
466
|
+
function composeMiddleware(middlewares, core) {
|
|
467
|
+
if (middlewares.length === 0) return core;
|
|
468
|
+
return (request) => {
|
|
469
|
+
let lastCalled = -1;
|
|
470
|
+
const dispatch = (i) => {
|
|
471
|
+
if (i <= lastCalled) {
|
|
472
|
+
return Promise.reject(new Error("middleware called next() more than once"));
|
|
473
|
+
}
|
|
474
|
+
lastCalled = i;
|
|
475
|
+
const mw = middlewares[i];
|
|
476
|
+
if (!mw) return Promise.resolve(core(request));
|
|
477
|
+
return Promise.resolve(mw(request, () => dispatch(i + 1)));
|
|
478
|
+
};
|
|
479
|
+
return dispatch(0);
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/handler-core.ts
|
|
484
|
+
function createHandlerFrom({ template, mod, cfg, middleware, preload }) {
|
|
485
|
+
const withPreload = (html, pathname) => preload ? injectPreload(html, preload(pathname)) : html;
|
|
486
|
+
let core;
|
|
487
|
+
if (!cfg.ssr) {
|
|
488
|
+
core = createRequestHandler(async (url) => withPreload(template, url.split("?")[0]));
|
|
489
|
+
} else if (typeof mod.renderStream === "function") {
|
|
490
|
+
core = async (request) => {
|
|
491
|
+
const url = new URL(request.url);
|
|
492
|
+
const { head, tail } = splitTemplate(withPreload(template, url.pathname), cfg.mountId);
|
|
493
|
+
const body = await mod.renderStream(url.pathname + url.search, request);
|
|
494
|
+
return streamDocument(head, body, tail);
|
|
495
|
+
};
|
|
496
|
+
} else {
|
|
497
|
+
const render = mod.render ?? mod.default;
|
|
498
|
+
core = createRequestHandler(
|
|
499
|
+
async (url, request) => (
|
|
500
|
+
// `url` here is `pathname + search`; matching only ever wants the path.
|
|
501
|
+
injectAppAndHead(withPreload(template, url.split("?")[0]), await render(url, request), cfg.mountId)
|
|
502
|
+
)
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
if (typeof mod.handleServerFn === "function" && typeof mod.isServerFnRequest === "function") {
|
|
506
|
+
const render = core;
|
|
507
|
+
core = async (request) => mod.isServerFnRequest(request) ? mod.handleServerFn(request) : render(request);
|
|
508
|
+
}
|
|
509
|
+
if (typeof mod.handleApiRequest === "function" && typeof mod.isApiRequest === "function") {
|
|
510
|
+
const next = core;
|
|
511
|
+
core = async (request) => mod.isApiRequest(request) ? mod.handleApiRequest(request) : next(request);
|
|
512
|
+
}
|
|
513
|
+
if (!middleware) return guardHandler(core);
|
|
514
|
+
return guardHandler(composeMiddleware(normalizeMiddleware(middleware), core));
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// src/preload.ts
|
|
518
|
+
var import_router = require("@fluixi/core/router");
|
|
519
|
+
function createPreloader(assets) {
|
|
520
|
+
const patterns = Object.keys(assets ?? {});
|
|
521
|
+
if (!patterns.length) return () => [];
|
|
522
|
+
const routes = patterns.map((path) => ({ path, meta: { path } }));
|
|
523
|
+
return (pathname) => {
|
|
524
|
+
const matched = (0, import_router.matchRoutes)(routes, pathname)?.matched;
|
|
525
|
+
const leaf = matched?.[matched.length - 1];
|
|
526
|
+
const pattern = leaf?.route.meta?.path;
|
|
527
|
+
return pattern ? assets[pattern] ?? [] : [];
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// src/adapter.ts
|
|
532
|
+
async function createProdHandler(config = {}) {
|
|
533
|
+
const cfg = resolveConfig(config);
|
|
534
|
+
const clientDir = (0, import_node_path4.resolve)(cfg.root, "dist/client");
|
|
535
|
+
const template = await readTemplate(clientDir, (0, import_node_path4.resolve)(cfg.root, "dist/server"));
|
|
536
|
+
const serverEntry = (0, import_node_path4.resolve)(cfg.root, "dist/server", "entry-server.js");
|
|
537
|
+
const mod = (0, import_node_fs3.existsSync)(serverEntry) ? await import(
|
|
538
|
+
/* @vite-ignore */
|
|
539
|
+
(0, import_node_url.pathToFileURL)(serverEntry).href
|
|
540
|
+
) : {};
|
|
541
|
+
const mwFile = (0, import_node_path4.resolve)(cfg.root, "dist/server", "middleware.js");
|
|
542
|
+
const middleware = (0, import_node_fs3.existsSync)(mwFile) ? await import(
|
|
543
|
+
/* @vite-ignore */
|
|
544
|
+
(0, import_node_url.pathToFileURL)(mwFile).href
|
|
545
|
+
) : void 0;
|
|
546
|
+
return createHandlerFrom({ template, mod, cfg, middleware, preload: await loadPreloader(cfg) });
|
|
547
|
+
}
|
|
548
|
+
async function readTemplate(clientDir, serverDir) {
|
|
549
|
+
const stashed = (0, import_node_path4.join)(serverDir, "template.html");
|
|
550
|
+
return (0, import_promises.readFile)((0, import_node_fs3.existsSync)(stashed) ? stashed : (0, import_node_path4.join)(clientDir, "index.html"), "utf-8");
|
|
551
|
+
}
|
|
552
|
+
async function loadPreloader(cfg) {
|
|
553
|
+
const file = (0, import_node_path4.resolve)(cfg.root, "dist/server", "route-assets.json");
|
|
554
|
+
if (!(0, import_node_fs3.existsSync)(file)) return void 0;
|
|
555
|
+
try {
|
|
556
|
+
return createPreloader(JSON.parse(await (0, import_promises.readFile)(file, "utf-8")));
|
|
557
|
+
} catch {
|
|
558
|
+
return void 0;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
var GENERATED_SUBPATHS = /* @__PURE__ */ new Set(["./routes", "./server-fns", "./api-routes"]);
|
|
562
|
+
function ssrBuildOptions(cfg) {
|
|
563
|
+
const noExternal = cfg.ssrNoExternal.length ? cfg.ssrNoExternal : void 0;
|
|
564
|
+
const mode = cfg.adapter?.bundle;
|
|
565
|
+
if (mode === "inline") return { noExternal: true };
|
|
566
|
+
if (mode !== "external") return { noExternal };
|
|
567
|
+
const external = frameworkDeps(cfg.root).filter((name) => !cfg.ssrNoExternal.includes(name)).flatMap((name) => withSubpaths(cfg.root, name));
|
|
568
|
+
if (!external.length) {
|
|
569
|
+
console.warn(
|
|
570
|
+
` fluixi: adapter "${cfg.adapter?.name}" builds an externalized server, but no fluixi or @fluixi/* dependency was found in ${(0, import_node_path4.join)(cfg.root, "package.json")} — bundling everything.`
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
return { noExternal, external: external.length ? external : void 0 };
|
|
574
|
+
}
|
|
575
|
+
function frameworkDeps(root) {
|
|
576
|
+
const pkg = readPackage((0, import_node_path4.join)(root, "package.json"));
|
|
577
|
+
if (!pkg) return [];
|
|
578
|
+
const declared = { ...pkg.dependencies, ...pkg.peerDependencies, ...pkg.optionalDependencies };
|
|
579
|
+
return Object.keys(declared).filter((id) => id === "@fluixi/core" || id.startsWith("@fluixi/"));
|
|
580
|
+
}
|
|
581
|
+
function withSubpaths(root, name) {
|
|
582
|
+
const exports2 = resolvePackage(root, name)?.exports;
|
|
583
|
+
if (!exports2 || typeof exports2 !== "object") return [name];
|
|
584
|
+
const ids = [name];
|
|
585
|
+
for (const key of Object.keys(exports2)) {
|
|
586
|
+
if (!key.startsWith("./") || key.includes("*")) continue;
|
|
587
|
+
if (key === "./package.json" || GENERATED_SUBPATHS.has(key)) continue;
|
|
588
|
+
ids.push(`${name}/${key.slice(2)}`);
|
|
589
|
+
}
|
|
590
|
+
return ids;
|
|
591
|
+
}
|
|
592
|
+
function resolvePackage(root, name) {
|
|
593
|
+
try {
|
|
594
|
+
return readPackage((0, import_node_module.createRequire)((0, import_node_path4.join)(root, "index.js")).resolve(`${name}/package.json`));
|
|
595
|
+
} catch {
|
|
596
|
+
}
|
|
597
|
+
for (let dir = root; ; dir = (0, import_node_path4.dirname)(dir)) {
|
|
598
|
+
const pkg = readPackage((0, import_node_path4.join)(dir, "node_modules", name, "package.json"));
|
|
599
|
+
if (pkg) return pkg;
|
|
600
|
+
if ((0, import_node_path4.dirname)(dir) === dir) return void 0;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
function readPackage(file) {
|
|
604
|
+
if (!(0, import_node_fs3.existsSync)(file)) return void 0;
|
|
605
|
+
try {
|
|
606
|
+
return JSON.parse((0, import_node_fs3.readFileSync)(file, "utf-8"));
|
|
607
|
+
} catch {
|
|
608
|
+
return void 0;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// src/commands/prerender.ts
|
|
613
|
+
var import_promises2 = require("node:fs/promises");
|
|
614
|
+
var import_node_path5 = require("node:path");
|
|
615
|
+
|
|
616
|
+
// src/ui.ts
|
|
617
|
+
var COLOR = process.env.NO_COLOR || process.env.FORCE_COLOR === "0" ? false : (!!process.stdout.isTTY || !!process.env.FORCE_COLOR) && process.env.TERM !== "dumb";
|
|
618
|
+
var sgr = (open, close) => (s) => COLOR ? `\x1B[${open}m${s}\x1B[${close}m` : `${s}`;
|
|
619
|
+
var c = {
|
|
620
|
+
bold: sgr(1, 22),
|
|
621
|
+
dim: sgr(2, 22),
|
|
622
|
+
italic: sgr(3, 23),
|
|
623
|
+
underline: sgr(4, 24),
|
|
624
|
+
red: sgr(31, 39),
|
|
625
|
+
green: sgr(32, 39),
|
|
626
|
+
yellow: sgr(33, 39),
|
|
627
|
+
blue: sgr(34, 39),
|
|
628
|
+
magenta: sgr(35, 39),
|
|
629
|
+
cyan: sgr(36, 39),
|
|
630
|
+
gray: sgr(90, 39)
|
|
631
|
+
};
|
|
632
|
+
var rgb = (r, g, b) => (s) => COLOR ? `\x1B[38;2;${r};${g};${b}m${s}\x1B[39m` : s;
|
|
633
|
+
var BRAND = [
|
|
634
|
+
[34, 211, 238],
|
|
635
|
+
// cyan-400
|
|
636
|
+
[56, 189, 248],
|
|
637
|
+
[129, 140, 248],
|
|
638
|
+
[167, 139, 250],
|
|
639
|
+
[192, 132, 252],
|
|
640
|
+
// violet-400
|
|
641
|
+
[217, 130, 245]
|
|
642
|
+
];
|
|
643
|
+
function gradient(text) {
|
|
644
|
+
if (!COLOR) return text;
|
|
645
|
+
const chars = [...text];
|
|
646
|
+
return chars.map((ch, i) => {
|
|
647
|
+
const [r, g, b] = BRAND[Math.min(BRAND.length - 1, Math.floor(i / Math.max(1, chars.length - 1) * (BRAND.length - 1)))];
|
|
648
|
+
return rgb(r, g, b)(ch);
|
|
649
|
+
}).join("");
|
|
650
|
+
}
|
|
651
|
+
function wordmark(version) {
|
|
652
|
+
const mark = `${gradient("◆")} ${c.bold(gradient("fluixi"))}`;
|
|
653
|
+
return version ? `${mark} ${c.dim(`v${version}`)}` : mark;
|
|
654
|
+
}
|
|
655
|
+
var SYM = {
|
|
656
|
+
arrow: c.gray("➜"),
|
|
657
|
+
ok: c.green("✓"),
|
|
658
|
+
err: c.red("✗")
|
|
659
|
+
};
|
|
660
|
+
function step(text) {
|
|
661
|
+
process.stdout.write(` ${c.cyan("•")} ${text}
|
|
662
|
+
`);
|
|
663
|
+
}
|
|
664
|
+
function done(text) {
|
|
665
|
+
process.stdout.write(` ${SYM.ok} ${text}
|
|
666
|
+
`);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// src/commands/prerender.ts
|
|
670
|
+
function htmlPath(url) {
|
|
671
|
+
const clean = url.split(/[?#]/)[0].replace(/\/+$/, "");
|
|
672
|
+
return clean === "" ? "index.html" : `${clean.replace(/^\//, "")}/index.html`;
|
|
673
|
+
}
|
|
674
|
+
function extractLinks(html) {
|
|
675
|
+
const out = /* @__PURE__ */ new Set();
|
|
676
|
+
for (const m of html.matchAll(/href=["'](\/[^"'#?]*)["']/g)) {
|
|
677
|
+
const href = m[1];
|
|
678
|
+
if (/\.[a-z0-9]+$/i.test(href)) continue;
|
|
679
|
+
out.add(href.replace(/\/+$/, "") || "/");
|
|
680
|
+
}
|
|
681
|
+
return [...out];
|
|
682
|
+
}
|
|
683
|
+
var norm = (u) => {
|
|
684
|
+
const p = (u.startsWith("/") ? u : "/" + u).replace(/\/+$/, "");
|
|
685
|
+
return p || "/";
|
|
686
|
+
};
|
|
687
|
+
async function prerender(config = {}) {
|
|
688
|
+
const cfg = resolveConfig(config);
|
|
689
|
+
if (!cfg.prerender) return [];
|
|
690
|
+
if (!cfg.ssr) {
|
|
691
|
+
process.stdout.write(` ${c.yellow("!")} prerender needs ssr:true — skipped
|
|
692
|
+
`);
|
|
693
|
+
return [];
|
|
694
|
+
}
|
|
695
|
+
step("prerender");
|
|
696
|
+
const clientDir = (0, import_node_path5.resolve)(cfg.root, "dist/client");
|
|
697
|
+
const handler = await createProdHandler(config);
|
|
698
|
+
const { routes, crawl } = cfg.prerender;
|
|
699
|
+
const queue = routes.map(norm);
|
|
700
|
+
const seen = /* @__PURE__ */ new Set();
|
|
701
|
+
const written = [];
|
|
702
|
+
while (queue.length) {
|
|
703
|
+
const url = queue.shift();
|
|
704
|
+
if (seen.has(url)) continue;
|
|
705
|
+
seen.add(url);
|
|
706
|
+
let html;
|
|
707
|
+
try {
|
|
708
|
+
const res = await handler(new Request("http://prerender.local" + url));
|
|
709
|
+
if (!res.ok) {
|
|
710
|
+
process.stdout.write(` ${c.yellow("skip")} ${url} ${c.gray("→ " + res.status)}
|
|
711
|
+
`);
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
if (!(res.headers.get("content-type") || "").includes("html")) continue;
|
|
715
|
+
html = await res.text();
|
|
716
|
+
} catch (e) {
|
|
717
|
+
process.stdout.write(` ${c.yellow("skip")} ${url} ${c.gray("→ " + e.message)}
|
|
718
|
+
`);
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
const file = (0, import_node_path5.join)(clientDir, htmlPath(url));
|
|
722
|
+
await (0, import_promises2.mkdir)((0, import_node_path5.dirname)(file), { recursive: true });
|
|
723
|
+
await (0, import_promises2.writeFile)(file, html);
|
|
724
|
+
written.push(url);
|
|
725
|
+
if (crawl) {
|
|
726
|
+
for (const link of extractLinks(html)) if (!seen.has(link)) queue.push(link);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
done(`${c.bold("prerendered")} ${c.bold(String(written.length))} page(s) ${c.gray("→ dist/client")}
|
|
730
|
+
`);
|
|
731
|
+
return written;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// src/commands/build.ts
|
|
735
|
+
async function build(config = {}) {
|
|
736
|
+
const startedAt = Date.now();
|
|
737
|
+
const cfg = resolveConfig(config);
|
|
738
|
+
await loadServerEnv(cfg.root, "production");
|
|
739
|
+
const { build: viteBuild } = await import("vite");
|
|
740
|
+
const plugins = await fluixiPlugins(cfg);
|
|
741
|
+
const ssr = ssrBuildOptions(cfg);
|
|
742
|
+
process.stdout.write(`
|
|
743
|
+
${wordmark()} ${c.gray("building for production")}
|
|
744
|
+
|
|
745
|
+
`);
|
|
746
|
+
step("client bundle");
|
|
747
|
+
await viteBuild({
|
|
748
|
+
root: cfg.root,
|
|
749
|
+
plugins,
|
|
750
|
+
logLevel: "warn",
|
|
751
|
+
build: { outDir: "dist/client", ssrManifest: true, emptyOutDir: true }
|
|
752
|
+
});
|
|
753
|
+
const serverEntrySrc = (0, import_node_path6.resolve)(cfg.root, cfg.serverEntry);
|
|
754
|
+
if (cfg.ssr || (0, import_node_fs4.existsSync)(serverEntrySrc)) {
|
|
755
|
+
step(`server bundle${bundleNote(cfg)}`);
|
|
756
|
+
await viteBuild({
|
|
757
|
+
root: cfg.root,
|
|
758
|
+
plugins,
|
|
759
|
+
ssr,
|
|
760
|
+
build: {
|
|
761
|
+
outDir: "dist/server",
|
|
762
|
+
ssr: serverEntrySrc,
|
|
763
|
+
emptyOutDir: true
|
|
764
|
+
}
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
const middlewareSrc = (0, import_node_path6.resolve)(cfg.root, cfg.middleware);
|
|
768
|
+
if ((0, import_node_fs4.existsSync)(middlewareSrc)) {
|
|
769
|
+
step("middleware");
|
|
770
|
+
await viteBuild({
|
|
771
|
+
root: cfg.root,
|
|
772
|
+
plugins,
|
|
773
|
+
logLevel: "warn",
|
|
774
|
+
ssr,
|
|
775
|
+
build: {
|
|
776
|
+
outDir: "dist/server",
|
|
777
|
+
ssr: middlewareSrc,
|
|
778
|
+
emptyOutDir: false
|
|
779
|
+
}
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
await collectRouteAssets(cfg);
|
|
783
|
+
if (cfg.prerender) {
|
|
784
|
+
await (0, import_promises3.copyFile)(
|
|
785
|
+
(0, import_node_path6.resolve)(cfg.root, "dist/client", "index.html"),
|
|
786
|
+
(0, import_node_path6.resolve)(cfg.root, "dist/server", "template.html")
|
|
787
|
+
);
|
|
788
|
+
process.stdout.write("\n");
|
|
789
|
+
await prerender(config);
|
|
790
|
+
}
|
|
791
|
+
if (cfg.adapter?.build) {
|
|
792
|
+
step(`${cfg.adapter.name} output`);
|
|
793
|
+
await cfg.adapter.build({
|
|
794
|
+
cfg,
|
|
795
|
+
root: cfg.root,
|
|
796
|
+
clientDir: (0, import_node_path6.resolve)(cfg.root, "dist/client"),
|
|
797
|
+
serverDir: (0, import_node_path6.resolve)(cfg.root, "dist/server"),
|
|
798
|
+
hasServer: (0, import_node_fs4.existsSync)((0, import_node_path6.resolve)(cfg.root, "dist/server", "entry-server.js")),
|
|
799
|
+
bundleEntry: (source, outFile) => bundleEntry(cfg, plugins, source, outFile)
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
const ms = Date.now() - startedAt;
|
|
803
|
+
process.stdout.write("\n");
|
|
804
|
+
done(`${c.bold("built")} in ${c.bold(String(ms))} ms ${c.gray("→ dist/client + dist/server")}
|
|
805
|
+
`);
|
|
806
|
+
}
|
|
807
|
+
async function collectRouteAssets(cfg) {
|
|
808
|
+
const clientDir = (0, import_node_path6.resolve)(cfg.root, "dist/client");
|
|
809
|
+
const manifestFile = (0, import_node_path6.join)(clientDir, ".vite", "ssr-manifest.json");
|
|
810
|
+
if (!(0, import_node_fs4.existsSync)(manifestFile)) return;
|
|
811
|
+
const serverDir = (0, import_node_path6.resolve)(cfg.root, "dist/server");
|
|
812
|
+
const routesDir = (0, import_node_path6.resolve)(cfg.root, cfg.routesDir);
|
|
813
|
+
if ((0, import_node_fs4.existsSync)(serverDir) && (0, import_node_fs4.existsSync)(routesDir)) {
|
|
814
|
+
const manifest = JSON.parse(await (0, import_promises3.readFile)(manifestFile, "utf-8"));
|
|
815
|
+
const { scanRoutes } = await import("@fluixi/core/plugins");
|
|
816
|
+
const base = cfg.routesDir.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/$/, "");
|
|
817
|
+
const assets = {};
|
|
818
|
+
const walk = (routes, inherited) => {
|
|
819
|
+
for (const route of routes) {
|
|
820
|
+
const files = [.../* @__PURE__ */ new Set([...inherited, ...manifest[`${base}/${route.filePath}`] ?? []])];
|
|
821
|
+
if (files.length) assets[route.urlPath] = [.../* @__PURE__ */ new Set([...assets[route.urlPath] ?? [], ...files])];
|
|
822
|
+
walk(route.children, files);
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
walk(scanRoutes(routesDir), []);
|
|
826
|
+
await (0, import_promises3.writeFile)((0, import_node_path6.join)(serverDir, "route-assets.json"), JSON.stringify(assets, null, 2) + "\n");
|
|
827
|
+
}
|
|
828
|
+
await (0, import_promises3.rm)((0, import_node_path6.join)(clientDir, ".vite"), { recursive: true, force: true });
|
|
829
|
+
}
|
|
830
|
+
async function bundleEntry(cfg, plugins, source, outFile) {
|
|
831
|
+
const { build: viteBuild } = await import("vite");
|
|
832
|
+
const dir = (0, import_node_path6.dirname)(outFile);
|
|
833
|
+
const temp = (0, import_node_path6.join)(dir, `.fluixi-entry-${Date.now()}.js`);
|
|
834
|
+
await (0, import_promises3.mkdir)(dir, { recursive: true });
|
|
835
|
+
await (0, import_promises3.writeFile)(temp, source);
|
|
836
|
+
try {
|
|
837
|
+
await viteBuild({
|
|
838
|
+
root: cfg.root,
|
|
839
|
+
plugins,
|
|
840
|
+
logLevel: "warn",
|
|
841
|
+
// Nothing may be left to resolve: these run as an uploaded file, not an install.
|
|
842
|
+
ssr: { noExternal: true },
|
|
843
|
+
build: {
|
|
844
|
+
outDir: dir,
|
|
845
|
+
emptyOutDir: false,
|
|
846
|
+
ssr: temp,
|
|
847
|
+
rollupOptions: {
|
|
848
|
+
output: {
|
|
849
|
+
entryFileNames: (0, import_node_path6.basename)(outFile),
|
|
850
|
+
// One file, no chunks. A lazy route would otherwise be split out beside
|
|
851
|
+
// the entry — and for Cloudflare that directory is the published one, so
|
|
852
|
+
// every server chunk would be a public download.
|
|
853
|
+
inlineDynamicImports: true
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
} finally {
|
|
859
|
+
await (0, import_promises3.rm)(temp, { force: true });
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
function bundleNote(cfg) {
|
|
863
|
+
const adapter = cfg.adapter;
|
|
864
|
+
if (!adapter?.bundle) return "";
|
|
865
|
+
return c.gray(` (${adapter.name} · ${adapter.bundle})`);
|
|
866
|
+
}
|
|
867
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
868
|
+
0 && (module.exports = {
|
|
869
|
+
build
|
|
870
|
+
});
|