@module-federation/dts-plugin 0.8.7 → 0.8.9

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/index.js CHANGED
@@ -89,6 +89,7 @@ module.exports = __toCommonJS(src_exports);
89
89
 
90
90
  // packages/dts-plugin/src/plugins/DevPlugin.ts
91
91
  var import_fs_extra3 = __toESM(require("fs-extra"));
92
+ var import_path9 = __toESM(require("path"));
92
93
 
93
94
  // packages/dts-plugin/src/dev-worker/createDevWorker.ts
94
95
  var path5 = __toESM(require("path"));
@@ -109,6 +110,7 @@ var import_path4 = __toESM(require("path"));
109
110
  var import_axios = __toESM(require("axios"));
110
111
  var import_http2 = __toESM(require("http"));
111
112
  var import_https = __toESM(require("https"));
113
+ var import_sdk6 = require("@module-federation/sdk");
112
114
  var import_ansi_colors = __toESM(require("ansi-colors"));
113
115
 
114
116
  // packages/dts-plugin/src/core/lib/DTSManager.ts
@@ -142,7 +144,7 @@ function writeTempTsConfig(tsConfig, context, name) {
142
144
  const createHash = /* @__PURE__ */ __name((contents) => {
143
145
  return import_crypto.default.createHash("md5").update(contents).digest("hex");
144
146
  }, "createHash");
145
- const hash = createHash(`${JSON.stringify(tsConfig)}${name}`);
147
+ const hash = createHash(`${JSON.stringify(tsConfig)}${name}${Date.now()}`);
146
148
  const tempTsConfigJsonPath = (0, import_path.resolve)(context, "node_modules", import_sdk.TEMP_DIR, `tsconfig.${hash}.json`);
147
149
  (0, import_fs_extra.ensureDirSync)((0, import_path.dirname)(tempTsConfigJsonPath));
148
150
  (0, import_fs_extra.writeFileSync)(tempTsConfigJsonPath, JSON.stringify(tsConfig, null, 2));
@@ -196,7 +198,7 @@ export { default } from './${relativePathToOutput}';`);
196
198
  }
197
199
  }), "processTypesFile");
198
200
  var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteOptions) => __async(void 0, null, function* () {
199
- var _a4, _b;
201
+ var _a3, _b;
200
202
  if (!Object.keys(mapComponentsToExpose).length) {
201
203
  return;
202
204
  }
@@ -209,7 +211,7 @@ var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteO
209
211
  const cmd = `npx ${remoteOptions.compilerInstance} --project ${tempTsConfigJsonPath}`;
210
212
  try {
211
213
  yield execPromise(cmd, {
212
- cwd: typeof remoteOptions.moduleFederationConfig.dts !== "boolean" ? (_b = (_a4 = remoteOptions.moduleFederationConfig.dts) == null ? void 0 : _a4.cwd) != null ? _b : void 0 : void 0
214
+ cwd: typeof remoteOptions.moduleFederationConfig.dts !== "boolean" ? (_b = (_a3 = remoteOptions.moduleFederationConfig.dts) == null ? void 0 : _a3.cwd) != null ? _b : void 0 : void 0
213
215
  });
214
216
  } catch (err) {
215
217
  throw new Error((0, import_error_codes.getShortErrorMsg)(import_error_codes.TYPE_001, import_error_codes.typeDescMap, {
@@ -374,8 +376,8 @@ var UpdateMode;
374
376
  // packages/dts-plugin/src/server/utils/log.ts
375
377
  var logger = (0, import_sdk2.createLogger)(`[ ${MF_SERVER_IDENTIFIER} ]`);
376
378
  function fileLog(msg, module2, level) {
377
- var _a4, _b;
378
- if (!((_a4 = process == null ? void 0 : process.env) == null ? void 0 : _a4["FEDERATION_DEBUG"])) {
379
+ var _a3, _b;
380
+ if (!((_a3 = process == null ? void 0 : process.env) == null ? void 0 : _a3["FEDERATION_DEBUG"])) {
379
381
  return;
380
382
  }
381
383
  log4js.configure({
@@ -634,13 +636,13 @@ ${err.message}
634
636
  this._webSocketServer = void 0;
635
637
  });
636
638
  server.on("upgrade", (req, socket, head) => {
637
- var _a4;
639
+ var _a3;
638
640
  if (req.url) {
639
641
  const { pathname } = (0, import_url.parse)(req.url);
640
642
  if (pathname === "/") {
641
- (_a4 = this._webSocketServer) == null ? void 0 : _a4.handleUpgrade(req, socket, head, (ws) => {
642
- var _a5;
643
- (_a5 = this._webSocketServer) == null ? void 0 : _a5.emit("connection", ws, req);
643
+ (_a3 = this._webSocketServer) == null ? void 0 : _a3.handleUpgrade(req, socket, head, (ws) => {
644
+ var _a4;
645
+ (_a4 = this._webSocketServer) == null ? void 0 : _a4.emit("connection", ws, req);
644
646
  });
645
647
  }
646
648
  }
@@ -1025,8 +1027,8 @@ ${err.message}
1025
1027
  }
1026
1028
  }
1027
1029
  _getTmpSubScribers(publisherIdentifier) {
1028
- var _a4;
1029
- return (_a4 = this._tmpSubscriberShelter.get(publisherIdentifier)) == null ? void 0 : _a4.subscribers;
1030
+ var _a3;
1031
+ return (_a3 = this._tmpSubscriberShelter.get(publisherIdentifier)) == null ? void 0 : _a3.subscribers;
1030
1032
  }
1031
1033
  // after adding publisher, it will change the temp subscriber to regular subscriber
1032
1034
  _consumeTmpSubScribers(publisher, tmpSubScribers) {
@@ -1131,9 +1133,9 @@ ${err.message}
1131
1133
  process.exit(0);
1132
1134
  }
1133
1135
  broadcast(message) {
1134
- var _a4, _b;
1136
+ var _a3, _b;
1135
1137
  fileLog(`[broadcast] exit info : ${JSON.stringify(message)}`, "Broker", "warn");
1136
- (_a4 = this._webSocketServer) == null ? void 0 : _a4.clients.forEach((client) => {
1138
+ (_a3 = this._webSocketServer) == null ? void 0 : _a3.clients.forEach((client) => {
1137
1139
  client.send(JSON.stringify(message));
1138
1140
  });
1139
1141
  (_b = this._secureWebSocketServer) == null ? void 0 : _b.clients.forEach((client) => {
@@ -1171,6 +1173,7 @@ var retrieveTypesArchiveDestinationPath = /* @__PURE__ */ __name((hostOptions, d
1171
1173
  var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
1172
1174
  let retries = 0;
1173
1175
  return (_0) => __async(void 0, [_0], function* ([destinationFolder, fileToDownload]) {
1176
+ var _a3;
1174
1177
  const destinationPath = retrieveTypesArchiveDestinationPath(hostOptions, destinationFolder);
1175
1178
  while (retries++ < hostOptions.maxRetries) {
1176
1179
  try {
@@ -1178,6 +1181,9 @@ var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
1178
1181
  const response = yield axiosGet(url, {
1179
1182
  responseType: "arraybuffer"
1180
1183
  }).catch(downloadErrorLogger(destinationFolder, url));
1184
+ if (typeof ((_a3 = response.headers) == null ? void 0 : _a3["content-type"]) === "string" && response.headers["content-type"].includes("text/html")) {
1185
+ throw new Error(`${url} receives invalid content-type: ${response.headers["content-type"]}`);
1186
+ }
1181
1187
  try {
1182
1188
  if (hostOptions.deleteTypesFolder) {
1183
1189
  yield (0, import_promises2.rm)(destinationPath, {
@@ -1190,6 +1196,7 @@ var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
1190
1196
  }
1191
1197
  const zip = new import_adm_zip.default(Buffer.from(response.data));
1192
1198
  zip.extractAllTo(destinationPath, true);
1199
+ fileLog(`zip.extractAllTo success destinationPath: ${destinationPath}; url: ${url}`, "downloadTypesArchive", "info");
1193
1200
  return [
1194
1201
  destinationFolder,
1195
1202
  destinationPath
@@ -1352,7 +1359,7 @@ var DTSManager = (_a = class {
1352
1359
  }
1353
1360
  generateTypes() {
1354
1361
  return __async(this, null, function* () {
1355
- var _a4;
1362
+ var _a3;
1356
1363
  try {
1357
1364
  const { options } = this;
1358
1365
  if (!options.remote) {
@@ -1389,8 +1396,10 @@ var DTSManager = (_a = class {
1389
1396
  }
1390
1397
  logger.success("Federated types created correctly");
1391
1398
  } catch (error2) {
1392
- if (((_a4 = this.options.remote) == null ? void 0 : _a4.abortOnError) === false) {
1393
- logger.error(`Unable to compile federated types, ${error2}`);
1399
+ if (((_a3 = this.options.remote) == null ? void 0 : _a3.abortOnError) === false) {
1400
+ if (this.options.displayErrorInTerminal) {
1401
+ logger.error(`Unable to compile federated types${error2}`);
1402
+ }
1394
1403
  } else {
1395
1404
  throw error2;
1396
1405
  }
@@ -1469,8 +1478,9 @@ var DTSManager = (_a = class {
1469
1478
  const filePath = import_path3.default.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
1470
1479
  import_fs.default.writeFileSync(filePath, apiTypeFile);
1471
1480
  this.loadedRemoteAPIAlias.add(remoteInfo.alias);
1481
+ fileLog(`success`, "downloadAPITypes", "info");
1472
1482
  } catch (err) {
1473
- fileLog(`Unable to download "${remoteInfo.name}" api types, ${err}`, "consumeTargetRemotes", "error");
1483
+ fileLog(`Unable to download "${remoteInfo.name}" api types, ${err}`, "downloadAPITypes", "error");
1474
1484
  }
1475
1485
  });
1476
1486
  }
@@ -1546,7 +1556,7 @@ var DTSManager = (_a = class {
1546
1556
  }
1547
1557
  consumeTypes() {
1548
1558
  return __async(this, null, function* () {
1549
- var _a4;
1559
+ var _a3;
1550
1560
  try {
1551
1561
  const { options } = this;
1552
1562
  if (!options.host) {
@@ -1573,7 +1583,7 @@ var DTSManager = (_a = class {
1573
1583
  }
1574
1584
  logger.success("Federated types extraction completed");
1575
1585
  } catch (err) {
1576
- if (((_a4 = this.options.host) == null ? void 0 : _a4.abortOnError) === false) {
1586
+ if (((_a3 = this.options.host) == null ? void 0 : _a3.abortOnError) === false) {
1577
1587
  fileLog(`Unable to consume federated types, ${err}`, "consumeTypes", "error");
1578
1588
  } else {
1579
1589
  throw err;
@@ -1583,11 +1593,12 @@ var DTSManager = (_a = class {
1583
1593
  }
1584
1594
  updateTypes(options) {
1585
1595
  return __async(this, null, function* () {
1586
- var _a4, _b, _c;
1596
+ var _a3, _b, _c;
1587
1597
  try {
1588
- const { remoteName, updateMode, remoteInfo: updatedRemoteInfo, once } = options;
1589
- const hostName = (_c = (_b = (_a4 = this.options) == null ? void 0 : _a4.host) == null ? void 0 : _b.moduleFederationConfig) == null ? void 0 : _c.name;
1590
- fileLog(`updateTypes options:, ${JSON.stringify(options, null, 2)}`, "consumeTypes", "info");
1598
+ const { remoteName, updateMode, remoteTarPath, remoteInfo: updatedRemoteInfo, once } = options;
1599
+ const hostName = (_c = (_b = (_a3 = this.options) == null ? void 0 : _a3.host) == null ? void 0 : _b.moduleFederationConfig) == null ? void 0 : _c.name;
1600
+ fileLog(`options: ${JSON.stringify(options, null, 2)};
1601
+ hostName: ${hostName}`, "updateTypes", "info");
1591
1602
  if (updateMode === UpdateMode.POSITIVE && remoteName === hostName) {
1592
1603
  if (!this.options.remote) {
1593
1604
  return;
@@ -1601,13 +1612,23 @@ var DTSManager = (_a = class {
1601
1612
  const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(this.options.host);
1602
1613
  const loadedRemoteInfo = Object.values(remoteAliasMap).find((i) => i.name === remoteName);
1603
1614
  const consumeTypes2 = /* @__PURE__ */ __name((requiredRemoteInfo) => __async(this, null, function* () {
1604
- const [_alias, destinationPath] = yield this.consumeTargetRemotes(hostOptions, requiredRemoteInfo);
1615
+ fileLog(`consumeTypes start`, "updateTypes", "info");
1616
+ if (!requiredRemoteInfo.zipUrl) {
1617
+ throw new Error(`Can not get ${requiredRemoteInfo.name}'s types archive url!`);
1618
+ }
1619
+ const [_alias, destinationPath] = yield this.consumeTargetRemotes(hostOptions, __spreadProps(__spreadValues({}, requiredRemoteInfo), {
1620
+ // use remoteTarPath first
1621
+ zipUrl: remoteTarPath || requiredRemoteInfo.zipUrl
1622
+ }));
1605
1623
  yield this.downloadAPITypes(requiredRemoteInfo, destinationPath);
