@module-federation/dts-plugin 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CHANGELOG.md +8 -0
- package/dist/DTSManagerOptions-c0728719.d.ts +29 -0
- package/dist/DtsWorker-d731dc2b.d.ts +97 -0
- package/dist/core.d.ts +84 -0
- package/dist/core.js +1137 -0
- package/dist/forkDevWorker.d.ts +2 -1
- package/dist/forkDevWorker.js +984 -27
- package/dist/forkGenerateDts.d.ts +9 -0
- package/dist/forkGenerateDts.js +831 -0
- package/dist/index.js +1226 -72
- package/dist/package.json +16 -4
- package/dist/startBroker.js +67 -6
- package/package.json +16 -4
package/dist/forkDevWorker.js
CHANGED
|
@@ -33,8 +33,967 @@ __export(forkDevWorker_exports, {
|
|
|
33
33
|
forkDevWorker: () => forkDevWorker
|
|
34
34
|
});
|
|
35
35
|
module.exports = __toCommonJS(forkDevWorker_exports);
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
|
|
37
|
+
// packages/dts-plugin/src/core/configurations/remotePlugin.ts
|
|
38
|
+
var import_fs2 = require("fs");
|
|
39
|
+
var import_path4 = require("path");
|
|
40
|
+
var import_managers2 = require("@module-federation/managers");
|
|
41
|
+
var import_typescript2 = __toESM(require("typescript"));
|
|
42
|
+
|
|
43
|
+
// packages/dts-plugin/src/core/lib/DTSManager.ts
|
|
44
|
+
var import_ansi_colors3 = __toESM(require("ansi-colors"));
|
|
45
|
+
var import_path3 = __toESM(require("path"));
|
|
46
|
+
var import_promises = require("fs/promises");
|
|
47
|
+
var import_fs = __toESM(require("fs"));
|
|
48
|
+
var import_sdk2 = require("@module-federation/sdk");
|
|
49
|
+
var import_lodash = __toESM(require("lodash.clonedeepwith"));
|
|
50
|
+
|
|
51
|
+
// packages/dts-plugin/src/core/lib/archiveHandler.ts
|
|
52
|
+
var import_adm_zip = __toESM(require("adm-zip"));
|
|
53
|
+
var import_ansi_colors2 = __toESM(require("ansi-colors"));
|
|
54
|
+
var import_axios = __toESM(require("axios"));
|
|
55
|
+
var import_path2 = require("path");
|
|
56
|
+
|
|
57
|
+
// packages/dts-plugin/src/core/lib/typeScriptCompiler.ts
|
|
58
|
+
var import_ansi_colors = __toESM(require("ansi-colors"));
|
|
59
|
+
var import_path = require("path");
|
|
60
|
+
var import_typescript = __toESM(require("typescript"));
|
|
61
|
+
var import_third_party_dts_extractor = require("@module-federation/third-party-dts-extractor");
|
|
62
|
+
var STARTS_WITH_SLASH = /^\//;
|
|
63
|
+
var DEFINITION_FILE_EXTENSION = ".d.ts";
|
|
64
|
+
var reportCompileDiagnostic = (diagnostic) => {
|
|
65
|
+
const { line } = diagnostic.file.getLineAndCharacterOfPosition(
|
|
66
|
+
diagnostic.start
|
|
67
|
+
);
|
|
68
|
+
console.error(
|
|
69
|
+
import_ansi_colors.default.red(
|
|
70
|
+
`TS Error ${diagnostic.code}':' ${import_typescript.default.flattenDiagnosticMessageText(
|
|
71
|
+
diagnostic.messageText,
|
|
72
|
+
import_typescript.default.sys.newLine
|
|
73
|
+
)}`
|
|
74
|
+
)
|
|
75
|
+
);
|
|
76
|
+
console.error(
|
|
77
|
+
import_ansi_colors.default.red(
|
|
78
|
+
` at ${diagnostic.file.fileName}:${line + 1} typescript.sys.newLine`
|
|
79
|
+
)
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
var retrieveMfTypesPath = (tsConfig, remoteOptions) => (0, import_path.normalize)(tsConfig.outDir.replace(remoteOptions.compiledTypesFolder, ""));
|
|
83
|
+
var retrieveOriginalOutDir = (tsConfig, remoteOptions) => (0, import_path.normalize)(
|
|
84
|
+
tsConfig.outDir.replace(remoteOptions.compiledTypesFolder, "").replace(remoteOptions.typesFolder, "")
|
|
85
|
+
);
|
|
86
|
+
var retrieveMfAPITypesPath = (tsConfig, remoteOptions) => (0, import_path.join)(
|
|
87
|
+
retrieveOriginalOutDir(tsConfig, remoteOptions),
|
|
88
|
+
`${remoteOptions.typesFolder}.d.ts`
|
|
89
|
+
);
|
|
90
|
+
var createHost = (mapComponentsToExpose, tsConfig, remoteOptions, cb) => {
|
|
91
|
+
const host = import_typescript.default.createCompilerHost(tsConfig);
|
|
92
|
+
const originalWriteFile = host.writeFile;
|
|
93
|
+
const mapExposeToEntry = Object.fromEntries(
|
|
94
|
+
Object.entries(mapComponentsToExpose).map((entry) => entry.reverse())
|
|
95
|
+
);
|
|
96
|
+
const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
97
|
+
host.writeFile = (filepath, text, writeOrderByteMark, onError, sourceFiles, data) => {
|
|
98
|
+
originalWriteFile(
|
|
99
|
+
filepath,
|
|
100
|
+
text,
|
|
101
|
+
writeOrderByteMark,
|
|
102
|
+
onError,
|
|
103
|
+
sourceFiles,
|
|
104
|
+
data
|
|
105
|
+
);
|
|
106
|
+
for (const sourceFile of sourceFiles || []) {
|
|
107
|
+
const sourceEntry = mapExposeToEntry[sourceFile.fileName];
|
|
108
|
+
if (sourceEntry) {
|
|
109
|
+
const mfeTypeEntry = (0, import_path.join)(
|
|
110
|
+
mfTypePath,
|
|
111
|
+
`${sourceEntry}${DEFINITION_FILE_EXTENSION}`
|
|
112
|
+
);
|
|
113
|
+
const mfeTypeEntryDirectory = (0, import_path.dirname)(mfeTypeEntry);
|
|
114
|
+
const relativePathToOutput = (0, import_path.relative)(mfeTypeEntryDirectory, filepath).replace(DEFINITION_FILE_EXTENSION, "").replace(STARTS_WITH_SLASH, "");
|
|
115
|
+
originalWriteFile(
|
|
116
|
+
mfeTypeEntry,
|
|
117
|
+
`export * from './${relativePathToOutput}';
|
|
118
|
+
export { default } from './${relativePathToOutput}';`,
|
|
119
|
+
writeOrderByteMark
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
cb(text);
|
|
124
|
+
};
|
|
125
|
+
return host;
|
|
126
|
+
};
|
|
127
|
+
var createVueTscProgram = (programOptions) => {
|
|
128
|
+
const vueTypescript = require("vue-tsc");
|
|
129
|
+
return vueTypescript.createProgram(programOptions);
|
|
130
|
+
};
|
|
131
|
+
var createProgram = (remoteOptions, programOptions) => {
|
|
132
|
+
switch (remoteOptions.compilerInstance) {
|
|
133
|
+
case "vue-tsc":
|
|
134
|
+
return createVueTscProgram(programOptions);
|
|
135
|
+
case "tsc":
|
|
136
|
+
default:
|
|
137
|
+
return import_typescript.default.createProgram(programOptions);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var compileTs = (mapComponentsToExpose, tsConfig, remoteOptions) => {
|
|
141
|
+
const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
142
|
+
const thirdPartyExtractor = new import_third_party_dts_extractor.ThirdPartyExtractor(
|
|
143
|
+
(0, import_path.resolve)(mfTypePath, "node_modules"),
|
|
144
|
+
remoteOptions.context
|
|
145
|
+
);
|
|
146
|
+
const cb = remoteOptions.extractThirdParty ? thirdPartyExtractor.collectPkgs.bind(thirdPartyExtractor) : () => void 0;
|
|
147
|
+
const tsHost = createHost(mapComponentsToExpose, tsConfig, remoteOptions, cb);
|
|
148
|
+
const filesToCompile = [
|
|
149
|
+
...Object.values(mapComponentsToExpose),
|
|
150
|
+
...remoteOptions.additionalFilesToCompile
|
|
151
|
+
];
|
|
152
|
+
const programOptions = {
|
|
153
|
+
rootNames: filesToCompile,
|
|
154
|
+
host: tsHost,
|
|
155
|
+
options: tsConfig
|
|
156
|
+
};
|
|
157
|
+
const tsProgram = createProgram(remoteOptions, programOptions);
|
|
158
|
+
const { diagnostics = [] } = tsProgram.emit();
|
|
159
|
+
diagnostics.forEach(reportCompileDiagnostic);
|
|
160
|
+
if (remoteOptions.extractThirdParty) {
|
|
161
|
+
thirdPartyExtractor.copyDts();
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// packages/dts-plugin/src/core/lib/archiveHandler.ts
|
|
166
|
+
var retrieveTypesZipPath = (mfTypesPath, remoteOptions) => (0, import_path2.join)(
|
|
167
|
+
mfTypesPath.replace(remoteOptions.typesFolder, ""),
|
|
168
|
+
`${remoteOptions.typesFolder}.zip`
|
|
169
|
+
);
|
|
170
|
+
var createTypesArchive = async (tsConfig, remoteOptions) => {
|
|
171
|
+
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
172
|
+
const zip = new import_adm_zip.default();
|
|
173
|
+
zip.addLocalFolder(mfTypesPath);
|
|
174
|
+
return zip.writeZipPromise(retrieveTypesZipPath(mfTypesPath, remoteOptions));
|
|
175
|
+
};
|
|
176
|
+
var downloadErrorLogger = (destinationFolder, fileToDownload) => (reason) => {
|
|
177
|
+
throw {
|
|
178
|
+
...reason,
|
|
179
|
+
message: `Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${reason.message}'`
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
var retrieveTypesArchiveDestinationPath = (hostOptions, destinationFolder) => {
|
|
183
|
+
return (0, import_path2.resolve)(
|
|
184
|
+
hostOptions.context,
|
|
185
|
+
hostOptions.typesFolder,
|
|
186
|
+
destinationFolder
|
|
187
|
+
);
|
|
188
|
+
};
|
|
189
|
+
var downloadTypesArchive = (hostOptions) => {
|
|
190
|
+
let retries = 0;
|
|
191
|
+
return async ([destinationFolder, fileToDownload]) => {
|
|
192
|
+
const destinationPath = retrieveTypesArchiveDestinationPath(
|
|
193
|
+
hostOptions,
|
|
194
|
+
destinationFolder
|
|
195
|
+
);
|
|
196
|
+
while (retries++ < hostOptions.maxRetries) {
|
|
197
|
+
try {
|
|
198
|
+
const url = replaceLocalhost(fileToDownload);
|
|
199
|
+
const response = await import_axios.default.get(url, { responseType: "arraybuffer" }).catch(downloadErrorLogger(destinationFolder, url));
|
|
200
|
+
const zip = new import_adm_zip.default(Buffer.from(response.data));
|
|
201
|
+
zip.extractAllTo(destinationPath, true);
|
|
202
|
+
return [destinationFolder, destinationPath];
|
|
203
|
+
} catch (error2) {
|
|
204
|
+
if (isDebugMode()) {
|
|
205
|
+
console.error(
|
|
206
|
+
import_ansi_colors2.default.red(
|
|
207
|
+
`Error during types archive download: ${(error2 == null ? void 0 : error2.message) || "unknown error"}`
|
|
208
|
+
)
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
if (retries >= hostOptions.maxRetries) {
|
|
212
|
+
if (hostOptions.abortOnError !== false) {
|
|
213
|
+
throw error2;
|
|
214
|
+
}
|
|
215
|
+
return void 0;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
// packages/dts-plugin/src/core/configurations/hostPlugin.ts
|
|
223
|
+
var import_sdk = require("@module-federation/sdk");
|
|
224
|
+
var import_managers = require("@module-federation/managers");
|
|
225
|
+
var defaultOptions = {
|
|
226
|
+
typesFolder: "@mf-types",
|
|
227
|
+
remoteTypesFolder: "@mf-types",
|
|
228
|
+
deleteTypesFolder: true,
|
|
229
|
+
maxRetries: 3,
|
|
230
|
+
implementation: "",
|
|
231
|
+
context: process.cwd(),
|
|
232
|
+
abortOnError: true,
|
|
233
|
+
consumeAPITypes: false
|
|
234
|
+
};
|
|
235
|
+
var buildZipUrl = (hostOptions, url) => {
|
|
236
|
+
const remoteUrl = new URL(url);
|
|
237
|
+
if (remoteUrl.href.includes(import_sdk.MANIFEST_EXT)) {
|
|
238
|
+
return void 0;
|
|
239
|
+
}
|
|
240
|
+
const pathnameWithoutEntry = remoteUrl.pathname.split("/").slice(0, -1).join("/");
|
|
241
|
+
remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.remoteTypesFolder}.zip`;
|
|
242
|
+
return remoteUrl.href;
|
|
243
|
+
};
|
|
244
|
+
var buildApiTypeUrl = (zipUrl) => {
|
|
245
|
+
if (!zipUrl) {
|
|
246
|
+
return void 0;
|
|
247
|
+
}
|
|
248
|
+
return zipUrl.replace(".zip", ".d.ts");
|
|
249
|
+
};
|
|
250
|
+
var retrieveRemoteInfo = (options) => {
|
|
251
|
+
const { hostOptions, remoteAlias, remote } = options;
|
|
252
|
+
const parsedInfo = (0, import_sdk.parseEntry)(remote, void 0, "@");
|
|
253
|
+
const url = "entry" in parsedInfo ? parsedInfo.entry : parsedInfo.name === remote ? remote : "";
|
|
254
|
+
const zipUrl = url ? buildZipUrl(hostOptions, url) : "";
|
|
255
|
+
return {
|
|
256
|
+
name: parsedInfo.name || remoteAlias,
|
|
257
|
+
url,
|
|
258
|
+
zipUrl,
|
|
259
|
+
apiTypeUrl: buildApiTypeUrl(zipUrl),
|
|
260
|
+
alias: remoteAlias
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
var resolveRemotes = (hostOptions) => {
|
|
264
|
+
const parsedOptions = import_managers.utils.parseOptions(
|
|
265
|
+
hostOptions.moduleFederationConfig.remotes || {},
|
|
266
|
+
(item, key) => ({
|
|
267
|
+
remote: Array.isArray(item) ? item[0] : item,
|
|
268
|
+
key
|
|
269
|
+
}),
|
|
270
|
+
(item, key) => ({
|
|
271
|
+
remote: Array.isArray(item.external) ? item.external[0] : item.external,
|
|
272
|
+
key
|
|
273
|
+
})
|
|
274
|
+
);
|
|
275
|
+
return parsedOptions.reduce(
|
|
276
|
+
(accumulator, item) => {
|
|
277
|
+
const { key, remote } = item[1];
|
|
278
|
+
accumulator[key] = retrieveRemoteInfo({
|
|
279
|
+
hostOptions,
|
|
280
|
+
remoteAlias: key,
|
|
281
|
+
remote
|
|
282
|
+
});
|
|
283
|
+
return accumulator;
|
|
284
|
+
},
|
|
285
|
+
{}
|
|
286
|
+
);
|
|
287
|
+
};
|
|
288
|
+
var retrieveHostConfig = (options) => {
|
|
289
|
+
validateOptions(options);
|
|
290
|
+
const hostOptions = { ...defaultOptions, ...options };
|
|
291
|
+
const mapRemotesToDownload = resolveRemotes(hostOptions);
|
|
292
|
+
return {
|
|
293
|
+
hostOptions,
|
|
294
|
+
mapRemotesToDownload
|
|
295
|
+
};
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
// packages/dts-plugin/src/core/constant.ts
|
|
299
|
+
var REMOTE_ALIAS_IDENTIFIER = "REMOTE_ALIAS_IDENTIFIER";
|
|
300
|
+
var REMOTE_API_TYPES_FILE_NAME = "apis.d.ts";
|
|
301
|
+
var HOST_API_TYPES_FILE_NAME = "index.d.ts";
|
|
302
|
+
|
|
303
|
+
// packages/dts-plugin/src/core/lib/DTSManager.ts
|
|
304
|
+
var import_axios2 = __toESM(require("axios"));
|
|
305
|
+
var DTSManager = class {
|
|
306
|
+
constructor(options) {
|
|
307
|
+
this.options = (0, import_lodash.default)(options, (_value, key) => {
|
|
308
|
+
if (key === "manifest") {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
this.runtimePkgs = [
|
|
313
|
+
"@module-federation/runtime",
|
|
314
|
+
"@module-federation/runtime-tools"
|
|
315
|
+
];
|
|
316
|
+
this.loadedRemoteAPIAlias = [];
|
|
317
|
+
this.remoteAliasMap = {};
|
|
318
|
+
this.extraOptions = (options == null ? void 0 : options.extraOptions) || {};
|
|
319
|
+
}
|
|
320
|
+
generateAPITypes(mapComponentsToExpose) {
|
|
321
|
+
const exposePaths = /* @__PURE__ */ new Set();
|
|
322
|
+
const packageType = Object.keys(mapComponentsToExpose).reduce(
|
|
323
|
+
(sum, exposeKey) => {
|
|
324
|
+
const exposePath = import_path3.default.join(REMOTE_ALIAS_IDENTIFIER, exposeKey);
|
|
325
|
+
exposePaths.add(`'${exposePath}'`);
|
|
326
|
+
const curType = `T extends '${exposePath}' ? typeof import('${exposePath}') :`;
|
|
327
|
+
sum = curType + sum;
|
|
328
|
+
return sum;
|
|
329
|
+
},
|
|
330
|
+
"any;"
|
|
331
|
+
);
|
|
332
|
+
const exposePathKeys = [...exposePaths].join(" | ");
|
|
333
|
+
return `
|
|
334
|
+
export type RemoteKeys = ${exposePathKeys};
|
|
335
|
+
type PackageType<T> = ${packageType}`;
|
|
336
|
+
}
|
|
337
|
+
async extractRemoteTypes(options) {
|
|
338
|
+
const { remoteOptions, tsConfig } = options;
|
|
339
|
+
if (!remoteOptions.extractRemoteTypes) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
let hasRemotes = false;
|
|
343
|
+
const remotes = remoteOptions.moduleFederationConfig.remotes;
|
|
344
|
+
if (remotes) {
|
|
345
|
+
if (Array.isArray(remotes)) {
|
|
346
|
+
hasRemotes = Boolean(remotes.length);
|
|
347
|
+
} else if (typeof remotes === "object") {
|
|
348
|
+
hasRemotes = Boolean(Object.keys(remotes).length);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
352
|
+
if (hasRemotes) {
|
|
353
|
+
const tempHostOptions = {
|
|
354
|
+
moduleFederationConfig: remoteOptions.moduleFederationConfig,
|
|
355
|
+
typesFolder: import_path3.default.join(mfTypesPath, "node_modules"),
|
|
356
|
+
remoteTypesFolder: (remoteOptions == null ? void 0 : remoteOptions.hostRemoteTypesFolder) || remoteOptions.typesFolder,
|
|
357
|
+
deleteTypesFolder: true,
|
|
358
|
+
context: remoteOptions.context,
|
|
359
|
+
implementation: remoteOptions.implementation,
|
|
360
|
+
abortOnError: false
|
|
361
|
+
};
|
|
362
|
+
await this.consumeArchiveTypes(tempHostOptions);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
async generateTypes() {
|
|
366
|
+
var _a;
|
|
367
|
+
try {
|
|
368
|
+
const { options } = this;
|
|
369
|
+
if (!options.remote) {
|
|
370
|
+
throw new Error(
|
|
371
|
+
"options.remote is required if you want to generateTypes"
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
const { remoteOptions, tsConfig, mapComponentsToExpose } = retrieveRemoteConfig(options.remote);
|
|
375
|
+
if (!Object.keys(mapComponentsToExpose).length) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
this.extractRemoteTypes({
|
|
379
|
+
remoteOptions,
|
|
380
|
+
tsConfig,
|
|
381
|
+
mapComponentsToExpose
|
|
382
|
+
});
|
|
383
|
+
compileTs(mapComponentsToExpose, tsConfig, remoteOptions);
|
|
384
|
+
await createTypesArchive(tsConfig, remoteOptions);
|
|
385
|
+
let apiTypesPath = "";
|
|
386
|
+
if (remoteOptions.generateAPITypes) {
|
|
387
|
+
const apiTypes = this.generateAPITypes(mapComponentsToExpose);
|
|
388
|
+
apiTypesPath = retrieveMfAPITypesPath(tsConfig, remoteOptions);
|
|
389
|
+
import_fs.default.writeFileSync(apiTypesPath, apiTypes);
|
|
390
|
+
}
|
|
391
|
+
if (remoteOptions.deleteTypesFolder) {
|
|
392
|
+
await (0, import_promises.rm)(retrieveMfTypesPath(tsConfig, remoteOptions), {
|
|
393
|
+
recursive: true,
|
|
394
|
+
force: true
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
console.log(import_ansi_colors3.default.green("Federated types created correctly"));
|
|
398
|
+
} catch (error2) {
|
|
399
|
+
if (((_a = this.options.remote) == null ? void 0 : _a.abortOnError) === false) {
|
|
400
|
+
console.error(
|
|
401
|
+
import_ansi_colors3.default.red(`Unable to compile federated types, ${error2}`)
|
|
402
|
+
);
|
|
403
|
+
} else {
|
|
404
|
+
throw error2;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
async requestRemoteManifest(remoteInfo) {
|
|
409
|
+
try {
|
|
410
|
+
if (!remoteInfo.url.includes(import_sdk2.MANIFEST_EXT)) {
|
|
411
|
+
return remoteInfo;
|
|
412
|
+
}
|
|
413
|
+
const url = replaceLocalhost(remoteInfo.url);
|
|
414
|
+
const res = await (0, import_axios2.default)({
|
|
415
|
+
method: "get",
|
|
416
|
+
url
|
|
417
|
+
});
|
|
418
|
+
const manifestJson = res.data;
|
|
419
|
+
if (!manifestJson.metaData.types.zip) {
|
|
420
|
+
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
|
|
421
|
+
}
|
|
422
|
+
const addProtocol = (u) => {
|
|
423
|
+
if (u.startsWith("//")) {
|
|
424
|
+
return `https:${u}`;
|
|
425
|
+
}
|
|
426
|
+
return u;
|
|
427
|
+
};
|
|
428
|
+
const publicPath = "publicPath" in manifestJson.metaData ? manifestJson.metaData.publicPath : new Function(manifestJson.metaData.getPublicPath)();
|
|
429
|
+
remoteInfo.zipUrl = new URL(
|
|
430
|
+
import_path3.default.join(addProtocol(publicPath), manifestJson.metaData.types.zip)
|
|
431
|
+
).href;
|
|
432
|
+
if (!manifestJson.metaData.types.api) {
|
|
433
|
+
console.warn(`Can not get ${remoteInfo.name}'s api types url!`);
|
|
434
|
+
remoteInfo.apiTypeUrl = "";
|
|
435
|
+
return remoteInfo;
|
|
436
|
+
}
|
|
437
|
+
remoteInfo.apiTypeUrl = new URL(
|
|
438
|
+
import_path3.default.join(addProtocol(publicPath), manifestJson.metaData.types.api)
|
|
439
|
+
).href;
|
|
440
|
+
return remoteInfo;
|
|
441
|
+
} catch (_err) {
|
|
442
|
+
console.error(_err);
|
|
443
|
+
return remoteInfo;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
async consumeTargetRemotes(hostOptions, remoteInfo) {
|
|
447
|
+
if (!remoteInfo.zipUrl) {
|
|
448
|
+
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
|
|
449
|
+
}
|
|
450
|
+
const typesDownloader = downloadTypesArchive(hostOptions);
|
|
451
|
+
return typesDownloader([remoteInfo.alias, remoteInfo.zipUrl]);
|
|
452
|
+
}
|
|
453
|
+
async downloadAPITypes(remoteInfo, destinationPath) {
|
|
454
|
+
const { apiTypeUrl } = remoteInfo;
|
|
455
|
+
if (!apiTypeUrl) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
try {
|
|
459
|
+
const url = replaceLocalhost(apiTypeUrl);
|
|
460
|
+
const res = await import_axios2.default.get(url);
|
|
461
|
+
let apiTypeFile = res.data;
|
|
462
|
+
apiTypeFile = apiTypeFile.replaceAll(
|
|
463
|
+
REMOTE_ALIAS_IDENTIFIER,
|
|
464
|
+
remoteInfo.alias
|
|
465
|
+
);
|
|
466
|
+
const filePath = import_path3.default.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
|
|
467
|
+
import_fs.default.writeFileSync(filePath, apiTypeFile);
|
|
468
|
+
this.loadedRemoteAPIAlias.push(remoteInfo.alias);
|
|
469
|
+
} catch (err) {
|
|
470
|
+
console.error(
|
|
471
|
+
import_ansi_colors3.default.red(
|
|
472
|
+
`Unable to download "${remoteInfo.name}" api types, ${err}`
|
|
473
|
+
)
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
consumeAPITypes(hostOptions) {
|
|
478
|
+
if (!this.loadedRemoteAPIAlias.length) {
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
const packageTypes = [];
|
|
482
|
+
const remoteKeys = [];
|
|
483
|
+
const importTypeStr = this.loadedRemoteAPIAlias.map((alias, index) => {
|
|
484
|
+
const remoteKey = `RemoteKeys_${index}`;
|
|
485
|
+
const packageType = `PackageType_${index}`;
|
|
486
|
+
packageTypes.push(`T extends ${remoteKey} ? ${packageType}<T>`);
|
|
487
|
+
remoteKeys.push(remoteKey);
|
|
488
|
+
return `import type { PackageType as ${packageType},RemoteKeys as ${remoteKey} } from './${alias}/apis.d.ts';`;
|
|
489
|
+
}).join("\n");
|
|
490
|
+
const remoteKeysStr = `type RemoteKeys = ${remoteKeys.join(" | ")};`;
|
|
491
|
+
const packageTypesStr = `type PackageType<T, Y=any> = ${[
|
|
492
|
+
...packageTypes,
|
|
493
|
+
"Y"
|
|
494
|
+
].join(" :\n")} ;`;
|
|
495
|
+
const pkgsDeclareStr = this.runtimePkgs.map((pkg) => {
|
|
496
|
+
return `declare module "${pkg}" {
|
|
497
|
+
${remoteKeysStr}
|
|
498
|
+
${packageTypesStr}
|
|
499
|
+
export function loadRemote<T extends RemoteKeys,Y>(packageName: T): Promise<PackageType<T, Y>>;
|
|
500
|
+
export function loadRemote<T extends string,Y>(packageName: T): Promise<PackageType<T, Y>>;
|
|
501
|
+
}`;
|
|
502
|
+
}).join("\n");
|
|
503
|
+
const fileStr = `${importTypeStr}
|
|
504
|
+
${pkgsDeclareStr}
|
|
505
|
+
`;
|
|
506
|
+
import_fs.default.writeFileSync(
|
|
507
|
+
import_path3.default.join(
|
|
508
|
+
hostOptions.context,
|
|
509
|
+
hostOptions.typesFolder,
|
|
510
|
+
HOST_API_TYPES_FILE_NAME
|
|
511
|
+
),
|
|
512
|
+
fileStr
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
async consumeArchiveTypes(options) {
|
|
516
|
+
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options);
|
|
517
|
+
if (hostOptions.deleteTypesFolder) {
|
|
518
|
+
await (0, import_promises.rm)(hostOptions.typesFolder, {
|
|
519
|
+
recursive: true,
|
|
520
|
+
force: true
|
|
521
|
+
}).catch(
|
|
522
|
+
(error2) => console.error(
|
|
523
|
+
import_ansi_colors3.default.red(`Unable to remove types folder, ${error2}`)
|
|
524
|
+
)
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
const downloadPromises = Object.entries(mapRemotesToDownload).map(
|
|
528
|
+
async (item) => {
|
|
529
|
+
const remoteInfo = item[1];
|
|
530
|
+
if (!this.remoteAliasMap[remoteInfo.alias]) {
|
|
531
|
+
const requiredRemoteInfo = await this.requestRemoteManifest(remoteInfo);
|
|
532
|
+
this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
|
|
533
|
+
}
|
|
534
|
+
return this.consumeTargetRemotes(
|
|
535
|
+
hostOptions,
|
|
536
|
+
this.remoteAliasMap[remoteInfo.alias]
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
);
|
|
540
|
+
const downloadPromisesResult = await Promise.allSettled(downloadPromises);
|
|
541
|
+
return {
|
|
542
|
+
hostOptions,
|
|
543
|
+
downloadPromisesResult
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
async consumeTypes() {
|
|
547
|
+
var _a;
|
|
548
|
+
try {
|
|
549
|
+
const { options } = this;
|
|
550
|
+
if (!options.host) {
|
|
551
|
+
throw new Error("options.host is required if you want to consumeTypes");
|
|
552
|
+
}
|
|
553
|
+
const { mapRemotesToDownload } = retrieveHostConfig(options.host);
|
|
554
|
+
if (!Object.keys(mapRemotesToDownload).length) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const { downloadPromisesResult, hostOptions } = await this.consumeArchiveTypes(options.host);
|
|
558
|
+
if (hostOptions.consumeAPITypes) {
|
|
559
|
+
await Promise.all(
|
|
560
|
+
downloadPromisesResult.map(async (item) => {
|
|
561
|
+
if (item.status === "rejected" || !item.value) {
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const [alias, destinationPath] = item.value;
|
|
565
|
+
const remoteInfo = this.remoteAliasMap[alias];
|
|
566
|
+
if (!remoteInfo) {
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
await this.downloadAPITypes(remoteInfo, destinationPath);
|
|
570
|
+
})
|
|
571
|
+
);
|
|
572
|
+
this.consumeAPITypes(hostOptions);
|
|
573
|
+
}
|
|
574
|
+
console.log(import_ansi_colors3.default.green("Federated types extraction completed"));
|
|
575
|
+
} catch (err) {
|
|
576
|
+
if (((_a = this.options.host) == null ? void 0 : _a.abortOnError) === false) {
|
|
577
|
+
console.error(
|
|
578
|
+
import_ansi_colors3.default.red(`Unable to consume federated types, ${err}`)
|
|
579
|
+
);
|
|
580
|
+
} else {
|
|
581
|
+
throw err;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
async updateTypes(options) {
|
|
586
|
+
var _a, _b, _c;
|
|
587
|
+
const { remoteName, updateMode } = options;
|
|
588
|
+
const hostName = (_c = (_b = (_a = this.options) == null ? void 0 : _a.host) == null ? void 0 : _b.moduleFederationConfig) == null ? void 0 : _c.name;
|
|
589
|
+
if (updateMode === "POSITIVE" /* POSITIVE */ && remoteName === hostName) {
|
|
590
|
+
if (!this.options.remote) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
this.generateTypes();
|
|
594
|
+
} else {
|
|
595
|
+
const { remoteAliasMap } = this;
|
|
596
|
+
if (!this.options.host) {
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(
|
|
600
|
+
this.options.host
|
|
601
|
+
);
|
|
602
|
+
const loadedRemoteInfo = Object.values(remoteAliasMap).find(
|
|
603
|
+
(i) => i.name === remoteName
|
|
604
|
+
);
|
|
605
|
+
if (!loadedRemoteInfo) {
|
|
606
|
+
const remoteInfo = Object.values(mapRemotesToDownload).find((item) => {
|
|
607
|
+
return item.name === remoteName;
|
|
608
|
+
});
|
|
609
|
+
if (remoteInfo) {
|
|
610
|
+
if (!this.remoteAliasMap[remoteInfo.alias]) {
|
|
611
|
+
const requiredRemoteInfo = await this.requestRemoteManifest(remoteInfo);
|
|
612
|
+
this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
|
|
613
|
+
}
|
|
614
|
+
await this.consumeTargetRemotes(
|
|
615
|
+
hostOptions,
|
|
616
|
+
this.remoteAliasMap[remoteInfo.alias]
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
} else {
|
|
620
|
+
await this.consumeTargetRemotes(hostOptions, loadedRemoteInfo);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
// packages/dts-plugin/src/core/lib/utils.ts
|
|
627
|
+
var import_ansi_colors4 = __toESM(require("ansi-colors"));
|
|
628
|
+
function getDTSManagerConstructor(implementation) {
|
|
629
|
+
if (implementation) {
|
|
630
|
+
const NewConstructor = require(implementation);
|
|
631
|
+
return NewConstructor.default ? NewConstructor.default : NewConstructor;
|
|
632
|
+
}
|
|
633
|
+
return DTSManager;
|
|
634
|
+
}
|
|
635
|
+
var validateOptions = (options) => {
|
|
636
|
+
if (!options.moduleFederationConfig) {
|
|
637
|
+
throw new Error("moduleFederationConfig is required");
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
function replaceLocalhost(url) {
|
|
641
|
+
return url.replace("localhost", "127.0.0.1");
|
|
642
|
+
}
|
|
643
|
+
function isDebugMode() {
|
|
644
|
+
return Boolean(process.env["FEDERATION_DEBUG"]);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// packages/dts-plugin/src/core/configurations/remotePlugin.ts
|
|
648
|
+
var defaultOptions2 = {
|
|
649
|
+
tsConfigPath: "./tsconfig.json",
|
|
650
|
+
typesFolder: "@mf-types",
|
|
651
|
+
compiledTypesFolder: "compiled-types",
|
|
652
|
+
hostRemoteTypesFolder: "@mf-types",
|
|
653
|
+
deleteTypesFolder: true,
|
|
654
|
+
additionalFilesToCompile: [],
|
|
655
|
+
compilerInstance: "tsc",
|
|
656
|
+
compileInChildProcess: false,
|
|
657
|
+
implementation: "",
|
|
658
|
+
generateAPITypes: false,
|
|
659
|
+
context: process.cwd(),
|
|
660
|
+
abortOnError: true,
|
|
661
|
+
extractRemoteTypes: false,
|
|
662
|
+
extractThirdParty: false
|
|
663
|
+
};
|
|
664
|
+
var readTsConfig = ({
|
|
665
|
+
tsConfigPath,
|
|
666
|
+
typesFolder,
|
|
667
|
+
compiledTypesFolder,
|
|
668
|
+
context
|
|
669
|
+
}) => {
|
|
670
|
+
const resolvedTsConfigPath = (0, import_path4.resolve)(context, tsConfigPath);
|
|
671
|
+
const readResult = import_typescript2.default.readConfigFile(
|
|
672
|
+
resolvedTsConfigPath,
|
|
673
|
+
import_typescript2.default.sys.readFile
|
|
674
|
+
);
|
|
675
|
+
if (readResult.error) {
|
|
676
|
+
throw new Error(readResult.error.messageText.toString());
|
|
677
|
+
}
|
|
678
|
+
const configContent = import_typescript2.default.parseJsonConfigFileContent(
|
|
679
|
+
readResult.config,
|
|
680
|
+
import_typescript2.default.sys,
|
|
681
|
+
(0, import_path4.dirname)(resolvedTsConfigPath)
|
|
682
|
+
);
|
|
683
|
+
const outDir = (0, import_path4.resolve)(
|
|
684
|
+
context,
|
|
685
|
+
configContent.options.outDir || "dist",
|
|
686
|
+
typesFolder,
|
|
687
|
+
compiledTypesFolder
|
|
688
|
+
);
|
|
689
|
+
return {
|
|
690
|
+
...configContent.options,
|
|
691
|
+
emitDeclarationOnly: true,
|
|
692
|
+
noEmit: false,
|
|
693
|
+
declaration: true,
|
|
694
|
+
outDir
|
|
695
|
+
};
|
|
696
|
+
};
|
|
697
|
+
var TS_EXTENSIONS = ["ts", "tsx", "vue", "svelte"];
|
|
698
|
+
var resolveWithExtension = (exposedPath, context) => {
|
|
699
|
+
if ((0, import_path4.extname)(exposedPath)) {
|
|
700
|
+
return (0, import_path4.resolve)(context, exposedPath);
|
|
701
|
+
}
|
|
702
|
+
for (const extension of TS_EXTENSIONS) {
|
|
703
|
+
const exposedPathWithExtension = (0, import_path4.resolve)(
|
|
704
|
+
context,
|
|
705
|
+
`${exposedPath}.${extension}`
|
|
706
|
+
);
|
|
707
|
+
if ((0, import_fs2.existsSync)(exposedPathWithExtension)) {
|
|
708
|
+
return exposedPathWithExtension;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
return void 0;
|
|
712
|
+
};
|
|
713
|
+
var resolveExposes = (remoteOptions) => {
|
|
714
|
+
const parsedOptions = import_managers2.utils.parseOptions(
|
|
715
|
+
remoteOptions.moduleFederationConfig.exposes || {},
|
|
716
|
+
(item, key) => ({
|
|
717
|
+
exposePath: Array.isArray(item) ? item[0] : item,
|
|
718
|
+
key
|
|
719
|
+
}),
|
|
720
|
+
(item, key) => ({
|
|
721
|
+
exposePath: Array.isArray(item.import) ? item.import[0] : item.import[0],
|
|
722
|
+
key
|
|
723
|
+
})
|
|
724
|
+
);
|
|
725
|
+
return parsedOptions.reduce(
|
|
726
|
+
(accumulator, item) => {
|
|
727
|
+
const { exposePath, key } = item[1];
|
|
728
|
+
accumulator[key] = resolveWithExtension(exposePath, remoteOptions.context) || resolveWithExtension(
|
|
729
|
+
(0, import_path4.join)(exposePath, "index"),
|
|
730
|
+
remoteOptions.context
|
|
731
|
+
) || exposePath;
|
|
732
|
+
return accumulator;
|
|
733
|
+
},
|
|
734
|
+
{}
|
|
735
|
+
);
|
|
736
|
+
};
|
|
737
|
+
var retrieveRemoteConfig = (options) => {
|
|
738
|
+
validateOptions(options);
|
|
739
|
+
const remoteOptions = {
|
|
740
|
+
...defaultOptions2,
|
|
741
|
+
...options
|
|
742
|
+
};
|
|
743
|
+
const mapComponentsToExpose = resolveExposes(remoteOptions);
|
|
744
|
+
const tsConfig = readTsConfig(remoteOptions);
|
|
745
|
+
return {
|
|
746
|
+
tsConfig,
|
|
747
|
+
mapComponentsToExpose,
|
|
748
|
+
remoteOptions
|
|
749
|
+
};
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
// packages/dts-plugin/src/core/lib/DtsWorker.ts
|
|
753
|
+
var import_lodash2 = __toESM(require("lodash.clonedeepwith"));
|
|
754
|
+
|
|
755
|
+
// packages/dts-plugin/src/core/rpc/index.ts
|
|
756
|
+
var rpc_exports = {};
|
|
757
|
+
__export(rpc_exports, {
|
|
758
|
+
RpcExitError: () => RpcExitError,
|
|
759
|
+
RpcGMCallTypes: () => RpcGMCallTypes,
|
|
760
|
+
createRpcWorker: () => createRpcWorker,
|
|
761
|
+
exposeRpc: () => exposeRpc,
|
|
762
|
+
getRpcWorkerData: () => getRpcWorkerData,
|
|
763
|
+
wrapRpc: () => wrapRpc
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
// packages/dts-plugin/src/core/rpc/expose-rpc.ts
|
|
767
|
+
var import_process = __toESM(require("process"));
|
|
768
|
+
|
|
769
|
+
// packages/dts-plugin/src/core/rpc/types.ts
|
|
770
|
+
var RpcGMCallTypes = /* @__PURE__ */ ((RpcGMCallTypes2) => {
|
|
771
|
+
RpcGMCallTypes2["CALL"] = "mf_call";
|
|
772
|
+
RpcGMCallTypes2["RESOLVE"] = "mf_resolve";
|
|
773
|
+
RpcGMCallTypes2["REJECT"] = "mf_reject";
|
|
774
|
+
RpcGMCallTypes2["EXIT"] = "mf_exit";
|
|
775
|
+
return RpcGMCallTypes2;
|
|
776
|
+
})(RpcGMCallTypes || {});
|
|
777
|
+
|
|
778
|
+
// packages/dts-plugin/src/core/rpc/expose-rpc.ts
|
|
779
|
+
function exposeRpc(fn) {
|
|
780
|
+
const sendMessage = (message) => new Promise((resolve4, reject) => {
|
|
781
|
+
if (!import_process.default.send) {
|
|
782
|
+
reject(new Error(`Process ${import_process.default.pid} doesn't have IPC channels`));
|
|
783
|
+
} else if (!import_process.default.connected) {
|
|
784
|
+
reject(
|
|
785
|
+
new Error(`Process ${import_process.default.pid} doesn't have open IPC channels`)
|
|
786
|
+
);
|
|
787
|
+
} else {
|
|
788
|
+
import_process.default.send(message, void 0, void 0, (error2) => {
|
|
789
|
+
if (error2) {
|
|
790
|
+
reject(error2);
|
|
791
|
+
} else {
|
|
792
|
+
resolve4(void 0);
|
|
793
|
+
}
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
const handleMessage = async (message) => {
|
|
798
|
+
if (message.type === "mf_call" /* CALL */) {
|
|
799
|
+
if (!import_process.default.send) {
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
let value, error2;
|
|
803
|
+
try {
|
|
804
|
+
value = await fn(...message.args);
|
|
805
|
+
} catch (fnError) {
|
|
806
|
+
error2 = fnError;
|
|
807
|
+
}
|
|
808
|
+
try {
|
|
809
|
+
if (error2) {
|
|
810
|
+
await sendMessage({
|
|
811
|
+
type: "mf_reject" /* REJECT */,
|
|
812
|
+
id: message.id,
|
|
813
|
+
error: error2
|
|
814
|
+
});
|
|
815
|
+
} else {
|
|
816
|
+
await sendMessage({
|
|
817
|
+
type: "mf_resolve" /* RESOLVE */,
|
|
818
|
+
id: message.id,
|
|
819
|
+
value
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
} catch (sendError) {
|
|
823
|
+
if (error2) {
|
|
824
|
+
if (error2 instanceof Error) {
|
|
825
|
+
console.error(error2);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
console.error(sendError);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
import_process.default.on("message", handleMessage);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// packages/dts-plugin/src/core/rpc/rpc-error.ts
|
|
836
|
+
var RpcExitError = class extends Error {
|
|
837
|
+
constructor(message, code, signal) {
|
|
838
|
+
super(message);
|
|
839
|
+
this.code = code;
|
|
840
|
+
this.signal = signal;
|
|
841
|
+
this.name = "RpcExitError";
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
// packages/dts-plugin/src/core/rpc/wrap-rpc.ts
|
|
846
|
+
function createControlledPromise() {
|
|
847
|
+
let resolve4 = () => void 0;
|
|
848
|
+
let reject = () => void 0;
|
|
849
|
+
const promise = new Promise((aResolve, aReject) => {
|
|
850
|
+
resolve4 = aResolve;
|
|
851
|
+
reject = aReject;
|
|
852
|
+
});
|
|
853
|
+
return {
|
|
854
|
+
promise,
|
|
855
|
+
resolve: resolve4,
|
|
856
|
+
reject
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
function wrapRpc(childProcess, options) {
|
|
860
|
+
return async (...args) => {
|
|
861
|
+
if (!childProcess.send) {
|
|
862
|
+
throw new Error(`Process ${childProcess.pid} doesn't have IPC channels`);
|
|
863
|
+
} else if (!childProcess.connected) {
|
|
864
|
+
throw new Error(
|
|
865
|
+
`Process ${childProcess.pid} doesn't have open IPC channels`
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
const { id, once } = options;
|
|
869
|
+
const {
|
|
870
|
+
promise: resultPromise,
|
|
871
|
+
resolve: resolveResult,
|
|
872
|
+
reject: rejectResult
|
|
873
|
+
} = createControlledPromise();
|
|
874
|
+
const {
|
|
875
|
+
promise: sendPromise,
|
|
876
|
+
resolve: resolveSend,
|
|
877
|
+
reject: rejectSend
|
|
878
|
+
} = createControlledPromise();
|
|
879
|
+
const handleMessage = (message) => {
|
|
880
|
+
if ((message == null ? void 0 : message.id) === id) {
|
|
881
|
+
if (message.type === "mf_resolve" /* RESOLVE */) {
|
|
882
|
+
resolveResult(message.value);
|
|
883
|
+
} else if (message.type === "mf_reject" /* REJECT */) {
|
|
884
|
+
rejectResult(message.error);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
if (once && (childProcess == null ? void 0 : childProcess.kill)) {
|
|
888
|
+
childProcess.kill("SIGTERM");
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
const handleClose = (code, signal) => {
|
|
892
|
+
rejectResult(
|
|
893
|
+
new RpcExitError(
|
|
894
|
+
code ? `Process ${childProcess.pid} exited with code ${code}${signal ? ` [${signal}]` : ""}` : `Process ${childProcess.pid} exited${signal ? ` [${signal}]` : ""}`,
|
|
895
|
+
code,
|
|
896
|
+
signal
|
|
897
|
+
)
|
|
898
|
+
);
|
|
899
|
+
removeHandlers();
|
|
900
|
+
};
|
|
901
|
+
const removeHandlers = () => {
|
|
902
|
+
childProcess.off("message", handleMessage);
|
|
903
|
+
childProcess.off("close", handleClose);
|
|
904
|
+
};
|
|
905
|
+
if (once) {
|
|
906
|
+
childProcess.once("message", handleMessage);
|
|
907
|
+
} else {
|
|
908
|
+
childProcess.on("message", handleMessage);
|
|
909
|
+
}
|
|
910
|
+
childProcess.on("close", handleClose);
|
|
911
|
+
childProcess.send(
|
|
912
|
+
{
|
|
913
|
+
type: "mf_call" /* CALL */,
|
|
914
|
+
id,
|
|
915
|
+
args
|
|
916
|
+
},
|
|
917
|
+
(error2) => {
|
|
918
|
+
if (error2) {
|
|
919
|
+
rejectSend(error2);
|
|
920
|
+
removeHandlers();
|
|
921
|
+
} else {
|
|
922
|
+
resolveSend(void 0);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
);
|
|
926
|
+
return sendPromise.then(() => resultPromise);
|
|
927
|
+
};
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// packages/dts-plugin/src/core/rpc/rpc-worker.ts
|
|
931
|
+
var child_process = __toESM(require("child_process"));
|
|
932
|
+
var process3 = __toESM(require("process"));
|
|
933
|
+
var import_crypto = require("crypto");
|
|
934
|
+
var FEDERATION_WORKER_DATA_ENV_KEY = "VMOK_WORKER_DATA_ENV";
|
|
935
|
+
function createRpcWorker(modulePath, data, memoryLimit, once) {
|
|
936
|
+
const options = {
|
|
937
|
+
env: {
|
|
938
|
+
...process3.env,
|
|
939
|
+
[FEDERATION_WORKER_DATA_ENV_KEY]: JSON.stringify(data || {})
|
|
940
|
+
},
|
|
941
|
+
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
942
|
+
serialization: "advanced"
|
|
943
|
+
};
|
|
944
|
+
if (memoryLimit) {
|
|
945
|
+
options.execArgv = [`--max-old-space-size=${memoryLimit}`];
|
|
946
|
+
}
|
|
947
|
+
let childProcess, remoteMethod;
|
|
948
|
+
const id = (0, import_crypto.randomUUID)();
|
|
949
|
+
const worker = {
|
|
950
|
+
connect(...args) {
|
|
951
|
+
if (childProcess && !childProcess.connected) {
|
|
952
|
+
childProcess.send({
|
|
953
|
+
type: "mf_exit" /* EXIT */,
|
|
954
|
+
id
|
|
955
|
+
});
|
|
956
|
+
childProcess = void 0;
|
|
957
|
+
remoteMethod = void 0;
|
|
958
|
+
}
|
|
959
|
+
if (!(childProcess == null ? void 0 : childProcess.connected)) {
|
|
960
|
+
childProcess = child_process.fork(modulePath, options);
|
|
961
|
+
remoteMethod = wrapRpc(childProcess, { id, once });
|
|
962
|
+
}
|
|
963
|
+
if (!remoteMethod) {
|
|
964
|
+
return Promise.reject(
|
|
965
|
+
new Error("Worker is not connected - cannot perform RPC.")
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
return remoteMethod(...args);
|
|
969
|
+
},
|
|
970
|
+
terminate() {
|
|
971
|
+
var _a;
|
|
972
|
+
(_a = childProcess == null ? void 0 : childProcess.send) == null ? void 0 : _a.call(childProcess, {
|
|
973
|
+
type: "mf_exit" /* EXIT */,
|
|
974
|
+
id
|
|
975
|
+
});
|
|
976
|
+
childProcess = void 0;
|
|
977
|
+
remoteMethod = void 0;
|
|
978
|
+
},
|
|
979
|
+
get connected() {
|
|
980
|
+
return Boolean(childProcess == null ? void 0 : childProcess.connected);
|
|
981
|
+
},
|
|
982
|
+
get process() {
|
|
983
|
+
return childProcess;
|
|
984
|
+
},
|
|
985
|
+
get id() {
|
|
986
|
+
return id;
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
return worker;
|
|
990
|
+
}
|
|
991
|
+
function getRpcWorkerData() {
|
|
992
|
+
return JSON.parse(process3.env[FEDERATION_WORKER_DATA_ENV_KEY] || "{}");
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
// packages/dts-plugin/src/dev-worker/forkDevWorker.ts
|
|
996
|
+
var import_sdk5 = require("@module-federation/sdk");
|
|
38
997
|
|
|
39
998
|
// packages/dts-plugin/src/server/message/Message.ts
|
|
40
999
|
var Message = class {
|
|
@@ -83,7 +1042,7 @@ var ReloadWebClientAPI = class extends API {
|
|
|
83
1042
|
|
|
84
1043
|
// packages/dts-plugin/src/server/utils/index.ts
|
|
85
1044
|
var import_net = __toESM(require("net"));
|
|
86
|
-
var
|
|
1045
|
+
var import_sdk4 = require("@module-federation/sdk");
|
|
87
1046
|
|
|
88
1047
|
// packages/dts-plugin/src/server/utils/logTransform.ts
|
|
89
1048
|
var import_chalk = __toESM(require("chalk"));
|
|
@@ -106,7 +1065,7 @@ var BrokerExitLog = class extends Log {
|
|
|
106
1065
|
};
|
|
107
1066
|
|
|
108
1067
|
// packages/dts-plugin/src/server/utils/log.ts
|
|
109
|
-
var
|
|
1068
|
+
var import_sdk3 = require("@module-federation/sdk");
|
|
110
1069
|
var log4js = __toESM(require("log4js"));
|
|
111
1070
|
var import_chalk2 = __toESM(require("chalk"));
|
|
112
1071
|
|
|
@@ -171,7 +1130,7 @@ var getIPV4 = () => {
|
|
|
171
1130
|
// packages/dts-plugin/src/server/utils/index.ts
|
|
172
1131
|
function getIdentifier(options) {
|
|
173
1132
|
const { ip, name } = options;
|
|
174
|
-
return `mf ${
|
|
1133
|
+
return `mf ${import_sdk4.SEPARATOR}${name}${ip ? `${import_sdk4.SEPARATOR}${ip}` : ""}`;
|
|
175
1134
|
}
|
|
176
1135
|
function fib(n) {
|
|
177
1136
|
let i = 2;
|
|
@@ -183,14 +1142,14 @@ function fib(n) {
|
|
|
183
1142
|
return res[n];
|
|
184
1143
|
}
|
|
185
1144
|
function getFreePort() {
|
|
186
|
-
return new Promise((
|
|
1145
|
+
return new Promise((resolve4, reject) => {
|
|
187
1146
|
const server = import_net.default.createServer();
|
|
188
1147
|
server.unref();
|
|
189
1148
|
server.on("error", reject);
|
|
190
1149
|
server.listen(0, () => {
|
|
191
1150
|
const { port } = server.address();
|
|
192
1151
|
server.close(() => {
|
|
193
|
-
|
|
1152
|
+
resolve4(port);
|
|
194
1153
|
});
|
|
195
1154
|
});
|
|
196
1155
|
});
|
|
@@ -295,11 +1254,9 @@ var Publisher = class {
|
|
|
295
1254
|
|
|
296
1255
|
// packages/dts-plugin/src/server/DevServer.ts
|
|
297
1256
|
var import_isomorphic_ws2 = __toESM(require("isomorphic-ws"));
|
|
298
|
-
var import_helpers2 = require("@module-federation/native-federation-typescript/helpers");
|
|
299
1257
|
|
|
300
1258
|
// packages/dts-plugin/src/server/broker/Broker.ts
|
|
301
1259
|
var import_http = require("http");
|
|
302
|
-
var import_helpers = require("@module-federation/native-federation-typescript/helpers");
|
|
303
1260
|
var import_isomorphic_ws = __toESM(require("isomorphic-ws"));
|
|
304
1261
|
var import_node_schedule = __toESM(require("node-schedule"));
|
|
305
1262
|
var import_url = require("url");
|
|
@@ -621,7 +1578,7 @@ ${err.message}
|
|
|
621
1578
|
}),
|
|
622
1579
|
{
|
|
623
1580
|
updateKind: "UPDATE_TYPE" /* UPDATE_TYPE */,
|
|
624
|
-
updateMode:
|
|
1581
|
+
updateMode: "PASSIVE" /* PASSIVE */,
|
|
625
1582
|
updateSourcePaths: [registeredPublisher.name],
|
|
626
1583
|
remoteTypeTarPath: registeredPublisher.remoteTypeTarPath,
|
|
627
1584
|
name: registeredPublisher.name
|
|
@@ -841,7 +1798,7 @@ ${err.message}
|
|
|
841
1798
|
publisher.addSubscriber(identifier, tmpSubScriber.client);
|
|
842
1799
|
publisher.notifySubscriber(identifier, {
|
|
843
1800
|
updateKind: "UPDATE_TYPE" /* UPDATE_TYPE */,
|
|
844
|
-
updateMode:
|
|
1801
|
+
updateMode: "PASSIVE" /* PASSIVE */,
|
|
845
1802
|
updateSourcePaths: [publisher.name],
|
|
846
1803
|
remoteTypeTarPath: publisher.remoteTypeTarPath,
|
|
847
1804
|
name: publisher.name
|
|
@@ -951,9 +1908,9 @@ var Broker = _Broker;
|
|
|
951
1908
|
|
|
952
1909
|
// packages/dts-plugin/src/server/broker/createBroker.ts
|
|
953
1910
|
var import_child_process = require("child_process");
|
|
954
|
-
var
|
|
1911
|
+
var import_path5 = __toESM(require("path"));
|
|
955
1912
|
function createBroker() {
|
|
956
|
-
const startBrokerPath =
|
|
1913
|
+
const startBrokerPath = import_path5.default.resolve(__dirname, "./startBroker.js");
|
|
957
1914
|
const sub = (0, import_child_process.fork)(startBrokerPath, [], {
|
|
958
1915
|
detached: true,
|
|
959
1916
|
stdio: "ignore",
|
|
@@ -1197,7 +2154,7 @@ var ModuleFederationDevServer = class {
|
|
|
1197
2154
|
name,
|
|
1198
2155
|
remoteTypeTarPath
|
|
1199
2156
|
} = options;
|
|
1200
|
-
if (updateMode ===
|
|
2157
|
+
if (updateMode === "PASSIVE" /* PASSIVE */ && updateSourcePaths.includes(this._name)) {
|
|
1201
2158
|
fileLog(
|
|
1202
2159
|
// eslint-disable-next-line max-len
|
|
1203
2160
|
`[_updateSubscriber] run, updateSourcePaths:${updateSourcePaths} includes ${this._name}, update ignore!`,
|
|
@@ -1231,7 +2188,7 @@ var ModuleFederationDevServer = class {
|
|
|
1231
2188
|
const updatePublisher = new UpdatePublisherAction({
|
|
1232
2189
|
name: this._name,
|
|
1233
2190
|
ip: this._ip,
|
|
1234
|
-
updateMode:
|
|
2191
|
+
updateMode: "PASSIVE" /* PASSIVE */,
|
|
1235
2192
|
updateKind,
|
|
1236
2193
|
updateSourcePaths: newUpdateSourcePaths,
|
|
1237
2194
|
remoteTypeTarPath: this._remoteTypeTarPath
|
|
@@ -1425,11 +2382,11 @@ function getLocalRemoteNames(options, encodeNameIdentifier) {
|
|
|
1425
2382
|
if (!options) {
|
|
1426
2383
|
return [];
|
|
1427
2384
|
}
|
|
1428
|
-
const { mapRemotesToDownload } =
|
|
2385
|
+
const { mapRemotesToDownload } = retrieveHostConfig(options);
|
|
1429
2386
|
return Object.keys(mapRemotesToDownload).reduce(
|
|
1430
2387
|
(sum, remoteModuleName) => {
|
|
1431
2388
|
const remoteInfo = mapRemotesToDownload[remoteModuleName];
|
|
1432
|
-
const name = encodeNameIdentifier ? (0,
|
|
2389
|
+
const name = encodeNameIdentifier ? (0, import_sdk5.decodeName)(remoteInfo.name, encodeNameIdentifier) : remoteInfo.name;
|
|
1433
2390
|
const ip = getIpFromEntry(remoteInfo.url);
|
|
1434
2391
|
if (!ip) {
|
|
1435
2392
|
return sum;
|
|
@@ -1458,7 +2415,7 @@ async function updateCallback({
|
|
|
1458
2415
|
if (!disableLiveReload && moduleServer) {
|
|
1459
2416
|
moduleServer.update({
|
|
1460
2417
|
updateKind: "RELOAD_PAGE" /* RELOAD_PAGE */,
|
|
1461
|
-
updateMode:
|
|
2418
|
+
updateMode: "PASSIVE" /* PASSIVE */
|
|
1462
2419
|
});
|
|
1463
2420
|
}
|
|
1464
2421
|
if (!disableHotTypesReload && typesManager) {
|
|
@@ -1472,7 +2429,7 @@ async function updateCallback({
|
|
|
1472
2429
|
async function forkDevWorker(options, action) {
|
|
1473
2430
|
if (!typesManager) {
|
|
1474
2431
|
const { name, remote, host, extraOptions } = options;
|
|
1475
|
-
const DTSManagerConstructor =
|
|
2432
|
+
const DTSManagerConstructor = getDTSManagerConstructor(
|
|
1476
2433
|
remote == null ? void 0 : remote.implementation
|
|
1477
2434
|
);
|
|
1478
2435
|
typesManager = new DTSManagerConstructor({
|
|
@@ -1481,9 +2438,9 @@ async function forkDevWorker(options, action) {
|
|
|
1481
2438
|
extraOptions
|
|
1482
2439
|
});
|
|
1483
2440
|
if (!options.disableHotTypesReload && remote) {
|
|
1484
|
-
const { remoteOptions, tsConfig } =
|
|
1485
|
-
const mfTypesPath =
|
|
1486
|
-
const mfTypesZipPath =
|
|
2441
|
+
const { remoteOptions, tsConfig } = retrieveRemoteConfig(remote);
|
|
2442
|
+
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
2443
|
+
const mfTypesZipPath = retrieveTypesZipPath(mfTypesPath, remoteOptions);
|
|
1487
2444
|
await Promise.all([
|
|
1488
2445
|
createKoaServer({
|
|
1489
2446
|
typeTarPath: mfTypesZipPath
|
|
@@ -1526,17 +2483,17 @@ async function forkDevWorker(options, action) {
|
|
|
1526
2483
|
if (!cacheOptions.disableLiveReload) {
|
|
1527
2484
|
moduleServer == null ? void 0 : moduleServer.update({
|
|
1528
2485
|
updateKind: "RELOAD_PAGE" /* RELOAD_PAGE */,
|
|
1529
|
-
updateMode:
|
|
2486
|
+
updateMode: "POSITIVE" /* POSITIVE */
|
|
1530
2487
|
});
|
|
1531
2488
|
}
|
|
1532
2489
|
if (!cacheOptions.disableHotTypesReload) {
|
|
1533
2490
|
typesManager == null ? void 0 : typesManager.updateTypes({
|
|
1534
|
-
updateMode:
|
|
2491
|
+
updateMode: "POSITIVE" /* POSITIVE */,
|
|
1535
2492
|
remoteName: cacheOptions.name
|
|
1536
2493
|
}).then(() => {
|
|
1537
2494
|
moduleServer == null ? void 0 : moduleServer.update({
|
|
1538
2495
|
updateKind: "UPDATE_TYPE" /* UPDATE_TYPE */,
|
|
1539
|
-
updateMode:
|
|
2496
|
+
updateMode: "POSITIVE" /* POSITIVE */
|
|
1540
2497
|
});
|
|
1541
2498
|
});
|
|
1542
2499
|
}
|
|
@@ -1548,7 +2505,7 @@ process.on("message", (message) => {
|
|
|
1548
2505
|
"forkDevWorker",
|
|
1549
2506
|
"info"
|
|
1550
2507
|
);
|
|
1551
|
-
if (message.type ===
|
|
2508
|
+
if (message.type === rpc_exports.RpcGMCallTypes.EXIT) {
|
|
1552
2509
|
fileLog(
|
|
1553
2510
|
`ChildProcess(${process.pid}) SIGTERM, Federation DevServer will exit...`,
|
|
1554
2511
|
"forkDevWorker",
|
|
@@ -1558,7 +2515,7 @@ process.on("message", (message) => {
|
|
|
1558
2515
|
process.exit(0);
|
|
1559
2516
|
}
|
|
1560
2517
|
});
|
|
1561
|
-
|
|
2518
|
+
rpc_exports.exposeRpc(forkDevWorker);
|
|
1562
2519
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1563
2520
|
0 && (module.exports = {
|
|
1564
2521
|
forkDevWorker
|