@moostjs/vite 0.5.21 → 0.5.23
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/LICENSE +21 -0
- package/dist/index.cjs +353 -316
- package/dist/index.d.ts +0 -72
- package/dist/index.mjs +330 -315
- package/package.json +14 -7
package/dist/index.cjs
CHANGED
|
@@ -1,352 +1,389 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const moost = __toESM(require("moost"));
|
|
26
|
+
const node_fs = __toESM(require("node:fs"));
|
|
27
|
+
const node_module = __toESM(require("node:module"));
|
|
7
28
|
|
|
29
|
+
//#region packages/vite/src/adapter-detector.ts
|
|
8
30
|
function createAdapterDetector(adapter, onInit) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
c instanceof this.constructor ||
|
|
25
|
-
c.prototype instanceof this.constructor);
|
|
26
|
-
}
|
|
27
|
-
return false;
|
|
28
|
-
},
|
|
29
|
-
};
|
|
31
|
+
return {
|
|
32
|
+
detected: false,
|
|
33
|
+
regex: new RegExp(`from\\s+["'](@moostjs\\/event-${adapter})["']`),
|
|
34
|
+
constructor: null,
|
|
35
|
+
async init() {
|
|
36
|
+
this.detected = true;
|
|
37
|
+
const module$1 = await import(`@moostjs/event-${adapter}`);
|
|
38
|
+
this.constructor = module$1[`Moost${adapter.charAt(0).toUpperCase() + adapter.slice(1)}`];
|
|
39
|
+
if (onInit) onInit(this.constructor);
|
|
40
|
+
},
|
|
41
|
+
compare(c) {
|
|
42
|
+
if (this.detected && this.constructor) return this.constructor === c || c instanceof this.constructor || c.prototype instanceof this.constructor;
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
30
46
|
}
|
|
31
47
|
|
|
32
|
-
|
|
33
|
-
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region packages/vite/src/moost-logging.ts
|
|
50
|
+
function _define_property(obj, key, value) {
|
|
51
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
52
|
+
value,
|
|
53
|
+
enumerable: true,
|
|
54
|
+
configurable: true,
|
|
55
|
+
writable: true
|
|
56
|
+
});
|
|
57
|
+
else obj[key] = value;
|
|
58
|
+
return obj;
|
|
34
59
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
startRecording() {
|
|
42
|
-
this.newItems = [];
|
|
43
|
-
}
|
|
44
|
-
record(item) {
|
|
45
|
-
const flat = flattenItem(item);
|
|
46
|
-
this.newItems.push(item);
|
|
47
|
-
return !this.oldMap.has(flat);
|
|
48
|
-
}
|
|
49
|
-
endRecording(logRemovedItem) {
|
|
50
|
-
const newMap = new Map();
|
|
51
|
-
for (const item of this.newItems) {
|
|
52
|
-
const flat = flattenItem(item);
|
|
53
|
-
newMap.set(flat, item);
|
|
54
|
-
}
|
|
55
|
-
for (const [flat, item] of this.oldMap.entries()) {
|
|
56
|
-
if (!newMap.has(flat)) {
|
|
57
|
-
logRemovedItem(item);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
this.oldMap = newMap;
|
|
61
|
-
this.newItems = [];
|
|
62
|
-
this.isFirstRun = false;
|
|
63
|
-
}
|
|
60
|
+
/**
|
|
61
|
+
* Converts a TItem into a flattened string to uniquely identify it.
|
|
62
|
+
* @param {TItem} item - The handler registration item to flatten.
|
|
63
|
+
* @returns {string} A unique string key for the item.
|
|
64
|
+
*/ function flattenItem(item) {
|
|
65
|
+
return `${item.eventName}||${item.classConstructor.name}||${item.method}`;
|
|
64
66
|
}
|
|
65
|
-
|
|
67
|
+
/**
|
|
68
|
+
* A storage mechanism to track old and new handler registrations
|
|
69
|
+
* across multiple initialization cycles. During each init:
|
|
70
|
+
* 1. Start recording newly encountered handlers.
|
|
71
|
+
* 2. Compare them against previously recorded handlers to detect
|
|
72
|
+
* which are newly added vs. removed.
|
|
73
|
+
* 3. Store the new state as the old map for the next cycle.
|
|
74
|
+
*/ let LogsStorage = class LogsStorage$1 {
|
|
75
|
+
/**
|
|
76
|
+
* Called at the start of an init cycle to
|
|
77
|
+
* clear out the list of new items.
|
|
78
|
+
*/ startRecording() {
|
|
79
|
+
this.newItems = [];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Records a newly encountered handler (via logMappedHandler).
|
|
83
|
+
*
|
|
84
|
+
* @param {TItem} item - The handler registration item to record.
|
|
85
|
+
* @returns {boolean} True if this item is newly added (not in oldMap),
|
|
86
|
+
* false if it existed already.
|
|
87
|
+
*/ record(item) {
|
|
88
|
+
const flat = flattenItem(item);
|
|
89
|
+
this.newItems.push(item);
|
|
90
|
+
return !this.oldMap.has(flat);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Called at the end of an init cycle.
|
|
94
|
+
* 1. Builds a newMap of all newly encountered handlers.
|
|
95
|
+
* 2. Logs/strokes out any handlers that have disappeared compared
|
|
96
|
+
* to the previous cycle.
|
|
97
|
+
* 3. Updates oldMap so it becomes the baseline for the next cycle.
|
|
98
|
+
*
|
|
99
|
+
* @param {(item: TItem) => void} logRemovedItem - A callback that logs
|
|
100
|
+
* removed items (presumably by striking them out).
|
|
101
|
+
*/ endRecording(logRemovedItem) {
|
|
102
|
+
const newMap = new Map();
|
|
103
|
+
for (const item of this.newItems) {
|
|
104
|
+
const flat = flattenItem(item);
|
|
105
|
+
newMap.set(flat, item);
|
|
106
|
+
}
|
|
107
|
+
for (const [flat, item] of this.oldMap.entries()) if (!newMap.has(flat)) logRemovedItem(item);
|
|
108
|
+
this.oldMap = newMap;
|
|
109
|
+
this.newItems = [];
|
|
110
|
+
this.isFirstRun = false;
|
|
111
|
+
}
|
|
112
|
+
constructor() {
|
|
113
|
+
/**
|
|
114
|
+
* A map of flattenItem() => TItem
|
|
115
|
+
* representing the previous cycle's known handlers.
|
|
116
|
+
*/ _define_property(this, "oldMap", new Map());
|
|
117
|
+
/**
|
|
118
|
+
* The handlers discovered in the current cycle,
|
|
119
|
+
* awaiting finalization.
|
|
120
|
+
*/ _define_property(this, "newItems", []);
|
|
121
|
+
_define_property(this, "isFirstRun", true);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* A reference to a shared LogsStorage instance that persists
|
|
126
|
+
* across init cycles in dev mode.
|
|
127
|
+
*/ let logsStorage;
|
|
66
128
|
function patchMoostHandlerLogging() {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
129
|
+
const origInit = moost.Moost.prototype.init;
|
|
130
|
+
/**
|
|
131
|
+
* Monkey-patch Moost.init:
|
|
132
|
+
* 1. Start a new recording cycle in LogsStorage.
|
|
133
|
+
* 2. Call the real .init().
|
|
134
|
+
* 3. End the recording, logging any removed handlers.
|
|
135
|
+
*/ moost.Moost.prototype.init = async function init() {
|
|
136
|
+
if (!logsStorage) logsStorage = new LogsStorage();
|
|
137
|
+
logsStorage.startRecording();
|
|
138
|
+
await origInit.call(this);
|
|
139
|
+
logsStorage.endRecording((item) => {
|
|
140
|
+
origLogMappedHandler.call(this, item.eventName, item.classConstructor, item.method, true, "❌ ");
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
const origLogMappedHandler = moost.Moost.prototype.logMappedHandler;
|
|
144
|
+
/**
|
|
145
|
+
* Monkey-patch Moost.logMappedHandler:
|
|
146
|
+
* If we haven’t seen this particular route+class+method combination
|
|
147
|
+
* in a previous cycle, call the original logger. Otherwise, skip it
|
|
148
|
+
* to avoid duplicate logs.
|
|
149
|
+
*/ moost.Moost.prototype.logMappedHandler = function logMappedHandler(eventName, classConstructor, method) {
|
|
150
|
+
if (logsStorage.record({
|
|
151
|
+
eventName,
|
|
152
|
+
classConstructor,
|
|
153
|
+
method
|
|
154
|
+
})) origLogMappedHandler.call(this, eventName, classConstructor, method, false, logsStorage.isFirstRun ? "" : "✅ ");
|
|
155
|
+
};
|
|
84
156
|
}
|
|
85
157
|
|
|
86
|
-
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region packages/vite/src/utils.ts
|
|
160
|
+
const PLUGIN_NAME = "moost-vite";
|
|
87
161
|
function gatherAllImporters(moduleNode, visited = new Set()) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
visited.add(moduleNode);
|
|
95
|
-
if (moduleNode.importers) {
|
|
96
|
-
for (const importer of moduleNode.importers) {
|
|
97
|
-
gatherAllImporters(importer, visited);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return visited;
|
|
162
|
+
if (!moduleNode) return visited;
|
|
163
|
+
if (visited.has(moduleNode)) return visited;
|
|
164
|
+
visited.add(moduleNode);
|
|
165
|
+
if (moduleNode.importers) for (const importer of moduleNode.importers) gatherAllImporters(importer, visited);
|
|
166
|
+
return visited;
|
|
101
167
|
}
|
|
102
|
-
const logger = new moost.EventLogger(
|
|
168
|
+
const logger = new moost.EventLogger("", { level: 99 }).createTopic("\x1B[2m\x1B[36m" + PLUGIN_NAME);
|
|
103
169
|
function getLogger() {
|
|
104
|
-
|
|
170
|
+
return logger;
|
|
105
171
|
}
|
|
106
172
|
function getExternals({ node, workspace }) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.filter(([key, ver]) => !ver.startsWith('workspace:'))
|
|
112
|
-
.map(i => i[0]);
|
|
113
|
-
if (node) {
|
|
114
|
-
externals.push(...node_module.builtinModules, ...node_module.builtinModules.map(m => `node:${m}`));
|
|
115
|
-
}
|
|
116
|
-
return externals;
|
|
173
|
+
const pkg = JSON.parse((0, node_fs.readFileSync)("package.json", "utf8").toString());
|
|
174
|
+
const externals = workspace ? Object.keys(pkg.dependencies || {}) : Object.entries(pkg.dependencies || {}).filter(([key, ver]) => !ver.startsWith("workspace:")).map((i) => i[0]);
|
|
175
|
+
if (node) externals.push(...node_module.builtinModules, ...node_module.builtinModules.map((m) => `node:${m}`));
|
|
176
|
+
return externals;
|
|
117
177
|
}
|
|
118
178
|
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region packages/vite/src/restart-cleanup.ts
|
|
119
181
|
function moostRestartCleanup(adapters, onEject, cleanupInstances) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
moost.getMoostMate()._cleanup();
|
|
160
|
-
moost.clearGlobalWooks();
|
|
182
|
+
const logger$1 = getLogger();
|
|
183
|
+
const infact = (0, moost.getMoostInfact)();
|
|
184
|
+
const { registry, scopes } = infact;
|
|
185
|
+
const registries = [registry, ...Object.values(scopes)];
|
|
186
|
+
infact._cleanup();
|
|
187
|
+
const mate = (0, moost.getMoostMate)();
|
|
188
|
+
if (cleanupInstances) {
|
|
189
|
+
for (const reg of registries) {
|
|
190
|
+
for (const key of Object.getOwnPropertySymbols(reg)) {
|
|
191
|
+
const instance = reg[key];
|
|
192
|
+
const viteId = mate.read(instance)?.__vite_id;
|
|
193
|
+
if (viteId && cleanupInstances.has(viteId)) {
|
|
194
|
+
logger$1.debug(`🔃 Replacing "${constructorName(instance)}"`);
|
|
195
|
+
delete reg[key];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
for (const key of Object.getOwnPropertySymbols(reg)) {
|
|
199
|
+
const instance = reg[key];
|
|
200
|
+
scanParams(instance, (type) => {
|
|
201
|
+
if ((type === moost.Moost || type instanceof moost.Moost || type.prototype instanceof moost.Moost) && (!onEject || onEject(instance, type))) {
|
|
202
|
+
delete reg[key];
|
|
203
|
+
logger$1.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "Moost")`);
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
for (const adapter of adapters) if (adapter.compare(type) && (!onEject || onEject(instance, type))) {
|
|
207
|
+
delete reg[key];
|
|
208
|
+
logger$1.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on re-instantiated "${adapter.constructor.name}")`);
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
clearDependantRegistry(reg, onEject);
|
|
214
|
+
}
|
|
215
|
+
infact.registry = registry;
|
|
216
|
+
infact.scopes = scopes;
|
|
217
|
+
}
|
|
218
|
+
(0, moost.getMoostMate)()._cleanup();
|
|
219
|
+
(0, moost.clearGlobalWooks)();
|
|
161
220
|
}
|
|
162
221
|
function clearDependantRegistry(registry, onEject) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
222
|
+
const logger$1 = getLogger();
|
|
223
|
+
const objSet = new Set();
|
|
224
|
+
let somethingIsDeleted = true;
|
|
225
|
+
while (somethingIsDeleted) {
|
|
226
|
+
somethingIsDeleted = false;
|
|
227
|
+
for (const key of Object.getOwnPropertySymbols(registry)) {
|
|
228
|
+
const instance = registry[key];
|
|
229
|
+
objSet.add(Object.getPrototypeOf(instance).constructor);
|
|
230
|
+
}
|
|
231
|
+
for (const key of Object.getOwnPropertySymbols(registry)) {
|
|
232
|
+
const instance = registry[key];
|
|
233
|
+
scanParams(instance, (type) => {
|
|
234
|
+
if (!objSet.has(type) && (!onEject || onEject(instance, type))) {
|
|
235
|
+
delete registry[key];
|
|
236
|
+
logger$1.debug(`✖️ Ejecting "${constructorName(instance)}" (depends on "${type.name}" which is not in registry)`);
|
|
237
|
+
somethingIsDeleted = true;
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
184
243
|
}
|
|
185
244
|
function scanParams(instance, cb) {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
245
|
+
const mate = (0, moost.getMoostMate)();
|
|
246
|
+
const params = mate.read(instance)?.params;
|
|
247
|
+
if (params?.length) for (const param of params) {
|
|
248
|
+
if (param.type === undefined || [
|
|
249
|
+
Array,
|
|
250
|
+
String,
|
|
251
|
+
Number,
|
|
252
|
+
Boolean,
|
|
253
|
+
Object
|
|
254
|
+
].includes(param.type)) continue;
|
|
255
|
+
if (cb(param.type)) break;
|
|
256
|
+
}
|
|
199
257
|
}
|
|
200
258
|
function constructorName(i) {
|
|
201
|
-
|
|
259
|
+
return Object.getPrototypeOf(i).constructor.name;
|
|
202
260
|
}
|
|
203
261
|
|
|
204
|
-
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region packages/vite/src/moost-vite.ts
|
|
264
|
+
/** Regex checks */ const REG_HAS_EXPORT_CLASS = /(^\s*@(Injectable|Controller)\()/m;
|
|
205
265
|
const REG_REPLACE_EXPORT_CLASS = /(^\s*@(Injectable|Controller)\()/gm;
|
|
206
266
|
function moostVite(options) {
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
await adapter.init();
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
if (REG_HAS_EXPORT_CLASS.test(code)) {
|
|
280
|
-
code = code.replace(REG_REPLACE_EXPORT_CLASS, '\n@__VITE_ID(import.meta.filename)\n$1');
|
|
281
|
-
code = `import { __VITE_ID } from 'virtual:vite-id'\n\n${code}`;
|
|
282
|
-
}
|
|
283
|
-
return code;
|
|
284
|
-
},
|
|
285
|
-
resolveId(id) {
|
|
286
|
-
if (id === 'virtual:vite-id') {
|
|
287
|
-
return '\0virtual:vite-id';
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
load(id) {
|
|
291
|
-
if (id === '\0virtual:vite-id') {
|
|
292
|
-
return `
|
|
267
|
+
const isTest = process.env.NODE_ENV === "test";
|
|
268
|
+
const isProd = process.env.NODE_ENV === "production";
|
|
269
|
+
let moostMiddleware = null;
|
|
270
|
+
const adapters = isTest ? [] : [
|
|
271
|
+
createAdapterDetector("http", (MoostHttp) => {
|
|
272
|
+
MoostHttp.prototype.listen = function(...args) {
|
|
273
|
+
moostMiddleware = this.getServerCb();
|
|
274
|
+
setTimeout(() => {
|
|
275
|
+
args.filter((a) => typeof a === "function").forEach((a) => a());
|
|
276
|
+
}, 1);
|
|
277
|
+
return Promise.resolve();
|
|
278
|
+
};
|
|
279
|
+
}),
|
|
280
|
+
createAdapterDetector("cli"),
|
|
281
|
+
createAdapterDetector("wf")
|
|
282
|
+
];
|
|
283
|
+
/** A logger instance for plugin debug output. */ const logger$1 = isTest ? console : getLogger();
|
|
284
|
+
let reloadRequired = false;
|
|
285
|
+
patchMoostHandlerLogging();
|
|
286
|
+
const pluginConfig = {
|
|
287
|
+
name: PLUGIN_NAME,
|
|
288
|
+
enforce: "pre",
|
|
289
|
+
config(cfg) {
|
|
290
|
+
const entry = cfg.build?.rollupOptions?.input || options.entry;
|
|
291
|
+
const outfile = typeof entry === "string" ? entry.split("/").pop().replace(/\.ts$/, ".js") : undefined;
|
|
292
|
+
return {
|
|
293
|
+
server: {
|
|
294
|
+
port: cfg.server?.port || options.port || 3e3,
|
|
295
|
+
host: cfg.server?.host || options.host
|
|
296
|
+
},
|
|
297
|
+
optimizeDeps: {
|
|
298
|
+
noDiscovery: cfg.optimizeDeps?.noDiscovery === undefined ? true : cfg.optimizeDeps.noDiscovery,
|
|
299
|
+
exclude: cfg.optimizeDeps?.exclude || ["@swc/core"]
|
|
300
|
+
},
|
|
301
|
+
build: {
|
|
302
|
+
target: cfg.build?.target || "node",
|
|
303
|
+
outDir: cfg.build?.outDir || options.outDir || "dist",
|
|
304
|
+
ssr: cfg.build?.ssr ?? true,
|
|
305
|
+
minify: cfg.build?.minify || false,
|
|
306
|
+
rollupOptions: {
|
|
307
|
+
external: isTest ? cfg.build?.rollupOptions?.external : cfg.build?.rollupOptions?.external || (options.externals === false ? [] : getExternals({
|
|
308
|
+
node: Boolean(options.externals === true || options.externals?.node),
|
|
309
|
+
workspace: Boolean(options.externals === true || options.externals?.workspace)
|
|
310
|
+
})),
|
|
311
|
+
input: entry,
|
|
312
|
+
output: {
|
|
313
|
+
format: options.format,
|
|
314
|
+
sourcemap: !!(options.sourcemap ?? true),
|
|
315
|
+
entryFileNames: outfile,
|
|
316
|
+
...cfg.build?.rollupOptions?.output
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
},
|
|
322
|
+
async transform(code, id) {
|
|
323
|
+
if (!id.endsWith(".ts")) return code;
|
|
324
|
+
for (const adapter of adapters) if (!adapter.detected && adapter.regex.test(code)) await adapter.init();
|
|
325
|
+
if (REG_HAS_EXPORT_CLASS.test(code)) {
|
|
326
|
+
code = code.replace(REG_REPLACE_EXPORT_CLASS, "\n@__VITE_ID(import.meta.filename)\n$1");
|
|
327
|
+
code = `import { __VITE_ID } from 'virtual:vite-id'\n\n${code}`;
|
|
328
|
+
}
|
|
329
|
+
return code;
|
|
330
|
+
},
|
|
331
|
+
resolveId(id) {
|
|
332
|
+
if (id === "virtual:vite-id") return "\0virtual:vite-id";
|
|
333
|
+
},
|
|
334
|
+
load(id) {
|
|
335
|
+
if (id === "\0virtual:vite-id") return `
|
|
293
336
|
import { getMoostMate } from "moost";
|
|
294
337
|
const mate = getMoostMate();
|
|
295
338
|
export function __VITE_ID(id) {
|
|
296
339
|
return mate.decorate("__vite_id", id)
|
|
297
340
|
}
|
|
298
341
|
`;
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
delete pluginConfig.configureServer;
|
|
344
|
-
delete pluginConfig.resolveId;
|
|
345
|
-
delete pluginConfig.load;
|
|
346
|
-
delete pluginConfig.transform;
|
|
347
|
-
delete pluginConfig.hotUpdate;
|
|
348
|
-
}
|
|
349
|
-
return pluginConfig;
|
|
342
|
+
},
|
|
343
|
+
async configureServer(server) {
|
|
344
|
+
moostRestartCleanup(adapters, options.onEject);
|
|
345
|
+
await server.ssrLoadModule(options.entry);
|
|
346
|
+
server.middlewares.use(async (req, res, next) => {
|
|
347
|
+
if (reloadRequired) {
|
|
348
|
+
reloadRequired = false;
|
|
349
|
+
console.log();
|
|
350
|
+
logger$1.debug("🚀 Reloading Moost App...");
|
|
351
|
+
console.log();
|
|
352
|
+
await server.ssrLoadModule(options.entry);
|
|
353
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
354
|
+
}
|
|
355
|
+
if (moostMiddleware) return moostMiddleware(req, res);
|
|
356
|
+
next();
|
|
357
|
+
});
|
|
358
|
+
},
|
|
359
|
+
hotUpdate({ file }) {
|
|
360
|
+
if (file.endsWith(".ts")) {
|
|
361
|
+
const modules = this.environment.moduleGraph.getModulesByFile(file);
|
|
362
|
+
if (modules) {
|
|
363
|
+
logger$1.debug(`🔃 Hot update: ${file}`);
|
|
364
|
+
const cleanupInstances = new Set();
|
|
365
|
+
for (const mod of modules) {
|
|
366
|
+
const allImporters = gatherAllImporters(mod);
|
|
367
|
+
for (const impModule of allImporters) if (impModule.id) cleanupInstances.add(impModule.id);
|
|
368
|
+
this.environment.moduleGraph.invalidateModule(mod);
|
|
369
|
+
}
|
|
370
|
+
moostMiddleware = null;
|
|
371
|
+
moostRestartCleanup(adapters, options.onEject, cleanupInstances);
|
|
372
|
+
reloadRequired = true;
|
|
373
|
+
}
|
|
374
|
+
return [];
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
if (isProd || isTest) {
|
|
379
|
+
delete pluginConfig.configureServer;
|
|
380
|
+
delete pluginConfig.resolveId;
|
|
381
|
+
delete pluginConfig.load;
|
|
382
|
+
delete pluginConfig.transform;
|
|
383
|
+
delete pluginConfig.hotUpdate;
|
|
384
|
+
}
|
|
385
|
+
return pluginConfig;
|
|
350
386
|
}
|
|
351
387
|
|
|
352
|
-
|
|
388
|
+
//#endregion
|
|
389
|
+
exports.moostVite = moostVite
|