@module-federation/dts-plugin 0.1.2 → 0.1.4
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 +18 -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 +1244 -75
- package/dist/package.json +16 -4
- package/dist/startBroker.js +67 -6
- package/package.json +16 -4
package/dist/index.js
CHANGED
|
@@ -39,23 +39,1083 @@ var import_fs_extra2 = __toESM(require("fs-extra"));
|
|
|
39
39
|
var import_chalk3 = __toESM(require("chalk"));
|
|
40
40
|
|
|
41
41
|
// packages/dts-plugin/src/dev-worker/createDevWorker.ts
|
|
42
|
-
var
|
|
42
|
+
var path5 = __toESM(require("path"));
|
|
43
43
|
var fse = __toESM(require("fs-extra"));
|
|
44
44
|
|
|
45
45
|
// packages/dts-plugin/src/dev-worker/DevWorker.ts
|
|
46
|
-
var
|
|
46
|
+
var import_path7 = __toESM(require("path"));
|
|
47
|
+
var import_lodash3 = __toESM(require("lodash.clonedeepwith"));
|
|
48
|
+
|
|
49
|
+
// packages/dts-plugin/src/core/configurations/remotePlugin.ts
|
|
50
|
+
var import_fs2 = require("fs");
|
|
51
|
+
var import_path5 = require("path");
|
|
52
|
+
var import_managers2 = require("@module-federation/managers");
|
|
53
|
+
var import_typescript2 = __toESM(require("typescript"));
|
|
54
|
+
|
|
55
|
+
// packages/dts-plugin/src/core/lib/utils.ts
|
|
56
|
+
var import_path4 = __toESM(require("path"));
|
|
57
|
+
|
|
58
|
+
// packages/dts-plugin/src/core/lib/DTSManager.ts
|
|
59
|
+
var import_ansi_colors3 = __toESM(require("ansi-colors"));
|
|
60
|
+
var import_path3 = __toESM(require("path"));
|
|
61
|
+
var import_promises = require("fs/promises");
|
|
62
|
+
var import_fs = __toESM(require("fs"));
|
|
63
|
+
var import_sdk2 = require("@module-federation/sdk");
|
|
47
64
|
var import_lodash = __toESM(require("lodash.clonedeepwith"));
|
|
48
|
-
|
|
65
|
+
|
|
66
|
+
// packages/dts-plugin/src/core/lib/archiveHandler.ts
|
|
67
|
+
var import_adm_zip = __toESM(require("adm-zip"));
|
|
68
|
+
var import_ansi_colors2 = __toESM(require("ansi-colors"));
|
|
69
|
+
var import_axios = __toESM(require("axios"));
|
|
70
|
+
var import_path2 = require("path");
|
|
71
|
+
|
|
72
|
+
// packages/dts-plugin/src/core/lib/typeScriptCompiler.ts
|
|
73
|
+
var import_ansi_colors = __toESM(require("ansi-colors"));
|
|
74
|
+
var import_path = require("path");
|
|
75
|
+
var import_typescript = __toESM(require("typescript"));
|
|
76
|
+
var import_third_party_dts_extractor = require("@module-federation/third-party-dts-extractor");
|
|
77
|
+
var STARTS_WITH_SLASH = /^\//;
|
|
78
|
+
var DEFINITION_FILE_EXTENSION = ".d.ts";
|
|
79
|
+
var reportCompileDiagnostic = (diagnostic) => {
|
|
80
|
+
const { line } = diagnostic.file.getLineAndCharacterOfPosition(
|
|
81
|
+
diagnostic.start
|
|
82
|
+
);
|
|
83
|
+
console.error(
|
|
84
|
+
import_ansi_colors.default.red(
|
|
85
|
+
`TS Error ${diagnostic.code}':' ${import_typescript.default.flattenDiagnosticMessageText(
|
|
86
|
+
diagnostic.messageText,
|
|
87
|
+
import_typescript.default.sys.newLine
|
|
88
|
+
)}`
|
|
89
|
+
)
|
|
90
|
+
);
|
|
91
|
+
console.error(
|
|
92
|
+
import_ansi_colors.default.red(
|
|
93
|
+
` at ${diagnostic.file.fileName}:${line + 1} typescript.sys.newLine`
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
var retrieveMfTypesPath = (tsConfig, remoteOptions) => (0, import_path.normalize)(tsConfig.outDir.replace(remoteOptions.compiledTypesFolder, ""));
|
|
98
|
+
var retrieveOriginalOutDir = (tsConfig, remoteOptions) => (0, import_path.normalize)(
|
|
99
|
+
tsConfig.outDir.replace(remoteOptions.compiledTypesFolder, "").replace(remoteOptions.typesFolder, "")
|
|
100
|
+
);
|
|
101
|
+
var retrieveMfAPITypesPath = (tsConfig, remoteOptions) => (0, import_path.join)(
|
|
102
|
+
retrieveOriginalOutDir(tsConfig, remoteOptions),
|
|
103
|
+
`${remoteOptions.typesFolder}.d.ts`
|
|
104
|
+
);
|
|
105
|
+
var createHost = (mapComponentsToExpose, tsConfig, remoteOptions, cb) => {
|
|
106
|
+
const host = import_typescript.default.createCompilerHost(tsConfig);
|
|
107
|
+
const originalWriteFile = host.writeFile;
|
|
108
|
+
const mapExposeToEntry = Object.fromEntries(
|
|
109
|
+
Object.entries(mapComponentsToExpose).map((entry) => entry.reverse())
|
|
110
|
+
);
|
|
111
|
+
const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
112
|
+
host.writeFile = (filepath, text, writeOrderByteMark, onError, sourceFiles, data) => {
|
|
113
|
+
originalWriteFile(
|
|
114
|
+
filepath,
|
|
115
|
+
text,
|
|
116
|
+
writeOrderByteMark,
|
|
117
|
+
onError,
|
|
118
|
+
sourceFiles,
|
|
119
|
+
data
|
|
120
|
+
);
|
|
121
|
+
for (const sourceFile of sourceFiles || []) {
|
|
122
|
+
const sourceEntry = mapExposeToEntry[sourceFile.fileName];
|
|
123
|
+
if (sourceEntry) {
|
|
124
|
+
const mfeTypeEntry = (0, import_path.join)(
|
|
125
|
+
mfTypePath,
|
|
126
|
+
`${sourceEntry}${DEFINITION_FILE_EXTENSION}`
|
|
127
|
+
);
|
|
128
|
+
const mfeTypeEntryDirectory = (0, import_path.dirname)(mfeTypeEntry);
|
|
129
|
+
const relativePathToOutput = (0, import_path.relative)(mfeTypeEntryDirectory, filepath).replace(DEFINITION_FILE_EXTENSION, "").replace(STARTS_WITH_SLASH, "");
|
|
130
|
+
originalWriteFile(
|
|
131
|
+
mfeTypeEntry,
|
|
132
|
+
`export * from './${relativePathToOutput}';
|
|
133
|
+
export { default } from './${relativePathToOutput}';`,
|
|
134
|
+
writeOrderByteMark
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
cb(text);
|
|
139
|
+
};
|
|
140
|
+
return host;
|
|
141
|
+
};
|
|
142
|
+
var createVueTscProgram = (programOptions) => {
|
|
143
|
+
const vueTypescript = require("vue-tsc");
|
|
144
|
+
return vueTypescript.createProgram(programOptions);
|
|
145
|
+
};
|
|
146
|
+
var createProgram = (remoteOptions, programOptions) => {
|
|
147
|
+
switch (remoteOptions.compilerInstance) {
|
|
148
|
+
case "vue-tsc":
|
|
149
|
+
return createVueTscProgram(programOptions);
|
|
150
|
+
case "tsc":
|
|
151
|
+
default:
|
|
152
|
+
return import_typescript.default.createProgram(programOptions);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
var compileTs = (mapComponentsToExpose, tsConfig, remoteOptions) => {
|
|
156
|
+
const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
157
|
+
const thirdPartyExtractor = new import_third_party_dts_extractor.ThirdPartyExtractor(
|
|
158
|
+
(0, import_path.resolve)(mfTypePath, "node_modules"),
|
|
159
|
+
remoteOptions.context
|
|
160
|
+
);
|
|
161
|
+
const cb = remoteOptions.extractThirdParty ? thirdPartyExtractor.collectPkgs.bind(thirdPartyExtractor) : () => void 0;
|
|
162
|
+
const tsHost = createHost(mapComponentsToExpose, tsConfig, remoteOptions, cb);
|
|
163
|
+
const filesToCompile = [
|
|
164
|
+
...Object.values(mapComponentsToExpose),
|
|
165
|
+
...remoteOptions.additionalFilesToCompile
|
|
166
|
+
];
|
|
167
|
+
const programOptions = {
|
|
168
|
+
rootNames: filesToCompile,
|
|
169
|
+
host: tsHost,
|
|
170
|
+
options: tsConfig
|
|
171
|
+
};
|
|
172
|
+
const tsProgram = createProgram(remoteOptions, programOptions);
|
|
173
|
+
const { diagnostics = [] } = tsProgram.emit();
|
|
174
|
+
diagnostics.forEach(reportCompileDiagnostic);
|
|
175
|
+
if (remoteOptions.extractThirdParty) {
|
|
176
|
+
thirdPartyExtractor.copyDts();
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
// packages/dts-plugin/src/core/lib/archiveHandler.ts
|
|
181
|
+
var retrieveTypesZipPath = (mfTypesPath, remoteOptions) => (0, import_path2.join)(
|
|
182
|
+
mfTypesPath.replace(remoteOptions.typesFolder, ""),
|
|
183
|
+
`${remoteOptions.typesFolder}.zip`
|
|
184
|
+
);
|
|
185
|
+
var createTypesArchive = async (tsConfig, remoteOptions) => {
|
|
186
|
+
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
187
|
+
const zip = new import_adm_zip.default();
|
|
188
|
+
zip.addLocalFolder(mfTypesPath);
|
|
189
|
+
return zip.writeZipPromise(retrieveTypesZipPath(mfTypesPath, remoteOptions));
|
|
190
|
+
};
|
|
191
|
+
var downloadErrorLogger = (destinationFolder, fileToDownload) => (reason) => {
|
|
192
|
+
throw {
|
|
193
|
+
...reason,
|
|
194
|
+
message: `Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${reason.message}'`
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
var retrieveTypesArchiveDestinationPath = (hostOptions, destinationFolder) => {
|
|
198
|
+
return (0, import_path2.resolve)(
|
|
199
|
+
hostOptions.context,
|
|
200
|
+
hostOptions.typesFolder,
|
|
201
|
+
destinationFolder
|
|
202
|
+
);
|
|
203
|
+
};
|
|
204
|
+
var downloadTypesArchive = (hostOptions) => {
|
|
205
|
+
let retries = 0;
|
|
206
|
+
return async ([destinationFolder, fileToDownload]) => {
|
|
207
|
+
const destinationPath = retrieveTypesArchiveDestinationPath(
|
|
208
|
+
hostOptions,
|
|
209
|
+
destinationFolder
|
|
210
|
+
);
|
|
211
|
+
while (retries++ < hostOptions.maxRetries) {
|
|
212
|
+
try {
|
|
213
|
+
const url = replaceLocalhost(fileToDownload);
|
|
214
|
+
const response = await import_axios.default.get(url, { responseType: "arraybuffer" }).catch(downloadErrorLogger(destinationFolder, url));
|
|
215
|
+
const zip = new import_adm_zip.default(Buffer.from(response.data));
|
|
216
|
+
zip.extractAllTo(destinationPath, true);
|
|
217
|
+
return [destinationFolder, destinationPath];
|
|
218
|
+
} catch (error2) {
|
|
219
|
+
if (isDebugMode()) {
|
|
220
|
+
console.error(
|
|
221
|
+
import_ansi_colors2.default.red(
|
|
222
|
+
`Error during types archive download: ${(error2 == null ? void 0 : error2.message) || "unknown error"}`
|
|
223
|
+
)
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
if (retries >= hostOptions.maxRetries) {
|
|
227
|
+
if (hostOptions.abortOnError !== false) {
|
|
228
|
+
throw error2;
|
|
229
|
+
}
|
|
230
|
+
return void 0;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// packages/dts-plugin/src/core/configurations/hostPlugin.ts
|
|
238
|
+
var import_sdk = require("@module-federation/sdk");
|
|
239
|
+
var import_managers = require("@module-federation/managers");
|
|
240
|
+
var defaultOptions = {
|
|
241
|
+
typesFolder: "@mf-types",
|
|
242
|
+
remoteTypesFolder: "@mf-types",
|
|
243
|
+
deleteTypesFolder: true,
|
|
244
|
+
maxRetries: 3,
|
|
245
|
+
implementation: "",
|
|
246
|
+
context: process.cwd(),
|
|
247
|
+
abortOnError: true,
|
|
248
|
+
consumeAPITypes: false
|
|
249
|
+
};
|
|
250
|
+
var buildZipUrl = (hostOptions, url) => {
|
|
251
|
+
const remoteUrl = new URL(url);
|
|
252
|
+
if (remoteUrl.href.includes(import_sdk.MANIFEST_EXT)) {
|
|
253
|
+
return void 0;
|
|
254
|
+
}
|
|
255
|
+
const pathnameWithoutEntry = remoteUrl.pathname.split("/").slice(0, -1).join("/");
|
|
256
|
+
remoteUrl.pathname = `${pathnameWithoutEntry}/${hostOptions.remoteTypesFolder}.zip`;
|
|
257
|
+
return remoteUrl.href;
|
|
258
|
+
};
|
|
259
|
+
var buildApiTypeUrl = (zipUrl) => {
|
|
260
|
+
if (!zipUrl) {
|
|
261
|
+
return void 0;
|
|
262
|
+
}
|
|
263
|
+
return zipUrl.replace(".zip", ".d.ts");
|
|
264
|
+
};
|
|
265
|
+
var retrieveRemoteInfo = (options) => {
|
|
266
|
+
const { hostOptions, remoteAlias, remote } = options;
|
|
267
|
+
const parsedInfo = (0, import_sdk.parseEntry)(remote, void 0, "@");
|
|
268
|
+
const url = "entry" in parsedInfo ? parsedInfo.entry : parsedInfo.name === remote ? remote : "";
|
|
269
|
+
const zipUrl = url ? buildZipUrl(hostOptions, url) : "";
|
|
270
|
+
return {
|
|
271
|
+
name: parsedInfo.name || remoteAlias,
|
|
272
|
+
url,
|
|
273
|
+
zipUrl,
|
|
274
|
+
apiTypeUrl: buildApiTypeUrl(zipUrl),
|
|
275
|
+
alias: remoteAlias
|
|
276
|
+
};
|
|
277
|
+
};
|
|
278
|
+
var resolveRemotes = (hostOptions) => {
|
|
279
|
+
const parsedOptions = import_managers.utils.parseOptions(
|
|
280
|
+
hostOptions.moduleFederationConfig.remotes || {},
|
|
281
|
+
(item, key) => ({
|
|
282
|
+
remote: Array.isArray(item) ? item[0] : item,
|
|
283
|
+
key
|
|
284
|
+
}),
|
|
285
|
+
(item, key) => ({
|
|
286
|
+
remote: Array.isArray(item.external) ? item.external[0] : item.external,
|
|
287
|
+
key
|
|
288
|
+
})
|
|
289
|
+
);
|
|
290
|
+
return parsedOptions.reduce(
|
|
291
|
+
(accumulator, item) => {
|
|
292
|
+
const { key, remote } = item[1];
|
|
293
|
+
accumulator[key] = retrieveRemoteInfo({
|
|
294
|
+
hostOptions,
|
|
295
|
+
remoteAlias: key,
|
|
296
|
+
remote
|
|
297
|
+
});
|
|
298
|
+
return accumulator;
|
|
299
|
+
},
|
|
300
|
+
{}
|
|
301
|
+
);
|
|
302
|
+
};
|
|
303
|
+
var retrieveHostConfig = (options) => {
|
|
304
|
+
validateOptions(options);
|
|
305
|
+
const hostOptions = { ...defaultOptions, ...options };
|
|
306
|
+
const mapRemotesToDownload = resolveRemotes(hostOptions);
|
|
307
|
+
return {
|
|
308
|
+
hostOptions,
|
|
309
|
+
mapRemotesToDownload
|
|
310
|
+
};
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// packages/dts-plugin/src/core/constant.ts
|
|
314
|
+
var REMOTE_ALIAS_IDENTIFIER = "REMOTE_ALIAS_IDENTIFIER";
|
|
315
|
+
var REMOTE_API_TYPES_FILE_NAME = "apis.d.ts";
|
|
316
|
+
var HOST_API_TYPES_FILE_NAME = "index.d.ts";
|
|
317
|
+
|
|
318
|
+
// packages/dts-plugin/src/core/lib/DTSManager.ts
|
|
319
|
+
var import_axios2 = __toESM(require("axios"));
|
|
320
|
+
var DTSManager = class {
|
|
321
|
+
constructor(options) {
|
|
322
|
+
this.options = (0, import_lodash.default)(options, (_value, key) => {
|
|
323
|
+
if (key === "manifest") {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
this.runtimePkgs = [
|
|
328
|
+
"@module-federation/runtime",
|
|
329
|
+
"@module-federation/runtime-tools"
|
|
330
|
+
];
|
|
331
|
+
this.loadedRemoteAPIAlias = [];
|
|
332
|
+
this.remoteAliasMap = {};
|
|
333
|
+
this.extraOptions = (options == null ? void 0 : options.extraOptions) || {};
|
|
334
|
+
}
|
|
335
|
+
generateAPITypes(mapComponentsToExpose) {
|
|
336
|
+
const exposePaths = /* @__PURE__ */ new Set();
|
|
337
|
+
const packageType = Object.keys(mapComponentsToExpose).reduce(
|
|
338
|
+
(sum, exposeKey) => {
|
|
339
|
+
const exposePath = import_path3.default.join(REMOTE_ALIAS_IDENTIFIER, exposeKey);
|
|
340
|
+
exposePaths.add(`'${exposePath}'`);
|
|
341
|
+
const curType = `T extends '${exposePath}' ? typeof import('${exposePath}') :`;
|
|
342
|
+
sum = curType + sum;
|
|
343
|
+
return sum;
|
|
344
|
+
},
|
|
345
|
+
"any;"
|
|
346
|
+
);
|
|
347
|
+
const exposePathKeys = [...exposePaths].join(" | ");
|
|
348
|
+
return `
|
|
349
|
+
export type RemoteKeys = ${exposePathKeys};
|
|
350
|
+
type PackageType<T> = ${packageType}`;
|
|
351
|
+
}
|
|
352
|
+
async extractRemoteTypes(options) {
|
|
353
|
+
const { remoteOptions, tsConfig } = options;
|
|
354
|
+
if (!remoteOptions.extractRemoteTypes) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
let hasRemotes = false;
|
|
358
|
+
const remotes = remoteOptions.moduleFederationConfig.remotes;
|
|
359
|
+
if (remotes) {
|
|
360
|
+
if (Array.isArray(remotes)) {
|
|
361
|
+
hasRemotes = Boolean(remotes.length);
|
|
362
|
+
} else if (typeof remotes === "object") {
|
|
363
|
+
hasRemotes = Boolean(Object.keys(remotes).length);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
367
|
+
if (hasRemotes) {
|
|
368
|
+
const tempHostOptions = {
|
|
369
|
+
moduleFederationConfig: remoteOptions.moduleFederationConfig,
|
|
370
|
+
typesFolder: import_path3.default.join(mfTypesPath, "node_modules"),
|
|
371
|
+
remoteTypesFolder: (remoteOptions == null ? void 0 : remoteOptions.hostRemoteTypesFolder) || remoteOptions.typesFolder,
|
|
372
|
+
deleteTypesFolder: true,
|
|
373
|
+
context: remoteOptions.context,
|
|
374
|
+
implementation: remoteOptions.implementation,
|
|
375
|
+
abortOnError: false
|
|
376
|
+
};
|
|
377
|
+
await this.consumeArchiveTypes(tempHostOptions);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async generateTypes() {
|
|
381
|
+
var _a;
|
|
382
|
+
try {
|
|
383
|
+
const { options } = this;
|
|
384
|
+
if (!options.remote) {
|
|
385
|
+
throw new Error(
|
|
386
|
+
"options.remote is required if you want to generateTypes"
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
const { remoteOptions, tsConfig, mapComponentsToExpose } = retrieveRemoteConfig(options.remote);
|
|
390
|
+
if (!Object.keys(mapComponentsToExpose).length) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
this.extractRemoteTypes({
|
|
394
|
+
remoteOptions,
|
|
395
|
+
tsConfig,
|
|
396
|
+
mapComponentsToExpose
|
|
397
|
+
});
|
|
398
|
+
compileTs(mapComponentsToExpose, tsConfig, remoteOptions);
|
|
399
|
+
await createTypesArchive(tsConfig, remoteOptions);
|
|
400
|
+
let apiTypesPath = "";
|
|
401
|
+
if (remoteOptions.generateAPITypes) {
|
|
402
|
+
const apiTypes = this.generateAPITypes(mapComponentsToExpose);
|
|
403
|
+
apiTypesPath = retrieveMfAPITypesPath(tsConfig, remoteOptions);
|
|
404
|
+
import_fs.default.writeFileSync(apiTypesPath, apiTypes);
|
|
405
|
+
}
|
|
406
|
+
if (remoteOptions.deleteTypesFolder) {
|
|
407
|
+
await (0, import_promises.rm)(retrieveMfTypesPath(tsConfig, remoteOptions), {
|
|
408
|
+
recursive: true,
|
|
409
|
+
force: true
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
console.log(import_ansi_colors3.default.green("Federated types created correctly"));
|
|
413
|
+
} catch (error2) {
|
|
414
|
+
if (((_a = this.options.remote) == null ? void 0 : _a.abortOnError) === false) {
|
|
415
|
+
console.error(
|
|
416
|
+
import_ansi_colors3.default.red(`Unable to compile federated types, ${error2}`)
|
|
417
|
+
);
|
|
418
|
+
} else {
|
|
419
|
+
throw error2;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
async requestRemoteManifest(remoteInfo) {
|
|
424
|
+
try {
|
|
425
|
+
if (!remoteInfo.url.includes(import_sdk2.MANIFEST_EXT)) {
|
|
426
|
+
return remoteInfo;
|
|
427
|
+
}
|
|
428
|
+
const url = replaceLocalhost(remoteInfo.url);
|
|
429
|
+
const res = await (0, import_axios2.default)({
|
|
430
|
+
method: "get",
|
|
431
|
+
url
|
|
432
|
+
});
|
|
433
|
+
const manifestJson = res.data;
|
|
434
|
+
if (!manifestJson.metaData.types.zip) {
|
|
435
|
+
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
|
|
436
|
+
}
|
|
437
|
+
const addProtocol = (u) => {
|
|
438
|
+
if (u.startsWith("//")) {
|
|
439
|
+
return `https:${u}`;
|
|
440
|
+
}
|
|
441
|
+
return u;
|
|
442
|
+
};
|
|
443
|
+
const publicPath = "publicPath" in manifestJson.metaData ? manifestJson.metaData.publicPath : new Function(manifestJson.metaData.getPublicPath)();
|
|
444
|
+
remoteInfo.zipUrl = new URL(
|
|
445
|
+
import_path3.default.join(addProtocol(publicPath), manifestJson.metaData.types.zip)
|
|
446
|
+
).href;
|
|
447
|
+
if (!manifestJson.metaData.types.api) {
|
|
448
|
+
console.warn(`Can not get ${remoteInfo.name}'s api types url!`);
|
|
449
|
+
remoteInfo.apiTypeUrl = "";
|
|
450
|
+
return remoteInfo;
|
|
451
|
+
}
|
|
452
|
+
remoteInfo.apiTypeUrl = new URL(
|
|
453
|
+
import_path3.default.join(addProtocol(publicPath), manifestJson.metaData.types.api)
|
|
454
|
+
).href;
|
|
455
|
+
return remoteInfo;
|
|
456
|
+
} catch (_err) {
|
|
457
|
+
console.error(_err);
|
|
458
|
+
return remoteInfo;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
async consumeTargetRemotes(hostOptions, remoteInfo) {
|
|
462
|
+
if (!remoteInfo.zipUrl) {
|
|
463
|
+
throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
|
|
464
|
+
}
|
|
465
|
+
const typesDownloader = downloadTypesArchive(hostOptions);
|
|
466
|
+
return typesDownloader([remoteInfo.alias, remoteInfo.zipUrl]);
|
|
467
|
+
}
|
|
468
|
+
async downloadAPITypes(remoteInfo, destinationPath) {
|
|
469
|
+
const { apiTypeUrl } = remoteInfo;
|
|
470
|
+
if (!apiTypeUrl) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
try {
|
|
474
|
+
const url = replaceLocalhost(apiTypeUrl);
|
|
475
|
+
const res = await import_axios2.default.get(url);
|
|
476
|
+
let apiTypeFile = res.data;
|
|
477
|
+
apiTypeFile = apiTypeFile.replaceAll(
|
|
478
|
+
REMOTE_ALIAS_IDENTIFIER,
|
|
479
|
+
remoteInfo.alias
|
|
480
|
+
);
|
|
481
|
+
const filePath = import_path3.default.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
|
|
482
|
+
import_fs.default.writeFileSync(filePath, apiTypeFile);
|
|
483
|
+
this.loadedRemoteAPIAlias.push(remoteInfo.alias);
|
|
484
|
+
} catch (err) {
|
|
485
|
+
console.error(
|
|
486
|
+
import_ansi_colors3.default.red(
|
|
487
|
+
`Unable to download "${remoteInfo.name}" api types, ${err}`
|
|
488
|
+
)
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
consumeAPITypes(hostOptions) {
|
|
493
|
+
if (!this.loadedRemoteAPIAlias.length) {
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const packageTypes = [];
|
|
497
|
+
const remoteKeys = [];
|
|
498
|
+
const importTypeStr = this.loadedRemoteAPIAlias.map((alias, index) => {
|
|
499
|
+
const remoteKey = `RemoteKeys_${index}`;
|
|
500
|
+
const packageType = `PackageType_${index}`;
|
|
501
|
+
packageTypes.push(`T extends ${remoteKey} ? ${packageType}<T>`);
|
|
502
|
+
remoteKeys.push(remoteKey);
|
|
503
|
+
return `import type { PackageType as ${packageType},RemoteKeys as ${remoteKey} } from './${alias}/apis.d.ts';`;
|
|
504
|
+
}).join("\n");
|
|
505
|
+
const remoteKeysStr = `type RemoteKeys = ${remoteKeys.join(" | ")};`;
|
|
506
|
+
const packageTypesStr = `type PackageType<T, Y=any> = ${[
|
|
507
|
+
...packageTypes,
|
|
508
|
+
"Y"
|
|
509
|
+
].join(" :\n")} ;`;
|
|
510
|
+
const pkgsDeclareStr = this.runtimePkgs.map((pkg) => {
|
|
511
|
+
return `declare module "${pkg}" {
|
|
512
|
+
${remoteKeysStr}
|
|
513
|
+
${packageTypesStr}
|
|
514
|
+
export function loadRemote<T extends RemoteKeys,Y>(packageName: T): Promise<PackageType<T, Y>>;
|
|
515
|
+
export function loadRemote<T extends string,Y>(packageName: T): Promise<PackageType<T, Y>>;
|
|
516
|
+
}`;
|
|
517
|
+
}).join("\n");
|
|
518
|
+
const fileStr = `${importTypeStr}
|
|
519
|
+
${pkgsDeclareStr}
|
|
520
|
+
`;
|
|
521
|
+
import_fs.default.writeFileSync(
|
|
522
|
+
import_path3.default.join(
|
|
523
|
+
hostOptions.context,
|
|
524
|
+
hostOptions.typesFolder,
|
|
525
|
+
HOST_API_TYPES_FILE_NAME
|
|
526
|
+
),
|
|
527
|
+
fileStr
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
async consumeArchiveTypes(options) {
|
|
531
|
+
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(options);
|
|
532
|
+
if (hostOptions.deleteTypesFolder) {
|
|
533
|
+
await (0, import_promises.rm)(hostOptions.typesFolder, {
|
|
534
|
+
recursive: true,
|
|
535
|
+
force: true
|
|
536
|
+
}).catch(
|
|
537
|
+
(error2) => console.error(
|
|
538
|
+
import_ansi_colors3.default.red(`Unable to remove types folder, ${error2}`)
|
|
539
|
+
)
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
const downloadPromises = Object.entries(mapRemotesToDownload).map(
|
|
543
|
+
async (item) => {
|
|
544
|
+
const remoteInfo = item[1];
|
|
545
|
+
if (!this.remoteAliasMap[remoteInfo.alias]) {
|
|
546
|
+
const requiredRemoteInfo = await this.requestRemoteManifest(remoteInfo);
|
|
547
|
+
this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
|
|
548
|
+
}
|
|
549
|
+
return this.consumeTargetRemotes(
|
|
550
|
+
hostOptions,
|
|
551
|
+
this.remoteAliasMap[remoteInfo.alias]
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
);
|
|
555
|
+
const downloadPromisesResult = await Promise.allSettled(downloadPromises);
|
|
556
|
+
return {
|
|
557
|
+
hostOptions,
|
|
558
|
+
downloadPromisesResult
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
async consumeTypes() {
|
|
562
|
+
var _a;
|
|
563
|
+
try {
|
|
564
|
+
const { options } = this;
|
|
565
|
+
if (!options.host) {
|
|
566
|
+
throw new Error("options.host is required if you want to consumeTypes");
|
|
567
|
+
}
|
|
568
|
+
const { mapRemotesToDownload } = retrieveHostConfig(options.host);
|
|
569
|
+
if (!Object.keys(mapRemotesToDownload).length) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
const { downloadPromisesResult, hostOptions } = await this.consumeArchiveTypes(options.host);
|
|
573
|
+
if (hostOptions.consumeAPITypes) {
|
|
574
|
+
await Promise.all(
|
|
575
|
+
downloadPromisesResult.map(async (item) => {
|
|
576
|
+
if (item.status === "rejected" || !item.value) {
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
const [alias, destinationPath] = item.value;
|
|
580
|
+
const remoteInfo = this.remoteAliasMap[alias];
|
|
581
|
+
if (!remoteInfo) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
await this.downloadAPITypes(remoteInfo, destinationPath);
|
|
585
|
+
})
|
|
586
|
+
);
|
|
587
|
+
this.consumeAPITypes(hostOptions);
|
|
588
|
+
}
|
|
589
|
+
console.log(import_ansi_colors3.default.green("Federated types extraction completed"));
|
|
590
|
+
} catch (err) {
|
|
591
|
+
if (((_a = this.options.host) == null ? void 0 : _a.abortOnError) === false) {
|
|
592
|
+
console.error(
|
|
593
|
+
import_ansi_colors3.default.red(`Unable to consume federated types, ${err}`)
|
|
594
|
+
);
|
|
595
|
+
} else {
|
|
596
|
+
throw err;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
async updateTypes(options) {
|
|
601
|
+
var _a, _b, _c;
|
|
602
|
+
const { remoteName, updateMode } = options;
|
|
603
|
+
const hostName = (_c = (_b = (_a = this.options) == null ? void 0 : _a.host) == null ? void 0 : _b.moduleFederationConfig) == null ? void 0 : _c.name;
|
|
604
|
+
if (updateMode === "POSITIVE" /* POSITIVE */ && remoteName === hostName) {
|
|
605
|
+
if (!this.options.remote) {
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
this.generateTypes();
|
|
609
|
+
} else {
|
|
610
|
+
const { remoteAliasMap } = this;
|
|
611
|
+
if (!this.options.host) {
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(
|
|
615
|
+
this.options.host
|
|
616
|
+
);
|
|
617
|
+
const loadedRemoteInfo = Object.values(remoteAliasMap).find(
|
|
618
|
+
(i) => i.name === remoteName
|
|
619
|
+
);
|
|
620
|
+
if (!loadedRemoteInfo) {
|
|
621
|
+
const remoteInfo = Object.values(mapRemotesToDownload).find((item) => {
|
|
622
|
+
return item.name === remoteName;
|
|
623
|
+
});
|
|
624
|
+
if (remoteInfo) {
|
|
625
|
+
if (!this.remoteAliasMap[remoteInfo.alias]) {
|
|
626
|
+
const requiredRemoteInfo = await this.requestRemoteManifest(remoteInfo);
|
|
627
|
+
this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
|
|
628
|
+
}
|
|
629
|
+
await this.consumeTargetRemotes(
|
|
630
|
+
hostOptions,
|
|
631
|
+
this.remoteAliasMap[remoteInfo.alias]
|
|
632
|
+
);
|
|
633
|
+
}
|
|
634
|
+
} else {
|
|
635
|
+
await this.consumeTargetRemotes(hostOptions, loadedRemoteInfo);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
// packages/dts-plugin/src/core/lib/utils.ts
|
|
642
|
+
var import_ansi_colors4 = __toESM(require("ansi-colors"));
|
|
643
|
+
function getDTSManagerConstructor(implementation) {
|
|
644
|
+
if (implementation) {
|
|
645
|
+
const NewConstructor = require(implementation);
|
|
646
|
+
return NewConstructor.default ? NewConstructor.default : NewConstructor;
|
|
647
|
+
}
|
|
648
|
+
return DTSManager;
|
|
649
|
+
}
|
|
650
|
+
var validateOptions = (options) => {
|
|
651
|
+
if (!options.moduleFederationConfig) {
|
|
652
|
+
throw new Error("moduleFederationConfig is required");
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
function retrieveTypesAssetsInfo(options) {
|
|
656
|
+
let apiTypesPath = "";
|
|
657
|
+
let zipTypesPath = "";
|
|
658
|
+
try {
|
|
659
|
+
const { tsConfig, remoteOptions, mapComponentsToExpose } = retrieveRemoteConfig(options);
|
|
660
|
+
if (!Object.keys(mapComponentsToExpose).length) {
|
|
661
|
+
return {
|
|
662
|
+
apiTypesPath,
|
|
663
|
+
zipTypesPath,
|
|
664
|
+
zipName: "",
|
|
665
|
+
apiFileName: ""
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
|
|
669
|
+
zipTypesPath = retrieveTypesZipPath(mfTypesPath, remoteOptions);
|
|
670
|
+
if (remoteOptions.generateAPITypes) {
|
|
671
|
+
apiTypesPath = retrieveMfAPITypesPath(tsConfig, remoteOptions);
|
|
672
|
+
}
|
|
673
|
+
return {
|
|
674
|
+
apiTypesPath,
|
|
675
|
+
zipTypesPath,
|
|
676
|
+
zipName: import_path4.default.basename(zipTypesPath),
|
|
677
|
+
apiFileName: import_path4.default.basename(apiTypesPath)
|
|
678
|
+
};
|
|
679
|
+
} catch (err) {
|
|
680
|
+
console.error(import_ansi_colors4.default.red(`Unable to compile federated types, ${err}`));
|
|
681
|
+
return {
|
|
682
|
+
apiTypesPath: "",
|
|
683
|
+
zipTypesPath: "",
|
|
684
|
+
zipName: "",
|
|
685
|
+
apiFileName: ""
|
|
686
|
+
};
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
function replaceLocalhost(url) {
|
|
690
|
+
return url.replace("localhost", "127.0.0.1");
|
|
691
|
+
}
|
|
692
|
+
function isDebugMode() {
|
|
693
|
+
return Boolean(process.env["FEDERATION_DEBUG"]);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// packages/dts-plugin/src/core/configurations/remotePlugin.ts
|
|
697
|
+
var defaultOptions2 = {
|
|
698
|
+
tsConfigPath: "./tsconfig.json",
|
|
699
|
+
typesFolder: "@mf-types",
|
|
700
|
+
compiledTypesFolder: "compiled-types",
|
|
701
|
+
hostRemoteTypesFolder: "@mf-types",
|
|
702
|
+
deleteTypesFolder: true,
|
|
703
|
+
additionalFilesToCompile: [],
|
|
704
|
+
compilerInstance: "tsc",
|
|
705
|
+
compileInChildProcess: false,
|
|
706
|
+
implementation: "",
|
|
707
|
+
generateAPITypes: false,
|
|
708
|
+
context: process.cwd(),
|
|
709
|
+
abortOnError: true,
|
|
710
|
+
extractRemoteTypes: false,
|
|
711
|
+
extractThirdParty: false
|
|
712
|
+
};
|
|
713
|
+
var readTsConfig = ({
|
|
714
|
+
tsConfigPath,
|
|
715
|
+
typesFolder,
|
|
716
|
+
compiledTypesFolder,
|
|
717
|
+
context
|
|
718
|
+
}) => {
|
|
719
|
+
const resolvedTsConfigPath = (0, import_path5.resolve)(context, tsConfigPath);
|
|
720
|
+
const readResult = import_typescript2.default.readConfigFile(
|
|
721
|
+
resolvedTsConfigPath,
|
|
722
|
+
import_typescript2.default.sys.readFile
|
|
723
|
+
);
|
|
724
|
+
if (readResult.error) {
|
|
725
|
+
throw new Error(readResult.error.messageText.toString());
|
|
726
|
+
}
|
|
727
|
+
const configContent = import_typescript2.default.parseJsonConfigFileContent(
|
|
728
|
+
readResult.config,
|
|
729
|
+
import_typescript2.default.sys,
|
|
730
|
+
(0, import_path5.dirname)(resolvedTsConfigPath)
|
|
731
|
+
);
|
|
732
|
+
const outDir = (0, import_path5.resolve)(
|
|
733
|
+
context,
|
|
734
|
+
configContent.options.outDir || "dist",
|
|
735
|
+
typesFolder,
|
|
736
|
+
compiledTypesFolder
|
|
737
|
+
);
|
|
738
|
+
return {
|
|
739
|
+
...configContent.options,
|
|
740
|
+
emitDeclarationOnly: true,
|
|
741
|
+
noEmit: false,
|
|
742
|
+
declaration: true,
|
|
743
|
+
outDir
|
|
744
|
+
};
|
|
745
|
+
};
|
|
746
|
+
var TS_EXTENSIONS = ["ts", "tsx", "vue", "svelte"];
|
|
747
|
+
var resolveWithExtension = (exposedPath, context) => {
|
|
748
|
+
if ((0, import_path5.extname)(exposedPath)) {
|
|
749
|
+
return (0, import_path5.resolve)(context, exposedPath);
|
|
750
|
+
}
|
|
751
|
+
for (const extension of TS_EXTENSIONS) {
|
|
752
|
+
const exposedPathWithExtension = (0, import_path5.resolve)(
|
|
753
|
+
context,
|
|
754
|
+
`${exposedPath}.${extension}`
|
|
755
|
+
);
|
|
756
|
+
if ((0, import_fs2.existsSync)(exposedPathWithExtension)) {
|
|
757
|
+
return exposedPathWithExtension;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
return void 0;
|
|
761
|
+
};
|
|
762
|
+
var resolveExposes = (remoteOptions) => {
|
|
763
|
+
const parsedOptions = import_managers2.utils.parseOptions(
|
|
764
|
+
remoteOptions.moduleFederationConfig.exposes || {},
|
|
765
|
+
(item, key) => ({
|
|
766
|
+
exposePath: Array.isArray(item) ? item[0] : item,
|
|
767
|
+
key
|
|
768
|
+
}),
|
|
769
|
+
(item, key) => ({
|
|
770
|
+
exposePath: Array.isArray(item.import) ? item.import[0] : item.import[0],
|
|
771
|
+
key
|
|
772
|
+
})
|
|
773
|
+
);
|
|
774
|
+
return parsedOptions.reduce(
|
|
775
|
+
(accumulator, item) => {
|
|
776
|
+
const { exposePath, key } = item[1];
|
|
777
|
+
accumulator[key] = resolveWithExtension(exposePath, remoteOptions.context) || resolveWithExtension(
|
|
778
|
+
(0, import_path5.join)(exposePath, "index"),
|
|
779
|
+
remoteOptions.context
|
|
780
|
+
) || exposePath;
|
|
781
|
+
return accumulator;
|
|
782
|
+
},
|
|
783
|
+
{}
|
|
784
|
+
);
|
|
785
|
+
};
|
|
786
|
+
var retrieveRemoteConfig = (options) => {
|
|
787
|
+
validateOptions(options);
|
|
788
|
+
const remoteOptions = {
|
|
789
|
+
...defaultOptions2,
|
|
790
|
+
...options
|
|
791
|
+
};
|
|
792
|
+
const mapComponentsToExpose = resolveExposes(remoteOptions);
|
|
793
|
+
const tsConfig = readTsConfig(remoteOptions);
|
|
794
|
+
return {
|
|
795
|
+
tsConfig,
|
|
796
|
+
mapComponentsToExpose,
|
|
797
|
+
remoteOptions
|
|
798
|
+
};
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
// packages/dts-plugin/src/core/lib/generateTypes.ts
|
|
802
|
+
async function generateTypes(options) {
|
|
803
|
+
var _a;
|
|
804
|
+
const DTSManagerConstructor = getDTSManagerConstructor(
|
|
805
|
+
(_a = options.remote) == null ? void 0 : _a.implementation
|
|
806
|
+
);
|
|
807
|
+
const dtsManager = new DTSManagerConstructor(options);
|
|
808
|
+
return dtsManager.generateTypes();
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// packages/dts-plugin/src/core/lib/DtsWorker.ts
|
|
812
|
+
var import_path6 = __toESM(require("path"));
|
|
813
|
+
var import_lodash2 = __toESM(require("lodash.clonedeepwith"));
|
|
814
|
+
|
|
815
|
+
// packages/dts-plugin/src/core/rpc/index.ts
|
|
816
|
+
var rpc_exports = {};
|
|
817
|
+
__export(rpc_exports, {
|
|
818
|
+
RpcExitError: () => RpcExitError,
|
|
819
|
+
RpcGMCallTypes: () => RpcGMCallTypes,
|
|
820
|
+
createRpcWorker: () => createRpcWorker,
|
|
821
|
+
exposeRpc: () => exposeRpc,
|
|
822
|
+
getRpcWorkerData: () => getRpcWorkerData,
|
|
823
|
+
wrapRpc: () => wrapRpc
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
// packages/dts-plugin/src/core/rpc/expose-rpc.ts
|
|
827
|
+
var import_process = __toESM(require("process"));
|
|
828
|
+
|
|
829
|
+
// packages/dts-plugin/src/core/rpc/types.ts
|
|
830
|
+
var RpcGMCallTypes = /* @__PURE__ */ ((RpcGMCallTypes2) => {
|
|
831
|
+
RpcGMCallTypes2["CALL"] = "mf_call";
|
|
832
|
+
RpcGMCallTypes2["RESOLVE"] = "mf_resolve";
|
|
833
|
+
RpcGMCallTypes2["REJECT"] = "mf_reject";
|
|
834
|
+
RpcGMCallTypes2["EXIT"] = "mf_exit";
|
|
835
|
+
return RpcGMCallTypes2;
|
|
836
|
+
})(RpcGMCallTypes || {});
|
|
837
|
+
|
|
838
|
+
// packages/dts-plugin/src/core/rpc/expose-rpc.ts
|
|
839
|
+
function exposeRpc(fn) {
|
|
840
|
+
const sendMessage = (message) => new Promise((resolve5, reject) => {
|
|
841
|
+
if (!import_process.default.send) {
|
|
842
|
+
reject(new Error(`Process ${import_process.default.pid} doesn't have IPC channels`));
|
|
843
|
+
} else if (!import_process.default.connected) {
|
|
844
|
+
reject(
|
|
845
|
+
new Error(`Process ${import_process.default.pid} doesn't have open IPC channels`)
|
|
846
|
+
);
|
|
847
|
+
} else {
|
|
848
|
+
import_process.default.send(message, void 0, void 0, (error2) => {
|
|
849
|
+
if (error2) {
|
|
850
|
+
reject(error2);
|
|
851
|
+
} else {
|
|
852
|
+
resolve5(void 0);
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
const handleMessage = async (message) => {
|
|
858
|
+
if (message.type === "mf_call" /* CALL */) {
|
|
859
|
+
if (!import_process.default.send) {
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
let value, error2;
|
|
863
|
+
try {
|
|
864
|
+
value = await fn(...message.args);
|
|
865
|
+
} catch (fnError) {
|
|
866
|
+
error2 = fnError;
|
|
867
|
+
}
|
|
868
|
+
try {
|
|
869
|
+
if (error2) {
|
|
870
|
+
await sendMessage({
|
|
871
|
+
type: "mf_reject" /* REJECT */,
|
|
872
|
+
id: message.id,
|
|
873
|
+
error: error2
|
|
874
|
+
});
|
|
875
|
+
} else {
|
|
876
|
+
await sendMessage({
|
|
877
|
+
type: "mf_resolve" /* RESOLVE */,
|
|
878
|
+
id: message.id,
|
|
879
|
+
value
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
} catch (sendError) {
|
|
883
|
+
if (error2) {
|
|
884
|
+
if (error2 instanceof Error) {
|
|
885
|
+
console.error(error2);
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
console.error(sendError);
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
};
|
|
892
|
+
import_process.default.on("message", handleMessage);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// packages/dts-plugin/src/core/rpc/rpc-error.ts
|
|
896
|
+
var RpcExitError = class extends Error {
|
|
897
|
+
constructor(message, code, signal) {
|
|
898
|
+
super(message);
|
|
899
|
+
this.code = code;
|
|
900
|
+
this.signal = signal;
|
|
901
|
+
this.name = "RpcExitError";
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
|
|
905
|
+
// packages/dts-plugin/src/core/rpc/wrap-rpc.ts
|
|
906
|
+
function createControlledPromise() {
|
|
907
|
+
let resolve5 = () => void 0;
|
|
908
|
+
let reject = () => void 0;
|
|
909
|
+
const promise = new Promise((aResolve, aReject) => {
|
|
910
|
+
resolve5 = aResolve;
|
|
911
|
+
reject = aReject;
|
|
912
|
+
});
|
|
913
|
+
return {
|
|
914
|
+
promise,
|
|
915
|
+
resolve: resolve5,
|
|
916
|
+
reject
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
function wrapRpc(childProcess, options) {
|
|
920
|
+
return async (...args) => {
|
|
921
|
+
if (!childProcess.send) {
|
|
922
|
+
throw new Error(`Process ${childProcess.pid} doesn't have IPC channels`);
|
|
923
|
+
} else if (!childProcess.connected) {
|
|
924
|
+
throw new Error(
|
|
925
|
+
`Process ${childProcess.pid} doesn't have open IPC channels`
|
|
926
|
+
);
|
|
927
|
+
}
|
|
928
|
+
const { id, once } = options;
|
|
929
|
+
const {
|
|
930
|
+
promise: resultPromise,
|
|
931
|
+
resolve: resolveResult,
|
|
932
|
+
reject: rejectResult
|
|
933
|
+
} = createControlledPromise();
|
|
934
|
+
const {
|
|
935
|
+
promise: sendPromise,
|
|
936
|
+
resolve: resolveSend,
|
|
937
|
+
reject: rejectSend
|
|
938
|
+
} = createControlledPromise();
|
|
939
|
+
const handleMessage = (message) => {
|
|
940
|
+
if ((message == null ? void 0 : message.id) === id) {
|
|
941
|
+
if (message.type === "mf_resolve" /* RESOLVE */) {
|
|
942
|
+
resolveResult(message.value);
|
|
943
|
+
} else if (message.type === "mf_reject" /* REJECT */) {
|
|
944
|
+
rejectResult(message.error);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
if (once && (childProcess == null ? void 0 : childProcess.kill)) {
|
|
948
|
+
childProcess.kill("SIGTERM");
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
const handleClose = (code, signal) => {
|
|
952
|
+
rejectResult(
|
|
953
|
+
new RpcExitError(
|
|
954
|
+
code ? `Process ${childProcess.pid} exited with code ${code}${signal ? ` [${signal}]` : ""}` : `Process ${childProcess.pid} exited${signal ? ` [${signal}]` : ""}`,
|
|
955
|
+
code,
|
|
956
|
+
signal
|
|
957
|
+
)
|
|
958
|
+
);
|
|
959
|
+
removeHandlers();
|
|
960
|
+
};
|
|
961
|
+
const removeHandlers = () => {
|
|
962
|
+
childProcess.off("message", handleMessage);
|
|
963
|
+
childProcess.off("close", handleClose);
|
|
964
|
+
};
|
|
965
|
+
if (once) {
|
|
966
|
+
childProcess.once("message", handleMessage);
|
|
967
|
+
} else {
|
|
968
|
+
childProcess.on("message", handleMessage);
|
|
969
|
+
}
|
|
970
|
+
childProcess.on("close", handleClose);
|
|
971
|
+
childProcess.send(
|
|
972
|
+
{
|
|
973
|
+
type: "mf_call" /* CALL */,
|
|
974
|
+
id,
|
|
975
|
+
args
|
|
976
|
+
},
|
|
977
|
+
(error2) => {
|
|
978
|
+
if (error2) {
|
|
979
|
+
rejectSend(error2);
|
|
980
|
+
removeHandlers();
|
|
981
|
+
} else {
|
|
982
|
+
resolveSend(void 0);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
);
|
|
986
|
+
return sendPromise.then(() => resultPromise);
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
// packages/dts-plugin/src/core/rpc/rpc-worker.ts
|
|
991
|
+
var child_process = __toESM(require("child_process"));
|
|
992
|
+
var process3 = __toESM(require("process"));
|
|
993
|
+
var import_crypto = require("crypto");
|
|
994
|
+
var FEDERATION_WORKER_DATA_ENV_KEY = "VMOK_WORKER_DATA_ENV";
|
|
995
|
+
function createRpcWorker(modulePath, data, memoryLimit, once) {
|
|
996
|
+
const options = {
|
|
997
|
+
env: {
|
|
998
|
+
...process3.env,
|
|
999
|
+
[FEDERATION_WORKER_DATA_ENV_KEY]: JSON.stringify(data || {})
|
|
1000
|
+
},
|
|
1001
|
+
stdio: ["inherit", "inherit", "inherit", "ipc"],
|
|
1002
|
+
serialization: "advanced"
|
|
1003
|
+
};
|
|
1004
|
+
if (memoryLimit) {
|
|
1005
|
+
options.execArgv = [`--max-old-space-size=${memoryLimit}`];
|
|
1006
|
+
}
|
|
1007
|
+
let childProcess, remoteMethod;
|
|
1008
|
+
const id = (0, import_crypto.randomUUID)();
|
|
1009
|
+
const worker = {
|
|
1010
|
+
connect(...args) {
|
|
1011
|
+
if (childProcess && !childProcess.connected) {
|
|
1012
|
+
childProcess.send({
|
|
1013
|
+
type: "mf_exit" /* EXIT */,
|
|
1014
|
+
id
|
|
1015
|
+
});
|
|
1016
|
+
childProcess = void 0;
|
|
1017
|
+
remoteMethod = void 0;
|
|
1018
|
+
}
|
|
1019
|
+
if (!(childProcess == null ? void 0 : childProcess.connected)) {
|
|
1020
|
+
childProcess = child_process.fork(modulePath, options);
|
|
1021
|
+
remoteMethod = wrapRpc(childProcess, { id, once });
|
|
1022
|
+
}
|
|
1023
|
+
if (!remoteMethod) {
|
|
1024
|
+
return Promise.reject(
|
|
1025
|
+
new Error("Worker is not connected - cannot perform RPC.")
|
|
1026
|
+
);
|
|
1027
|
+
}
|
|
1028
|
+
return remoteMethod(...args);
|
|
1029
|
+
},
|
|
1030
|
+
terminate() {
|
|
1031
|
+
var _a;
|
|
1032
|
+
(_a = childProcess == null ? void 0 : childProcess.send) == null ? void 0 : _a.call(childProcess, {
|
|
1033
|
+
type: "mf_exit" /* EXIT */,
|
|
1034
|
+
id
|
|
1035
|
+
});
|
|
1036
|
+
childProcess = void 0;
|
|
1037
|
+
remoteMethod = void 0;
|
|
1038
|
+
},
|
|
1039
|
+
get connected() {
|
|
1040
|
+
return Boolean(childProcess == null ? void 0 : childProcess.connected);
|
|
1041
|
+
},
|
|
1042
|
+
get process() {
|
|
1043
|
+
return childProcess;
|
|
1044
|
+
},
|
|
1045
|
+
get id() {
|
|
1046
|
+
return id;
|
|
1047
|
+
}
|
|
1048
|
+
};
|
|
1049
|
+
return worker;
|
|
1050
|
+
}
|
|
1051
|
+
function getRpcWorkerData() {
|
|
1052
|
+
return JSON.parse(process3.env[FEDERATION_WORKER_DATA_ENV_KEY] || "{}");
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// packages/dts-plugin/src/core/lib/DtsWorker.ts
|
|
1056
|
+
var DtsWorker = class {
|
|
1057
|
+
constructor(options) {
|
|
1058
|
+
this._options = (0, import_lodash2.default)(options, (_value, key) => {
|
|
1059
|
+
if (key === "manifest") {
|
|
1060
|
+
return false;
|
|
1061
|
+
}
|
|
1062
|
+
});
|
|
1063
|
+
this.removeUnSerializationOptions();
|
|
1064
|
+
this.rpcWorker = createRpcWorker(
|
|
1065
|
+
import_path6.default.resolve(__dirname, "./forkGenerateDts.js"),
|
|
1066
|
+
{},
|
|
1067
|
+
void 0,
|
|
1068
|
+
true
|
|
1069
|
+
);
|
|
1070
|
+
this._res = this.rpcWorker.connect(this._options);
|
|
1071
|
+
}
|
|
1072
|
+
removeUnSerializationOptions() {
|
|
1073
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1074
|
+
if ((_b = (_a = this._options.remote) == null ? void 0 : _a.moduleFederationConfig) == null ? void 0 : _b.manifest) {
|
|
1075
|
+
(_d = (_c = this._options.remote) == null ? void 0 : _c.moduleFederationConfig) == null ? true : delete _d.manifest;
|
|
1076
|
+
}
|
|
1077
|
+
if ((_f = (_e = this._options.host) == null ? void 0 : _e.moduleFederationConfig) == null ? void 0 : _f.manifest) {
|
|
1078
|
+
(_h = (_g = this._options.host) == null ? void 0 : _g.moduleFederationConfig) == null ? true : delete _h.manifest;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
get controlledPromise() {
|
|
1082
|
+
return Promise.resolve(this._res).then(() => {
|
|
1083
|
+
this.exit();
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
exit() {
|
|
1087
|
+
var _a;
|
|
1088
|
+
(_a = this.rpcWorker) == null ? void 0 : _a.terminate();
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
// packages/dts-plugin/src/core/lib/generateTypesInChildProcess.ts
|
|
1093
|
+
async function generateTypesInChildProcess(options) {
|
|
1094
|
+
const dtsWorker = new DtsWorker(options);
|
|
1095
|
+
return dtsWorker.controlledPromise;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
// packages/dts-plugin/src/core/lib/consumeTypes.ts
|
|
1099
|
+
async function consumeTypes(options) {
|
|
1100
|
+
var _a;
|
|
1101
|
+
const DTSManagerConstructor = getDTSManagerConstructor(
|
|
1102
|
+
(_a = options.host) == null ? void 0 : _a.implementation
|
|
1103
|
+
);
|
|
1104
|
+
const dtsManager = new DTSManagerConstructor(options);
|
|
1105
|
+
await dtsManager.consumeTypes();
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
// packages/dts-plugin/src/dev-worker/DevWorker.ts
|
|
49
1109
|
var DevWorker = class {
|
|
50
1110
|
constructor(options) {
|
|
51
|
-
this._options = (0,
|
|
1111
|
+
this._options = (0, import_lodash3.default)(options, (_value, key) => {
|
|
52
1112
|
if (key === "manifest") {
|
|
53
1113
|
return false;
|
|
54
1114
|
}
|
|
55
1115
|
});
|
|
56
1116
|
this.removeUnSerializationOptions();
|
|
57
|
-
this._rpcWorker =
|
|
58
|
-
|
|
1117
|
+
this._rpcWorker = rpc_exports.createRpcWorker(
|
|
1118
|
+
import_path7.default.resolve(__dirname, "./forkDevWorker.js"),
|
|
59
1119
|
{},
|
|
60
1120
|
void 0,
|
|
61
1121
|
false
|
|
@@ -74,7 +1134,7 @@ var DevWorker = class {
|
|
|
74
1134
|
update() {
|
|
75
1135
|
var _a, _b;
|
|
76
1136
|
(_b = (_a = this._rpcWorker.process) == null ? void 0 : _a.send) == null ? void 0 : _b.call(_a, {
|
|
77
|
-
type:
|
|
1137
|
+
type: rpc_exports.RpcGMCallTypes.CALL,
|
|
78
1138
|
id: this._rpcWorker.id,
|
|
79
1139
|
args: [void 0, "update"]
|
|
80
1140
|
});
|
|
@@ -88,7 +1148,7 @@ var DevWorker = class {
|
|
|
88
1148
|
// packages/dts-plugin/src/dev-worker/createDevWorker.ts
|
|
89
1149
|
async function removeLogFile() {
|
|
90
1150
|
try {
|
|
91
|
-
const logDir =
|
|
1151
|
+
const logDir = path5.resolve(process.cwd(), ".mf/typesGenerate.log");
|
|
92
1152
|
await fse.remove(logDir);
|
|
93
1153
|
} catch (err) {
|
|
94
1154
|
console.error("removeLogFile error", "forkDevWorker", err);
|
|
@@ -100,7 +1160,7 @@ function createDevWorker(options) {
|
|
|
100
1160
|
}
|
|
101
1161
|
|
|
102
1162
|
// packages/dts-plugin/src/plugins/DevPlugin.ts
|
|
103
|
-
var
|
|
1163
|
+
var import_sdk5 = require("@module-federation/sdk");
|
|
104
1164
|
|
|
105
1165
|
// packages/dts-plugin/src/server/message/Message.ts
|
|
106
1166
|
var Message = class {
|
|
@@ -148,7 +1208,7 @@ var ReloadWebClientAPI = class extends API {
|
|
|
148
1208
|
};
|
|
149
1209
|
|
|
150
1210
|
// packages/dts-plugin/src/server/utils/index.ts
|
|
151
|
-
var
|
|
1211
|
+
var import_sdk4 = require("@module-federation/sdk");
|
|
152
1212
|
|
|
153
1213
|
// packages/dts-plugin/src/server/utils/logTransform.ts
|
|
154
1214
|
var import_chalk = __toESM(require("chalk"));
|
|
@@ -171,7 +1231,7 @@ var BrokerExitLog = class extends Log {
|
|
|
171
1231
|
};
|
|
172
1232
|
|
|
173
1233
|
// packages/dts-plugin/src/server/utils/log.ts
|
|
174
|
-
var
|
|
1234
|
+
var import_sdk3 = require("@module-federation/sdk");
|
|
175
1235
|
var log4js = __toESM(require("log4js"));
|
|
176
1236
|
var import_chalk2 = __toESM(require("chalk"));
|
|
177
1237
|
|
|
@@ -209,7 +1269,7 @@ function error(error2, action, from) {
|
|
|
209
1269
|
// packages/dts-plugin/src/server/utils/index.ts
|
|
210
1270
|
function getIdentifier(options) {
|
|
211
1271
|
const { ip, name } = options;
|
|
212
|
-
return `mf ${
|
|
1272
|
+
return `mf ${import_sdk4.SEPARATOR}${name}${ip ? `${import_sdk4.SEPARATOR}${ip}` : ""}`;
|
|
213
1273
|
}
|
|
214
1274
|
|
|
215
1275
|
// packages/dts-plugin/src/server/Publisher.ts
|
|
@@ -311,11 +1371,9 @@ var Publisher = class {
|
|
|
311
1371
|
|
|
312
1372
|
// packages/dts-plugin/src/server/DevServer.ts
|
|
313
1373
|
var import_isomorphic_ws2 = __toESM(require("isomorphic-ws"));
|
|
314
|
-
var import_helpers3 = require("@module-federation/native-federation-typescript/helpers");
|
|
315
1374
|
|
|
316
1375
|
// packages/dts-plugin/src/server/broker/Broker.ts
|
|
317
1376
|
var import_http = require("http");
|
|
318
|
-
var import_helpers2 = require("@module-federation/native-federation-typescript/helpers");
|
|
319
1377
|
var import_isomorphic_ws = __toESM(require("isomorphic-ws"));
|
|
320
1378
|
var import_node_schedule = __toESM(require("node-schedule"));
|
|
321
1379
|
var import_url = require("url");
|
|
@@ -554,7 +1612,7 @@ ${err.message}
|
|
|
554
1612
|
}),
|
|
555
1613
|
{
|
|
556
1614
|
updateKind: "UPDATE_TYPE" /* UPDATE_TYPE */,
|
|
557
|
-
updateMode:
|
|
1615
|
+
updateMode: "PASSIVE" /* PASSIVE */,
|
|
558
1616
|
updateSourcePaths: [registeredPublisher.name],
|
|
559
1617
|
remoteTypeTarPath: registeredPublisher.remoteTypeTarPath,
|
|
560
1618
|
name: registeredPublisher.name
|
|
@@ -774,7 +1832,7 @@ ${err.message}
|
|
|
774
1832
|
publisher.addSubscriber(identifier, tmpSubScriber.client);
|
|
775
1833
|
publisher.notifySubscriber(identifier, {
|
|
776
1834
|
updateKind: "UPDATE_TYPE" /* UPDATE_TYPE */,
|
|
777
|
-
updateMode:
|
|
1835
|
+
updateMode: "PASSIVE" /* PASSIVE */,
|
|
778
1836
|
updateSourcePaths: [publisher.name],
|
|
779
1837
|
remoteTypeTarPath: publisher.remoteTypeTarPath,
|
|
780
1838
|
name: publisher.name
|
|
@@ -887,34 +1945,34 @@ var import_fs_extra = __toESM(require("fs-extra"));
|
|
|
887
1945
|
var import_koa = __toESM(require("koa"));
|
|
888
1946
|
|
|
889
1947
|
// packages/dts-plugin/src/plugins/DevPlugin.ts
|
|
890
|
-
var
|
|
1948
|
+
var import_path9 = __toESM(require("path"));
|
|
891
1949
|
|
|
892
1950
|
// packages/dts-plugin/src/plugins/utils.ts
|
|
893
|
-
var
|
|
894
|
-
var
|
|
1951
|
+
var import_path8 = __toESM(require("path"));
|
|
1952
|
+
var import_fs3 = __toESM(require("fs"));
|
|
895
1953
|
var isTSProject = (tsConfigPath, context = process.cwd()) => {
|
|
896
1954
|
try {
|
|
897
|
-
let filepath = tsConfigPath ? tsConfigPath :
|
|
898
|
-
if (!
|
|
899
|
-
filepath =
|
|
1955
|
+
let filepath = tsConfigPath ? tsConfigPath : import_path8.default.resolve(context, "./tsconfig.json");
|
|
1956
|
+
if (!import_path8.default.isAbsolute(filepath)) {
|
|
1957
|
+
filepath = import_path8.default.resolve(context, filepath);
|
|
900
1958
|
}
|
|
901
|
-
return
|
|
1959
|
+
return import_fs3.default.existsSync(filepath);
|
|
902
1960
|
} catch (err) {
|
|
903
1961
|
return false;
|
|
904
1962
|
}
|
|
905
1963
|
};
|
|
1964
|
+
function isDev() {
|
|
1965
|
+
return process.env["NODE_ENV"] === "development";
|
|
1966
|
+
}
|
|
906
1967
|
|
|
907
1968
|
// packages/dts-plugin/src/plugins/DevPlugin.ts
|
|
908
1969
|
function ensureTempDir(filePath) {
|
|
909
1970
|
try {
|
|
910
|
-
const dir =
|
|
1971
|
+
const dir = import_path9.default.dirname(filePath);
|
|
911
1972
|
import_fs_extra2.default.ensureDirSync(dir);
|
|
912
1973
|
} catch (_err) {
|
|
913
1974
|
}
|
|
914
1975
|
}
|
|
915
|
-
function isDev() {
|
|
916
|
-
return process.env["NODE_ENV"] === "development";
|
|
917
|
-
}
|
|
918
1976
|
var DevPlugin = class _DevPlugin {
|
|
919
1977
|
constructor(options) {
|
|
920
1978
|
this.name = "MFDevPlugin";
|
|
@@ -922,7 +1980,7 @@ var DevPlugin = class _DevPlugin {
|
|
|
922
1980
|
}
|
|
923
1981
|
static ensureLiveReloadEntry(options, filePath) {
|
|
924
1982
|
ensureTempDir(filePath);
|
|
925
|
-
const liveReloadEntry = import_fs_extra2.default.readFileSync(
|
|
1983
|
+
const liveReloadEntry = import_fs_extra2.default.readFileSync(import_path9.default.join(__dirname, "./iife/launch-web-client.js")).toString("utf-8");
|
|
926
1984
|
const liveReloadEntryWithOptions = liveReloadEntry.replace(
|
|
927
1985
|
WEB_CLIENT_OPTIONS_IDENTIFIER,
|
|
928
1986
|
JSON.stringify(options)
|
|
@@ -972,7 +2030,7 @@ var DevPlugin = class _DevPlugin {
|
|
|
972
2030
|
const {
|
|
973
2031
|
_options: { name, dev, dts }
|
|
974
2032
|
} = this;
|
|
975
|
-
const normalizedDev = (0,
|
|
2033
|
+
const normalizedDev = (0, import_sdk5.normalizeOptions)(
|
|
976
2034
|
true,
|
|
977
2035
|
{
|
|
978
2036
|
disableLiveReload: true,
|
|
@@ -990,11 +2048,11 @@ var DevPlugin = class _DevPlugin {
|
|
|
990
2048
|
throw new Error("name is required if you want to enable dev server!");
|
|
991
2049
|
}
|
|
992
2050
|
if (!normalizedDev.disableLiveReload) {
|
|
993
|
-
const TEMP_DIR =
|
|
2051
|
+
const TEMP_DIR = import_path9.default.join(
|
|
994
2052
|
`${process.cwd()}/node_modules`,
|
|
995
2053
|
`.federation`
|
|
996
2054
|
);
|
|
997
|
-
const filepath =
|
|
2055
|
+
const filepath = import_path9.default.join(TEMP_DIR, `live-reload.js`);
|
|
998
2056
|
_DevPlugin.ensureLiveReloadEntry({ name }, filepath);
|
|
999
2057
|
compiler.hooks.afterPlugins.tap("MFDevPlugin", () => {
|
|
1000
2058
|
new compiler.webpack.EntryPlugin(compiler.context, filepath, {
|
|
@@ -1004,8 +2062,11 @@ var DevPlugin = class _DevPlugin {
|
|
|
1004
2062
|
}
|
|
1005
2063
|
const defaultGenerateTypes = { compileInChildProcess: true };
|
|
1006
2064
|
const defaultConsumeTypes = { consumeAPITypes: true };
|
|
1007
|
-
const normalizedDtsOptions = (0,
|
|
1008
|
-
isTSProject(
|
|
2065
|
+
const normalizedDtsOptions = (0, import_sdk5.normalizeOptions)(
|
|
2066
|
+
isTSProject(
|
|
2067
|
+
typeof dts === "object" ? dts.tsConfigPath : void 0,
|
|
2068
|
+
compiler.context
|
|
2069
|
+
),
|
|
1009
2070
|
{
|
|
1010
2071
|
// remote types dist(.dev-server) not be used currently, so no need to set extractThirdParty etc
|
|
1011
2072
|
generateTypes: defaultGenerateTypes,
|
|
@@ -1014,8 +2075,8 @@ var DevPlugin = class _DevPlugin {
|
|
|
1014
2075
|
},
|
|
1015
2076
|
"mfOptions.dts"
|
|
1016
2077
|
)(dts);
|
|
1017
|
-
const normalizedGenerateTypes = (0,
|
|
1018
|
-
normalizedDtsOptions
|
|
2078
|
+
const normalizedGenerateTypes = (0, import_sdk5.normalizeOptions)(
|
|
2079
|
+
Boolean(normalizedDtsOptions),
|
|
1019
2080
|
defaultGenerateTypes,
|
|
1020
2081
|
"mfOptions.dts.generateTypes"
|
|
1021
2082
|
)(
|
|
@@ -1031,7 +2092,7 @@ var DevPlugin = class _DevPlugin {
|
|
|
1031
2092
|
...normalizedGenerateTypes,
|
|
1032
2093
|
typesFolder: `.dev-server`
|
|
1033
2094
|
};
|
|
1034
|
-
const normalizedConsumeTypes = (0,
|
|
2095
|
+
const normalizedConsumeTypes = (0, import_sdk5.normalizeOptions)(
|
|
1035
2096
|
Boolean(normalizedDtsOptions),
|
|
1036
2097
|
defaultConsumeTypes,
|
|
1037
2098
|
"mfOptions.dts.consumeTypes"
|
|
@@ -1047,6 +2108,12 @@ var DevPlugin = class _DevPlugin {
|
|
|
1047
2108
|
...normalizedConsumeTypes
|
|
1048
2109
|
};
|
|
1049
2110
|
const extraOptions = normalizedDtsOptions ? normalizedDtsOptions.extraOptions || {} : {};
|
|
2111
|
+
if (!remote && !host && normalizedDev.disableLiveReload) {
|
|
2112
|
+
return;
|
|
2113
|
+
}
|
|
2114
|
+
if (remote && !(remote == null ? void 0 : remote.tsConfigPath) && typeof normalizedDtsOptions === "object" && normalizedDtsOptions.tsConfigPath) {
|
|
2115
|
+
remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
2116
|
+
}
|
|
1050
2117
|
this._devWorker = createDevWorker({
|
|
1051
2118
|
name,
|
|
1052
2119
|
remote,
|
|
@@ -1062,8 +2129,130 @@ var DevPlugin = class _DevPlugin {
|
|
|
1062
2129
|
};
|
|
1063
2130
|
|
|
1064
2131
|
// packages/dts-plugin/src/plugins/TypesPlugin.ts
|
|
1065
|
-
var
|
|
1066
|
-
|
|
2132
|
+
var import_sdk8 = require("@module-federation/sdk");
|
|
2133
|
+
|
|
2134
|
+
// packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts
|
|
2135
|
+
var import_sdk6 = require("@module-federation/sdk");
|
|
2136
|
+
var ConsumeTypesPlugin = class {
|
|
2137
|
+
constructor(pluginOptions, dtsOptions, defaultOptions3) {
|
|
2138
|
+
this.pluginOptions = pluginOptions;
|
|
2139
|
+
this.dtsOptions = dtsOptions;
|
|
2140
|
+
this.defaultOptions = defaultOptions3;
|
|
2141
|
+
}
|
|
2142
|
+
apply(compiler) {
|
|
2143
|
+
const { dtsOptions, defaultOptions: defaultOptions3, pluginOptions } = this;
|
|
2144
|
+
const normalizedConsumeTypes = (0, import_sdk6.normalizeOptions)(
|
|
2145
|
+
true,
|
|
2146
|
+
defaultOptions3,
|
|
2147
|
+
"mfOptions.dts.consumeTypes"
|
|
2148
|
+
)(dtsOptions.consumeTypes);
|
|
2149
|
+
if (!normalizedConsumeTypes) {
|
|
2150
|
+
return;
|
|
2151
|
+
}
|
|
2152
|
+
const finalOptions = {
|
|
2153
|
+
host: {
|
|
2154
|
+
implementation: dtsOptions.implementation,
|
|
2155
|
+
context: compiler.context,
|
|
2156
|
+
moduleFederationConfig: pluginOptions,
|
|
2157
|
+
...normalizedConsumeTypes
|
|
2158
|
+
},
|
|
2159
|
+
extraOptions: dtsOptions.extraOptions || {}
|
|
2160
|
+
};
|
|
2161
|
+
validateOptions(finalOptions.host);
|
|
2162
|
+
consumeTypes(finalOptions);
|
|
2163
|
+
}
|
|
2164
|
+
};
|
|
2165
|
+
|
|
2166
|
+
// packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts
|
|
2167
|
+
var import_fs4 = __toESM(require("fs"));
|
|
2168
|
+
var import_sdk7 = require("@module-federation/sdk");
|
|
2169
|
+
var GenerateTypesPlugin = class {
|
|
2170
|
+
constructor(pluginOptions, dtsOptions, defaultOptions3) {
|
|
2171
|
+
this.pluginOptions = pluginOptions;
|
|
2172
|
+
this.dtsOptions = dtsOptions;
|
|
2173
|
+
this.defaultOptions = defaultOptions3;
|
|
2174
|
+
}
|
|
2175
|
+
apply(compiler) {
|
|
2176
|
+
const { dtsOptions, defaultOptions: defaultOptions3, pluginOptions } = this;
|
|
2177
|
+
const normalizedGenerateTypes = (0, import_sdk7.normalizeOptions)(
|
|
2178
|
+
true,
|
|
2179
|
+
defaultOptions3,
|
|
2180
|
+
"mfOptions.dts.generateTypes"
|
|
2181
|
+
)(dtsOptions.generateTypes);
|
|
2182
|
+
if (!normalizedGenerateTypes) {
|
|
2183
|
+
return;
|
|
2184
|
+
}
|
|
2185
|
+
const finalOptions = {
|
|
2186
|
+
remote: {
|
|
2187
|
+
implementation: dtsOptions.implementation,
|
|
2188
|
+
context: compiler.context,
|
|
2189
|
+
moduleFederationConfig: pluginOptions,
|
|
2190
|
+
...normalizedGenerateTypes
|
|
2191
|
+
},
|
|
2192
|
+
extraOptions: dtsOptions.extraOptions || {}
|
|
2193
|
+
};
|
|
2194
|
+
if (dtsOptions.tsConfigPath && !finalOptions.remote.tsConfigPath) {
|
|
2195
|
+
finalOptions.remote.tsConfigPath = dtsOptions.tsConfigPath;
|
|
2196
|
+
}
|
|
2197
|
+
validateOptions(finalOptions.remote);
|
|
2198
|
+
const isProd = !isDev();
|
|
2199
|
+
const getGenerateTypesFn = () => {
|
|
2200
|
+
let fn = generateTypes;
|
|
2201
|
+
let res;
|
|
2202
|
+
if (finalOptions.remote.compileInChildProcess) {
|
|
2203
|
+
fn = generateTypesInChildProcess;
|
|
2204
|
+
}
|
|
2205
|
+
if (isProd) {
|
|
2206
|
+
res = fn(finalOptions);
|
|
2207
|
+
return () => res;
|
|
2208
|
+
}
|
|
2209
|
+
return fn;
|
|
2210
|
+
};
|
|
2211
|
+
const generateTypesFn = getGenerateTypesFn();
|
|
2212
|
+
compiler.hooks.thisCompilation.tap("mf:generateTypes", (compilation) => {
|
|
2213
|
+
compilation.hooks.processAssets.tapPromise(
|
|
2214
|
+
{
|
|
2215
|
+
name: "mf:generateTypes",
|
|
2216
|
+
stage: (
|
|
2217
|
+
// @ts-expect-error use runtime variable in case peer dep not installed
|
|
2218
|
+
compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
|
|
2219
|
+
)
|
|
2220
|
+
},
|
|
2221
|
+
async () => {
|
|
2222
|
+
try {
|
|
2223
|
+
const { zipTypesPath, apiTypesPath, zipName, apiFileName } = retrieveTypesAssetsInfo(finalOptions.remote);
|
|
2224
|
+
if (zipName && compilation.getAsset(zipName)) {
|
|
2225
|
+
return;
|
|
2226
|
+
}
|
|
2227
|
+
await generateTypesFn(finalOptions);
|
|
2228
|
+
if (zipTypesPath) {
|
|
2229
|
+
compilation.emitAsset(
|
|
2230
|
+
zipName,
|
|
2231
|
+
new compiler.webpack.sources.RawSource(
|
|
2232
|
+
import_fs4.default.readFileSync(zipTypesPath),
|
|
2233
|
+
false
|
|
2234
|
+
)
|
|
2235
|
+
);
|
|
2236
|
+
}
|
|
2237
|
+
if (apiTypesPath) {
|
|
2238
|
+
compilation.emitAsset(
|
|
2239
|
+
apiFileName,
|
|
2240
|
+
new compiler.webpack.sources.RawSource(
|
|
2241
|
+
import_fs4.default.readFileSync(apiTypesPath),
|
|
2242
|
+
false
|
|
2243
|
+
)
|
|
2244
|
+
);
|
|
2245
|
+
}
|
|
2246
|
+
} catch (err) {
|
|
2247
|
+
console.error(err);
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
);
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
2253
|
+
};
|
|
2254
|
+
|
|
2255
|
+
// packages/dts-plugin/src/plugins/TypesPlugin.ts
|
|
1067
2256
|
var TypesPlugin = class {
|
|
1068
2257
|
constructor(options) {
|
|
1069
2258
|
this.options = options;
|
|
@@ -1078,8 +2267,11 @@ var TypesPlugin = class {
|
|
|
1078
2267
|
extractRemoteTypes: true
|
|
1079
2268
|
};
|
|
1080
2269
|
const defaultConsumeTypes = { abortOnError: false, consumeAPITypes: true };
|
|
1081
|
-
const normalizedDtsOptions = (0,
|
|
1082
|
-
isTSProject(
|
|
2270
|
+
const normalizedDtsOptions = (0, import_sdk8.normalizeOptions)(
|
|
2271
|
+
isTSProject(
|
|
2272
|
+
typeof options.dts === "object" ? options.dts.tsConfigPath : void 0,
|
|
2273
|
+
compiler.context
|
|
2274
|
+
),
|
|
1083
2275
|
{
|
|
1084
2276
|
generateTypes: defaultGenerateTypes,
|
|
1085
2277
|
consumeTypes: defaultConsumeTypes,
|
|
@@ -1087,42 +2279,19 @@ var TypesPlugin = class {
|
|
|
1087
2279
|
},
|
|
1088
2280
|
"mfOptions.dts"
|
|
1089
2281
|
)(options.dts);
|
|
1090
|
-
if (typeof normalizedDtsOptions
|
|
1091
|
-
|
|
1092
|
-
true,
|
|
1093
|
-
defaultGenerateTypes,
|
|
1094
|
-
"mfOptions.dts.generateTypes"
|
|
1095
|
-
)(normalizedDtsOptions.generateTypes);
|
|
1096
|
-
if (normalizedGenerateTypes) {
|
|
1097
|
-
(0, import_webpack.NativeFederationTypeScriptRemote)({
|
|
1098
|
-
remote: {
|
|
1099
|
-
implementation: normalizedDtsOptions.implementation,
|
|
1100
|
-
context: compiler.context,
|
|
1101
|
-
moduleFederationConfig: options,
|
|
1102
|
-
...normalizedGenerateTypes
|
|
1103
|
-
},
|
|
1104
|
-
extraOptions: normalizedDtsOptions.extraOptions || {}
|
|
1105
|
-
// @ts-ignore
|
|
1106
|
-
}).apply(compiler);
|
|
1107
|
-
}
|
|
1108
|
-
const normalizedConsumeTypes = (0, import_sdk4.normalizeOptions)(
|
|
1109
|
-
true,
|
|
1110
|
-
defaultConsumeTypes,
|
|
1111
|
-
"mfOptions.dts.consumeTypes"
|
|
1112
|
-
)(normalizedDtsOptions.consumeTypes);
|
|
1113
|
-
if (normalizedConsumeTypes) {
|
|
1114
|
-
(0, import_webpack.NativeFederationTypeScriptHost)({
|
|
1115
|
-
host: {
|
|
1116
|
-
implementation: normalizedDtsOptions.implementation,
|
|
1117
|
-
context: compiler.context,
|
|
1118
|
-
moduleFederationConfig: options,
|
|
1119
|
-
...normalizedConsumeTypes
|
|
1120
|
-
},
|
|
1121
|
-
extraOptions: normalizedDtsOptions.extraOptions || {}
|
|
1122
|
-
// @ts-ignore
|
|
1123
|
-
}).apply(compiler);
|
|
1124
|
-
}
|
|
2282
|
+
if (typeof normalizedDtsOptions !== "object") {
|
|
2283
|
+
return;
|
|
1125
2284
|
}
|
|
2285
|
+
new GenerateTypesPlugin(
|
|
2286
|
+
options,
|
|
2287
|
+
normalizedDtsOptions,
|
|
2288
|
+
defaultGenerateTypes
|
|
2289
|
+
).apply(compiler);
|
|
2290
|
+
new ConsumeTypesPlugin(
|
|
2291
|
+
options,
|
|
2292
|
+
normalizedDtsOptions,
|
|
2293
|
+
defaultConsumeTypes
|
|
2294
|
+
).apply(compiler);
|
|
1126
2295
|
}
|
|
1127
2296
|
};
|
|
1128
2297
|
|