@module-federation/dts-plugin 0.2.8 → 0.3.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.
@@ -0,0 +1,283 @@
1
+ import {
2
+ RpcGMCallTypes,
3
+ cloneDeepOptions,
4
+ exposeRpc,
5
+ getDTSManagerConstructor,
6
+ isDebugMode
7
+ } from "./chunk-SFEGBRA3.js";
8
+ import {
9
+ __async,
10
+ __export,
11
+ __name,
12
+ __publicField,
13
+ __spreadProps,
14
+ __spreadValues
15
+ } from "./chunk-6DND574L.js";
16
+
17
+ // packages/dts-plugin/src/core/lib/DtsWorker.ts
18
+ import path from "path";
19
+
20
+ // packages/dts-plugin/src/core/rpc/index.ts
21
+ var rpc_exports = {};
22
+ __export(rpc_exports, {
23
+ RpcExitError: () => RpcExitError,
24
+ RpcGMCallTypes: () => RpcGMCallTypes,
25
+ createRpcWorker: () => createRpcWorker,
26
+ exposeRpc: () => exposeRpc,
27
+ getRpcWorkerData: () => getRpcWorkerData,
28
+ wrapRpc: () => wrapRpc
29
+ });
30
+
31
+ // packages/dts-plugin/src/core/rpc/rpc-error.ts
32
+ var _a;
33
+ var RpcExitError = (_a = class extends Error {
34
+ constructor(message, code, signal) {
35
+ super(message);
36
+ __publicField(this, "code");
37
+ __publicField(this, "signal");
38
+ this.code = code;
39
+ this.signal = signal;
40
+ this.name = "RpcExitError";
41
+ }
42
+ }, __name(_a, "RpcExitError"), _a);
43
+
44
+ // packages/dts-plugin/src/core/rpc/wrap-rpc.ts
45
+ function createControlledPromise() {
46
+ let resolve = /* @__PURE__ */ __name(() => void 0, "resolve");
47
+ let reject = /* @__PURE__ */ __name(() => void 0, "reject");
48
+ const promise = new Promise((aResolve, aReject) => {
49
+ resolve = aResolve;
50
+ reject = aReject;
51
+ });
52
+ return {
53
+ promise,
54
+ resolve,
55
+ reject
56
+ };
57
+ }
58
+ __name(createControlledPromise, "createControlledPromise");
59
+ function wrapRpc(childProcess, options) {
60
+ return (...args) => __async(this, null, function* () {
61
+ if (!childProcess.send) {
62
+ throw new Error(`Process ${childProcess.pid} doesn't have IPC channels`);
63
+ } else if (!childProcess.connected) {
64
+ throw new Error(`Process ${childProcess.pid} doesn't have open IPC channels`);
65
+ }
66
+ const { id, once } = options;
67
+ const { promise: resultPromise, resolve: resolveResult, reject: rejectResult } = createControlledPromise();
68
+ const { promise: sendPromise, resolve: resolveSend, reject: rejectSend } = createControlledPromise();
69
+ const handleMessage = /* @__PURE__ */ __name((message) => {
70
+ if ((message == null ? void 0 : message.id) === id) {
71
+ if (message.type === RpcGMCallTypes.RESOLVE) {
72
+ resolveResult(message.value);
73
+ } else if (message.type === RpcGMCallTypes.REJECT) {
74
+ rejectResult(message.error);
75
+ }
76
+ }
77
+ if (once && (childProcess == null ? void 0 : childProcess.kill)) {
78
+ childProcess.kill("SIGTERM");
79
+ }
80
+ }, "handleMessage");
81
+ const handleClose = /* @__PURE__ */ __name((code, signal) => {
82
+ rejectResult(new RpcExitError(code ? `Process ${childProcess.pid} exited with code ${code}${signal ? ` [${signal}]` : ""}` : `Process ${childProcess.pid} exited${signal ? ` [${signal}]` : ""}`, code, signal));
83
+ removeHandlers();
84
+ }, "handleClose");
85
+ const removeHandlers = /* @__PURE__ */ __name(() => {
86
+ childProcess.off("message", handleMessage);
87
+ childProcess.off("close", handleClose);
88
+ }, "removeHandlers");
89
+ if (once) {
90
+ childProcess.once("message", handleMessage);
91
+ } else {
92
+ childProcess.on("message", handleMessage);
93
+ }
94
+ childProcess.on("close", handleClose);
95
+ childProcess.send({
96
+ type: RpcGMCallTypes.CALL,
97
+ id,
98
+ args
99
+ }, (error) => {
100
+ if (error) {
101
+ rejectSend(error);
102
+ removeHandlers();
103
+ } else {
104
+ resolveSend(void 0);
105
+ }
106
+ });
107
+ return sendPromise.then(() => resultPromise);
108
+ });
109
+ }
110
+ __name(wrapRpc, "wrapRpc");
111
+
112
+ // packages/dts-plugin/src/core/rpc/rpc-worker.ts
113
+ import * as child_process from "child_process";
114
+ import * as process2 from "process";
115
+ import { randomUUID } from "crypto";
116
+ var FEDERATION_WORKER_DATA_ENV_KEY = "VMOK_WORKER_DATA_ENV";
117
+ function createRpcWorker(modulePath, data, memoryLimit, once) {
118
+ const options = {
119
+ env: __spreadProps(__spreadValues({}, process2.env), {
120
+ [FEDERATION_WORKER_DATA_ENV_KEY]: JSON.stringify(data || {})
121
+ }),
122
+ stdio: [
123
+ "inherit",
124
+ "inherit",
125
+ "inherit",
126
+ "ipc"
127
+ ],
128
+ serialization: "advanced"
129
+ };
130
+ if (memoryLimit) {
131
+ options.execArgv = [
132
+ `--max-old-space-size=${memoryLimit}`
133
+ ];
134
+ }
135
+ let childProcess, remoteMethod;
136
+ const id = randomUUID();
137
+ const worker = {
138
+ connect(...args) {
139
+ if (childProcess && !childProcess.connected) {
140
+ childProcess.send({
141
+ type: RpcGMCallTypes.EXIT,
142
+ id
143
+ });
144
+ childProcess = void 0;
145
+ remoteMethod = void 0;
146
+ }
147
+ if (!(childProcess == null ? void 0 : childProcess.connected)) {
148
+ childProcess = child_process.fork(modulePath, options);
149
+ remoteMethod = wrapRpc(childProcess, {
150
+ id,
151
+ once
152
+ });
153
+ }
154
+ if (!remoteMethod) {
155
+ return Promise.reject(new Error("Worker is not connected - cannot perform RPC."));
156
+ }
157
+ return remoteMethod(...args);
158
+ },
159
+ terminate() {
160
+ try {
161
+ if (childProcess.connected) {
162
+ childProcess.send({
163
+ type: RpcGMCallTypes.EXIT,
164
+ id
165
+ }, (err) => {
166
+ if (err) {
167
+ console.error("Error sending message:", err);
168
+ }
169
+ });
170
+ }
171
+ } catch (error) {
172
+ if (error.code === "EPIPE") {
173
+ console.error("Pipe closed before message could be sent:", error);
174
+ } else {
175
+ console.error("Unexpected error:", error);
176
+ }
177
+ }
178
+ childProcess = void 0;
179
+ remoteMethod = void 0;
180
+ },
181
+ get connected() {
182
+ return Boolean(childProcess == null ? void 0 : childProcess.connected);
183
+ },
184
+ get process() {
185
+ return childProcess;
186
+ },
187
+ get id() {
188
+ return id;
189
+ }
190
+ };
191
+ return worker;
192
+ }
193
+ __name(createRpcWorker, "createRpcWorker");
194
+ function getRpcWorkerData() {
195
+ return JSON.parse(process2.env[FEDERATION_WORKER_DATA_ENV_KEY] || "{}");
196
+ }
197
+ __name(getRpcWorkerData, "getRpcWorkerData");
198
+
199
+ // packages/dts-plugin/src/core/lib/DtsWorker.ts
200
+ var _DtsWorker = class _DtsWorker {
201
+ constructor(options) {
202
+ __publicField(this, "rpcWorker");
203
+ __publicField(this, "_options");
204
+ __publicField(this, "_res");
205
+ this._options = cloneDeepOptions(options);
206
+ this.removeUnSerializationOptions();
207
+ this.rpcWorker = createRpcWorker(path.resolve(__dirname, "./fork-generate-dts.js"), {}, void 0, true);
208
+ this._res = this.rpcWorker.connect(this._options);
209
+ }
210
+ removeUnSerializationOptions() {
211
+ var _a2, _b, _c, _d, _e, _f, _g, _h;
212
+ if ((_b = (_a2 = this._options.remote) == null ? void 0 : _a2.moduleFederationConfig) == null ? void 0 : _b.manifest) {
213
+ (_d = (_c = this._options.remote) == null ? void 0 : _c.moduleFederationConfig) == null ? true : delete _d.manifest;
214
+ }
215
+ if ((_f = (_e = this._options.host) == null ? void 0 : _e.moduleFederationConfig) == null ? void 0 : _f.manifest) {
216
+ (_h = (_g = this._options.host) == null ? void 0 : _g.moduleFederationConfig) == null ? true : delete _h.manifest;
217
+ }
218
+ }
219
+ get controlledPromise() {
220
+ const ensureChildProcessExit = /* @__PURE__ */ __name(() => {
221
+ var _a2;
222
+ try {
223
+ const pid = (_a2 = this.rpcWorker.process) == null ? void 0 : _a2.pid;
224
+ const rootPid = process.pid;
225
+ if (pid && rootPid !== pid) {
226
+ process.kill(pid, 0);
227
+ }
228
+ } catch (error) {
229
+ if (isDebugMode()) {
230
+ console.error(error);
231
+ }
232
+ }
233
+ }, "ensureChildProcessExit");
234
+ return Promise.resolve(this._res).then(() => {
235
+ this.exit();
236
+ ensureChildProcessExit();
237
+ }).catch((err) => {
238
+ if (isDebugMode()) {
239
+ console.error(err);
240
+ }
241
+ ensureChildProcessExit();
242
+ });
243
+ }
244
+ exit() {
245
+ var _a2;
246
+ try {
247
+ (_a2 = this.rpcWorker) == null ? void 0 : _a2.terminate();
248
+ } catch (err) {
249
+ if (isDebugMode()) {
250
+ console.error(err);
251
+ }
252
+ }
253
+ }
254
+ };
255
+ __name(_DtsWorker, "DtsWorker");
256
+ var DtsWorker = _DtsWorker;
257
+
258
+ // packages/dts-plugin/src/core/lib/generateTypesInChildProcess.ts
259
+ function generateTypesInChildProcess(options) {
260
+ return __async(this, null, function* () {
261
+ const dtsWorker = new DtsWorker(options);
262
+ return dtsWorker.controlledPromise;
263
+ });
264
+ }
265
+ __name(generateTypesInChildProcess, "generateTypesInChildProcess");
266
+
267
+ // packages/dts-plugin/src/core/lib/consumeTypes.ts
268
+ function consumeTypes(options) {
269
+ return __async(this, null, function* () {
270
+ var _a2;
271
+ const DTSManagerConstructor = getDTSManagerConstructor((_a2 = options.host) == null ? void 0 : _a2.implementation);
272
+ const dtsManager = new DTSManagerConstructor(options);
273
+ yield dtsManager.consumeTypes();
274
+ });
275
+ }
276
+ __name(consumeTypes, "consumeTypes");
277
+
278
+ export {
279
+ rpc_exports,
280
+ DtsWorker,
281
+ generateTypesInChildProcess,
282
+ consumeTypes
283
+ };