@module-federation/dts-plugin 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/Action-CzhPMw2i.js +153 -0
  2. package/dist/Broker-DRlzScTT.js +800 -0
  3. package/dist/CHANGELOG.md +34 -1
  4. package/dist/DtsWorker-Dtem3-FM.d.ts +166 -0
  5. package/dist/constant-BwEkyidO.d.ts +42 -0
  6. package/dist/consumeTypes-C78VTe8r.js +237 -0
  7. package/dist/core.d.ts +3 -68
  8. package/dist/core.js +26 -2443
  9. package/dist/dynamic-remote-type-hints-plugin.d.ts +622 -3
  10. package/dist/dynamic-remote-type-hints-plugin.js +65 -187
  11. package/dist/esm/Action-DNNg2YDh.mjs +47 -0
  12. package/dist/esm/Broker-BU4gToNr.mjs +736 -0
  13. package/dist/esm/consumeTypes-IRrP9NIN.mjs +204 -0
  14. package/dist/esm/core.mjs +5 -0
  15. package/dist/esm/dynamic-remote-type-hints-plugin.mjs +73 -0
  16. package/dist/esm/expose-rpc-DsABkfLc.mjs +1305 -0
  17. package/dist/esm/fork-dev-worker.mjs +113 -0
  18. package/dist/esm/fork-generate-dts.mjs +14 -0
  19. package/dist/esm/index.mjs +485 -0
  20. package/dist/esm/start-broker.mjs +22 -0
  21. package/dist/esm/utils-CkPvDGOy.mjs +13 -0
  22. package/dist/expose-rpc-DkHFNYya.js +1419 -0
  23. package/dist/fork-dev-worker.d.ts +10 -9
  24. package/dist/fork-dev-worker.js +110 -2932
  25. package/dist/fork-generate-dts.d.ts +4 -8
  26. package/dist/fork-generate-dts.js +11 -2103
  27. package/dist/iife/launch-web-client.iife.js +117 -0
  28. package/dist/index.d.ts +57 -46
  29. package/dist/index.js +469 -3043
  30. package/dist/package.json +35 -12
  31. package/dist/start-broker.d.ts +41 -39
  32. package/dist/start-broker.js +17 -952
  33. package/dist/utils-7KqCZHbb.js +19 -0
  34. package/package.json +40 -17
  35. package/dist/DTSManager-b15Gfat3.d.ts +0 -53
  36. package/dist/DTSManagerOptions-QVchWb0x.d.ts +0 -32
  37. package/dist/DtsWorker-BrHsGz8C.d.ts +0 -56
  38. package/dist/core.d.mts +0 -68
  39. package/dist/dynamic-remote-type-hints-plugin.d.mts +0 -5
  40. package/dist/esm/chunk-647HGGGS.js +0 -241
  41. package/dist/esm/chunk-G65LOFTY.js +0 -24
  42. package/dist/esm/chunk-LJTUMI5K.js +0 -282
  43. package/dist/esm/chunk-MV6M4VFH.js +0 -1642
  44. package/dist/esm/chunk-WWV5RWOP.js +0 -902
  45. package/dist/esm/core.js +0 -44
  46. package/dist/esm/dynamic-remote-type-hints-plugin.js +0 -73
  47. package/dist/esm/fork-dev-worker.js +0 -145
  48. package/dist/esm/fork-generate-dts.js +0 -27
  49. package/dist/esm/index.js +0 -646
  50. package/dist/esm/start-broker.js +0 -36
  51. package/dist/fork-dev-worker.d.mts +0 -15
  52. package/dist/fork-generate-dts.d.mts +0 -10
  53. package/dist/iife/launch-web-client.js +0 -152
  54. package/dist/index.d.mts +0 -56
  55. package/dist/start-broker.d.mts +0 -42
  56. package/dist/utils-C4sQemLR.d.ts +0 -15
