@module-federation/dts-plugin 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -289,6 +289,7 @@ var retrieveRemoteConfig = /* @__PURE__ */ __name((options) => {
289
289
  var import_path4 = __toESM(require("path"));
290
290
  var import_promises3 = require("fs/promises");
291
291
  var import_fs2 = __toESM(require("fs"));
292
+ var import_fs_extra3 = __toESM(require("fs-extra"));
292
293
  var import_sdk5 = require("@module-federation/sdk");
293
294
  var import_third_party_dts_extractor2 = require("@module-federation/third-party-dts-extractor");
294
295
 
@@ -1322,7 +1323,8 @@ var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
1322
1323
  try {
1323
1324
  const url = fileToDownload;
1324
1325
  const response = yield axiosGet(url, {
1325
- responseType: "arraybuffer"
1326
+ responseType: "arraybuffer",
1327
+ timeout: hostOptions.timeout
1326
1328
  }).catch(downloadErrorLogger(destinationFolder, url));
1327
1329
  if (typeof ((_a2 = response.headers) == null ? void 0 : _a2["content-type"]) === "string" && response.headers["content-type"].includes("text/html")) {
1328
1330
  throw new Error(`${url} receives invalid content-type: ${response.headers["content-type"]}`);
@@ -1369,7 +1371,9 @@ var defaultOptions2 = {
1369
1371
  context: process.cwd(),
1370
1372
  abortOnError: true,
1371
1373
  consumeAPITypes: false,
1372
- runtimePkgs: []
1374
+ runtimePkgs: [],
1375
+ remoteTypeUrls: {},
1376
+ timeout: 6e4
1373
1377
  };
1374
1378
  var buildZipUrl = /* @__PURE__ */ __name((hostOptions, url) => {
1375
1379
  const remoteUrl = new URL(url, "file:");
@@ -1385,18 +1389,31 @@ var buildApiTypeUrl = /* @__PURE__ */ __name((zipUrl) => {
1385
1389
  }, "buildApiTypeUrl");
1386
1390
  var retrieveRemoteInfo = /* @__PURE__ */ __name((options) => {
1387
1391
  const { hostOptions, remoteAlias, remote } = options;
1392
+ const { remoteTypeUrls } = hostOptions;
1388
1393
  let decodedRemote = remote;
1389
1394
  if (decodedRemote.startsWith(import_sdk4.ENCODE_NAME_PREFIX)) {
1390
1395
  decodedRemote = (0, import_sdk4.decodeName)(decodedRemote, import_sdk4.ENCODE_NAME_PREFIX);
1391
1396
  }
1392
1397
  const parsedInfo = (0, import_sdk4.parseEntry)(decodedRemote, void 0, "@");
1393
1398
  const url = "entry" in parsedInfo ? parsedInfo.entry : parsedInfo.name === decodedRemote ? decodedRemote : "";
1394
- const zipUrl = url ? buildZipUrl(hostOptions, url) : "";
1399
+ let zipUrl = "";
1400
+ let apiTypeUrl = "";
1401
+ const name = parsedInfo.name || remoteAlias;
1402
+ if (typeof remoteTypeUrls === "object" && remoteTypeUrls[name]) {
1403
+ zipUrl = remoteTypeUrls[name].zip;
1404
+ apiTypeUrl = remoteTypeUrls[name].api;
1405
+ }
1406
+ if (!zipUrl && url) {
1407
+ zipUrl = buildZipUrl(hostOptions, url);
1408
+ }
1409
+ if (!apiTypeUrl && zipUrl) {
1410
+ apiTypeUrl = buildApiTypeUrl(zipUrl);
1411
+ }
1395
1412
  return {
1396
- name: parsedInfo.name || remoteAlias,
1413
+ name,
1397
1414
  url,
1398
1415
  zipUrl,
1399
- apiTypeUrl: buildApiTypeUrl(zipUrl),
1416
+ apiTypeUrl,
1400
1417
  alias: remoteAlias
1401
1418
  };
1402
1419
  }, "retrieveRemoteInfo");
@@ -1472,6 +1489,7 @@ var DTSManager = (_a = class {
1472
1489
  }
1473
1490
  extractRemoteTypes(options) {
1474
1491
  return __async(this, null, function* () {
1492
+ var _a2;
1475
1493
  const { remoteOptions, tsConfig } = options;
1476
1494
  if (!remoteOptions.extractRemoteTypes) {
1477
1495
  return;
@@ -1486,20 +1504,29 @@ var DTSManager = (_a = class {
1486
1504
  }
1487
1505
  }
1488
1506
  const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
1489
- if (hasRemotes) {
1490
- const tempHostOptions = {
1491
- moduleFederationConfig: remoteOptions.moduleFederationConfig,
1492
- typesFolder: import_path4.default.join(mfTypesPath, "node_modules"),
1493
- remoteTypesFolder: (remoteOptions == null ? void 0 : remoteOptions.hostRemoteTypesFolder) || remoteOptions.typesFolder,
1494
- deleteTypesFolder: true,
1495
- context: remoteOptions.context,
1496
- implementation: remoteOptions.implementation,
1497
- abortOnError: false
1498
- };
1499
- yield this.consumeArchiveTypes(tempHostOptions);
1507
+ if (hasRemotes && this.options.host) {
1508
+ try {
1509
+ const { hostOptions } = retrieveHostConfig(this.options.host);
1510
+ const remoteTypesFolder = import_path4.default.resolve(hostOptions.context, hostOptions.typesFolder);
1511
+ const targetDir = import_path4.default.join(mfTypesPath, "node_modules");
1512
+ if (import_fs2.default.existsSync(remoteTypesFolder)) {
1513
+ const targetFolder = import_path4.default.resolve(remoteOptions.context, targetDir);
1514
+ yield import_fs_extra3.default.ensureDir(targetFolder);
1515
+ yield import_fs_extra3.default.copy(remoteTypesFolder, targetFolder, {
1516
+ overwrite: true
1517
+ });
1518
+ }
1519
+ } catch (err) {
1520
+ if (((_a2 = this.options.host) == null ? void 0 : _a2.abortOnError) === false) {
1521
+ fileLog(`Unable to copy remote types, ${err}`, "extractRemoteTypes", "error");
1522
+ } else {
1523
+ throw err;
1524
+ }
1525
+ }
1500
1526
  }
1501
1527
  });
1502
1528
  }
1529
+ // it must execute after consumeTypes
1503
1530
  generateTypes() {
1504
1531
  return __async(this, null, function* () {
1505
1532
  var _a2;
@@ -1512,6 +1539,18 @@ var DTSManager = (_a = class {
1512
1539
  if (!Object.keys(mapComponentsToExpose).length) {
1513
1540
  return;
1514
1541
  }
1542
+ if (tsConfig.compilerOptions.tsBuildInfoFile) {
1543
+ try {
1544
+ const tsBuildInfoFile = import_path4.default.resolve(remoteOptions.context, tsConfig.compilerOptions.tsBuildInfoFile);
1545
+ const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
1546
+ if (!import_fs2.default.existsSync(mfTypesPath)) {
1547
+ import_fs2.default.rmSync(tsBuildInfoFile, {
1548
+ force: true
1549
+ });
1550
+ }
1551
+ } catch (e) {
1552
+ }
1553
+ }
1515
1554
  yield this.extractRemoteTypes({
1516
1555
  remoteOptions,
1517
1556
  tsConfig,
@@ -1541,7 +1580,7 @@ var DTSManager = (_a = class {
1541
1580
  } catch (error2) {
1542
1581
  if (((_a2 = this.options.remote) == null ? void 0 : _a2.abortOnError) === false) {
1543
1582
  if (this.options.displayErrorInTerminal) {
1544
- logger.error(`Unable to compile federated types${error2}`);
1583
+ logger.error(`Unable to compile federated types ${error2}`);
1545
1584
  }
1546
1585
  } else {
1547
1586
  throw error2;
@@ -1549,14 +1588,19 @@ var DTSManager = (_a = class {
1549
1588
  }
1550
1589
  });
1551
1590
  }
1552
- requestRemoteManifest(remoteInfo) {
1591
+ requestRemoteManifest(remoteInfo, hostOptions) {
1553
1592
  return __async(this, null, function* () {
1554
1593
  try {
1555
1594
  if (!remoteInfo.url.includes(import_sdk5.MANIFEST_EXT)) {
1556
1595
  return remoteInfo;
1557
1596
  }
1597
+ if (remoteInfo.zipUrl) {
1598
+ return remoteInfo;
1599
+ }
1558
1600
  const url = remoteInfo.url;
1559
- const res = yield axiosGet(url);
1601
+ const res = yield axiosGet(url, {
1602
+ timeout: hostOptions.timeout
1603
+ });
1560
1604
  const manifestJson = res.data;
1561
1605
  if (!manifestJson.metaData.types.zip) {
1562
1606
  throw new Error(`Can not get ${remoteInfo.name}'s types archive url!`);
@@ -1607,7 +1651,7 @@ var DTSManager = (_a = class {
1607
1651
  ]);
1608
1652
  });
1609
1653
  }
1610
- downloadAPITypes(remoteInfo, destinationPath) {
1654
+ downloadAPITypes(remoteInfo, destinationPath, hostOptions) {
1611
1655
  return __async(this, null, function* () {
1612
1656
  const { apiTypeUrl } = remoteInfo;
1613
1657
  if (!apiTypeUrl) {
@@ -1615,13 +1659,17 @@ var DTSManager = (_a = class {
1615
1659
  }
1616
1660
  try {
1617
1661
  const url = apiTypeUrl;
1618
- const res = yield axiosGet(url);
1662
+ const res = yield axiosGet(url, {
1663
+ timeout: hostOptions.timeout
1664
+ });
1619
1665
  let apiTypeFile = res.data;
1620
1666
  apiTypeFile = apiTypeFile.replaceAll(REMOTE_ALIAS_IDENTIFIER, remoteInfo.alias);
1621
1667
  const filePath = import_path4.default.join(destinationPath, REMOTE_API_TYPES_FILE_NAME);
1622
1668
  import_fs2.default.writeFileSync(filePath, apiTypeFile);
1669
+ const existed = this.loadedRemoteAPIAlias.has(remoteInfo.alias);
1623
1670
  this.loadedRemoteAPIAlias.add(remoteInfo.alias);
1624
1671
  fileLog(`success`, "downloadAPITypes", "info");
1672
+ return existed;
1625
1673
  } catch (err) {
1626
1674
  fileLog(`Unable to download "${remoteInfo.name}" api types, ${err}`, "downloadAPITypes", "error");
1627
1675
  }
@@ -1685,7 +1733,7 @@ var DTSManager = (_a = class {
1685
1733
  const downloadPromises = Object.entries(mapRemotesToDownload).map((item) => __async(this, null, function* () {
1686
1734
  const remoteInfo = item[1];
1687
1735
  if (!this.remoteAliasMap[remoteInfo.alias]) {
1688
- const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo);
1736
+ const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo, hostOptions);
1689
1737
  this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
1690
1738
  }
1691
1739
  return this.consumeTargetRemotes(hostOptions, this.remoteAliasMap[remoteInfo.alias]);
@@ -1720,7 +1768,7 @@ var DTSManager = (_a = class {
1720
1768
  if (!remoteInfo) {
1721
1769
  return;
1722
1770
  }
1723
- yield this.downloadAPITypes(remoteInfo, destinationPath);
1771
+ yield this.downloadAPITypes(remoteInfo, destinationPath, hostOptions);
1724
1772
  })));
1725
1773
  this.consumeAPITypes(hostOptions);
1726
1774
  }
@@ -1763,7 +1811,10 @@ hostName: ${hostName}`, "updateTypes", "info");
1763
1811
  // use remoteTarPath first
1764
1812
  zipUrl: remoteTarPath || requiredRemoteInfo.zipUrl
1765
1813
  }));
1766
- yield this.downloadAPITypes(requiredRemoteInfo, destinationPath);
1814
+ const addNew = yield this.downloadAPITypes(requiredRemoteInfo, destinationPath, hostOptions);
1815
+ if (addNew) {
1816
+ this.consumeAPITypes(hostOptions);
1817
+ }
1767
1818
  fileLog(`consumeTypes end`, "updateTypes", "info");
1768
1819
  }), "consumeTypes");
1769
1820
  fileLog(`loadedRemoteInfo: ${JSON.stringify(loadedRemoteInfo, null, 2)}`, "updateTypes", "info");
@@ -1774,14 +1825,13 @@ hostName: ${hostName}`, "updateTypes", "info");
1774
1825
  fileLog(`remoteInfo: ${JSON.stringify(remoteInfo, null, 2)}`, "updateTypes", "info");
1775
1826
  if (remoteInfo) {
1776
1827
  if (!this.remoteAliasMap[remoteInfo.alias]) {
1777
- const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo);
1828
+ const requiredRemoteInfo = yield this.requestRemoteManifest(remoteInfo, hostOptions);
1778
1829
  this.remoteAliasMap[remoteInfo.alias] = requiredRemoteInfo;
1779
1830
  }
1780
1831
  yield consumeTypes(this.remoteAliasMap[remoteInfo.alias]);
1781
1832
  } else if (updatedRemoteInfo) {
1782
1833
  const consumeDynamicRemoteTypes = /* @__PURE__ */ __name(() => __async(this, null, function* () {
1783
1834
  yield consumeTypes(this.updatedRemoteInfos[updatedRemoteInfo.name]);
1784
- this.consumeAPITypes(hostOptions);
1785
1835
  }), "consumeDynamicRemoteTypes");
1786
1836
  if (!this.updatedRemoteInfos[updatedRemoteInfo.name]) {
1787
1837
  const parsedRemoteInfo = retrieveRemoteInfo({
@@ -1790,7 +1840,7 @@ hostName: ${hostName}`, "updateTypes", "info");
1790
1840
  remote: updatedRemoteInfo.url
1791
1841
  });
1792
1842
  fileLog(`start request manifest`, "consumeTypes", "info");
1793
- this.updatedRemoteInfos[updatedRemoteInfo.name] = yield this.requestRemoteManifest(parsedRemoteInfo);
1843
+ this.updatedRemoteInfos[updatedRemoteInfo.name] = yield this.requestRemoteManifest(parsedRemoteInfo, hostOptions);
1794
1844
  fileLog(`end request manifest, this.updatedRemoteInfos[updatedRemoteInfo.name]: ${JSON.stringify(this.updatedRemoteInfos[updatedRemoteInfo.name], null, 2)}`, "updateTypes", "info");
1795
1845
  yield consumeDynamicRemoteTypes();
1796
1846
  }
@@ -1833,10 +1883,13 @@ function cloneDeepOptions(options) {
1833
1883
  "manifest",
1834
1884
  "async"
1835
1885
  ];
1836
- return (0, import_lodash.default)(options, (_value, key) => {
1886
+ return (0, import_lodash.default)(options, (value, key) => {
1837
1887
  if (typeof key === "string" && excludeKeys.includes(key)) {
1838
1888
  return false;
1839
1889
  }
1890
+ if (typeof value === "function") {
1891
+ return false;
1892
+ }
1840
1893
  });
1841
1894
  }
1842
1895
  __name(cloneDeepOptions, "cloneDeepOptions");
@@ -1852,12 +1905,14 @@ function axiosGet(url, config) {
1852
1905
  const httpsAgent = new import_https.default.Agent({
1853
1906
  family: 4
1854
1907
  });
1855
- return import_axios.default.get(url, __spreadValues(__spreadValues({
1908
+ return import_axios.default.get(url, __spreadProps(__spreadValues(__spreadValues({
1856
1909
  httpAgent,
1857
1910
  httpsAgent
1858
1911
  }, {
1859
1912
  headers: getEnvHeaders()
1860
- }), config));
1913
+ }), config), {
1914
+ timeout: (config == null ? void 0 : config.timeout) || 6e4
1915
+ }));
1861
1916
  });
1862
1917
  }
1863
1918
  __name(axiosGet, "axiosGet");