1624
+ fileLog(`consumeTypes end`, "updateTypes", "info");
1606
1625
  }), "consumeTypes");
1626
+ fileLog(`loadedRemoteInfo: ${JSON.stringify(loadedRemoteInfo, null, 2)}`, "updateTypes", "info");
1607
1627
  if (!loadedRemoteInfo) {
1608
1628
  const remoteInfo = Object.values(mapRemotesToDownload).find((item) => {
1609
1629
  return item.name === remoteName;
1610
1630
  });
1631
+ fileLog(`remoteInfo: ${JSON.stringify(remoteInfo, null, 2)}`, "updateTypes", "info");
1611
1632
  if (remoteInfo) {
1612
1633
  if (!this.remoteAliasMap[remoteInfo.alias]) {
1613
1634
  const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo);
@@ -1627,7 +1648,7 @@ var DTSManager = (_a = class {
1627
1648
  });
1628
1649
  fileLog(`start request manifest`, "consumeTypes", "info");
1629
1650
  this.updatedRemoteInfos[updatedRemoteInfo.name] = yield this.requestRemoteManifest(parsedRemoteInfo);
1630
- fileLog(`end request manifest, this.updatedRemoteInfos[updatedRemoteInfo.name]: ${JSON.stringify(this.updatedRemoteInfos[updatedRemoteInfo.name], null, 2)}`, "consumeTypes", "info");
1651
+ fileLog(`end request manifest, this.updatedRemoteInfos[updatedRemoteInfo.name]: ${JSON.stringify(this.updatedRemoteInfos[updatedRemoteInfo.name], null, 2)}`, "updateTypes", "info");
1631
1652
  yield consumeDynamicRemoteTypes();
1632
1653
  }
1633
1654
  if (!once && this.updatedRemoteInfos[updatedRemoteInfo.name]) {
@@ -1742,6 +1763,10 @@ function cloneDeepOptions(options) {
1742
1763
  });
1743
1764
  }