@@ -0,0 +1,1305 @@
1
+ import { a as MF_SERVER_IDENTIFIER, n as ActionKind, o as UpdateMode, r as DEFAULT_TAR_NAME, t as Action } from "./Action-DNNg2YDh.mjs";
2
+ import { a as getIdentifier, c as logger$1, i as getFreePort, l as LogKind, n as UpdateKind, o as getIPV4, r as fib, s as fileLog, t as Broker, u as APIKind } from "./Broker-BU4gToNr.mjs";
3
+ import { createRequire } from "node:module";
4
+ import fse from "fs-extra";
5
+ import path, { dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "path";
6
+ import fs, { existsSync } from "fs";
7
+ import { utils } from "@module-federation/managers";
8
+ import typescript from "typescript";
9
+ import axios from "axios";
10
+ import http from "http";
11
+ import https from "https";
12
+ import { ENCODE_NAME_PREFIX, MANIFEST_EXT, TEMP_DIR, decodeName, getProcessEnv, inferAutoPublicPath, parseEntry } from "@module-federation/sdk";
13
+ import ansiColors from "ansi-colors";
14
+ import { readFile, readdir, rm, stat, writeFile } from "fs/promises";
15
+ import { ThirdPartyExtractor } from "@module-federation/third-party-dts-extractor";
16
+ import AdmZip from "adm-zip";
17
+ import crypto from "crypto";
18
+ import { TYPE_001, typeDescMap } from "@module-federation/error-codes";
19
+ import { logAndReport } from "@module-federation/error-codes/node";
20
+ import { execFile, fork } from "child_process";
21
+ import util from "util";
22
+ import WebSocket from "isomorphic-ws";
23
+ import cloneDeepWith from "lodash.clonedeepwith";
24
+ import process$1 from "process";
25
+
26
+ //#region \0rolldown/runtime.js
27
+ var __defProp = Object.defineProperty;
28
+ var __exportAll = (all, no_symbols) => {
29
+ let target = {};
30
+ for (var name in all) {
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true
34
+ });
35
+ }
36
+ if (!no_symbols) {
37
+ __defProp(target, Symbol.toStringTag, { value: "Module" });
38
+ }
39
+ return target;
40
+ };
41
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
42
+
43
+ //#endregion
44
+ //#region src/server/message/Action/AddPublisher.ts
45
+ var AddPublisherAction = class extends Action {
46
+ constructor(payload) {
47
+ super({ payload }, ActionKind.ADD_PUBLISHER);
48
+ }
49
+ };
50
+
51
+ //#endregion
52
+ //#region src/server/message/Action/AddSubscriber.ts
53
+ var AddSubscriberAction = class extends Action {
54
+ constructor(payload) {
55
+ super({ payload }, ActionKind.ADD_SUBSCRIBER);
56
+ }
57
+ };
58
+
59
+ //#endregion
60
+ //#region src/server/message/Action/ExitSubscriber.ts
61
+ var ExitSubscriberAction = class extends Action {
62
+ constructor(payload) {
63
+ super({ payload }, ActionKind.EXIT_SUBSCRIBER);
64
+ }
65
+ };
66
+
67
+ //#endregion
68
+ //#region src/server/message/Action/ExitPublisher.ts
69
+ var ExitPublisherAction = class extends Action {
70
+ constructor(payload) {
71
+ super({ payload }, ActionKind.EXIT_PUBLISHER);
72
+ }
73
+ };
74
+
75
+ //#endregion
76
+ //#region src/server/message/Action/NotifyWebClient.ts
77
+ var NotifyWebClientAction = class extends Action {
78
+ constructor(payload) {
79
+ super({ payload }, ActionKind.NOTIFY_WEB_CLIENT);
80
+ }
81
+ };
82
+
83
+ //#endregion
84
+ //#region src/server/message/Action/UpdatePublisher.ts
85
+ var UpdatePublisherAction = class extends Action {
86
+ constructor(payload) {
87
+ super({ payload }, ActionKind.UPDATE_PUBLISHER);
88
+ }
89
+ };
90
+
91
+ //#endregion
92
+ //#region src/server/broker/createBroker.ts
93
+ function createBroker() {
94
+ const sub = fork(path.resolve(__dirname, "./start-broker.js"), [], {
95
+ detached: true,
96
+ stdio: "ignore",
97
+ env: process.env
98
+ });
99
+ sub.send("start");
100
+ sub.unref();
101
+ return sub;
102
+ }
103
+
104
+ //#endregion
105
+ //#region src/server/DevServer.ts
106
+ var ModuleFederationDevServer = class {
107
+ constructor(ctx) {
108
+ this._publishWebSocket = null;
109
+ this._subscriberWebsocketMap = {};
110
+ this._reconnect = true;
111
+ this._reconnectTimes = 0;
112
+ this._isConnected = false;
113
+ this._isReconnecting = false;
114
+ this._updateCallback = () => Promise.resolve(void 0);
115
+ const { name, remotes, remoteTypeTarPath, updateCallback } = ctx;
116
+ this._ip = getIPV4();
117
+ this._name = name;
118
+ this._remotes = remotes;
119
+ this._remoteTypeTarPath = remoteTypeTarPath;
120
+ this._updateCallback = updateCallback;
121
+ this._stopWhenSIGTERMOrSIGINT();
122
+ this._handleUnexpectedExit();
123
+ this._connectPublishToServer();
124
+ }
125
+ _connectPublishToServer() {
126
+ if (!this._reconnect) return;
127
+ fileLog(`Publisher:${this._name} Trying to connect to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`, MF_SERVER_IDENTIFIER, "info");
128
+ this._publishWebSocket = new WebSocket(`ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}?WEB_SOCKET_CONNECT_MAGIC_ID=${Broker.WEB_SOCKET_CONNECT_MAGIC_ID}`);
129
+ this._publishWebSocket.on("open", () => {
130
+ fileLog(`Current pid: ${process.pid}, publisher:${this._name} connected to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}, starting service...`, MF_SERVER_IDENTIFIER, "info");
131
+ this._isConnected = true;
132
+ const addPublisherAction = new AddPublisherAction({
133
+ name: this._name,
134
+ ip: this._ip,
135
+ remoteTypeTarPath: this._remoteTypeTarPath
136
+ });
137
+ this._publishWebSocket?.send(JSON.stringify(addPublisherAction));
138
+ this._connectSubscribers();
139
+ });
140
+ this._publishWebSocket.on("message", async (message) => {
141
+ try {
142
+ const parsedMessage = JSON.parse(message.toString());
143
+ if (parsedMessage.type === "Log") {
144
+ if (parsedMessage.kind === LogKind.BrokerExitLog) {
145
+ fileLog(`Receive broker exit signal, ${this._name} service will exit...`, MF_SERVER_IDENTIFIER, "warn");
146
+ this._exit();
147
+ }
148
+ }
149
+ if (parsedMessage.type === "API") {
150
+ if (parsedMessage.kind === APIKind.FETCH_TYPES) {
151
+ const { payload: { remoteInfo } } = parsedMessage;
152
+ fileLog(`${this._name} Receive broker FETCH_TYPES, payload as follows: ${JSON.stringify(remoteInfo, null, 2)}.`, MF_SERVER_IDENTIFIER, "info");
153
+ await this.fetchDynamicRemoteTypes({ remoteInfo });
154
+ }
155
+ }
156
+ } catch (err) {
157
+ console.error(err);
158
+ const exitPublisher = new ExitPublisherAction({
159
+ name: this._name,
160
+ ip: this._ip
161
+ });
162
+ const exitSubscriber = new ExitSubscriberAction({
163
+ name: this._name,
164
+ ip: this._ip,
165
+ publishers: this._remotes.map((remote) => ({
166
+ name: remote.name,
167
+ ip: remote.ip
168
+ }))
169
+ });
170
+ this._publishWebSocket?.send(JSON.stringify(exitPublisher));
171
+ this._publishWebSocket?.send(JSON.stringify(exitSubscriber));
172
+ fileLog("Parse messages error, ModuleFederationDevServer will exit...", MF_SERVER_IDENTIFIER, "fatal");
173
+ this._exit();
174
+ }
175
+ });
176
+ this._publishWebSocket.on("close", (code) => {
177
+ fileLog(`Connection closed with code ${code}.`, MF_SERVER_IDENTIFIER, "warn");
178
+ this._publishWebSocket && this._publishWebSocket.close();
179
+ this._publishWebSocket = null;
180
+ if (!this._reconnect) return;
181
+ const reconnectTime = fib(++this._reconnectTimes);
182
+ fileLog(`start reconnecting to server after ${reconnectTime}s.`, MF_SERVER_IDENTIFIER, "info");
183
+ setTimeout(() => this._connectPublishToServer(), reconnectTime * 1e3);
184
+ });
185
+ this._publishWebSocket.on("error", this._tryCreateBackgroundBroker.bind(this));
186
+ }
187
+ _connectSubscriberToServer(remote) {
188
+ const { name, ip } = remote;
189
+ fileLog(`remote module:${name} trying to connect to ws://${ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`, MF_SERVER_IDENTIFIER, "info");
190
+ const identifier = getIdentifier({
191
+ name,
192
+ ip
193
+ });
194
+ this._subscriberWebsocketMap[identifier] = new WebSocket(`ws://${ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}?WEB_SOCKET_CONNECT_MAGIC_ID=${Broker.WEB_SOCKET_CONNECT_MAGIC_ID}`);
195
+ this._subscriberWebsocketMap[identifier].on("open", () => {
196
+ fileLog(`Current pid: ${process.pid} remote module: ${name} connected to ws://${ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}, starting service...`, MF_SERVER_IDENTIFIER, "info");
197
+ const addSubscriber = new AddSubscriberAction({
198
+ name: this._name,
199
+ ip: this._ip,
200
+ publishers: [{
201
+ name,
202
+ ip
203
+ }]
204
+ });
205
+ this._subscriberWebsocketMap[identifier].send(JSON.stringify(addSubscriber));
206
+ });
207
+ this._subscriberWebsocketMap[identifier].on("message", async (message) => {
208
+ try {
209
+ const parsedMessage = JSON.parse(message.toString());
210
+ if (parsedMessage.type === "Log") {
211
+ if (parsedMessage.kind === LogKind.BrokerExitLog) {
212
+ fileLog(`${identifier}'s Server exit, thus ${identifier} will no longer has reload ability.`, MF_SERVER_IDENTIFIER, "warn");
213
+ this._exit();
214
+ }
215
+ }
216
+ if (parsedMessage.type === "API") {
217
+ if (parsedMessage.kind === APIKind.UPDATE_SUBSCRIBER) {
218
+ const { payload: { updateKind, updateSourcePaths, name: subscribeName, remoteTypeTarPath, updateMode } } = parsedMessage;
219
+ await this._updateSubscriber({
220
+ remoteTypeTarPath,
221
+ name: subscribeName,
222
+ updateKind,
223
+ updateMode,
224
+ updateSourcePaths
225
+ });
226
+ }
227
+ }
228
+ } catch (err) {
229
+ console.error(err);
230
+ const exitSubscriber = new ExitSubscriberAction({
231
+ name: this._name,
232
+ ip: this._ip,
233
+ publishers: [{
234
+ name,
235
+ ip
236
+ }]
237
+ });
238
+ this._subscriberWebsocketMap[identifier].send(JSON.stringify(exitSubscriber));
239
+ fileLog(`${identifier} exit,
240
+ error: ${err instanceof Error ? err.toString() : JSON.stringify(err)}
241
+ `, MF_SERVER_IDENTIFIER, "warn");
242
+ }
243
+ });
244
+ this._subscriberWebsocketMap[identifier].on("close", (code) => {
245
+ fileLog(`Connection closed with code ${code}.`, MF_SERVER_IDENTIFIER, "warn");
246
+ this._subscriberWebsocketMap[identifier]?.close();
247
+ delete this._subscriberWebsocketMap[identifier];
248
+ });
249
+ this._subscriberWebsocketMap[identifier].on("error", (err) => {
250
+ if ("code" in err && err.code === "ETIMEDOUT") fileLog(`Can not connect ${JSON.stringify(remote)}, please make sure this remote is started locally.`, MF_SERVER_IDENTIFIER, "warn");
251
+ else console.error(err);
252
+ this._subscriberWebsocketMap[identifier]?.close();
253
+ delete this._subscriberWebsocketMap[identifier];
254
+ });
255
+ }
256
+ _connectSubscribers() {
257
+ this._remotes.forEach((remote) => {
258
+ this._connectSubscriberToServer(remote);
259
+ });
260
+ }
261
+ async _updateSubscriber(options) {
262
+ const { updateMode, updateKind, updateSourcePaths, name, remoteTypeTarPath, remoteInfo } = options;
263
+ fileLog(`[_updateSubscriber] run, options: ${JSON.stringify(options, null, 2)}`, MF_SERVER_IDENTIFIER, "warn");
264
+ if (updateMode === UpdateMode.PASSIVE && updateSourcePaths.includes(this._name)) {
265
+ fileLog(`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths} includes ${this._name}, update ignore!`, MF_SERVER_IDENTIFIER, "warn");
266
+ return;
267
+ }
268
+ if (updateSourcePaths.slice(-1)[0] === this._name) {
269
+ fileLog(`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths} ends is ${this._name}, update ignore!`, MF_SERVER_IDENTIFIER, "warn");
270
+ return;
271
+ }
272
+ fileLog(`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths}, current module:${this._name}, update start...`, MF_SERVER_IDENTIFIER, "info");
273
+ await this._updateCallback({
274
+ name,
275
+ updateMode,
276
+ updateKind,
277
+ updateSourcePaths,
278
+ remoteTypeTarPath,
279
+ remoteInfo
280
+ });
281
+ const newUpdateSourcePaths = updateSourcePaths.concat(this._name);
282
+ const updatePublisher = new UpdatePublisherAction({
283
+ name: this._name,
284
+ ip: this._ip,
285
+ updateMode: UpdateMode.PASSIVE,
286
+ updateKind,
287
+ updateSourcePaths: newUpdateSourcePaths,
288
+ remoteTypeTarPath: this._remoteTypeTarPath
289
+ });
290
+ fileLog(`[_updateSubscriber] run, updateSourcePaths:${newUpdateSourcePaths}, update publisher ${this._name} start...`, MF_SERVER_IDENTIFIER, "info");
291
+ this._publishWebSocket?.send(JSON.stringify(updatePublisher));
292
+ }
293
+ _tryCreateBackgroundBroker(err) {
294
+ if (!((err?.code === "ECONNREFUSED" || err?.code === "ETIMEDOUT") && err.port === Broker.DEFAULT_WEB_SOCKET_PORT)) {
295
+ fileLog(`websocket error: ${err.stack}`, MF_SERVER_IDENTIFIER, "fatal");
296
+ return;
297
+ }
298
+ fileLog(`Failed to connect to ws://${this._ip}:${Broker.DEFAULT_WEB_SOCKET_PORT}...`, MF_SERVER_IDENTIFIER, "fatal");
299
+ this._isReconnecting = true;
300
+ setTimeout(() => {
301
+ this._isReconnecting = false;
302
+ if (this._reconnect === false) return;
303
+ fileLog("Creating new background broker...", MF_SERVER_IDENTIFIER, "warn");
304
+ createBroker().on("message", (message) => {
305
+ if (message === "ready") {
306
+ fileLog("background broker started.", MF_SERVER_IDENTIFIER, "info");
307
+ this._reconnectTimes = 1;
308
+ if (process.send) process.send("ready");
309
+ }
310
+ });
311
+ }, Math.ceil(100 * Math.random()));
312
+ }
313
+ _stopWhenSIGTERMOrSIGINT() {
314
+ process.on("SIGTERM", () => {
315
+ fileLog(`Process(${process.pid}) SIGTERM, ModuleFederationDevServer will exit...`, MF_SERVER_IDENTIFIER, "warn");
316
+ this._exit();
317
+ });
318
+ process.on("SIGINT", () => {
319
+ fileLog(`Process(${process.pid}) SIGINT, ModuleFederationDevServer will exit...`, MF_SERVER_IDENTIFIER, "warn");
320
+ this._exit();
321
+ });
322
+ }
323
+ _handleUnexpectedExit() {
324
+ process.on("unhandledRejection", (error) => {
325
+ if (this._isReconnecting) return;
326
+ console.error("Unhandled Rejection Error: ", error);
327
+ fileLog(`Process(${process.pid}) unhandledRejection, garfishModuleServer will exit...`, MF_SERVER_IDENTIFIER, "error");
328
+ this._exit();
329
+ });
330
+ process.on("uncaughtException", (error) => {
331
+ if (this._isReconnecting) return;
332
+ console.error("Unhandled Exception Error: ", error);
333
+ fileLog(`Process(${process.pid}) uncaughtException, garfishModuleServer will exit...`, MF_SERVER_IDENTIFIER, "error");
334
+ this._exit();
335
+ });
336
+ }
337
+ _exit() {
338
+ this._reconnect = false;
339
+ if (this._publishWebSocket) {
340
+ const exitPublisher = new ExitPublisherAction({
341
+ name: this._name,
342
+ ip: this._ip
343
+ });
344
+ this._publishWebSocket.send(JSON.stringify(exitPublisher));
345
+ this._publishWebSocket.on("message", (message) => {
346
+ const parsedMessage = JSON.parse(message.toString());
347
+ fileLog(`[${parsedMessage.kind}]: ${JSON.stringify(parsedMessage)}`, MF_SERVER_IDENTIFIER, "info");
348
+ });
349
+ }
350
+ if (this._publishWebSocket) {
351
+ this._publishWebSocket.close();
352
+ this._publishWebSocket = null;
353
+ }
354
+ process.exit(0);
355
+ }
356
+ exit() {
357
+ this._exit();
358
+ }
359
+ update(options) {
360
+ if (!this._publishWebSocket || !this._isConnected) return;
361
+ const { updateKind, updateMode, updateSourcePaths, clientName } = options;
362
+ fileLog(`update run, ${this._name} module update, updateKind: ${updateKind}, updateMode: ${updateMode}, updateSourcePaths: ${updateSourcePaths}`, MF_SERVER_IDENTIFIER, "info");
363
+ if (updateKind === UpdateKind.RELOAD_PAGE) {
364
+ const notifyWebClient = new NotifyWebClientAction({
365
+ name: clientName || this._name,
366
+ updateMode
367
+ });
368
+ this._publishWebSocket.send(JSON.stringify(notifyWebClient));
369
+ return;
370
+ }
371
+ const updatePublisher = new UpdatePublisherAction({
372
+ name: this._name,
373
+ ip: this._ip,
374
+ updateMode,
375
+ updateKind,
376
+ updateSourcePaths: [this._name],
377
+ remoteTypeTarPath: this._remoteTypeTarPath
378
+ });
379
+ this._publishWebSocket.send(JSON.stringify(updatePublisher));
380
+ }
381
+ async fetchDynamicRemoteTypes(options) {
382
+ const { remoteInfo, once } = options;
383
+ const updateMode = UpdateMode.PASSIVE;
384
+ const updateKind = UpdateKind.UPDATE_TYPE;
385
+ fileLog(`fetchDynamicRemoteTypes: remoteInfo: ${JSON.stringify(remoteInfo)}`, MF_SERVER_IDENTIFIER, "info");
386
+ await this._updateCallback({
387
+ name: this._name,
388
+ updateMode,
389
+ updateKind,
390
+ updateSourcePaths: [],
391
+ remoteTypeTarPath: "",
392
+ remoteInfo,
393
+ once
394
+ });
395
+ const updatePublisher = new UpdatePublisherAction({
396
+ name: this._name,
397
+ ip: this._ip,
398
+ updateMode,
399
+ updateKind,
400
+ updateSourcePaths: [this._name],
401
+ remoteTypeTarPath: this._remoteTypeTarPath
402
+ });
403
+ this._publishWebSocket.send(JSON.stringify(updatePublisher));
404
+ }
405
+ };
406
+
407
+ //#endregion
408
+ //#region src/server/createHttpServer.ts
409
+ async function createHttpServer(options) {
410
+ const { typeTarPath } = options;
411
+ const freeport = await getFreePort();
412
+ const server = http.createServer((req, res) => {
413
+ if ((req.url?.split("?")[0] ?? "/") === `/${DEFAULT_TAR_NAME}`) {
414
+ res.statusCode = 200;
415
+ res.setHeader("Content-Type", "application/x-gzip");
416
+ if (req.method === "HEAD") {
417
+ res.end();
418
+ return;
419
+ }
420
+ const stream = fse.createReadStream(typeTarPath);
421
+ stream.on("error", () => {
422
+ if (!res.headersSent) res.statusCode = 500;
423
+ res.end();
424
+ });
425
+ res.on("close", () => {
426
+ stream.destroy();
427
+ });
428
+ stream.pipe(res);
429
+ return;
430
+ }
431
+ res.statusCode = 404;
432
+ res.end();
433
+ });
434
+ server.listen(freeport);
435
+ return {
436
+ server,
437
+ serverAddress: `http://${getIPV4()}:${freeport}`
438
+ };
439
+ }
440
+
441
+ //#endregion
442
+ //#region src/core/lib/typeScriptCompiler.ts
443
+ const { ensureDirSync, writeFileSync, existsSync: existsSync$1 } = fse;
444
+ const STARTS_WITH_SLASH = /^\//;
445
+ const DEFINITION_FILE_EXTENSION = ".d.ts";
446
+ const retrieveMfTypesPath = (tsConfig, remoteOptions) => normalize(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, ""));
447
+ const retrieveOriginalOutDir = (tsConfig, remoteOptions) => normalize(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "").replace(remoteOptions.typesFolder, ""));
448
+ const retrieveMfAPITypesPath = (tsConfig, remoteOptions) => join(retrieveOriginalOutDir(tsConfig, remoteOptions), `${remoteOptions.typesFolder}.d.ts`);
449
+ function writeTempTsConfig(tsConfig, context, name, cwd) {
450
+ const createHash = (contents) => {
451
+ return crypto.createHash("md5").update(contents).digest("hex");
452
+ };
453
+ const hash = createHash(`${JSON.stringify(tsConfig)}${name}${Date.now()}`);
454
+ const tempTsConfigJsonPath = resolve(cwd ?? context, "node_modules", TEMP_DIR, `tsconfig.${hash}.json`);
455
+ ensureDirSync(dirname(tempTsConfigJsonPath));
456
+ writeFileSync(tempTsConfigJsonPath, JSON.stringify(tsConfig, null, 2));
457
+ return tempTsConfigJsonPath;
458
+ }
459
+ const removeExt = (f) => {
460
+ const vueExt = ".vue";
461
+ const ext = extname(f);
462
+ if (ext === vueExt) return f;
463
+ const regexPattern = new RegExp(`\\${ext}$`);
464
+ return f.replace(regexPattern, "");
465
+ };
466
+ function getExposeKey(options) {
467
+ const { filePath, rootDir, outDir, mapExposeToEntry } = options;
468
+ return mapExposeToEntry[relative(outDir, filePath.replace(new RegExp(`\\.d.ts$`), ""))];
469
+ }
470
+ const processTypesFile = async (options) => {
471
+ const { outDir, filePath, rootDir, cb, mapExposeToEntry, mfTypePath } = options;
472
+ if (!existsSync$1(filePath)) return;
473
+ if ((await stat(filePath)).isDirectory()) {
474
+ const files = await readdir(filePath);
475
+ await Promise.all(files.map((file) => processTypesFile({
476
+ ...options,
477
+ filePath: join(filePath, file)
478
+ })));
479
+ } else if (filePath.endsWith(".d.ts")) {
480
+ const exposeKey = getExposeKey({
481
+ filePath,
482
+ rootDir,
483
+ outDir,
484
+ mapExposeToEntry
485
+ });
486
+ if (exposeKey) {
487
+ const mfeTypeEntry = join(mfTypePath, `${exposeKey === "." ? "index" : exposeKey}${DEFINITION_FILE_EXTENSION}`);
488
+ const mfeTypeEntryDirectory = dirname(mfeTypeEntry);
489
+ const relativePathToOutput = relative(mfeTypeEntryDirectory, filePath).replace(DEFINITION_FILE_EXTENSION, "").replace(STARTS_WITH_SLASH, "").split(sep).join("/");
490
+ ensureDirSync(mfeTypeEntryDirectory);
491
+ await writeFile(mfeTypeEntry, `export * from './${relativePathToOutput}';\nexport { default } from './${relativePathToOutput}';`);
492
+ }
493
+ cb(await readFile(filePath, "utf8"));
494
+ }
495
+ };
496
+ const getPMFromUserAgent = () => {
497
+ const userAgent = process.env["npm_config_user_agent"];
498
+ if (userAgent == null) return "null";
499
+ return userAgent.split("/")[0];
500
+ };
501
+ const resolvePackageManagerExecutable = () => {
502
+ switch (getPMFromUserAgent()) {
503
+ case "yarn": return "yarn";
504
+ default: return "npx";
505
+ }
506
+ };
507
+ const splitCommandArgs = (value) => {
508
+ const args = [];
509
+ let current = "";
510
+ let quote = null;
511
+ let escaped = false;
512
+ for (const char of value) {
513
+ if (escaped) {
514
+ current += char;
515
+ escaped = false;
516
+ continue;
517
+ }
518
+ if (char === "\\") {
519
+ escaped = true;
520
+ continue;
521
+ }
522
+ if (quote) {
523
+ if (char === quote) quote = null;
524
+ else current += char;
525
+ continue;
526
+ }
527
+ if (char === "\"" || char === "'") {
528
+ quote = char;
529
+ continue;
530
+ }
531
+ if (char.trim() === "") {
532
+ if (current) {
533
+ args.push(current);
534
+ current = "";
535
+ }
536
+ continue;
537
+ }
538
+ current += char;
539
+ }
540
+ if (current) args.push(current);
541
+ return args;
542
+ };
543
+ const formatCommandForDisplay = (executable, args) => {
544
+ const formatArg = (arg) => {
545
+ if (/[\s'"]/.test(arg)) return JSON.stringify(arg);
546
+ return arg;
547
+ };
548
+ return [executable, ...args].map(formatArg).join(" ");
549
+ };
550
+ const compileTs = async (mapComponentsToExpose, tsConfig, remoteOptions) => {
551
+ if (!Object.keys(mapComponentsToExpose).length) return;
552
+ const { compilerOptions } = tsConfig;
553
+ const tempTsConfigJsonPath = writeTempTsConfig(tsConfig, remoteOptions.context, remoteOptions.moduleFederationConfig.name || "mf", typeof remoteOptions.moduleFederationConfig.dts !== "boolean" ? remoteOptions.moduleFederationConfig.dts?.cwd ?? void 0 : void 0);
554
+ logger$1.debug(`tempTsConfigJsonPath: ${tempTsConfigJsonPath}`);
555
+ try {
556
+ const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
557
+ const thirdPartyExtractor = new ThirdPartyExtractor({
558
+ destDir: resolve(mfTypePath, "node_modules"),
559
+ context: remoteOptions.context,
560
+ exclude: typeof remoteOptions.extractThirdParty === "object" ? remoteOptions.extractThirdParty.exclude : void 0
561
+ });
562
+ const execPromise = util.promisify(execFile);
563
+ const pmExecutable = resolvePackageManagerExecutable();
564
+ const compilerArgs = splitCommandArgs(remoteOptions.compilerInstance);
565
+ const cmdArgs = [
566
+ ...compilerArgs.length > 0 ? compilerArgs : [remoteOptions.compilerInstance],
567
+ "--project",
568
+ tempTsConfigJsonPath
569
+ ];
570
+ const cmd = formatCommandForDisplay(pmExecutable, cmdArgs);
571
+ try {
572
+ await execPromise(pmExecutable, cmdArgs, {
573
+ cwd: typeof remoteOptions.moduleFederationConfig.dts !== "boolean" ? remoteOptions.moduleFederationConfig.dts?.cwd ?? void 0 : void 0,
574
+ shell: process.platform === "win32"
575
+ });
576
+ } catch (err) {
577
+ if (compilerOptions.tsBuildInfoFile) try {
578
+ await rm(compilerOptions.tsBuildInfoFile);
579
+ } catch (e) {}
580
+ logAndReport(TYPE_001, typeDescMap, { cmd }, (msg) => {
581
+ throw new Error(msg);
582
+ }, void 0);
583
+ }
584
+ const mapExposeToEntry = Object.fromEntries(Object.entries(mapComponentsToExpose).map(([exposed, filename]) => {
585
+ const normalizedFileName = normalize(filename);
586
+ let relativeFileName = "";
587
+ if (isAbsolute(normalizedFileName)) relativeFileName = relative(tsConfig.compilerOptions.rootDir, normalizedFileName);
588
+ else relativeFileName = relative(tsConfig.compilerOptions.rootDir, resolve(remoteOptions.context, normalizedFileName));
589
+ return [removeExt(relativeFileName), exposed];
590
+ }));
591
+ const cb = remoteOptions.extractThirdParty ? thirdPartyExtractor.collectPkgs.bind(thirdPartyExtractor) : () => void 0;
592
+ await processTypesFile({
593
+ outDir: compilerOptions.outDir,
594
+ filePath: compilerOptions.outDir,
595
+ rootDir: compilerOptions.rootDir,
596
+ mfTypePath,
597
+ cb,
598
+ mapExposeToEntry
599
+ });
600
+ if (remoteOptions.extractThirdParty) await thirdPartyExtractor.copyDts();
601
+ if (remoteOptions.deleteTsConfig) await rm(tempTsConfigJsonPath);
602
+ } catch (err) {
603
+ throw err;
604
+ }
605
+ };
606
+
607
+ //#endregion
608
+ //#region src/core/lib/archiveHandler.ts
609
+ const retrieveTypesZipPath = (mfTypesPath, remoteOptions) => join(mfTypesPath.replace(remoteOptions.typesFolder, ""), `${remoteOptions.typesFolder}.zip`);
610
+ const createTypesArchive = async (tsConfig, remoteOptions) => {
611
+ const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
612
+ const zip = new AdmZip();
613
+ zip.addLocalFolder(mfTypesPath);
614
+ return zip.writeZipPromise(retrieveTypesZipPath(mfTypesPath, remoteOptions));
615
+ };
616
+ const downloadErrorLogger = (destinationFolder, fileToDownload) => (reason) => {
617
+ throw {
618
+ ...reason,
619
+ message: `Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${reason.message}'`
620
+ };
621
+ };
622
+ const retrieveTypesArchiveDestinationPath = (hostOptions, destinationFolder) => {
623
+ return resolve(hostOptions.context, hostOptions.typesFolder, destinationFolder);
624
+ };
625
+ const downloadTypesArchive = (hostOptions) => {
626
+ let retries = 0;
627
+ return async ([destinationFolder, fileToDownload]) => {
628
+ const destinationPath = retrieveTypesArchiveDestinationPath(hostOptions, destinationFolder);
629
+ while (retries++ < hostOptions.maxRetries) try {
630
+ const url = new URL(fileToDownload).href;
631
+ const response = await axiosGet(url, {
632
+ responseType: "arraybuffer",
633
+ timeout: hostOptions.timeout,
634
+ family: hostOptions.family
635
+ }).catch(downloadErrorLogger(destinationFolder, url));
636
+ if (typeof response.headers?.["content-type"] === "string" && response.headers["content-type"].includes("text/html")) throw new Error(`${url} receives invalid content-type: ${response.headers["content-type"]}`);
637
+ try {
638
+ if (hostOptions.deleteTypesFolder) await rm(destinationPath, {
639
+ recursive: true,
640
+ force: true
641
+ });
642
+ } catch (error) {
643
+ fileLog(`Unable to remove types folder, ${error}`, "downloadTypesArchive", "error");
644
+ }
645
+ new AdmZip(Buffer.from(response.data)).extractAllTo(destinationPath, true);
646
+ fileLog(`zip.extractAllTo success destinationPath: ${destinationPath}; url: ${url}`, "downloadTypesArchive", "info");
647
+ return [destinationFolder, destinationPath];
648
+ } catch (error) {
649
+ fileLog(`Error during types archive download: ${error?.message || "unknown error"}`, "downloadTypesArchive", "error");
650
+ if (retries >= hostOptions.maxRetries) {
651
+ logger$1.error(`Failed to download types archive from "${fileToDownload}". Set FEDERATION_DEBUG=true for details.`);
652
+ if (hostOptions.abortOnError !== false) throw error;
653
+ return;
654
+ }
655
+ }
656
+ };
657
+ };
658
+
659
+ //#endregion
660
+ //#region src/core/configurations/hostPlugin.ts
661
+ const defaultOptions$1 = {
662
+ typesFolder: "@mf-types",
663
+ remoteTypesFolder: "@mf-types",
664
+ deleteTypesFolder: true,
665
+ maxRetries: 3,
666
+ implementation: "",
667
+ context: process.cwd(),
668
+ abortOnError: true,
669
+ consumeAPITypes: false,
670
+ runtimePkgs: [],
671
+ remoteTypeUrls: {},
672
+ timeout: 6e4,
673
+ typesOnBuild: false,
674
+ family: 4
675
+ };
676
+ const buildZipUrl = (hostOptions, url) => {
677
+ const remoteUrl = new URL(url, "file:");
678
+ remoteUrl.pathname = `${remoteUrl.pathname.split("/").slice(0, -1).join("/")}/${hostOptions.remoteTypesFolder}.zip`;
679
+ return remoteUrl.protocol === "file:" ? remoteUrl.pathname : remoteUrl.href;
680
+ };
681
+ const buildApiTypeUrl = (zipUrl) => {
682
+ if (!zipUrl) return;
683
+ return zipUrl.replace(".zip", ".d.ts");
684
+ };
685
+ const retrieveRemoteInfo = (options) => {
686
+ const { hostOptions, remoteAlias, remote } = options;
687
+ const { remoteTypeUrls } = hostOptions;
688
+ let decodedRemote = remote;
689
+ if (decodedRemote.startsWith(ENCODE_NAME_PREFIX)) decodedRemote = decodeName(decodedRemote, ENCODE_NAME_PREFIX);
690
+ const parsedInfo = parseEntry(decodedRemote, void 0, "@");
691
+ const url = "entry" in parsedInfo ? parsedInfo.entry : parsedInfo.name === decodedRemote ? decodedRemote : "";
692
+ let zipUrl = "";
693
+ let apiTypeUrl = "";
694
+ const name = parsedInfo.name || remoteAlias;
695
+ if (typeof remoteTypeUrls === "object" && remoteTypeUrls[name]) {
696
+ zipUrl = remoteTypeUrls[name].zip;
697
+ apiTypeUrl = remoteTypeUrls[name].api;
698
+ }
699
+ if (!zipUrl && url) zipUrl = buildZipUrl(hostOptions, url);
700
+ if (!apiTypeUrl && zipUrl) apiTypeUrl = buildApiTypeUrl(zipUrl);
701
+ return {
702
+ name,
703
+ url,
704
+ zipUrl,
705
+ apiTypeUrl,
706
+ alias: remoteAlias
707
+ };
708
+ };
709
+ const resolveRemotes = (hostOptions) => {
710
+ const parsedOptions = utils.parseOptions(hostOptions.moduleFederationConfig.remotes || {}, (item, key) => ({
711
+ remote: Array.isArray(item) ? item[0] : item,
712
+ key
713
+ }), (item, key) => ({
714
+ remote: Array.isArray(item.external) ? item.external[0] : item.external,
715
+ key
716
+ }));
717
+ const remoteTypeUrls = hostOptions.remoteTypeUrls ?? {};
718
+ if (typeof remoteTypeUrls !== "object") throw new Error("remoteTypeUrls must be consumed before resolveRemotes");
719
+ const remoteInfos = Object.keys(remoteTypeUrls).reduce((sum, remoteName) => {
720
+ const { zip, api, alias } = remoteTypeUrls[remoteName];
721
+ sum[alias] = {
722
+ name: remoteName,
723
+ url: "",
724
+ zipUrl: zip,
725
+ apiTypeUrl: api,
726
+ alias: alias || remoteName
727
+ };
728
+ return sum;
729
+ }, {});
730
+ return parsedOptions.reduce((accumulator, item) => {
731
+ const { key, remote } = item[1];
732
+ const res = retrieveRemoteInfo({
733
+ hostOptions,
734
+ remoteAlias: key,
735
+ remote
736
+ });
737
+ if (accumulator[key]) {
738
+ accumulator[key] = {
739
+ ...accumulator[key],
740
+ url: res.url,
741
+ apiTypeUrl: accumulator[key].apiTypeUrl || res.apiTypeUrl
742
+ };
743
+ return accumulator;
744
+ }
745
+ accumulator[key] = res;
746
+ return accumulator;
747
+ }, remoteInfos);
748
+ };
749
+ const retrieveHostConfig = (options) => {
750
+ validateOptions(options);
751
+ const hostOptions = {
752
+ ...defaultOptions$1,
753
+ ...options
754
+ };
755
+ return {
756
+ hostOptions,
757
+ mapRemotesToDownload: resolveRemotes(hostOptions)
758
+ };
759
+ };
760
+
761
+ //#endregion
762
+ //#region src/core/constant.ts
763
+ const REMOTE_ALIAS_IDENTIFIER = "REMOTE_ALIAS_IDENTIFIER";
764
+ const REMOTE_API_TYPES_FILE_NAME = "apis.d.ts";
765
+ const HOST_API_TYPES_FILE_NAME = "index.d.ts";
766
+
767
+ //#endregion
768
+ //#region src/core/lib/DTSManager.ts
769
+ var DTSManager = class {
770
+ constructor(options) {
771
+ this.options = cloneDeepOptions(options);
772
+ this.runtimePkgs = [
773
+ "@module-federation/runtime",
774
+ "@module-federation/enhanced/runtime",
775
+ "@module-federation/runtime-tools"
776
+ ];
777
+ this.loadedRemoteAPIAlias = /* @__PURE__ */ new Set();
778
+ this.remoteAliasMap = {};
779
+ this.extraOptions = options?.extraOptions || {};
780
+ this.updatedRemoteInfos = {};
781
+ }
782
+ generateAPITypes(mapComponentsToExpose) {
783
+ const exposePaths = /* @__PURE__ */ new Set();
784
+ const packageType = Object.keys(mapComponentsToExpose).reduce((sum, exposeKey) => {
785
+ const exposePath = path.join(REMOTE_ALIAS_IDENTIFIER, exposeKey).split(path.sep).join("/");
786
+ exposePaths.add(`'${exposePath}'`);
787
+ sum = `T extends '${exposePath}' ? typeof import('${exposePath}') :` + sum;
788
+ return sum;
789
+ }, "any;");
790
+ return `
791
+ export type RemoteKeys = ${[...exposePaths].join(" | ")};
792
+ type PackageType<T> = ${packageType}`;
793
+ }
794
+ async extractRemoteTypes(options) {
795
+ const { remoteOptions, tsConfig } = options;
796
+ if (!remoteOptions.extractRemoteTypes) return;
797
+ let hasRemotes = false;
798
+ const remotes = remoteOptions.moduleFederationConfig.remotes;
799
+ if (remotes) {
800
+ if (Array.isArray(remotes)) hasRemotes = Boolean(remotes.length);
801
+ else if (typeof remotes === "object") hasRemotes = Boolean(Object.keys(remotes).length);
802
+ }
803
+ const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
804
+ if (hasRemotes && this.options.host) try {
805
+ const { hostOptions } = retrieveHostConfig(this.options.host);
806
+ const remoteTypesFolder = path.resolve(hostOptions.context, hostOptions.typesFolder);
807
+ const targetDir = path.join(mfTypesPath, "node_modules");
808
+ if (fs.existsSync(remoteTypesFolder)) {
809
+ const targetFolder = path.resolve(remoteOptions.context, targetDir);
810
+ await fse.ensureDir(targetFolder);
811
+ await fse.copy(remoteTypesFolder, targetFolder, { overwrite: true });
812
+ }
813
+ } catch (err) {
814
+ if (this.options.host?.abortOnError === false) fileLog(`Unable to copy remote types, ${err}`, "extractRemoteTypes", "error");
815
+ else throw err;
816
+ }
817
+ }
818
+ async generateTypes() {
819
+ try {
820
+ const { options } = this;
821
+ if (!options.remote) throw new Error("options.remote is required if you want to generateTypes");
822
+ const { remoteOptions, tsConfig, mapComponentsToExpose } = retrieveRemoteConfig(options.remote);
823
+ if (!Object.keys(mapComponentsToExpose).length) return;
824
+ if (!tsConfig.files?.length) {
825
+ logger$1.info("No type files to compile, skip");
826
+ return;
827
+ }
828
+ if (tsConfig.compilerOptions.tsBuildInfoFile) try {
829
+ const tsBuildInfoFile = path.resolve(remoteOptions.context, tsConfig.compilerOptions.tsBuildInfoFile);
830
+ const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
831
+ if (!fs.existsSync(mfTypesPath)) fs.rmSync(tsBuildInfoFile, { force: true });
832
+ } catch (e) {}
833
+ await this.extractRemoteTypes({
834
+ remoteOptions,
835
+ tsConfig,
836
+ mapComponentsToExpose
837
+ });
838
+ await compileTs(mapComponentsToExpose, tsConfig, remoteOptions);
839
+ await createTypesArchive(tsConfig, remoteOptions);
840
+ let apiTypesPath = "";
841
+ if (remoteOptions.generateAPITypes) {
842
+ const apiTypes = this.generateAPITypes(mapComponentsToExpose);
843
+ apiTypesPath = retrieveMfAPITypesPath(tsConfig, remoteOptions);
844
+ fs.writeFileSync(apiTypesPath, apiTypes);
845
+ }
846
+ try {
847
+ if (remoteOptions.deleteTypesFolder) await rm(retrieveMfTypesPath(tsConfig, remoteOptions), {
848
+ recursive: true,
849
+ force: true
850
+ });
851
+ } catch (err) {
852
+ if (isDebugMode()) console.error(err);
853
+ }
854
+ logger$1.success("Federated types created correctly");
855
+ } catch (error) {
856
+ if (this.options.remote?.abortOnError === false) {
857
+ if (this.options.displayErrorInTerminal) logger$1.error(error);
858
+ } else throw error;
859
+ }
860
+ }
861
+ async requestRemoteManifest(remoteInfo, hostOptions) {
862
+ try {
863
+ if (!remoteInfo.url.includes(MANIFEST_EXT)) return remoteInfo;
864
+ if (remoteInfo.zipUrl) return remoteInfo;
865
+ const url = remoteInfo.url;
866
+ const manifestJson = (await axiosGet(url, {
867
+ timeout: hostOptions.timeout,
868
+ family: hostOptions.family
869
+ })).data;
870
+ if (!manifestJson.metaData.types.zip) throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
871
+ const addProtocol = (u) => {
872
+ if (u.startsWith("//")) return `https:${u}`;
873
+ return u;
874
+ };
875
+ let publicPath;
876
+ if ("publicPath" in manifestJson.metaData) publicPath = manifestJson.metaData.publicPath;
877
+ else {
878
+ const getPublicPath = new Function(manifestJson.metaData.getPublicPath);
879
+ if (manifestJson.metaData.getPublicPath.startsWith("function")) publicPath = getPublicPath()();
880
+ else publicPath = getPublicPath();
881
+ }
882
+ if (publicPath === "auto") publicPath = inferAutoPublicPath(remoteInfo.url);
883
+ remoteInfo.zipUrl = new URL(path.join(addProtocol(publicPath), manifestJson.metaData.types.zip)).href;
884
+ if (!manifestJson.metaData.types.api) {
885
+ console.warn(`Can not get ${remoteInfo.name}'s api types url!`);
886
+ remoteInfo.apiTypeUrl = "";
887
+ return remoteInfo;
888
+ }
889
+ remoteInfo.apiTypeUrl = new URL(path.join(addProtocol(publicPath), manifestJson.metaData.types.api)).href;
890
+ return remoteInfo;
891
+ } catch (_err) {
892
+ fileLog(`fetch manifest failed, ${_err}, ${remoteInfo.name} will be ignored`, "requestRemoteManifest", "error");
893
+ return remoteInfo;
894
+ }
895
+ }
896
+ async consumeTargetRemotes(hostOptions, remoteInfo) {
897
+ if (!remoteInfo.zipUrl) throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
898
+ return downloadTypesArchive(hostOptions)([remoteInfo.alias, remoteInfo.zipUrl]);
899
+ }
900
+ async downloadAPITypes(remoteInfo, destinationPath, hostOptions) {
901
+ const { apiTypeUrl } = remoteInfo;
902
+ if (!apiTypeUrl) return;
903
+ try {
904
+ let apiTypeFile = (await axiosGet(apiTypeUrl, {
905
+ timeout: hostOptions.timeout,
906
+ family: hostOptions.family
907
+ })).data;
908
+ apiTypeFile = apiTypeFile.replaceAll(REMOTE_ALIAS_IDENTIFIER, remoteInfo.alias);
909
+ const filePath = path.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
910
+ fs.writeFileSync(filePath, apiTypeFile);
911
+ const existed = this.loadedRemoteAPIAlias.has(remoteInfo.alias);
912
+ this.loadedRemoteAPIAlias.add(remoteInfo.alias);
913
+ fileLog(`success`, "downloadAPITypes", "info");
914
+ return existed;
915
+ } catch (err) {
916
+ fileLog(`Unable to download "${remoteInfo.name}" api types, ${err}`, "downloadAPITypes", "error");
917
+ }
918
+ }
919
+ consumeAPITypes(hostOptions) {
920
+ const apiTypeFileName = path.join(hostOptions.context, hostOptions.typesFolder, HOST_API_TYPES_FILE_NAME);
921
+ try {
922
+ const existedFile = fs.readFileSync(apiTypeFileName, "utf-8");
923
+ new ThirdPartyExtractor({ destDir: "" }).collectTypeImports(existedFile).forEach((existedImport) => {
924
+ const alias = existedImport.split("./").slice(1).join("./").replace("/apis.d.ts", "");
925
+ this.loadedRemoteAPIAlias.add(alias);
926
+ });
927
+ } catch (err) {}
928
+ if (!this.loadedRemoteAPIAlias.size) return;
929
+ const packageTypes = [];
930
+ const remoteKeys = [];
931
+ const importTypeStr = [...this.loadedRemoteAPIAlias].sort().map((alias, index) => {
932
+ const remoteKey = `RemoteKeys_${index}`;
933
+ const packageType = `PackageType_${index}`;
934
+ packageTypes.push(`T extends ${remoteKey} ? ${packageType}<T>`);
935
+ remoteKeys.push(remoteKey);
936
+ return `import type { PackageType as ${packageType},RemoteKeys as ${remoteKey} } from './${alias}/apis.d.ts';`;
937
+ }).join("\n");
938
+ const remoteKeysStr = `type RemoteKeys = ${remoteKeys.join(" | ")};`;
939
+ const packageTypesStr = `type PackageType<T, Y=any> = ${[...packageTypes, "Y"].join(" :\n")} ;`;
940
+ const runtimePkgs = /* @__PURE__ */ new Set();
941
+ [...this.runtimePkgs, ...hostOptions.runtimePkgs].forEach((pkg) => {
942
+ runtimePkgs.add(pkg);
943
+ });
944
+ const fileStr = `${importTypeStr}
945
+ ${[...runtimePkgs].map((pkg) => {
946
+ return `declare module "${pkg}" {
947
+ ${remoteKeysStr}
948
+ ${packageTypesStr}
949
+ export function loadRemote<T extends RemoteKeys,Y>(packageName: T): Promise<PackageType<T, Y>>;
950
+ export function loadRemote<T extends string,Y>(packageName: T): Promise<PackageType<T, Y>>;
951
+ }`;
952
+ }).join("\n")}
953
+ `;
954
+ fs.writeFileSync(path.join(hostOptions.context, hostOptions.typesFolder, HOST_API_TYPES_FILE_NAME), fileStr);
955
+ }
956
+ async consumeArchiveTypes(options) {
957
+ const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options);
958
+ const downloadPromises = Object.entries(mapRemotesToDownload).map(async (item) => {
959
+ const remoteInfo = item[1];
960
+ if (!this.remoteAliasMap[remoteInfo.alias]) {
961
+ const requiredRemoteInfo = await this.requestRemoteManifest(remoteInfo, hostOptions);
962
+ this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
963
+ }
964
+ return this.consumeTargetRemotes(hostOptions, this.remoteAliasMap[remoteInfo.alias]);
965
+ });
966
+ return {
967
+ hostOptions,
968
+ downloadPromisesResult: await Promise.allSettled(downloadPromises)
969
+ };
970
+ }
971
+ async consumeTypes() {
972
+ try {
973
+ const { options } = this;
974
+ if (!options.host) throw new Error("options.host is required if you want to consumeTypes");
975
+ const { mapRemotesToDownload } = retrieveHostConfig(options.host);
976
+ if (!Object.keys(mapRemotesToDownload).length) return;
977
+ const { downloadPromisesResult, hostOptions } = await this.consumeArchiveTypes(options.host);
978
+ if (hostOptions.consumeAPITypes) {
979
+ await Promise.all(downloadPromisesResult.map(async (item) => {
980
+ if (item.status === "rejected" || !item.value) return;
981
+ const [alias, destinationPath] = item.value;
982
+ const remoteInfo = this.remoteAliasMap[alias];
983
+ if (!remoteInfo) return;
984
+ await this.downloadAPITypes(remoteInfo, destinationPath, hostOptions);
985
+ }));
986
+ this.consumeAPITypes(hostOptions);
987
+ }
988
+ logger$1.success("Federated types extraction completed");
989
+ } catch (err) {
990
+ if (this.options.host?.abortOnError === false) fileLog(`Unable to consume federated types, ${err}`, "consumeTypes", "error");
991
+ else throw err;
992
+ }
993
+ }
994
+ async updateTypes(options) {
995
+ try {
996
+ const { remoteName, updateMode, remoteTarPath, remoteInfo: updatedRemoteInfo, once } = options;
997
+ const hostName = this.options?.host?.moduleFederationConfig?.name;
998
+ fileLog(`options: ${JSON.stringify(options, null, 2)};\nhostName: ${hostName}`, "updateTypes", "info");
999
+ if (updateMode === UpdateMode.POSITIVE && remoteName === hostName) {
1000
+ if (!this.options.remote) return;
1001
+ await this.generateTypes();
1002
+ } else {
1003
+ const { remoteAliasMap } = this;
1004
+ if (!this.options.host) return;
1005
+ const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(this.options.host);
1006
+ const loadedRemoteInfo = Object.values(remoteAliasMap).find((i) => i.name === remoteName);
1007
+ const consumeTypes = async (requiredRemoteInfo) => {
1008
+ fileLog(`consumeTypes start`, "updateTypes", "info");
1009
+ if (!requiredRemoteInfo.zipUrl) throw new Error(`Can not get ${requiredRemoteInfo.name}'s types archive url!`);
1010
+ const [_alias, destinationPath] = await this.consumeTargetRemotes(hostOptions, {
1011
+ ...requiredRemoteInfo,
1012
+ zipUrl: remoteTarPath || requiredRemoteInfo.zipUrl
1013
+ });
1014
+ if (await this.downloadAPITypes(requiredRemoteInfo, destinationPath, hostOptions)) this.consumeAPITypes(hostOptions);
1015
+ fileLog(`consumeTypes end`, "updateTypes", "info");
1016
+ };
1017
+ fileLog(`loadedRemoteInfo: ${JSON.stringify(loadedRemoteInfo, null, 2)}`, "updateTypes", "info");
1018
+ if (!loadedRemoteInfo) {
1019
+ const remoteInfo = Object.values(mapRemotesToDownload).find((item) => {
1020
+ return item.name === remoteName;
1021
+ });
1022
+ fileLog(`remoteInfo: ${JSON.stringify(remoteInfo, null, 2)}`, "updateTypes", "info");
1023
+ if (remoteInfo) {
1024
+ if (!this.remoteAliasMap[remoteInfo.alias]) {
1025
+ const requiredRemoteInfo = await this.requestRemoteManifest(remoteInfo, hostOptions);
1026
+ this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
1027
+ }
1028
+ await consumeTypes(this.remoteAliasMap[remoteInfo.alias]);
1029
+ } else if (updatedRemoteInfo) {
1030
+ const consumeDynamicRemoteTypes = async () => {
1031
+ await consumeTypes(this.updatedRemoteInfos[updatedRemoteInfo.name]);
1032
+ };
1033
+ if (!this.updatedRemoteInfos[updatedRemoteInfo.name]) {
1034
+ const parsedRemoteInfo = retrieveRemoteInfo({
1035
+ hostOptions,
1036
+ remoteAlias: updatedRemoteInfo.alias || updatedRemoteInfo.name,
1037
+ remote: updatedRemoteInfo.url
1038
+ });
1039
+ fileLog(`start request manifest`, "consumeTypes", "info");
1040
+ this.updatedRemoteInfos[updatedRemoteInfo.name] = await this.requestRemoteManifest(parsedRemoteInfo, hostOptions);
1041
+ fileLog(`end request manifest, this.updatedRemoteInfos[updatedRemoteInfo.name]: ${JSON.stringify(this.updatedRemoteInfos[updatedRemoteInfo.name], null, 2)}`, "updateTypes", "info");
1042
+ await consumeDynamicRemoteTypes();
1043
+ }
1044
+ if (!once && this.updatedRemoteInfos[updatedRemoteInfo.name]) await consumeDynamicRemoteTypes();
1045
+ }
1046
+ } else await consumeTypes(loadedRemoteInfo);
1047
+ }
1048
+ } catch (err) {
1049
+ fileLog(`updateTypes fail, ${err}`, "updateTypes", "error");
1050
+ }
1051
+ }
1052
+ };
1053
+
1054
+ //#endregion
1055
+ //#region src/core/lib/utils.ts
1056
+ function getDTSManagerConstructor(implementation) {
1057
+ if (implementation) {
1058
+ const NewConstructor = __require(implementation);
1059
+ return NewConstructor.default ? NewConstructor.default : NewConstructor;
1060
+ }
1061
+ return DTSManager;
1062
+ }
1063
+ const validateOptions = (options) => {
1064
+ if (!options.moduleFederationConfig) throw new Error("moduleFederationConfig is required");
1065
+ };
1066
+ function retrieveTypesAssetsInfo(options) {
1067
+ let apiTypesPath = "";
1068
+ let zipTypesPath = "";
1069
+ try {
1070
+ const { tsConfig, remoteOptions, mapComponentsToExpose } = retrieveRemoteConfig(options);
1071
+ if (!Object.keys(mapComponentsToExpose).length || !tsConfig.files.length) return {
1072
+ apiTypesPath,
1073
+ zipTypesPath,
1074
+ zipName: "",
1075
+ apiFileName: ""
1076
+ };
1077
+ zipTypesPath = retrieveTypesZipPath(retrieveMfTypesPath(tsConfig, remoteOptions), remoteOptions);
1078
+ if (remoteOptions.generateAPITypes) apiTypesPath = retrieveMfAPITypesPath(tsConfig, remoteOptions);
1079
+ return {
1080
+ apiTypesPath,
1081
+ zipTypesPath,
1082
+ zipName: path.basename(zipTypesPath),
1083
+ apiFileName: path.basename(apiTypesPath)
1084
+ };
1085
+ } catch (err) {
1086
+ console.error(ansiColors.red(`Unable to compile federated types, ${err}`));
1087
+ return {
1088
+ apiTypesPath: "",
1089
+ zipTypesPath: "",
1090
+ zipName: "",
1091
+ apiFileName: ""
1092
+ };
1093
+ }
1094
+ }
1095
+ function isDebugMode() {
1096
+ return Boolean(process.env["FEDERATION_DEBUG"]) || process.env["NODE_ENV"] === "test";
1097
+ }
1098
+ const isTSProject = (dtsOptions, context = process.cwd()) => {
1099
+ if (dtsOptions === false) return false;
1100
+ try {
1101
+ let filepath = "";
1102
+ if (typeof dtsOptions === "object" && dtsOptions.tsConfigPath) filepath = dtsOptions.tsConfigPath;
1103
+ else filepath = path.resolve(context, "./tsconfig.json");
1104
+ if (!path.isAbsolute(filepath)) filepath = path.resolve(context, filepath);
1105
+ return fs.existsSync(filepath);
1106
+ } catch (err) {
1107
+ return false;
1108
+ }
1109
+ };
1110
+ function cloneDeepOptions(options) {
1111
+ const excludeKeys = ["manifest", "async"];
1112
+ return cloneDeepWith(options, (value, key) => {
1113
+ if (typeof key === "string" && excludeKeys.includes(key)) return false;
1114
+ if (typeof value === "function") return false;
1115
+ if (key === "extractThirdParty" && Array.isArray(value)) return value.map((item) => {
1116
+ return item.toString();
1117
+ });
1118
+ });
1119
+ }
1120
+ const getEnvHeaders = () => {
1121
+ const headersStr = getProcessEnv()["MF_ENV_HEADERS"] || "{}";
1122
+ return { ...JSON.parse(headersStr) };
1123
+ };
1124
+ async function axiosGet(url, config) {
1125
+ const httpAgent = new http.Agent({ family: config?.family ?? 4 });
1126
+ const httpsAgent = new https.Agent({ family: config?.family ?? 4 });
1127
+ return axios.get(url, {
1128
+ httpAgent,
1129
+ httpsAgent,
1130
+ headers: getEnvHeaders(),
1131
+ ...config,
1132
+ timeout: config?.timeout || 6e4
1133
+ });
1134
+ }
1135
+
1136
+ //#endregion
1137
+ //#region src/core/configurations/remotePlugin.ts
1138
+ const defaultOptions = {
1139
+ tsConfigPath: "./tsconfig.json",
1140
+ typesFolder: "@mf-types",
1141
+ compiledTypesFolder: "compiled-types",
1142
+ hostRemoteTypesFolder: "@mf-types",
1143
+ deleteTypesFolder: true,
1144
+ additionalFilesToCompile: [],
1145
+ compilerInstance: "tsc",
1146
+ compileInChildProcess: false,
1147
+ implementation: "",
1148
+ generateAPITypes: false,
1149
+ context: process.cwd(),
1150
+ abortOnError: true,
1151
+ extractRemoteTypes: false,
1152
+ extractThirdParty: false,
1153
+ outputDir: "",
1154
+ deleteTsConfig: true
1155
+ };
1156
+ function getEffectiveRootDir(parsedCommandLine) {
1157
+ const compilerOptions = parsedCommandLine.options;
1158
+ if (compilerOptions.rootDir) return compilerOptions.rootDir;
1159
+ const files = parsedCommandLine.fileNames;
1160
+ if (files.length > 0) return files.map((file) => dirname(file)).reduce((commonPath, fileDir) => {
1161
+ while (!fileDir.startsWith(commonPath)) commonPath = dirname(commonPath);
1162
+ return commonPath;
1163
+ }, files[0]);
1164
+ throw new Error("Can not get effective rootDir, please set compilerOptions.rootDir !");
1165
+ }
1166
+ const getDependentFiles = (rootFiles, configContent, rootDir) => {
1167
+ const dependentFiles = typescript.createProgram(rootFiles, configContent.options).getSourceFiles().map((file) => file.fileName).filter((file) => !file.endsWith(".d.ts") && file.startsWith(rootDir));
1168
+ return dependentFiles.length ? dependentFiles : rootFiles;
1169
+ };
1170
+ const readTsConfig = ({ tsConfigPath, typesFolder, compiledTypesFolder, context, additionalFilesToCompile, outputDir }, mapComponentsToExpose) => {
1171
+ const resolvedTsConfigPath = resolve(context, tsConfigPath);
1172
+ const readResult = typescript.readConfigFile(resolvedTsConfigPath, typescript.sys.readFile);
1173
+ if (readResult.error) throw new Error(readResult.error.messageText.toString());
1174
+ const rawTsConfigJson = readResult.config;
1175
+ const configContent = typescript.parseJsonConfigFileContent(rawTsConfigJson, typescript.sys, dirname(resolvedTsConfigPath));
1176
+ const rootDir = getEffectiveRootDir(configContent);
1177
+ const defaultCompilerOptions = {
1178
+ rootDir,
1179
+ emitDeclarationOnly: true,
1180
+ noEmit: false,
1181
+ declaration: true,
1182
+ outDir: resolve(context, outputDir || configContent.options.outDir || "dist", typesFolder, compiledTypesFolder)
1183
+ };
1184
+ rawTsConfigJson.compilerOptions = rawTsConfigJson.compilerOptions || {};
1185
+ rawTsConfigJson.compilerOptions = {
1186
+ incremental: true,
1187
+ tsBuildInfoFile: resolve(context, "node_modules/.cache/mf-types/.tsbuildinfo"),
1188
+ ...rawTsConfigJson.compilerOptions,
1189
+ ...defaultCompilerOptions
1190
+ };
1191
+ const { paths, baseUrl, ...restCompilerOptions } = rawTsConfigJson.compilerOptions || {};
1192
+ rawTsConfigJson.compilerOptions = restCompilerOptions;
1193
+ const outDirWithoutTypesFolder = resolve(context, outputDir || configContent.options.outDir || "dist");
1194
+ const excludeExtensions = [".mdx", ".md"];
1195
+ const filesToCompile = [...getDependentFiles([...Object.values(mapComponentsToExpose), ...additionalFilesToCompile].filter((filename) => !excludeExtensions.some((ext) => filename.endsWith(ext))), configContent, rootDir), ...configContent.fileNames.filter((filename) => filename.endsWith(".d.ts") && !filename.startsWith(outDirWithoutTypesFolder))];
1196
+ rawTsConfigJson.include = [];
1197
+ rawTsConfigJson.files = [...new Set(filesToCompile)];
1198
+ rawTsConfigJson.exclude = [];
1199
+ "references" in rawTsConfigJson && delete rawTsConfigJson.references;
1200
+ rawTsConfigJson.extends = resolvedTsConfigPath;
1201
+ if (rawTsConfigJson.compilerOptions.declarationDir) delete rawTsConfigJson.compilerOptions.declarationDir;
1202
+ return rawTsConfigJson;
1203
+ };
1204
+ const TS_EXTENSIONS = [
1205
+ "ts",
1206
+ "tsx",
1207
+ "vue",
1208
+ "svelte"
1209
+ ];
1210
+ const resolveWithExtension = (exposedPath, context) => {
1211
+ if (extname(exposedPath)) return resolve(context, exposedPath);
1212
+ for (const extension of TS_EXTENSIONS) {
1213
+ const exposedPathWithExtension = resolve(context, `${exposedPath}.${extension}`);
1214
+ if (existsSync(exposedPathWithExtension)) return exposedPathWithExtension;
1215
+ }
1216
+ };
1217
+ const resolveExposes = (remoteOptions) => {
1218
+ return utils.parseOptions(remoteOptions.moduleFederationConfig.exposes || {}, (item, key) => ({
1219
+ exposePath: Array.isArray(item) ? item[0] : item,
1220
+ key
1221
+ }), (item, key) => ({
1222
+ exposePath: Array.isArray(item.import) ? item.import[0] : item.import[0],
1223
+ key
1224
+ })).reduce((accumulator, item) => {
1225
+ const { exposePath, key } = item[1];
1226
+ accumulator[key] = resolveWithExtension(exposePath, remoteOptions.context) || resolveWithExtension(join(exposePath, "index"), remoteOptions.context) || exposePath;
1227
+ return accumulator;
1228
+ }, {});
1229
+ };
1230
+ const retrieveRemoteConfig = (options) => {
1231
+ validateOptions(options);
1232
+ const remoteOptions = {
1233
+ ...defaultOptions,
1234
+ ...options
1235
+ };
1236
+ const mapComponentsToExpose = resolveExposes(remoteOptions);
1237
+ const tsConfig = readTsConfig(remoteOptions, mapComponentsToExpose);
1238
+ if (tsConfig.compilerOptions.incremental && tsConfig.compilerOptions.tsBuildInfoFile && options.deleteTypesFolder !== true) remoteOptions.deleteTypesFolder = false;
1239
+ return {
1240
+ tsConfig,
1241
+ mapComponentsToExpose,
1242
+ remoteOptions
1243
+ };
1244
+ };
1245
+
1246
+ //#endregion
1247
+ //#region src/core/lib/generateTypes.ts
1248
+ async function generateTypes(options) {
1249
+ return new (getDTSManagerConstructor(options.remote?.implementation))(options).generateTypes();
1250
+ }
1251
+
1252
+ //#endregion
1253
+ //#region src/core/rpc/types.ts
1254
+ let RpcGMCallTypes = /* @__PURE__ */ function(RpcGMCallTypes) {
1255
+ RpcGMCallTypes["CALL"] = "mf_call";
1256
+ RpcGMCallTypes["RESOLVE"] = "mf_resolve";
1257
+ RpcGMCallTypes["REJECT"] = "mf_reject";
1258
+ RpcGMCallTypes["EXIT"] = "mf_exit";
1259
+ return RpcGMCallTypes;
1260
+ }({});
1261
+
1262
+ //#endregion
1263
+ //#region src/core/rpc/expose-rpc.ts
1264
+ function exposeRpc(fn) {
1265
+ const sendMessage = (message) => new Promise((resolve, reject) => {
1266
+ if (!process$1.send) reject(/* @__PURE__ */ new Error(`Process ${process$1.pid} doesn't have IPC channels`));
1267
+ else if (!process$1.connected) reject(/* @__PURE__ */ new Error(`Process ${process$1.pid} doesn't have open IPC channels`));
1268
+ else process$1.send(message, void 0, void 0, (error) => {
1269
+ if (error) reject(error);
1270
+ else resolve(void 0);
1271
+ });
1272
+ });
1273
+ const handleMessage = async (message) => {
1274
+ if (message.type === RpcGMCallTypes.CALL) {
1275
+ if (!process$1.send) return;
1276
+ let value, error;
1277
+ try {
1278
+ value = await fn(...message.args);
1279
+ } catch (fnError) {
1280
+ error = fnError;
1281
+ }
1282
+ try {
1283
+ if (error) await sendMessage({
1284
+ type: RpcGMCallTypes.REJECT,
1285
+ id: message.id,
1286
+ error
1287
+ });
1288
+ else await sendMessage({
1289
+ type: RpcGMCallTypes.RESOLVE,
1290
+ id: message.id,
1291
+ value
1292
+ });
1293
+ } catch (sendError) {
1294
+ if (error) {
1295
+ if (error instanceof Error) console.error(error);
1296
+ }
1297
+ console.error(sendError);
1298
+ }
1299
+ }
1300
+ };
1301
+ process$1.on("message", handleMessage);
1302
+ }
1303
+
1304
+ //#endregion
1305
+ export { retrieveMfTypesPath as _, cloneDeepOptions as a, ModuleFederationDevServer as b, isTSProject as c, DTSManager as d, HOST_API_TYPES_FILE_NAME as f, retrieveTypesZipPath as g, retrieveHostConfig as h, retrieveRemoteConfig as i, retrieveTypesAssetsInfo as l, REMOTE_API_TYPES_FILE_NAME as m, RpcGMCallTypes as n, getDTSManagerConstructor as o, REMOTE_ALIAS_IDENTIFIER as p, generateTypes as r, isDebugMode as s, exposeRpc as t, validateOptions as u, retrieveOriginalOutDir as v, __exportAll as x, createHttpServer as y };