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