1744
1765
  __name(cloneDeepOptions, "cloneDeepOptions");
1766
+ var getEnvHeaders = /* @__PURE__ */ __name(() => {
1767
+ const headersStr = (0, import_sdk6.getProcessEnv)()["MF_ENV_HEADERS"] || "{}";
1768
+ return __spreadValues({}, JSON.parse(headersStr));
1769
+ }, "getEnvHeaders");
1745
1770
  function axiosGet(url, config) {
1746
1771
  return __async(this, null, function* () {
1747
1772
  const httpAgent = new import_http2.default.Agent({
@@ -1750,10 +1775,12 @@ function axiosGet(url, config) {
1750
1775
  const httpsAgent = new import_https.default.Agent({
1751
1776
  family: 4
1752
1777
  });
1753
- return import_axios.default.get(url, __spreadValues({
1778
+ return import_axios.default.get(url, __spreadValues(__spreadValues({
1754
1779
  httpAgent,
1755
1780
  httpsAgent
1756
- }, config));
1781
+ }, {
1782
+ headers: getEnvHeaders()
1783
+ }), config));
1757
1784
  });
1758
1785
  }
1759
1786
  __name(axiosGet, "axiosGet");
@@ -1813,7 +1840,7 @@ var readTsConfig = /* @__PURE__ */ __name(({ tsConfigPath, typesFolder, compiled
1813
1840
  };
1814
1841
  rawTsConfigJson.compilerOptions = rawTsConfigJson.compilerOptions || {};
1815
1842
  rawTsConfigJson.compilerOptions = __spreadValues(__spreadValues({}, rawTsConfigJson.compilerOptions), defaultCompilerOptions);
1816
- const _a4 = rawTsConfigJson.compilerOptions || {}, { paths, baseUrl } = _a4, restCompilerOptions = __objRest(_a4, ["paths", "baseUrl"]);
1843
+ const _a3 = rawTsConfigJson.compilerOptions || {}, { paths, baseUrl } = _a3, restCompilerOptions = __objRest(_a3, ["paths", "baseUrl"]);
1817
1844
  rawTsConfigJson.compilerOptions = restCompilerOptions;
1818
1845
  const filesToCompile = [
1819
1846
  ...Object.values(mapComponentsToExpose),
@@ -1874,8 +1901,8 @@ var retrieveRemoteConfig = /* @__PURE__ */ __name((options) => {
1874
1901
  // packages/dts-plugin/src/core/lib/generateTypes.ts
1875
1902
  function generateTypes(options) {
1876
1903
  return __async(this, null, function* () {
1877
- var _a4;
1878
- const DTSManagerConstructor = getDTSManagerConstructor((_a4 = options.remote) == null ? void 0 : _a4.implementation);
1904
+ var _a3;
1905
+ const DTSManagerConstructor = getDTSManagerConstructor((_a3 = options.remote) == null ? void 0 : _a3.implementation);
1879
1906
  const dtsManager = new DTSManagerConstructor(options);
1880
1907
  return dtsManager.generateTypes();
1881
1908
  });
@@ -2143,8 +2170,8 @@ var _DtsWorker = class _DtsWorker {
2143
2170
  this._res = this.rpcWorker.connect(this._options);
2144
2171
  }
2145
2172
  removeUnSerializationOptions() {
2146
- var _a4, _b, _c, _d, _e, _f, _g, _h;
2147
- if ((_b = (_a4 = this._options.remote) == null ? void 0 : _a4.moduleFederationConfig) == null ? void 0 : _b.manifest) {
2173
+ var _a3, _b, _c, _d, _e, _f, _g, _h;
2174
+ if ((_b = (_a3 = this._options.remote) == null ? void 0 : _a3.moduleFederationConfig) == null ? void 0 : _b.manifest) {
2148
2175
  (_d = (_c = this._options.remote) == null ? void 0 : _c.moduleFederationConfig) == null ? true : delete _d.manifest;
2149
2176
  }
2150
2177
  if ((_f = (_e = this._options.host) == null ? void 0 : _e.moduleFederationConfig) == null ? void 0 : _f.manifest) {
@@ -2153,9 +2180,9 @@ var _DtsWorker = class _DtsWorker {
2153
2180
  }
2154
2181
  get controlledPromise() {
2155
2182
  const ensureChildProcessExit = /* @__PURE__ */ __name(() => {
2156
- var _a4;
2183
+ var _a3;
2157
2184
  try {
2158
- const pid = (_a4 = this.rpcWorker.process) == null ? void 0 : _a4.pid;
2185
+ const pid = (_a3 = this.rpcWorker.process) == null ? void 0 : _a3.pid;
2159
2186
  const rootPid = process.pid;
2160
2187
  if (pid && rootPid !== pid) {
2161
2188
  process.kill(pid, 0);
@@ -2177,9 +2204,9 @@ var _DtsWorker = class _DtsWorker {
2177
2204
  });
2178
2205
  }
2179
2206
  exit() {
2180
- var _a4;
2207
+ var _a3;
2181
2208
  try {
2182
- (_a4 = this.rpcWorker) == null ? void 0 : _a4.terminate();
2209
+ (_a3 = this.rpcWorker) == null ? void 0 : _a3.terminate();
2183
2210
  } catch (err) {
2184
2211
  if (isDebugMode()) {
2185
2212
  console.error(err);
@@ -2202,8 +2229,8 @@ __name(generateTypesInChildProcess, "generateTypesInChildProcess");
2202
2229
  // packages/dts-plugin/src/core/lib/consumeTypes.ts
2203
2230
  function consumeTypes(options) {
2204
2231
  return __async(this, null, function* () {
2205
- var _a4;
2206
- const DTSManagerConstructor = getDTSManagerConstructor((_a4 = options.host) == null ? void 0 : _a4.implementation);
2232
+ var _a3;
2233
+ const DTSManagerConstructor = getDTSManagerConstructor((_a3 = options.host) == null ? void 0 : _a3.implementation);
2207
2234
  const dtsManager = new DTSManagerConstructor(options);
2208
2235
  yield dtsManager.consumeTypes();
2209
2236
  });
@@ -2223,16 +2250,16 @@ var _DevWorker = class _DevWorker {
2223
2250
  }
2224
2251
  // moduleFederationConfig.manifest may have un serialization options
2225
2252
  removeUnSerializationOptions() {
2226
- var _a4, _b, _c, _d;
2227
- (_b = (_a4 = this._options.host) == null ? void 0 : _a4.moduleFederationConfig) == null ? true : delete _b.manifest;
2253
+ var _a3, _b, _c, _d;
2254
+ (_b = (_a3 = this._options.host) == null ? void 0 : _a3.moduleFederationConfig) == null ? true : delete _b.manifest;
2228
2255
  (_d = (_c = this._options.remote) == null ? void 0 : _c.moduleFederationConfig) == null ? true : delete _d.manifest;
2229
2256
  }
2230
2257
  get controlledPromise() {
2231
2258
  return this._res;
2232
2259
  }
2233
2260
  update() {
2234
- var _a4, _b;
2235
- (_b = (_a4 = this._rpcWorker.process) == null ? void 0 : _a4.send) == null ? void 0 : _b.call(_a4, {
2261
+ var _a3, _b;
2262
+ (_b = (_a3 = this._rpcWorker.process) == null ? void 0 : _a3.send) == null ? void 0 : _b.call(_a3, {
2236
2263
  type: rpc_exports.RpcGMCallTypes.CALL,
2237
2264
  id: this._rpcWorker.id,
2238
2265
  args: [
@@ -2242,8 +2269,8 @@ var _DevWorker = class _DevWorker {
2242
2269
  });
2243
2270
  }
2244
2271
  exit() {
2245
- var _a4;
2246
- (_a4 = this._rpcWorker) == null ? void 0 : _a4.terminate();
2272
+ var _a3;
2273
+ (_a3 = this._rpcWorker) == null ? void 0 : _a3.terminate();
2247
2274
  }
2248
2275
  };
2249
2276
  __name(_DevWorker, "DevWorker");
@@ -2268,14 +2295,26 @@ function createDevWorker(options) {
2268
2295
  __name(createDevWorker, "createDevWorker");
2269
2296
 
2270
2297
  // packages/dts-plugin/src/plugins/DevPlugin.ts
2271
- var import_sdk6 = require("@module-federation/sdk");
2272
- var import_path8 = __toESM(require("path"));
2298
+ var import_sdk7 = require("@module-federation/sdk");
2273
2299
 
2274
2300
  // packages/dts-plugin/src/plugins/utils.ts
2301
+ var import_path8 = __toESM(require("path"));
2275
2302
  function isDev() {
2276
2303
  return process.env["NODE_ENV"] === "development";
2277
2304
  }
2278
2305
  __name(isDev, "isDev");
2306
+ function isPrd() {
2307
+ return process.env["NODE_ENV"] === "production";
2308
+ }
2309
+ __name(isPrd, "isPrd");
2310
+ function getCompilerOutputDir(compiler) {
2311
+ try {
2312
+ return import_path8.default.relative(compiler.context, compiler.outputPath || compiler.options.output.path);
2313
+ } catch (err) {
2314
+ return "";
2315
+ }
2316
+ }
2317
+ __name(getCompilerOutputDir, "getCompilerOutputDir");
2279
2318
 
2280
2319
  // packages/dts-plugin/src/plugins/DevPlugin.ts
2281
2320
  var PROCESS_EXIT_CODE;
@@ -2285,22 +2324,26 @@ var PROCESS_EXIT_CODE;
2285
2324
  })(PROCESS_EXIT_CODE || (PROCESS_EXIT_CODE = {}));
2286
2325
  function ensureTempDir(filePath) {
2287
2326
  try {
2288
- const dir = import_path8.default.dirname(filePath);
2327
+ const dir = import_path9.default.dirname(filePath);
2289
2328
  import_fs_extra3.default.ensureDirSync(dir);
2290
2329
  } catch (_err) {
2291
2330
  }
2292
2331
  }
2293
2332
  __name(ensureTempDir, "ensureTempDir");
2294
2333
  var _DevPlugin = class _DevPlugin {
2295
- constructor(options) {
2334
+ constructor(options, dtsOptions, fetchTypesPromise) {
2296
2335
  __publicField(this, "name", "MFDevPlugin");
2297
2336
  __publicField(this, "_options");
2298
2337
  __publicField(this, "_devWorker");
2338
+ __publicField(this, "dtsOptions");
2339
+ __publicField(this, "fetchTypesPromise");
2299
2340
  this._options = options;
2341
+ this.fetchTypesPromise = fetchTypesPromise;
2342
+ this.dtsOptions = dtsOptions;
2300
2343
  }
2301
2344
  static ensureLiveReloadEntry(options, filePath) {
2302
2345
  ensureTempDir(filePath);
2303
- const liveReloadEntry = import_fs_extra3.default.readFileSync(import_path8.default.join(__dirname, "./iife/launch-web-client.js")).toString("utf-8");
2346
+ const liveReloadEntry = import_fs_extra3.default.readFileSync(import_path9.default.join(__dirname, "./iife/launch-web-client.js")).toString("utf-8");
2304
2347
  const liveReloadEntryWithOptions = liveReloadEntry.replace(WEB_CLIENT_OPTIONS_IDENTIFIER, JSON.stringify(options));
2305
2348
  import_fs_extra3.default.writeFileSync(filePath, liveReloadEntryWithOptions);
2306
2349
  }
@@ -2327,20 +2370,21 @@ var _DevPlugin = class _DevPlugin {
2327
2370
  });
2328
2371
  }
2329
2372
  _exit(exitCode = 0) {
2330
- var _a4;
2331
- (_a4 = this._devWorker) == null ? void 0 : _a4.exit();
2373
+ var _a3;
2374
+ (_a3 = this._devWorker) == null ? void 0 : _a3.exit();
2332
2375
  process.exit(exitCode);
2333
2376
  }
2334
2377
  _afterEmit() {
2335
- var _a4;
2336
- (_a4 = this._devWorker) == null ? void 0 : _a4.update();
2378
+ var _a3;
2379
+ (_a3 = this._devWorker) == null ? void 0 : _a3.update();
2337
2380
  }
2338
2381
  apply(compiler) {
2382
+ var _a3;
2339
2383
  const { _options: { name, dev, dts } } = this;
2340
2384
  new compiler.webpack.DefinePlugin({
2341
2385
  FEDERATION_IPV4: JSON.stringify(getIPV4())
2342
2386
  }).apply(compiler);
2343
- const normalizedDev = (0, import_sdk6.normalizeOptions)(true, {
2387
+ const normalizedDev = (0, import_sdk7.normalizeOptions)(true, {
2344
2388
  disableLiveReload: true,
2345
2389
  disableHotTypesReload: false,
2346
2390
  disableDynamicRemoteTypeHints: false
@@ -2358,11 +2402,11 @@ var _DevPlugin = class _DevPlugin {
2358
2402
  if (!this._options.runtimePlugins) {
2359
2403
  this._options.runtimePlugins = [];
2360
2404
  }
2361
- this._options.runtimePlugins.push(import_path8.default.resolve(__dirname, "dynamic-remote-type-hints-plugin.js"));
2405
+ this._options.runtimePlugins.push(import_path9.default.resolve(__dirname, "dynamic-remote-type-hints-plugin.js"));
2362
2406
  }
2363
2407
  if (!normalizedDev.disableLiveReload) {
2364
- const TEMP_DIR2 = import_path8.default.join(`${process.cwd()}/node_modules`, import_sdk6.TEMP_DIR);
2365
- const filepath = import_path8.default.join(TEMP_DIR2, `live-reload.js`);
2408
+ const TEMP_DIR2 = import_path9.default.join(`${process.cwd()}/node_modules`, import_sdk7.TEMP_DIR);
2409
+ const filepath = import_path9.default.join(TEMP_DIR2, `live-reload.js`);
2366
2410
  if (typeof compiler.options.entry === "object") {
2367
2411
  _DevPlugin.ensureLiveReloadEntry({
2368
2412
  name
@@ -2381,23 +2425,24 @@ var _DevPlugin = class _DevPlugin {
2381
2425
  const defaultConsumeTypes = {
2382
2426
  consumeAPITypes: true
2383
2427
  };
2384
- const normalizedDtsOptions = (0, import_sdk6.normalizeOptions)(isTSProject(dts, compiler.context), {
2428
+ const normalizedDtsOptions = (0, import_sdk7.normalizeOptions)(isTSProject(dts, compiler.context), {
2385
2429
  // remote types dist(.dev-server) not be used currently, so no need to set extractThirdParty etc
2386
2430
  generateTypes: defaultGenerateTypes,
2387
2431
  consumeTypes: defaultConsumeTypes,
2388
- extraOptions: {}
2432
+ extraOptions: {},
2433
+ displayErrorInTerminal: (_a3 = this.dtsOptions) == null ? void 0 : _a3.displayErrorInTerminal
2389
2434
  }, "mfOptions.dts")(dts);
2390
- const normalizedGenerateTypes = (0, import_sdk6.normalizeOptions)(Boolean(normalizedDtsOptions), defaultGenerateTypes, "mfOptions.dts.generateTypes")(normalizedDtsOptions === false ? void 0 : normalizedDtsOptions.generateTypes);
2435
+ const normalizedGenerateTypes = (0, import_sdk7.normalizeOptions)(Boolean(normalizedDtsOptions), defaultGenerateTypes, "mfOptions.dts.generateTypes")(normalizedDtsOptions === false ? void 0 : normalizedDtsOptions.generateTypes);
2391
2436
  const remote = normalizedGenerateTypes === false ? void 0 : __spreadProps(__spreadValues({
2392
2437
  implementation: normalizedDtsOptions === false ? void 0 : normalizedDtsOptions.implementation,
2393
2438
  context: compiler.context,
2394
- outputDir: import_path8.default.relative(compiler.context, compiler.outputPath || compiler.options.output.path),
2439
+ outputDir: getCompilerOutputDir(compiler),
2395
2440
  moduleFederationConfig: __spreadValues({}, this._options),
2396
2441
  hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || "@mf-types"
2397
2442
  }, normalizedGenerateTypes), {
2398
2443
  typesFolder: `.dev-server`
2399
2444
  });
2400
- const normalizedConsumeTypes = (0, import_sdk6.normalizeOptions)(Boolean(normalizedDtsOptions), defaultConsumeTypes, "mfOptions.dts.consumeTypes")(normalizedDtsOptions === false ? void 0 : normalizedDtsOptions.consumeTypes);
2445
+ const normalizedConsumeTypes = (0, import_sdk7.normalizeOptions)(Boolean(normalizedDtsOptions), defaultConsumeTypes, "mfOptions.dts.consumeTypes")(normalizedDtsOptions === false ? void 0 : normalizedDtsOptions.consumeTypes);
2401
2446
  const host = normalizedConsumeTypes === false ? void 0 : __spreadValues({
2402
2447
  implementation: normalizedDtsOptions === false ? void 0 : normalizedDtsOptions.implementation,
2403
2448
  context: compiler.context,
@@ -2412,13 +2457,15 @@ var _DevPlugin = class _DevPlugin {
2412
2457
  if (remote && !(remote == null ? void 0 : remote.tsConfigPath) && typeof normalizedDtsOptions === "object" && normalizedDtsOptions.tsConfigPath) {
2413
2458
  remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
2414
2459
  }
2415
- this._devWorker = createDevWorker({
2416
- name,
2417
- remote,
2418
- host,
2419
- extraOptions,
2420
- disableLiveReload: normalizedDev.disableHotTypesReload,
2421
- disableHotTypesReload: normalizedDev.disableHotTypesReload
2460
+ this.fetchTypesPromise.then(() => {
2461
+ this._devWorker = createDevWorker({
2462
+ name,
2463
+ remote,
2464
+ host,
2465
+ extraOptions,
2466
+ disableLiveReload: normalizedDev.disableHotTypesReload,
2467
+ disableHotTypesReload: normalizedDev.disableHotTypesReload
2468
+ });
2422
2469
  });
2423
2470
  this._stopWhenSIGTERMOrSIGINT();
2424
2471
  this._handleUnexpectedExit();
@@ -2428,24 +2475,31 @@ var _DevPlugin = class _DevPlugin {
2428
2475
  __name(_DevPlugin, "DevPlugin");
2429
2476
  var DevPlugin = _DevPlugin;
2430
2477
 
2431
- // packages/dts-plugin/src/plugins/TypesPlugin.ts
2432
- var import_sdk9 = require("@module-federation/sdk");
2478
+ // packages/dts-plugin/src/plugins/DtsPlugin.ts
2479
+ var import_sdk10 = require("@module-federation/sdk");
2433
2480
 
2434
2481
  // packages/dts-plugin/src/plugins/ConsumeTypesPlugin.ts
2435
- var import_sdk7 = require("@module-federation/sdk");
2482
+ var import_sdk8 = require("@module-federation/sdk");
2436
2483
  var _ConsumeTypesPlugin = class _ConsumeTypesPlugin {
2437
- constructor(pluginOptions, dtsOptions, defaultOptions3) {
2484
+ constructor(pluginOptions, dtsOptions, defaultOptions3, callback) {
2438
2485
  __publicField(this, "pluginOptions");
2439
2486
  __publicField(this, "dtsOptions");
2440
2487
  __publicField(this, "defaultOptions");
2488
+ __publicField(this, "callback");
2441
2489
  this.pluginOptions = pluginOptions;
2442
2490
  this.dtsOptions = dtsOptions;
2443
2491
  this.defaultOptions = defaultOptions3;
2492
+ this.callback = callback;
2444
2493
  }
2445
2494
  apply(compiler) {
2446
- const { dtsOptions, defaultOptions: defaultOptions3, pluginOptions } = this;
2447
- const normalizedConsumeTypes = (0, import_sdk7.normalizeOptions)(true, defaultOptions3, "mfOptions.dts.consumeTypes")(dtsOptions.consumeTypes);
2495
+ const { dtsOptions, defaultOptions: defaultOptions3, pluginOptions, callback } = this;
2496
+ if (isPrd()) {
2497
+ callback();
2498
+ return;
2499
+ }
2500
+ const normalizedConsumeTypes = (0, import_sdk8.normalizeOptions)(true, defaultOptions3, "mfOptions.dts.consumeTypes")(dtsOptions.consumeTypes);
2448
2501
  if (!normalizedConsumeTypes) {
2502
+ callback();
2449
2503
  return;
2450
2504
  }
2451
2505
  const finalOptions = {
@@ -2454,10 +2508,15 @@ var _ConsumeTypesPlugin = class _ConsumeTypesPlugin {
2454
2508
  context: compiler.context,
2455
2509
  moduleFederationConfig: pluginOptions
2456
2510
  }, normalizedConsumeTypes),
2457
- extraOptions: dtsOptions.extraOptions || {}
2511
+ extraOptions: dtsOptions.extraOptions || {},
2512
+ displayErrorInTerminal: dtsOptions.displayErrorInTerminal
2458
2513
  };
2459
2514
  validateOptions(finalOptions.host);
2460
- consumeTypes(finalOptions);
2515
+ consumeTypes(finalOptions).then(() => {
2516
+ callback();
2517
+ }).catch(() => {
2518
+ callback();
2519
+ });
2461
2520
  }
2462
2521
  };
2463
2522
  __name(_ConsumeTypesPlugin, "ConsumeTypesPlugin");
@@ -2465,31 +2524,37 @@ var ConsumeTypesPlugin = _ConsumeTypesPlugin;
2465
2524
 
2466
2525
  // packages/dts-plugin/src/plugins/GenerateTypesPlugin.ts
2467
2526
  var import_fs4 = __toESM(require("fs"));
2468
- var import_sdk8 = require("@module-federation/sdk");
2469
- var import_path9 = __toESM(require("path"));
2527
+ var import_path10 = __toESM(require("path"));
2528
+ var import_sdk9 = require("@module-federation/sdk");
2470
2529
  var _GenerateTypesPlugin = class _GenerateTypesPlugin {
2471
- constructor(pluginOptions, dtsOptions, defaultOptions3) {
2530
+ constructor(pluginOptions, dtsOptions, defaultOptions3, consumeTypesPromise, callback) {
2472
2531
  __publicField(this, "pluginOptions");
2473
2532
  __publicField(this, "dtsOptions");
2474
2533
  __publicField(this, "defaultOptions");
2534
+ __publicField(this, "consumeTypesPromise");
2535
+ __publicField(this, "callback");
2475
2536
  this.pluginOptions = pluginOptions;
2476
2537
  this.dtsOptions = dtsOptions;
2477
2538
  this.defaultOptions = defaultOptions3;
2539
+ this.consumeTypesPromise = consumeTypesPromise;
2540
+ this.callback = callback;
2478
2541
  }
2479
2542
  apply(compiler) {
2480
- const { dtsOptions, defaultOptions: defaultOptions3, pluginOptions } = this;
2481
- const normalizedGenerateTypes = (0, import_sdk8.normalizeOptions)(true, defaultOptions3, "mfOptions.dts.generateTypes")(dtsOptions.generateTypes);
2543
+ const { dtsOptions, defaultOptions: defaultOptions3, pluginOptions, consumeTypesPromise, callback } = this;
2544
+ const normalizedGenerateTypes = (0, import_sdk9.normalizeOptions)(true, defaultOptions3, "mfOptions.dts.generateTypes")(dtsOptions.generateTypes);
2482
2545
  if (!normalizedGenerateTypes) {
2546
+ callback();
2483
2547
  return;
2484
2548
  }
2485
2549
  const finalOptions = {
2486
2550
  remote: __spreadValues({
2487
2551
  implementation: dtsOptions.implementation,
2488
2552
  context: compiler.context,
2489
- outputDir: import_path9.default.relative(compiler.context, compiler.outputPath || compiler.options.output.path),
2553
+ outputDir: getCompilerOutputDir(compiler),
2490
2554
  moduleFederationConfig: pluginOptions
2491
2555
  }, normalizedGenerateTypes),
2492
- extraOptions: dtsOptions.extraOptions || {}
2556
+ extraOptions: dtsOptions.extraOptions || {},
2557
+ displayErrorInTerminal: dtsOptions.displayErrorInTerminal
2493
2558
  };
2494
2559
  if (dtsOptions.tsConfigPath && !finalOptions.remote.tsConfigPath) {
2495
2560
  finalOptions.remote.tsConfigPath = dtsOptions.tsConfigPath;
@@ -2510,62 +2575,6 @@ var _GenerateTypesPlugin = class _GenerateTypesPlugin {
2510
2575
  }, "getGenerateTypesFn");
2511
2576
  const generateTypesFn = getGenerateTypesFn();
2512
2577
  let compiledOnce = false;
2513
- const emitTypesFilesDev = /* @__PURE__ */ __name(() => __async(this, null, function* () {
2514
- try {
2515
- if (!isDev()) {
2516
- return;
2517
- }
2518
- const { zipTypesPath, apiTypesPath, zipName, apiFileName } = retrieveTypesAssetsInfo(finalOptions.remote);
2519
- yield generateTypesFn(finalOptions);
2520
- const config = finalOptions.remote.moduleFederationConfig;
2521
- let zipPrefix = "";
2522
- if (typeof config.manifest === "object" && config.manifest.filePath) {
2523
- zipPrefix = config.manifest.filePath;
2524
- } else if (typeof config.manifest === "object" && config.manifest.fileName) {
2525
- zipPrefix = import_path9.default.dirname(config.manifest.fileName);
2526
- } else if (config.filename) {
2527
- zipPrefix = import_path9.default.dirname(config.filename);
2528
- }
2529
- if (zipTypesPath) {
2530
- const zipContent = import_fs4.default.readFileSync(zipTypesPath);
2531
- const zipOutputPath = import_path9.default.join(compiler.outputPath, zipPrefix, zipName);
2532
- yield new Promise((resolve5, reject) => {
2533
- compiler.outputFileSystem.mkdir(import_path9.default.dirname(zipOutputPath), (err) => {
2534
- if (err)
2535
- reject(err);
2536
- else {
2537
- compiler.outputFileSystem.writeFile(zipOutputPath, zipContent, (writeErr) => {
2538
- if (writeErr)
2539
- reject(writeErr);
2540
- else
2541
- resolve5();
2542
- });
2543
- }
2544
- });
2545
- });
2546
- }
2547
- if (apiTypesPath) {
2548
- const apiContent = import_fs4.default.readFileSync(apiTypesPath);
2549
- const apiOutputPath = import_path9.default.join(compiler.outputPath, zipPrefix, apiFileName);
2550
- yield new Promise((resolve5, reject) => {
2551
- compiler.outputFileSystem.mkdir(import_path9.default.dirname(apiOutputPath), (err) => {
2552
- if (err)
2553
- reject(err);
2554
- else {
2555
- compiler.outputFileSystem.writeFile(apiOutputPath, apiContent, (writeErr) => {
2556
- if (writeErr)
2557
- reject(writeErr);
2558
- else
2559
- resolve5();
2560
- });
2561
- }
2562
- });
2563
- });
2564
- }
2565
- } catch (err) {
2566
- console.error(err);
2567
- }
2568
- }), "emitTypesFilesDev");
2569
2578
  compiler.hooks.thisCompilation.tap("mf:generateTypes", (compilation) => {
2570
2579
  compilation.hooks.processAssets.tapPromise({
2571
2580
  name: "mf:generateTypes",
@@ -2574,12 +2583,12 @@ var _GenerateTypesPlugin = class _GenerateTypesPlugin {
2574
2583
  compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_TRANSFER
2575
2584
  )
2576
2585
  }, () => __async(this, null, function* () {
2586
+ yield consumeTypesPromise;
2577
2587
  try {
2578
2588
  if (pluginOptions.dev === false && compiledOnce) {
2579
2589
  return;
2580
2590
  }
2581
2591
  if (compiledOnce) {
2582
- emitTypesFilesDev();
2583
2592
  return;
2584
2593
  }
2585
2594
  const { zipTypesPath, apiTypesPath, zipName, apiFileName } = retrieveTypesAssetsInfo(finalOptions.remote);
@@ -2592,19 +2601,25 @@ var _GenerateTypesPlugin = class _GenerateTypesPlugin {
2592
2601
  if (typeof config.manifest === "object" && config.manifest.filePath) {
2593
2602
  zipPrefix = config.manifest.filePath;
2594
2603
  } else if (typeof config.manifest === "object" && config.manifest.fileName) {
2595
- zipPrefix = import_path9.default.dirname(config.manifest.fileName);
2604
+ zipPrefix = import_path10.default.dirname(config.manifest.fileName);
2596
2605
  } else if (config.filename) {
2597
- zipPrefix = import_path9.default.dirname(config.filename);
2606
+ zipPrefix = import_path10.default.dirname(config.filename);
2598
2607
  }
2599
- if (zipTypesPath) {
2600
- compilation.emitAsset(import_path9.default.join(zipPrefix, zipName), new compiler.webpack.sources.RawSource(import_fs4.default.readFileSync(zipTypesPath), false));
2608
+ const zipAssetName = import_path10.default.join(zipPrefix, zipName);
2609
+ if (zipTypesPath && !compilation.getAsset(zipAssetName)) {
2610
+ compilation.emitAsset(import_path10.default.join(zipPrefix, zipName), new compiler.webpack.sources.RawSource(import_fs4.default.readFileSync(zipTypesPath), false));
2601
2611
  }
2602
- if (apiTypesPath) {
2603
- compilation.emitAsset(import_path9.default.join(zipPrefix, apiFileName), new compiler.webpack.sources.RawSource(import_fs4.default.readFileSync(apiTypesPath), false));
2612
+ const apiAssetName = import_path10.default.join(zipPrefix, apiFileName);
2613
+ if (apiTypesPath && !compilation.getAsset(apiAssetName)) {
2614
+ compilation.emitAsset(import_path10.default.join(zipPrefix, apiFileName), new compiler.webpack.sources.RawSource(import_fs4.default.readFileSync(apiTypesPath), false));
2604
2615
  }
2605
2616
  compiledOnce = true;
2617
+ callback();
2606
2618
  } catch (err) {
2607
- console.error("Error in mf:generateTypes processAssets hook:", err);
2619
+ callback();
2620
+ if (finalOptions.displayErrorInTerminal) {
2621
+ console.error("Error in mf:generateTypes processAssets hook:", err);
2622
+ }
2608
2623
  }
2609
2624
  }));
2610
2625
  });
@@ -2613,9 +2628,8 @@ var _GenerateTypesPlugin = class _GenerateTypesPlugin {
2613
2628
  __name(_GenerateTypesPlugin, "GenerateTypesPlugin");
2614
2629
  var GenerateTypesPlugin = _GenerateTypesPlugin;
2615
2630
 
2616
- // packages/dts-plugin/src/plugins/TypesPlugin.ts
2617
- var _a3;
2618
- var TypesPlugin = (_a3 = class {
2631
+ // packages/dts-plugin/src/plugins/DtsPlugin.ts
2632
+ var _DtsPlugin = class _DtsPlugin {
2619
2633
  constructor(options) {
2620
2634
  __publicField(this, "options");
2621
2635
  this.options = options;
@@ -2633,29 +2647,26 @@ var TypesPlugin = (_a3 = class {
2633
2647
  abortOnError: false,
2634
2648
  consumeAPITypes: true
2635
2649
  };
2636
- const normalizedDtsOptions = (0, import_sdk9.normalizeOptions)(isTSProject(options.dts, compiler.context), {
2650
+ const normalizedDtsOptions = (0, import_sdk10.normalizeOptions)(isTSProject(options.dts, compiler.context), {
2637
2651
  generateTypes: defaultGenerateTypes,
2638
2652
  consumeTypes: defaultConsumeTypes,
2639
- extraOptions: {}
2653
+ extraOptions: {},
2654
+ displayErrorInTerminal: true
2640
2655
  }, "mfOptions.dts")(options.dts);
2641
2656
  if (typeof normalizedDtsOptions !== "object") {
2642
2657
  return;
2643
2658
  }
2644
- new GenerateTypesPlugin(options, normalizedDtsOptions, defaultGenerateTypes).apply(compiler);
2645
- new ConsumeTypesPlugin(options, normalizedDtsOptions, defaultConsumeTypes).apply(compiler);
2646
- }
2647
- }, __name(_a3, "TypesPlugin"), _a3);
2648
-
2649
- // packages/dts-plugin/src/plugins/DtsPlugin.ts
2650
- var _DtsPlugin = class _DtsPlugin {
2651
- constructor(options) {
2652
- __publicField(this, "options");
2653
- this.options = options;
2654
- }
2655
- apply(compiler) {
2656
- const { options } = this;
2657
- new DevPlugin(options).apply(compiler);
2658
- new TypesPlugin(options).apply(compiler);
2659
+ let consumeTypesPromiseResolve;
2660
+ const consumeTypesPromise = new Promise((resolve5) => {
2661
+ consumeTypesPromiseResolve = resolve5;
2662
+ });
2663
+ let generateTypesPromiseResolve;
2664
+ const generateTypesPromise = new Promise((resolve5) => {
2665
+ generateTypesPromiseResolve = resolve5;
2666
+ });
2667
+ new DevPlugin(options, normalizedDtsOptions, generateTypesPromise).apply(compiler);
2668
+ new GenerateTypesPlugin(options, normalizedDtsOptions, defaultGenerateTypes, consumeTypesPromise, generateTypesPromiseResolve).apply(compiler);
2669
+ new ConsumeTypesPlugin(options, normalizedDtsOptions, defaultConsumeTypes, consumeTypesPromiseResolve).apply(compiler);
2659
2670
  }
2660
2671
  };
2661
2672
  __name(_DtsPlugin, "DtsPlugin");