@moostjs/vite 0.6.9 → 0.6.12
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/index.cjs +57 -55
- package/dist/index.d.ts +25 -0
- package/dist/index.mjs +39 -53
- package/dist/prod-server.cjs +28 -50
- package/dist/prod-server.mjs +26 -24
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
2
3
|
var __create = Object.create;
|
|
3
4
|
var __defProp = Object.defineProperty;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -6,12 +7,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
7
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
8
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
9
|
var __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
@@ -21,12 +26,13 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
26
|
}) : target, mod));
|
|
22
27
|
|
|
23
28
|
//#endregion
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
let node_fs = require("node:fs");
|
|
30
|
+
let node_path = require("node:path");
|
|
31
|
+
let vite = require("vite");
|
|
32
|
+
let magic_string = require("magic-string");
|
|
33
|
+
magic_string = __toESM(magic_string);
|
|
34
|
+
let node_module = require("node:module");
|
|
35
|
+
let moost = require("moost");
|
|
30
36
|
|
|
31
37
|
//#region packages/vite/src/utils.ts
|
|
32
38
|
const PLUGIN_NAME = "moost-vite";
|
|
@@ -57,9 +63,8 @@ function escapeRegex(str) {
|
|
|
57
63
|
}
|
|
58
64
|
function getExternals({ node, workspace }) {
|
|
59
65
|
const pkg = JSON.parse((0, node_fs.readFileSync)("package.json", "utf8").toString());
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
if (node) externals.push(...node_module.builtinModules.map((m) => /* @__PURE__ */ new RegExp(`^${escapeRegex(m)}(/|$)`)), ...node_module.builtinModules.map((m) => /* @__PURE__ */ new RegExp(`^node:${escapeRegex(m)}(/|$)`)));
|
|
66
|
+
const externals = (workspace ? Object.keys(pkg.dependencies || {}) : Object.entries(pkg.dependencies || {}).filter(([key, ver]) => !ver.startsWith("workspace:")).map((i) => i[0])).map((name) => new RegExp(`^${escapeRegex(name)}(/|$)`));
|
|
67
|
+
if (node) externals.push(...node_module.builtinModules.map((m) => new RegExp(`^${escapeRegex(m)}(/|$)`)), ...node_module.builtinModules.map((m) => new RegExp(`^node:${escapeRegex(m)}(/|$)`)));
|
|
63
68
|
return externals;
|
|
64
69
|
}
|
|
65
70
|
|
|
@@ -73,17 +78,17 @@ function getExternals({ node, workspace }) {
|
|
|
73
78
|
*/ function createAdapterDetector(adapter, onInit) {
|
|
74
79
|
return {
|
|
75
80
|
detected: false,
|
|
76
|
-
regex:
|
|
81
|
+
regex: new RegExp(`from\\s+["'](@moostjs\\/event-${adapter})["']`),
|
|
77
82
|
constructor: null,
|
|
78
83
|
ssrLoadModule: null,
|
|
79
84
|
async init() {
|
|
80
85
|
this.detected = true;
|
|
81
86
|
const moduleId = `@moostjs/event-${adapter}`;
|
|
82
|
-
const module
|
|
83
|
-
const constructorName
|
|
84
|
-
getLogger().log(`🔍 [2mExtracting Adapter "${constructorName
|
|
85
|
-
this.constructor = module
|
|
86
|
-
if (onInit && this.constructor) onInit(this.constructor, module
|
|
87
|
+
const module = this.ssrLoadModule ? await this.ssrLoadModule(moduleId) : await import(moduleId);
|
|
88
|
+
const constructorName = `Moost${adapter.charAt(0).toUpperCase() + adapter.slice(1)}`;
|
|
89
|
+
getLogger().log(`🔍 [2mExtracting Adapter "${constructorName}"`);
|
|
90
|
+
this.constructor = module[constructorName];
|
|
91
|
+
if (onInit && this.constructor) onInit(this.constructor, module);
|
|
87
92
|
},
|
|
88
93
|
compare(c) {
|
|
89
94
|
if (this.detected && this.constructor) return this.constructor === c || c instanceof this.constructor || c.prototype instanceof this.constructor;
|
|
@@ -118,7 +123,7 @@ function _define_property(obj, key, value) {
|
|
|
118
123
|
* 2. Compare them against previously recorded handlers to detect
|
|
119
124
|
* which are newly added vs. removed.
|
|
120
125
|
* 3. Store the new state as the old map for the next cycle.
|
|
121
|
-
*/ let LogsStorage = class LogsStorage
|
|
126
|
+
*/ let LogsStorage = class LogsStorage {
|
|
122
127
|
/**
|
|
123
128
|
* Called at the start of an init cycle to
|
|
124
129
|
* clear out the list of new items.
|
|
@@ -214,7 +219,7 @@ function _define_property(obj, key, value) {
|
|
|
214
219
|
*
|
|
215
220
|
* @param {Set<string>} [cleanupInstances] A set of module IDs to remove from the registry.
|
|
216
221
|
*/ function moostRestartCleanup(adapters, onEject, cleanupInstances) {
|
|
217
|
-
const logger
|
|
222
|
+
const logger = getLogger();
|
|
218
223
|
const infact = (0, moost.getMoostInfact)();
|
|
219
224
|
const { registry, scopes } = infact;
|
|
220
225
|
const registries = [registry, ...Object.values(scopes)];
|
|
@@ -226,7 +231,7 @@ function _define_property(obj, key, value) {
|
|
|
226
231
|
const instance = reg[key];
|
|
227
232
|
const viteId = mate.read(instance)?.__vite_id;
|
|
228
233
|
if (viteId && cleanupInstances.has(viteId)) {
|
|
229
|
-
logger
|
|
234
|
+
logger.debug(`🔃 Replacing "${constructorName(instance)}"`);
|
|
230
235
|
delete reg[key];
|
|
231
236
|
}
|
|
232
237
|
}
|
|
@@ -235,12 +240,12 @@ function _define_property(obj, key, value) {
|
|
|
235
240
|
scanParams(instance, (type) => {
|
|
236
241
|
if ((type === moost.Moost || type instanceof moost.Moost || type.prototype instanceof moost.Moost) && (!onEject || onEject(instance, type))) {
|
|
237
242
|
delete reg[key];
|
|
238
|
-
logger
|
|
243
|
+
logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "Moost")`);
|
|
239
244
|
return true;
|
|
240
245
|
}
|
|
241
246
|
for (const adapter of adapters) if (adapter.compare(type) && (!onEject || onEject(instance, type))) {
|
|
242
247
|
delete reg[key];
|
|
243
|
-
logger
|
|
248
|
+
logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "${adapter.constructor.name}")`);
|
|
244
249
|
return true;
|
|
245
250
|
}
|
|
246
251
|
});
|
|
@@ -254,7 +259,7 @@ function _define_property(obj, key, value) {
|
|
|
254
259
|
(0, moost.clearGlobalWooks)();
|
|
255
260
|
}
|
|
256
261
|
function clearDependantRegistry(registry, onEject) {
|
|
257
|
-
const logger
|
|
262
|
+
const logger = getLogger();
|
|
258
263
|
const objSet = /* @__PURE__ */ new Set();
|
|
259
264
|
let somethingIsDeleted = true;
|
|
260
265
|
while (somethingIsDeleted) {
|
|
@@ -265,17 +270,16 @@ function clearDependantRegistry(registry, onEject) {
|
|
|
265
270
|
}
|
|
266
271
|
for (const key of Object.getOwnPropertySymbols(registry)) {
|
|
267
272
|
const instance = registry[key];
|
|
268
|
-
|
|
269
|
-
if (ejected) somethingIsDeleted = true;
|
|
273
|
+
if (checkAndEject(instance, objSet, onEject, registry, key, logger)) somethingIsDeleted = true;
|
|
270
274
|
}
|
|
271
275
|
}
|
|
272
276
|
}
|
|
273
|
-
function checkAndEject(instance, objSet, onEject, registry, key, logger
|
|
277
|
+
function checkAndEject(instance, objSet, onEject, registry, key, logger) {
|
|
274
278
|
let ejected = false;
|
|
275
279
|
scanParams(instance, (type) => {
|
|
276
280
|
if (!objSet.has(type) && (!onEject || onEject(instance, type))) {
|
|
277
281
|
delete registry[key];
|
|
278
|
-
logger
|
|
282
|
+
logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on "${type.name}" which is not in registry)`);
|
|
279
283
|
ejected = true;
|
|
280
284
|
return true;
|
|
281
285
|
}
|
|
@@ -283,8 +287,7 @@ function checkAndEject(instance, objSet, onEject, registry, key, logger$1) {
|
|
|
283
287
|
return ejected;
|
|
284
288
|
}
|
|
285
289
|
function scanParams(instance, cb) {
|
|
286
|
-
const
|
|
287
|
-
const params = mate.read(instance)?.params;
|
|
290
|
+
const params = (0, moost.getMoostMate)().read(instance)?.params;
|
|
288
291
|
if (params?.length) for (const param of params) {
|
|
289
292
|
if (param.type === void 0 || [
|
|
290
293
|
Array,
|
|
@@ -350,7 +353,7 @@ function moostVite(options) {
|
|
|
350
353
|
const adapters = isTest ? [] : [
|
|
351
354
|
createAdapterDetector("http", (MoostHttp, moduleExports) => {
|
|
352
355
|
MoostHttp.prototype.listen = function(...args) {
|
|
353
|
-
logger
|
|
356
|
+
logger.log(`🔌 [2mOvertaking HTTP.listen`);
|
|
354
357
|
if (options.middleware) moostMiddleware = this.getServerCb((req) => {
|
|
355
358
|
pendingNextMap.get(req)?.();
|
|
356
359
|
});
|
|
@@ -358,7 +361,7 @@ function moostVite(options) {
|
|
|
358
361
|
if (options.ssrFetch !== false && moduleExports?.enableLocalFetch) {
|
|
359
362
|
if (localFetchTeardown) localFetchTeardown();
|
|
360
363
|
localFetchTeardown = moduleExports.enableLocalFetch(this);
|
|
361
|
-
logger
|
|
364
|
+
logger.log(`🔀 [2mLocal fetch enabled for SSR`);
|
|
362
365
|
}
|
|
363
366
|
setTimeout(() => {
|
|
364
367
|
args.filter((a) => typeof a === "function").forEach((a) => a());
|
|
@@ -369,25 +372,20 @@ function moostVite(options) {
|
|
|
369
372
|
createAdapterDetector("cli"),
|
|
370
373
|
createAdapterDetector("wf")
|
|
371
374
|
];
|
|
372
|
-
/** A logger instance for plugin debug output. */ const logger
|
|
375
|
+
/** A logger instance for plugin debug output. */ const logger = isTest ? console : getLogger();
|
|
373
376
|
let reloadRequired = false;
|
|
374
377
|
patchMoostHandlerLogging();
|
|
375
378
|
const pluginConfig = {
|
|
376
379
|
name: PLUGIN_NAME,
|
|
377
380
|
enforce: "pre",
|
|
378
|
-
config(cfg) {
|
|
381
|
+
config(cfg, env) {
|
|
379
382
|
if (options.middleware) {
|
|
380
|
-
const defaultExternals = getExternals({
|
|
381
|
-
node: true,
|
|
382
|
-
workspace: true
|
|
383
|
-
});
|
|
384
383
|
const serverDefines = {
|
|
385
384
|
"process.env.MOOST_DEFERRED_ENV": "\"production\"",
|
|
386
385
|
__MOOST_ENTRY__: JSON.stringify(options.entry),
|
|
387
386
|
__MOOST_PREFIX__: JSON.stringify(prefix || "/api")
|
|
388
387
|
};
|
|
389
|
-
const
|
|
390
|
-
const ssrInput = { server: serverEntry };
|
|
388
|
+
const ssrInput = { server: options.serverEntry || generatedServerEntry(cfg.root) };
|
|
391
389
|
if (options.ssrEntry) {
|
|
392
390
|
const ssrBasename = entryBasename(options.ssrEntry);
|
|
393
391
|
ssrInput[`ssr/${ssrBasename.replace(/\.js$/, "")}`] = options.ssrEntry;
|
|
@@ -396,8 +394,17 @@ function moostVite(options) {
|
|
|
396
394
|
serverDefines.__MOOST_SSR_STATE__ = JSON.stringify(options.ssrState || DEFAULT_SSR_STATE);
|
|
397
395
|
}
|
|
398
396
|
const outDir = cfg.build?.outDir || "dist";
|
|
397
|
+
const isBuild = env.command === "build";
|
|
398
|
+
const userNoExternal = cfg.ssr?.noExternal;
|
|
399
|
+
const ssrNoExternal = isBuild ? true : Array.from(new Set([...Array.isArray(userNoExternal) ? userNoExternal : userNoExternal && typeof userNoExternal !== "boolean" ? [userNoExternal] : [], /^@moostjs\/vite($|\/)/]));
|
|
400
|
+
const userExternal = cfg.ssr?.external;
|
|
401
|
+
const ssrExternal = isBuild ? [...Array.isArray(userExternal) ? userExternal : [], ...options.ssrExternal ?? []] : void 0;
|
|
399
402
|
return {
|
|
400
403
|
...options.ssrEntry && { appType: "custom" },
|
|
404
|
+
ssr: {
|
|
405
|
+
noExternal: ssrNoExternal,
|
|
406
|
+
...ssrExternal && ssrExternal.length > 0 ? { external: ssrExternal } : {}
|
|
407
|
+
},
|
|
401
408
|
builder: { async buildApp(builder) {
|
|
402
409
|
(0, node_fs.rmSync)((0, node_path.resolve)(cfg.root || process.cwd(), outDir), {
|
|
403
410
|
recursive: true,
|
|
@@ -419,10 +426,6 @@ function moostVite(options) {
|
|
|
419
426
|
sourcemap: !!(options.sourcemap ?? true),
|
|
420
427
|
rollupOptions: {
|
|
421
428
|
input: ssrInput,
|
|
422
|
-
external: (id) => {
|
|
423
|
-
if (id === "@moostjs/vite/server") return false;
|
|
424
|
-
return defaultExternals.some((ext) => ext.test(id));
|
|
425
|
-
},
|
|
426
429
|
output: { format: "esm" }
|
|
427
430
|
}
|
|
428
431
|
},
|
|
@@ -510,10 +513,10 @@ function moostVite(options) {
|
|
|
510
513
|
if (reloadRequired) {
|
|
511
514
|
reloadRequired = false;
|
|
512
515
|
console.log();
|
|
513
|
-
logger
|
|
516
|
+
logger.debug("🚀 Reloading Moost App...");
|
|
514
517
|
console.log();
|
|
515
518
|
await ssrImport(options.entry);
|
|
516
|
-
await new Promise((resolve
|
|
519
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
517
520
|
}
|
|
518
521
|
if (options.middleware && prefix) {
|
|
519
522
|
const url = req.url || "";
|
|
@@ -534,7 +537,7 @@ function moostVite(options) {
|
|
|
534
537
|
if (file.endsWith(".ts")) {
|
|
535
538
|
const modules = this.environment.moduleGraph.getModulesByFile(file);
|
|
536
539
|
if (modules) {
|
|
537
|
-
logger
|
|
540
|
+
logger.debug(`🔃 Hot update: ${file}`);
|
|
538
541
|
const cleanupInstances = /* @__PURE__ */ new Set();
|
|
539
542
|
for (const mod of modules) {
|
|
540
543
|
const allImporters = gatherAllImporters(mod);
|
|
@@ -560,7 +563,7 @@ function moostVite(options) {
|
|
|
560
563
|
delete pluginConfig.transform;
|
|
561
564
|
delete pluginConfig.hotUpdate;
|
|
562
565
|
}
|
|
563
|
-
|
|
566
|
+
return [pluginConfig, !isProd && !isTest && options.middleware && options.ssrEntry ? {
|
|
564
567
|
name: `${PLUGIN_NAME}:ssr-fallback`,
|
|
565
568
|
async configureServer(server) {
|
|
566
569
|
const ssrOutlet = options.ssrOutlet || DEFAULT_SSR_OUTLET;
|
|
@@ -577,7 +580,7 @@ function moostVite(options) {
|
|
|
577
580
|
const { html: appHtml, state } = await render(url);
|
|
578
581
|
res.statusCode = 200;
|
|
579
582
|
res.setHeader("Content-Type", "text/html");
|
|
580
|
-
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}
|
|
583
|
+
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}<\/script>` : ""));
|
|
581
584
|
} catch (error) {
|
|
582
585
|
server.ssrFixStacktrace(error);
|
|
583
586
|
console.error(error);
|
|
@@ -587,8 +590,7 @@ function moostVite(options) {
|
|
|
587
590
|
});
|
|
588
591
|
};
|
|
589
592
|
}
|
|
590
|
-
} : null;
|
|
591
|
-
return [pluginConfig, ssrFallbackPlugin];
|
|
593
|
+
} : null];
|
|
592
594
|
}
|
|
593
595
|
|
|
594
596
|
//#endregion
|
package/dist/index.d.ts
CHANGED
|
@@ -120,6 +120,31 @@ interface TMoostViteDevOptions {
|
|
|
120
120
|
* production server that serves static files + Moost API (+ SSR if configured).
|
|
121
121
|
*/
|
|
122
122
|
serverEntry?: string;
|
|
123
|
+
/**
|
|
124
|
+
* Packages to keep external in the production SSR bundle.
|
|
125
|
+
*
|
|
126
|
+
* In middleware mode during `vite build`, the plugin defaults to
|
|
127
|
+
* `ssr.noExternal: true` — every dependency is bundled into the SSR
|
|
128
|
+
* output. This avoids two common failure modes:
|
|
129
|
+
* - duplicate module instances of packages like `@wooksjs/event-http`
|
|
130
|
+
* whose Symbol-identity slot keys do not match across instances;
|
|
131
|
+
* - pnpm strict-resolution failures where externalized transitive deps
|
|
132
|
+
* are not hoist-accessible from the consumer's `node_modules`.
|
|
133
|
+
*
|
|
134
|
+
* Use `ssrExternal` to opt specific packages OUT of bundling for cases
|
|
135
|
+
* where bundling is impossible or undesirable — native bindings such as
|
|
136
|
+
* `mongodb`, `ioredis`, or anything that loads `.node` modules.
|
|
137
|
+
*
|
|
138
|
+
* Only applied in `middleware: true` mode during `vite build`. Dev
|
|
139
|
+
* (`vite serve`) is unaffected: Vite's default externalizer continues
|
|
140
|
+
* to handle node_modules, which is required so CJS-only packages like
|
|
141
|
+
* `@vue/server-renderer` can be loaded by Node's ESM/CJS interop
|
|
142
|
+
* instead of evaluated by Vite's ESM-only SSR module runner.
|
|
143
|
+
*
|
|
144
|
+
* Concatenated with any `cfg.ssr.external` the consumer sets directly
|
|
145
|
+
* in `vite.config.ts`.
|
|
146
|
+
*/
|
|
147
|
+
ssrExternal?: string[];
|
|
123
148
|
}
|
|
124
149
|
declare function moostVite(options: TMoostViteDevOptions): PluginOption;
|
|
125
150
|
|
package/dist/index.mjs
CHANGED
|
@@ -34,9 +34,8 @@ function escapeRegex(str) {
|
|
|
34
34
|
}
|
|
35
35
|
function getExternals({ node, workspace }) {
|
|
36
36
|
const pkg = JSON.parse(readFileSync("package.json", "utf8").toString());
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
if (node) externals.push(...builtinModules.map((m) => /* @__PURE__ */ new RegExp(`^${escapeRegex(m)}(/|$)`)), ...builtinModules.map((m) => /* @__PURE__ */ new RegExp(`^node:${escapeRegex(m)}(/|$)`)));
|
|
37
|
+
const externals = (workspace ? Object.keys(pkg.dependencies || {}) : Object.entries(pkg.dependencies || {}).filter(([key, ver]) => !ver.startsWith("workspace:")).map((i) => i[0])).map((name) => new RegExp(`^${escapeRegex(name)}(/|$)`));
|
|
38
|
+
if (node) externals.push(...builtinModules.map((m) => new RegExp(`^${escapeRegex(m)}(/|$)`)), ...builtinModules.map((m) => new RegExp(`^node:${escapeRegex(m)}(/|$)`)));
|
|
40
39
|
return externals;
|
|
41
40
|
}
|
|
42
41
|
|
|
@@ -50,16 +49,16 @@ function getExternals({ node, workspace }) {
|
|
|
50
49
|
*/ function createAdapterDetector(adapter, onInit) {
|
|
51
50
|
return {
|
|
52
51
|
detected: false,
|
|
53
|
-
regex:
|
|
52
|
+
regex: new RegExp(`from\\s+["'](@moostjs\\/event-${adapter})["']`),
|
|
54
53
|
constructor: null,
|
|
55
54
|
ssrLoadModule: null,
|
|
56
55
|
async init() {
|
|
57
56
|
this.detected = true;
|
|
58
57
|
const moduleId = `@moostjs/event-${adapter}`;
|
|
59
58
|
const module = this.ssrLoadModule ? await this.ssrLoadModule(moduleId) : await import(moduleId);
|
|
60
|
-
const constructorName
|
|
61
|
-
getLogger().log(`🔍 [2mExtracting Adapter "${constructorName
|
|
62
|
-
this.constructor = module[constructorName
|
|
59
|
+
const constructorName = `Moost${adapter.charAt(0).toUpperCase() + adapter.slice(1)}`;
|
|
60
|
+
getLogger().log(`🔍 [2mExtracting Adapter "${constructorName}"`);
|
|
61
|
+
this.constructor = module[constructorName];
|
|
63
62
|
if (onInit && this.constructor) onInit(this.constructor, module);
|
|
64
63
|
},
|
|
65
64
|
compare(c) {
|
|
@@ -95,7 +94,7 @@ function _define_property(obj, key, value) {
|
|
|
95
94
|
* 2. Compare them against previously recorded handlers to detect
|
|
96
95
|
* which are newly added vs. removed.
|
|
97
96
|
* 3. Store the new state as the old map for the next cycle.
|
|
98
|
-
*/ let LogsStorage = class LogsStorage
|
|
97
|
+
*/ let LogsStorage = class LogsStorage {
|
|
99
98
|
/**
|
|
100
99
|
* Called at the start of an init cycle to
|
|
101
100
|
* clear out the list of new items.
|
|
@@ -191,7 +190,7 @@ function _define_property(obj, key, value) {
|
|
|
191
190
|
*
|
|
192
191
|
* @param {Set<string>} [cleanupInstances] A set of module IDs to remove from the registry.
|
|
193
192
|
*/ function moostRestartCleanup(adapters, onEject, cleanupInstances) {
|
|
194
|
-
const logger
|
|
193
|
+
const logger = getLogger();
|
|
195
194
|
const infact = getMoostInfact();
|
|
196
195
|
const { registry, scopes } = infact;
|
|
197
196
|
const registries = [registry, ...Object.values(scopes)];
|
|
@@ -203,7 +202,7 @@ function _define_property(obj, key, value) {
|
|
|
203
202
|
const instance = reg[key];
|
|
204
203
|
const viteId = mate.read(instance)?.__vite_id;
|
|
205
204
|
if (viteId && cleanupInstances.has(viteId)) {
|
|
206
|
-
logger
|
|
205
|
+
logger.debug(`🔃 Replacing "${constructorName(instance)}"`);
|
|
207
206
|
delete reg[key];
|
|
208
207
|
}
|
|
209
208
|
}
|
|
@@ -212,12 +211,12 @@ function _define_property(obj, key, value) {
|
|
|
212
211
|
scanParams(instance, (type) => {
|
|
213
212
|
if ((type === Moost || type instanceof Moost || type.prototype instanceof Moost) && (!onEject || onEject(instance, type))) {
|
|
214
213
|
delete reg[key];
|
|
215
|
-
logger
|
|
214
|
+
logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "Moost")`);
|
|
216
215
|
return true;
|
|
217
216
|
}
|
|
218
217
|
for (const adapter of adapters) if (adapter.compare(type) && (!onEject || onEject(instance, type))) {
|
|
219
218
|
delete reg[key];
|
|
220
|
-
logger
|
|
219
|
+
logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "${adapter.constructor.name}")`);
|
|
221
220
|
return true;
|
|
222
221
|
}
|
|
223
222
|
});
|
|
@@ -231,7 +230,7 @@ function _define_property(obj, key, value) {
|
|
|
231
230
|
clearGlobalWooks();
|
|
232
231
|
}
|
|
233
232
|
function clearDependantRegistry(registry, onEject) {
|
|
234
|
-
const logger
|
|
233
|
+
const logger = getLogger();
|
|
235
234
|
const objSet = /* @__PURE__ */ new Set();
|
|
236
235
|
let somethingIsDeleted = true;
|
|
237
236
|
while (somethingIsDeleted) {
|
|
@@ -242,17 +241,16 @@ function clearDependantRegistry(registry, onEject) {
|
|
|
242
241
|
}
|
|
243
242
|
for (const key of Object.getOwnPropertySymbols(registry)) {
|
|
244
243
|
const instance = registry[key];
|
|
245
|
-
|
|
246
|
-
if (ejected) somethingIsDeleted = true;
|
|
244
|
+
if (checkAndEject(instance, objSet, onEject, registry, key, logger)) somethingIsDeleted = true;
|
|
247
245
|
}
|
|
248
246
|
}
|
|
249
247
|
}
|
|
250
|
-
function checkAndEject(instance, objSet, onEject, registry, key, logger
|
|
248
|
+
function checkAndEject(instance, objSet, onEject, registry, key, logger) {
|
|
251
249
|
let ejected = false;
|
|
252
250
|
scanParams(instance, (type) => {
|
|
253
251
|
if (!objSet.has(type) && (!onEject || onEject(instance, type))) {
|
|
254
252
|
delete registry[key];
|
|
255
|
-
logger
|
|
253
|
+
logger.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on "${type.name}" which is not in registry)`);
|
|
256
254
|
ejected = true;
|
|
257
255
|
return true;
|
|
258
256
|
}
|
|
@@ -260,8 +258,7 @@ function checkAndEject(instance, objSet, onEject, registry, key, logger$1) {
|
|
|
260
258
|
return ejected;
|
|
261
259
|
}
|
|
262
260
|
function scanParams(instance, cb) {
|
|
263
|
-
const
|
|
264
|
-
const params = mate.read(instance)?.params;
|
|
261
|
+
const params = getMoostMate().read(instance)?.params;
|
|
265
262
|
if (params?.length) for (const param of params) {
|
|
266
263
|
if (param.type === void 0 || [
|
|
267
264
|
Array,
|
|
@@ -311,8 +308,6 @@ function generatedServerEntry(root) {
|
|
|
311
308
|
return entryPath;
|
|
312
309
|
}
|
|
313
310
|
function moostVite(options) {
|
|
314
|
-
const isTest = false;
|
|
315
|
-
const isProd = false;
|
|
316
311
|
const externals = options.externals ?? true;
|
|
317
312
|
let prefix = options.prefix;
|
|
318
313
|
if (prefix) {
|
|
@@ -324,10 +319,10 @@ function moostVite(options) {
|
|
|
324
319
|
let moostMiddleware = null;
|
|
325
320
|
let localFetchTeardown = null;
|
|
326
321
|
/** In middleware mode: maps req → next() for the onNoMatch callback */ const pendingNextMap = /* @__PURE__ */ new WeakMap();
|
|
327
|
-
const adapters =
|
|
322
|
+
const adapters = [
|
|
328
323
|
createAdapterDetector("http", (MoostHttp, moduleExports) => {
|
|
329
324
|
MoostHttp.prototype.listen = function(...args) {
|
|
330
|
-
logger
|
|
325
|
+
logger.log(`🔌 [2mOvertaking HTTP.listen`);
|
|
331
326
|
if (options.middleware) moostMiddleware = this.getServerCb((req) => {
|
|
332
327
|
pendingNextMap.get(req)?.();
|
|
333
328
|
});
|
|
@@ -335,7 +330,7 @@ function moostVite(options) {
|
|
|
335
330
|
if (options.ssrFetch !== false && moduleExports?.enableLocalFetch) {
|
|
336
331
|
if (localFetchTeardown) localFetchTeardown();
|
|
337
332
|
localFetchTeardown = moduleExports.enableLocalFetch(this);
|
|
338
|
-
logger
|
|
333
|
+
logger.log(`🔀 [2mLocal fetch enabled for SSR`);
|
|
339
334
|
}
|
|
340
335
|
setTimeout(() => {
|
|
341
336
|
args.filter((a) => typeof a === "function").forEach((a) => a());
|
|
@@ -346,25 +341,20 @@ function moostVite(options) {
|
|
|
346
341
|
createAdapterDetector("cli"),
|
|
347
342
|
createAdapterDetector("wf")
|
|
348
343
|
];
|
|
349
|
-
/** A logger instance for plugin debug output. */ const logger
|
|
344
|
+
/** A logger instance for plugin debug output. */ const logger = getLogger();
|
|
350
345
|
let reloadRequired = false;
|
|
351
346
|
patchMoostHandlerLogging();
|
|
352
|
-
|
|
347
|
+
return [{
|
|
353
348
|
name: PLUGIN_NAME,
|
|
354
349
|
enforce: "pre",
|
|
355
|
-
config(cfg) {
|
|
350
|
+
config(cfg, env) {
|
|
356
351
|
if (options.middleware) {
|
|
357
|
-
const defaultExternals = getExternals({
|
|
358
|
-
node: true,
|
|
359
|
-
workspace: true
|
|
360
|
-
});
|
|
361
352
|
const serverDefines = {
|
|
362
353
|
"process.env.MOOST_DEFERRED_ENV": "\"production\"",
|
|
363
354
|
__MOOST_ENTRY__: JSON.stringify(options.entry),
|
|
364
355
|
__MOOST_PREFIX__: JSON.stringify(prefix || "/api")
|
|
365
356
|
};
|
|
366
|
-
const
|
|
367
|
-
const ssrInput = { server: serverEntry };
|
|
357
|
+
const ssrInput = { server: options.serverEntry || generatedServerEntry(cfg.root) };
|
|
368
358
|
if (options.ssrEntry) {
|
|
369
359
|
const ssrBasename = entryBasename(options.ssrEntry);
|
|
370
360
|
ssrInput[`ssr/${ssrBasename.replace(/\.js$/, "")}`] = options.ssrEntry;
|
|
@@ -373,8 +363,17 @@ function moostVite(options) {
|
|
|
373
363
|
serverDefines.__MOOST_SSR_STATE__ = JSON.stringify(options.ssrState || DEFAULT_SSR_STATE);
|
|
374
364
|
}
|
|
375
365
|
const outDir = cfg.build?.outDir || "dist";
|
|
366
|
+
const isBuild = env.command === "build";
|
|
367
|
+
const userNoExternal = cfg.ssr?.noExternal;
|
|
368
|
+
const ssrNoExternal = isBuild ? true : Array.from(new Set([...Array.isArray(userNoExternal) ? userNoExternal : userNoExternal && typeof userNoExternal !== "boolean" ? [userNoExternal] : [], /^@moostjs\/vite($|\/)/]));
|
|
369
|
+
const userExternal = cfg.ssr?.external;
|
|
370
|
+
const ssrExternal = isBuild ? [...Array.isArray(userExternal) ? userExternal : [], ...options.ssrExternal ?? []] : void 0;
|
|
376
371
|
return {
|
|
377
372
|
...options.ssrEntry && { appType: "custom" },
|
|
373
|
+
ssr: {
|
|
374
|
+
noExternal: ssrNoExternal,
|
|
375
|
+
...ssrExternal && ssrExternal.length > 0 ? { external: ssrExternal } : {}
|
|
376
|
+
},
|
|
378
377
|
builder: { async buildApp(builder) {
|
|
379
378
|
rmSync(resolve(cfg.root || process.cwd(), outDir), {
|
|
380
379
|
recursive: true,
|
|
@@ -396,10 +395,6 @@ function moostVite(options) {
|
|
|
396
395
|
sourcemap: !!(options.sourcemap ?? true),
|
|
397
396
|
rollupOptions: {
|
|
398
397
|
input: ssrInput,
|
|
399
|
-
external: (id) => {
|
|
400
|
-
if (id === "@moostjs/vite/server") return false;
|
|
401
|
-
return defaultExternals.some((ext) => ext.test(id));
|
|
402
|
-
},
|
|
403
398
|
output: { format: "esm" }
|
|
404
399
|
}
|
|
405
400
|
},
|
|
@@ -423,7 +418,7 @@ function moostVite(options) {
|
|
|
423
418
|
minify: cfg.build?.minify || false,
|
|
424
419
|
sourcemap: !!(options.sourcemap ?? true),
|
|
425
420
|
rollupOptions: {
|
|
426
|
-
external:
|
|
421
|
+
external: cfg.build?.rollupOptions?.external || (externals === false ? [] : getExternals({
|
|
427
422
|
node: Boolean(externals === true || externals?.node),
|
|
428
423
|
workspace: Boolean(externals === true || externals?.workspace)
|
|
429
424
|
})),
|
|
@@ -487,10 +482,10 @@ function moostVite(options) {
|
|
|
487
482
|
if (reloadRequired) {
|
|
488
483
|
reloadRequired = false;
|
|
489
484
|
console.log();
|
|
490
|
-
logger
|
|
485
|
+
logger.debug("🚀 Reloading Moost App...");
|
|
491
486
|
console.log();
|
|
492
487
|
await ssrImport(options.entry);
|
|
493
|
-
await new Promise((resolve
|
|
488
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
494
489
|
}
|
|
495
490
|
if (options.middleware && prefix) {
|
|
496
491
|
const url = req.url || "";
|
|
@@ -511,7 +506,7 @@ function moostVite(options) {
|
|
|
511
506
|
if (file.endsWith(".ts")) {
|
|
512
507
|
const modules = this.environment.moduleGraph.getModulesByFile(file);
|
|
513
508
|
if (modules) {
|
|
514
|
-
logger
|
|
509
|
+
logger.debug(`🔃 Hot update: ${file}`);
|
|
515
510
|
const cleanupInstances = /* @__PURE__ */ new Set();
|
|
516
511
|
for (const mod of modules) {
|
|
517
512
|
const allImporters = gatherAllImporters(mod);
|
|
@@ -529,15 +524,7 @@ function moostVite(options) {
|
|
|
529
524
|
return [];
|
|
530
525
|
}
|
|
531
526
|
}
|
|
532
|
-
}
|
|
533
|
-
if (isProd || isTest) {
|
|
534
|
-
delete pluginConfig.configureServer;
|
|
535
|
-
delete pluginConfig.resolveId;
|
|
536
|
-
delete pluginConfig.load;
|
|
537
|
-
delete pluginConfig.transform;
|
|
538
|
-
delete pluginConfig.hotUpdate;
|
|
539
|
-
}
|
|
540
|
-
const ssrFallbackPlugin = !isProd && !isTest && options.middleware && options.ssrEntry ? {
|
|
527
|
+
}, options.middleware && options.ssrEntry ? {
|
|
541
528
|
name: `${PLUGIN_NAME}:ssr-fallback`,
|
|
542
529
|
async configureServer(server) {
|
|
543
530
|
const ssrOutlet = options.ssrOutlet || DEFAULT_SSR_OUTLET;
|
|
@@ -554,7 +541,7 @@ function moostVite(options) {
|
|
|
554
541
|
const { html: appHtml, state } = await render(url);
|
|
555
542
|
res.statusCode = 200;
|
|
556
543
|
res.setHeader("Content-Type", "text/html");
|
|
557
|
-
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}
|
|
544
|
+
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}<\/script>` : ""));
|
|
558
545
|
} catch (error) {
|
|
559
546
|
server.ssrFixStacktrace(error);
|
|
560
547
|
console.error(error);
|
|
@@ -564,8 +551,7 @@ function moostVite(options) {
|
|
|
564
551
|
});
|
|
565
552
|
};
|
|
566
553
|
}
|
|
567
|
-
} : null;
|
|
568
|
-
return [pluginConfig, ssrFallbackPlugin];
|
|
554
|
+
} : null];
|
|
569
555
|
}
|
|
570
556
|
|
|
571
557
|
//#endregion
|
package/dist/prod-server.cjs
CHANGED
|
@@ -1,29 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
9
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
-
key = keys[i];
|
|
11
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
-
get: ((k) => from[k]).bind(null, key),
|
|
13
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
-
value: mod,
|
|
20
|
-
enumerable: true
|
|
21
|
-
}) : target, mod));
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
require("node:fs");
|
|
25
|
-
require("node:module");
|
|
26
|
-
const moost = __toESM(require("moost"));
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let moost = require("moost");
|
|
27
3
|
|
|
28
4
|
//#region packages/vite/src/utils.ts
|
|
29
5
|
const PLUGIN_NAME = "moost-vite";
|
|
@@ -39,33 +15,31 @@ function stripUndefined(obj) {
|
|
|
39
15
|
async function createSSRServer(options) {
|
|
40
16
|
const userMiddlewares = [];
|
|
41
17
|
const opts = options ? stripUndefined(options) : {};
|
|
42
|
-
|
|
43
|
-
if (!isProd) {
|
|
18
|
+
if (!(process.env.MOOST_DEFERRED_ENV === "production")) {
|
|
44
19
|
const { createServer: createViteServer } = await import("vite");
|
|
45
20
|
const vite = await createViteServer();
|
|
46
|
-
const pluginOpts = vite.config.__moostViteOptions || {};
|
|
47
21
|
const config = {
|
|
48
|
-
...
|
|
22
|
+
...vite.config.__moostViteOptions || {},
|
|
49
23
|
...opts
|
|
50
24
|
};
|
|
51
25
|
const ssrEntry = config.ssrEntry;
|
|
52
|
-
const ssrOutlet
|
|
53
|
-
const ssrState
|
|
26
|
+
const ssrOutlet = config.ssrOutlet || DEFAULT_SSR_OUTLET;
|
|
27
|
+
const ssrState = config.ssrState || DEFAULT_SSR_STATE;
|
|
54
28
|
let ssrFallback = null;
|
|
55
29
|
if (ssrEntry) {
|
|
56
|
-
const fs
|
|
57
|
-
const path
|
|
30
|
+
const fs = await import("node:fs/promises");
|
|
31
|
+
const path = await import("node:path");
|
|
58
32
|
ssrFallback = async (req, res, next) => {
|
|
59
33
|
if (req.method !== "GET") return next();
|
|
60
34
|
const url = req.originalUrl || req.url || "/";
|
|
61
35
|
try {
|
|
62
|
-
let template
|
|
63
|
-
template
|
|
64
|
-
const { render
|
|
65
|
-
const { html: appHtml, state } = await render
|
|
36
|
+
let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf8");
|
|
37
|
+
template = await vite.transformIndexHtml(url, template);
|
|
38
|
+
const { render } = await vite.ssrLoadModule(ssrEntry);
|
|
39
|
+
const { html: appHtml, state } = await render(url);
|
|
66
40
|
res.statusCode = 200;
|
|
67
41
|
res.setHeader("Content-Type", "text/html");
|
|
68
|
-
res.end(template
|
|
42
|
+
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}<\/script>` : ""));
|
|
69
43
|
} catch (error) {
|
|
70
44
|
vite.ssrFixStacktrace(error);
|
|
71
45
|
console.error(error);
|
|
@@ -99,32 +73,36 @@ async function createSSRServer(options) {
|
|
|
99
73
|
const defaultPort = opts.port || Number(process.env.PORT) || 3e3;
|
|
100
74
|
const template = await fs.readFile(path.resolve(clientDir, "index.html"), "utf8");
|
|
101
75
|
let render = null;
|
|
102
|
-
|
|
103
|
-
if (hasSsr) {
|
|
104
|
-
const ssrModule = await import(
|
|
76
|
+
if (__MOOST_SSR_ENTRY__ !== void 0 && !!__MOOST_SSR_ENTRY__) render = (await import(
|
|
105
77
|
/* @vite-ignore */
|
|
106
|
-
__MOOST_SSR_ENTRY__
|
|
107
|
-
|
|
108
|
-
}
|
|
78
|
+
__MOOST_SSR_ENTRY__
|
|
79
|
+
)).render;
|
|
109
80
|
const { MoostHttp, enableLocalFetch } = await import("@moostjs/event-http");
|
|
110
81
|
let moostHandler = null;
|
|
111
82
|
const moostHttpRef = { instance: null };
|
|
83
|
+
let captured = false;
|
|
112
84
|
const origListen = MoostHttp.prototype.listen;
|
|
113
85
|
MoostHttp.prototype.listen = function(...args) {
|
|
114
86
|
moostHandler = this.getServerCb();
|
|
115
87
|
moostHttpRef.instance = this;
|
|
88
|
+
captured = true;
|
|
116
89
|
setTimeout(() => args.filter((a) => typeof a === "function").forEach((a) => a()), 1);
|
|
117
90
|
return Promise.resolve();
|
|
118
91
|
};
|
|
119
92
|
if (typeof opts.entry === "function") await opts.entry();
|
|
120
93
|
else if (typeof opts.entry === "string") await import(
|
|
121
|
-
|
|
122
|
-
|
|
94
|
+
/* @vite-ignore */
|
|
95
|
+
opts.entry
|
|
96
|
+
);
|
|
123
97
|
else await import(__MOOST_ENTRY__);
|
|
98
|
+
if (!captured) {
|
|
99
|
+
const deadline = Date.now() + 3e4;
|
|
100
|
+
while (!captured && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 10));
|
|
101
|
+
if (!captured) console.warn("[moost-vite] MoostHttp.listen was not called within 30s after entry import. The Moost HTTP handler will not be active. Ensure your entry uses top-level await for async setup.");
|
|
102
|
+
}
|
|
124
103
|
MoostHttp.prototype.listen = origListen;
|
|
125
104
|
if (enableLocalFetch && moostHttpRef.instance) enableLocalFetch(moostHttpRef.instance);
|
|
126
|
-
const
|
|
127
|
-
const sirv = sirvModule.default;
|
|
105
|
+
const sirv = (await import("sirv")).default;
|
|
128
106
|
const serve = sirv(path.resolve(clientDir), { extensions: [] });
|
|
129
107
|
return {
|
|
130
108
|
use(mw) {
|
|
@@ -155,7 +133,7 @@ async function createSSRServer(options) {
|
|
|
155
133
|
const { html: appHtml, state } = await render(url);
|
|
156
134
|
res.statusCode = 200;
|
|
157
135
|
res.setHeader("Content-Type", "text/html");
|
|
158
|
-
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}
|
|
136
|
+
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}<\/script>` : ""));
|
|
159
137
|
} else {
|
|
160
138
|
res.statusCode = 200;
|
|
161
139
|
res.setHeader("Content-Type", "text/html");
|
package/dist/prod-server.mjs
CHANGED
|
@@ -16,33 +16,31 @@ function stripUndefined(obj) {
|
|
|
16
16
|
async function createSSRServer(options) {
|
|
17
17
|
const userMiddlewares = [];
|
|
18
18
|
const opts = options ? stripUndefined(options) : {};
|
|
19
|
-
|
|
20
|
-
if (!isProd) {
|
|
19
|
+
if (!(process.env.MOOST_DEFERRED_ENV === "production")) {
|
|
21
20
|
const { createServer: createViteServer } = await import("vite");
|
|
22
21
|
const vite = await createViteServer();
|
|
23
|
-
const pluginOpts = vite.config.__moostViteOptions || {};
|
|
24
22
|
const config = {
|
|
25
|
-
...
|
|
23
|
+
...vite.config.__moostViteOptions || {},
|
|
26
24
|
...opts
|
|
27
25
|
};
|
|
28
26
|
const ssrEntry = config.ssrEntry;
|
|
29
|
-
const ssrOutlet
|
|
30
|
-
const ssrState
|
|
27
|
+
const ssrOutlet = config.ssrOutlet || DEFAULT_SSR_OUTLET;
|
|
28
|
+
const ssrState = config.ssrState || DEFAULT_SSR_STATE;
|
|
31
29
|
let ssrFallback = null;
|
|
32
30
|
if (ssrEntry) {
|
|
33
|
-
const fs
|
|
34
|
-
const path
|
|
31
|
+
const fs = await import("node:fs/promises");
|
|
32
|
+
const path = await import("node:path");
|
|
35
33
|
ssrFallback = async (req, res, next) => {
|
|
36
34
|
if (req.method !== "GET") return next();
|
|
37
35
|
const url = req.originalUrl || req.url || "/";
|
|
38
36
|
try {
|
|
39
|
-
let template
|
|
40
|
-
template
|
|
41
|
-
const { render
|
|
42
|
-
const { html: appHtml, state } = await render
|
|
37
|
+
let template = await fs.readFile(path.resolve(process.cwd(), "index.html"), "utf8");
|
|
38
|
+
template = await vite.transformIndexHtml(url, template);
|
|
39
|
+
const { render } = await vite.ssrLoadModule(ssrEntry);
|
|
40
|
+
const { html: appHtml, state } = await render(url);
|
|
43
41
|
res.statusCode = 200;
|
|
44
42
|
res.setHeader("Content-Type", "text/html");
|
|
45
|
-
res.end(template
|
|
43
|
+
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}<\/script>` : ""));
|
|
46
44
|
} catch (error) {
|
|
47
45
|
vite.ssrFixStacktrace(error);
|
|
48
46
|
console.error(error);
|
|
@@ -76,32 +74,36 @@ async function createSSRServer(options) {
|
|
|
76
74
|
const defaultPort = opts.port || Number(process.env.PORT) || 3e3;
|
|
77
75
|
const template = await fs.readFile(path.resolve(clientDir, "index.html"), "utf8");
|
|
78
76
|
let render = null;
|
|
79
|
-
|
|
80
|
-
if (hasSsr) {
|
|
81
|
-
const ssrModule = await import(
|
|
77
|
+
if (__MOOST_SSR_ENTRY__ !== void 0 && !!__MOOST_SSR_ENTRY__) render = (await import(
|
|
82
78
|
/* @vite-ignore */
|
|
83
|
-
__MOOST_SSR_ENTRY__
|
|
84
|
-
|
|
85
|
-
}
|
|
79
|
+
__MOOST_SSR_ENTRY__
|
|
80
|
+
)).render;
|
|
86
81
|
const { MoostHttp, enableLocalFetch } = await import("@moostjs/event-http");
|
|
87
82
|
let moostHandler = null;
|
|
88
83
|
const moostHttpRef = { instance: null };
|
|
84
|
+
let captured = false;
|
|
89
85
|
const origListen = MoostHttp.prototype.listen;
|
|
90
86
|
MoostHttp.prototype.listen = function(...args) {
|
|
91
87
|
moostHandler = this.getServerCb();
|
|
92
88
|
moostHttpRef.instance = this;
|
|
89
|
+
captured = true;
|
|
93
90
|
setTimeout(() => args.filter((a) => typeof a === "function").forEach((a) => a()), 1);
|
|
94
91
|
return Promise.resolve();
|
|
95
92
|
};
|
|
96
93
|
if (typeof opts.entry === "function") await opts.entry();
|
|
97
94
|
else if (typeof opts.entry === "string") await import(
|
|
98
|
-
|
|
99
|
-
|
|
95
|
+
/* @vite-ignore */
|
|
96
|
+
opts.entry
|
|
97
|
+
);
|
|
100
98
|
else await import(__MOOST_ENTRY__);
|
|
99
|
+
if (!captured) {
|
|
100
|
+
const deadline = Date.now() + 3e4;
|
|
101
|
+
while (!captured && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 10));
|
|
102
|
+
if (!captured) console.warn("[moost-vite] MoostHttp.listen was not called within 30s after entry import. The Moost HTTP handler will not be active. Ensure your entry uses top-level await for async setup.");
|
|
103
|
+
}
|
|
101
104
|
MoostHttp.prototype.listen = origListen;
|
|
102
105
|
if (enableLocalFetch && moostHttpRef.instance) enableLocalFetch(moostHttpRef.instance);
|
|
103
|
-
const
|
|
104
|
-
const sirv = sirvModule.default;
|
|
106
|
+
const sirv = (await import("sirv")).default;
|
|
105
107
|
const serve = sirv(path.resolve(clientDir), { extensions: [] });
|
|
106
108
|
return {
|
|
107
109
|
use(mw) {
|
|
@@ -132,7 +134,7 @@ async function createSSRServer(options) {
|
|
|
132
134
|
const { html: appHtml, state } = await render(url);
|
|
133
135
|
res.statusCode = 200;
|
|
134
136
|
res.setHeader("Content-Type", "text/html");
|
|
135
|
-
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}
|
|
137
|
+
res.end(template.replace(ssrOutlet, appHtml).replace(ssrState, state ? `<script>window.__SSR_STATE__=${state}<\/script>` : ""));
|
|
136
138
|
} else {
|
|
137
139
|
res.statusCode = 200;
|
|
138
140
|
res.setHeader("Content-Type", "text/html");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/vite",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.12",
|
|
4
4
|
"description": "Vite Dev plugin for moostjs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"composables",
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"sirv": "^3.0.0",
|
|
60
60
|
"vite": "^8.0.0",
|
|
61
|
-
"@moostjs/event-http": "^0.6.
|
|
62
|
-
"moost": "^0.6.
|
|
61
|
+
"@moostjs/event-http": "^0.6.12",
|
|
62
|
+
"moost": "^0.6.12"
|
|
63
63
|
},
|
|
64
64
|
"peerDependenciesMeta": {
|
|
65
65
|
"sirv": {
|