@module-federation/vite 1.9.8 → 1.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.
package/lib/index.cjs CHANGED
@@ -347,6 +347,344 @@ function PluginDevProxyModuleTopLevelAwait() {
347
347
  };
348
348
  }
349
349
 
350
+ function _catch(body, recover) {
351
+ try {
352
+ var result = body();
353
+ } catch (e) {
354
+ return recover(e);
355
+ }
356
+ if (result && result.then) {
357
+ return result.then(void 0, recover);
358
+ }
359
+ return result;
360
+ }
361
+ var DEFAULT_DEV_OPTIONS = {
362
+ disableLiveReload: true,
363
+ disableHotTypesReload: false,
364
+ disableDynamicRemoteTypeHints: false
365
+ };
366
+ var DYNAMIC_HINTS_PLUGIN = '@module-federation/dts-plugin/dynamic-remote-type-hints-plugin';
367
+ var getIPv4 = function getIPv4() {
368
+ return process.env['FEDERATION_IPV4'] || '127.0.0.1';
369
+ };
370
+ var forkDevWorkerPath = function () {
371
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
372
+ return require.resolve('@module-federation/dts-plugin/dist/fork-dev-worker.js');
373
+ }();
374
+ var DevWorker = /*#__PURE__*/function () {
375
+ function DevWorker(options) {
376
+ this.worker = core.rpc.createRpcWorker(forkDevWorkerPath, {}, undefined, false);
377
+ this.worker.connect(options);
378
+ }
379
+ var _proto = DevWorker.prototype;
380
+ _proto.update = function update() {
381
+ var _this$worker$process;
382
+ (_this$worker$process = this.worker.process) == null || _this$worker$process.send == null || _this$worker$process.send({
383
+ type: core.rpc.RpcGMCallTypes.CALL,
384
+ id: this.worker.id,
385
+ args: [undefined, 'update']
386
+ });
387
+ };
388
+ _proto.exit = function exit() {
389
+ this.worker.terminate();
390
+ };
391
+ return DevWorker;
392
+ }();
393
+ var normalizeDevOptions = function normalizeDevOptions(dev) {
394
+ if (dev === false) {
395
+ return false;
396
+ }
397
+ if (dev === true || typeof dev === 'undefined') {
398
+ return _extends({}, DEFAULT_DEV_OPTIONS);
399
+ }
400
+ return _extends({}, DEFAULT_DEV_OPTIONS, dev);
401
+ };
402
+ var buildDtsModuleFederationConfig = function buildDtsModuleFederationConfig(options) {
403
+ var exposes = {};
404
+ Object.entries(options.exposes).forEach(function (_ref) {
405
+ var key = _ref[0],
406
+ value = _ref[1];
407
+ if (typeof value === 'string') {
408
+ exposes[key] = value;
409
+ return;
410
+ }
411
+ var importValue = Array.isArray(value["import"]) ? value["import"][0] : value["import"];
412
+ if (importValue) {
413
+ exposes[key] = importValue;
414
+ }
415
+ });
416
+ var remotes = {};
417
+ Object.entries(options.remotes).forEach(function (_ref2) {
418
+ var _remote$entryGlobalNa, _remote$entryGlobalNa2;
419
+ var key = _ref2[0],
420
+ remote = _ref2[1];
421
+ if (typeof remote === 'string') {
422
+ remotes[key] = remote;
423
+ return;
424
+ }
425
+ if (!remote.entry) {
426
+ return;
427
+ }
428
+ var entryLooksLikeUrl = ((_remote$entryGlobalNa = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa.startsWith('http')) || ((_remote$entryGlobalNa2 = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa2.includes('.json'));
429
+ var entryGlobalName = entryLooksLikeUrl ? remote.name || key : remote.entryGlobalName || remote.name || key;
430
+ remotes[key] = entryGlobalName + "@" + remote.entry;
431
+ });
432
+ return _extends({}, options, {
433
+ exposes: exposes,
434
+ remotes: remotes
435
+ });
436
+ };
437
+ var resolveOutputDir = function resolveOutputDir(config) {
438
+ var outDir = config.build.outDir;
439
+ if (path__namespace.isAbsolute(outDir)) {
440
+ return path__namespace.relative(config.root, outDir);
441
+ }
442
+ return outDir;
443
+ };
444
+ var ensureRuntimePlugin = function ensureRuntimePlugin(options, pluginId) {
445
+ var hasPlugin = options.runtimePlugins.some(function (plugin) {
446
+ if (typeof plugin === 'string') {
447
+ return plugin === pluginId;
448
+ }
449
+ return plugin[0] === pluginId;
450
+ });
451
+ if (!hasPlugin) {
452
+ options.runtimePlugins.push(pluginId);
453
+ }
454
+ };
455
+ var normalizeDevDtsOptions = function normalizeDevDtsOptions(dts, context) {
456
+ var defaultGenerateTypes = {
457
+ compileInChildProcess: true
458
+ };
459
+ var defaultConsumeTypes = {
460
+ consumeAPITypes: true
461
+ };
462
+ return sdk.normalizeOptions(dtsPlugin.isTSProject(dts, context), {
463
+ generateTypes: defaultGenerateTypes,
464
+ consumeTypes: defaultConsumeTypes,
465
+ extraOptions: {},
466
+ displayErrorInTerminal: typeof dts === 'object' && dts ? dts.displayErrorInTerminal : undefined
467
+ }, 'mfOptions.dts')(dts);
468
+ };
469
+ var logDtsError = function logDtsError(error, dtsOptions) {
470
+ if (dtsOptions === false) {
471
+ return;
472
+ }
473
+ if (typeof dtsOptions === 'object' && dtsOptions && dtsOptions.displayErrorInTerminal === false) {
474
+ return;
475
+ }
476
+ console.error(error);
477
+ };
478
+ function pluginDts(options) {
479
+ if (options.dts === false) {
480
+ return [];
481
+ }
482
+ var dtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
483
+ var resolvedConfig;
484
+ var devWorker;
485
+ var normalizedDevOptions;
486
+ var hasGeneratedBundle = false;
487
+ var devPlugin = {
488
+ name: 'module-federation-dts-dev',
489
+ apply: 'serve',
490
+ config: function config(_config) {
491
+ normalizedDevOptions = normalizeDevOptions(options.dev);
492
+ if (!normalizedDevOptions) {
493
+ return;
494
+ }
495
+ if (normalizedDevOptions.disableDynamicRemoteTypeHints) {
496
+ return;
497
+ }
498
+ ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
499
+ var define = _config.define ? _extends({}, _config.define) : {};
500
+ if (!('FEDERATION_IPV4' in define)) {
501
+ define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
502
+ }
503
+ _config.define = define;
504
+ },
505
+ configResolved: function configResolved(config) {
506
+ resolvedConfig = config;
507
+ },
508
+ configureServer: function configureServer(server) {
509
+ if (!normalizedDevOptions || !resolvedConfig) {
510
+ return;
511
+ }
512
+ var devOptions = normalizedDevOptions;
513
+ if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) {
514
+ return;
515
+ }
516
+ if (!options.name) {
517
+ throw new Error('name is required if you want to enable dev server!');
518
+ }
519
+ var outputDir = resolveOutputDir(resolvedConfig);
520
+ var normalizedDtsOptions = normalizeDevDtsOptions(options.dts, resolvedConfig.root);
521
+ if (typeof normalizedDtsOptions !== 'object') {
522
+ return;
523
+ }
524
+ var normalizedGenerateTypes = sdk.normalizeOptions(Boolean(normalizedDtsOptions), {
525
+ compileInChildProcess: true
526
+ }, 'mfOptions.dts.generateTypes')(normalizedDtsOptions.generateTypes);
527
+ var remote = normalizedGenerateTypes === false ? undefined : _extends({
528
+ implementation: normalizedDtsOptions.implementation,
529
+ context: resolvedConfig.root,
530
+ outputDir: outputDir,
531
+ moduleFederationConfig: _extends({}, dtsModuleFederationConfig),
532
+ hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || '@mf-types'
533
+ }, normalizedGenerateTypes, {
534
+ typesFolder: '.dev-server'
535
+ });
536
+ if (remote && !remote.tsConfigPath && typeof normalizedDtsOptions === 'object' && normalizedDtsOptions.tsConfigPath) {
537
+ remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
538
+ }
539
+ var normalizedConsumeTypes = sdk.normalizeOptions(Boolean(normalizedDtsOptions), {
540
+ consumeAPITypes: true
541
+ }, 'mfOptions.dts.consumeTypes')(normalizedDtsOptions.consumeTypes);
542
+ var host = normalizedConsumeTypes === false ? undefined : _extends({
543
+ implementation: normalizedDtsOptions.implementation,
544
+ context: resolvedConfig.root,
545
+ moduleFederationConfig: dtsModuleFederationConfig,
546
+ typesFolder: normalizedConsumeTypes.typesFolder || '@mf-types',
547
+ abortOnError: false
548
+ }, normalizedConsumeTypes);
549
+ var extraOptions = normalizedDtsOptions.extraOptions || {};
550
+ if (!remote && !host && devOptions.disableLiveReload) {
551
+ return;
552
+ }
553
+ var startDevWorker = function startDevWorker() {
554
+ try {
555
+ var _temp2 = function _temp2() {
556
+ var _server$httpServer;
557
+ devWorker = new DevWorker({
558
+ name: options.name,
559
+ remote: remote,
560
+ host: host ? _extends({}, host, {
561
+ remoteTypeUrls: remoteTypeUrls
562
+ }) : undefined,
563
+ extraOptions: extraOptions,
564
+ disableLiveReload: devOptions.disableLiveReload,
565
+ disableHotTypesReload: devOptions.disableHotTypesReload
566
+ });
567
+ var update = function update() {
568
+ var _devWorker;
569
+ return (_devWorker = devWorker) == null ? void 0 : _devWorker.update();
570
+ };
571
+ server.watcher.on('change', update);
572
+ server.watcher.on('add', update);
573
+ server.watcher.on('unlink', update);
574
+ (_server$httpServer = server.httpServer) == null || _server$httpServer.once('close', function () {
575
+ var _devWorker2;
576
+ (_devWorker2 = devWorker) == null || _devWorker2.exit();
577
+ server.watcher.off('change', update);
578
+ server.watcher.off('add', update);
579
+ server.watcher.off('unlink', update);
580
+ });
581
+ };
582
+ var remoteTypeUrls;
583
+ var _temp = function () {
584
+ if (host) {
585
+ return Promise.resolve(new Promise(function (resolve) {
586
+ dtsPlugin.consumeTypesAPI({
587
+ host: host,
588
+ extraOptions: extraOptions,
589
+ displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
590
+ }, resolve);
591
+ })).then(function (_Promise) {
592
+ remoteTypeUrls = _Promise;
593
+ });
594
+ }
595
+ }();
596
+ return Promise.resolve(_temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp));
597
+ } catch (e) {
598
+ return Promise.reject(e);
599
+ }
600
+ };
601
+ startDevWorker()["catch"](function (error) {
602
+ logDtsError(error, normalizedDtsOptions);
603
+ });
604
+ }
605
+ };
606
+ var buildPlugin = {
607
+ name: 'module-federation-dts-build',
608
+ apply: 'build',
609
+ configResolved: function configResolved(config) {
610
+ resolvedConfig = config;
611
+ },
612
+ generateBundle: function generateBundle() {
613
+ try {
614
+ var _temp6 = function _temp6() {
615
+ var generateOptions;
616
+ try {
617
+ generateOptions = dtsPlugin.normalizeGenerateTypesOptions({
618
+ context: context,
619
+ outputDir: outputDir,
620
+ dtsOptions: normalizedDtsOptions,
621
+ pluginOptions: dtsModuleFederationConfig
622
+ });
623
+ } catch (error) {
624
+ logDtsError(error, normalizedDtsOptions);
625
+ return;
626
+ }
627
+ if (!generateOptions) {
628
+ return;
629
+ }
630
+ var _temp4 = _catch(function () {
631
+ return Promise.resolve(dtsPlugin.generateTypesAPI({
632
+ dtsManagerOptions: generateOptions
633
+ })).then(function () {});
634
+ }, function (error) {
635
+ logDtsError(error, normalizedDtsOptions);
636
+ });
637
+ if (_temp4 && _temp4.then) return _temp4.then(function () {});
638
+ };
639
+ if (hasGeneratedBundle) {
640
+ return Promise.resolve();
641
+ }
642
+ hasGeneratedBundle = true;
643
+ if (!resolvedConfig) {
644
+ return Promise.resolve();
645
+ }
646
+ var normalizedDtsOptions;
647
+ try {
648
+ normalizedDtsOptions = dtsPlugin.normalizeDtsOptions(dtsModuleFederationConfig, resolvedConfig.root);
649
+ } catch (error) {
650
+ logDtsError(error, options.dts);
651
+ return Promise.resolve();
652
+ }
653
+ if (typeof normalizedDtsOptions !== 'object') {
654
+ return Promise.resolve();
655
+ }
656
+ var context = resolvedConfig.root;
657
+ var outputDir = resolveOutputDir(resolvedConfig);
658
+ var consumeOptions;
659
+ try {
660
+ consumeOptions = dtsPlugin.normalizeConsumeTypesOptions({
661
+ context: context,
662
+ dtsOptions: normalizedDtsOptions,
663
+ pluginOptions: dtsModuleFederationConfig
664
+ });
665
+ } catch (error) {
666
+ logDtsError(error, normalizedDtsOptions);
667
+ return Promise.resolve();
668
+ }
669
+ var _temp5 = function (_consumeOptions) {
670
+ if ((_consumeOptions = consumeOptions) != null && (_consumeOptions = _consumeOptions.host) != null && _consumeOptions.typesOnBuild) {
671
+ var _temp3 = _catch(function () {
672
+ return Promise.resolve(dtsPlugin.consumeTypesAPI(consumeOptions)).then(function () {});
673
+ }, function (error) {
674
+ logDtsError(error, normalizedDtsOptions);
675
+ });
676
+ if (_temp3 && _temp3.then) return _temp3.then(function () {});
677
+ }
678
+ }();
679
+ return Promise.resolve(_temp5 && _temp5.then ? _temp5.then(_temp6) : _temp6(_temp5));
680
+ } catch (e) {
681
+ return Promise.reject(e);
682
+ }
683
+ }
684
+ };
685
+ return [devPlugin, buildPlugin];
686
+ }
687
+
350
688
  function normalizeExposesItem(key, item) {
351
689
  var importPath = '';
352
690
  if (typeof item === 'string') {
@@ -539,7 +877,8 @@ function normalizeModuleFederationOptions(options) {
539
877
  ignoreOrigin: options.ignoreOrigin || false,
540
878
  virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
541
879
  hostInitInjectLocation: options.hostInitInjectLocation || 'html',
542
- bundleAllCSS: options.bundleAllCSS || false
880
+ bundleAllCSS: options.bundleAllCSS || false,
881
+ moduleParseTimeout: options.moduleParseTimeout || 10
543
882
  };
544
883
  }
545
884
 
@@ -1405,15 +1744,28 @@ var Manifest = function Manifest() {
1405
1744
  }
1406
1745
  };
1407
1746
 
1408
- var _resolve,
1409
- promise = new Promise(function (resolve, reject) {
1410
- _resolve = resolve;
1411
- });
1747
+ var _resolve, _parseTimeout;
1748
+ var promise = new Promise(function (resolve, reject) {
1749
+ _resolve = function _resolve(v) {
1750
+ clearTimeout(_parseTimeout);
1751
+ _parseTimeout = null;
1752
+ resolve(v);
1753
+ };
1754
+ });
1755
+ function setParseTimeout(timeout) {
1756
+ if (!_parseTimeout) {
1757
+ _parseTimeout = setTimeout(function () {
1758
+ console.warn("Parse timeout (" + timeout + "s) - forcing resolve");
1759
+ _resolve(1);
1760
+ }, timeout * 1000);
1761
+ }
1762
+ }
1412
1763
  var parsePromise = promise;
1413
1764
  var exposesParseEnd = false;
1414
1765
  var parseStartSet = new Set();
1415
1766
  var parseEndSet = new Set();
1416
- function pluginModuleParseEnd (excludeFn) {
1767
+ function pluginModuleParseEnd (excludeFn, options) {
1768
+ setParseTimeout(options.moduleParseTimeout);
1417
1769
  return [{
1418
1770
  name: '_',
1419
1771
  apply: 'serve',
@@ -1434,447 +1786,109 @@ function pluginModuleParseEnd (excludeFn) {
1434
1786
  }, {
1435
1787
  enforce: 'post',
1436
1788
  name: 'parseEnd',
1437
- apply: 'build',
1438
- moduleParsed: function moduleParsed(module) {
1439
- var id = module.id;
1440
- if (id === VIRTUAL_EXPOSES) {
1441
- // When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
1442
- exposesParseEnd = true;
1443
- }
1444
- if (excludeFn(id)) {
1445
- return;
1446
- }
1447
- parseEndSet.add(id);
1448
- if (exposesParseEnd && parseStartSet.size === parseEndSet.size) {
1449
- _resolve(1);
1450
- }
1451
- }
1452
- }];
1453
- }
1454
-
1455
- var filter = pluginutils.createFilter();
1456
- function pluginProxyRemoteEntry () {
1457
- var viteConfig, _command;
1458
- return {
1459
- name: 'proxyRemoteEntry',
1460
- enforce: 'post',
1461
- configResolved: function configResolved(config) {
1462
- viteConfig = config;
1463
- },
1464
- config: function config(_config, _ref) {
1465
- var command = _ref.command;
1466
- _command = command;
1467
- },
1468
- resolveId: function resolveId(id) {
1469
- if (id === REMOTE_ENTRY_ID) {
1470
- return REMOTE_ENTRY_ID;
1471
- }
1472
- if (id === VIRTUAL_EXPOSES) {
1473
- return VIRTUAL_EXPOSES;
1474
- }
1475
- if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
1476
- return id;
1477
- }
1478
- },
1479
- load: function load(id) {
1480
- if (id === REMOTE_ENTRY_ID) {
1481
- return parsePromise.then(function (_) {
1482
- return generateRemoteEntry(getNormalizeModuleFederationOptions());
1483
- });
1484
- }
1485
- if (id === VIRTUAL_EXPOSES) {
1486
- return generateExposes();
1487
- }
1488
- if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
1489
- return id;
1490
- }
1491
- },
1492
- transform: function transform(code, id) {
1493
- var transformedCode = function () {
1494
- if (!filter(id)) return;
1495
- if (id.includes(REMOTE_ENTRY_ID)) {
1496
- return parsePromise.then(function (_) {
1497
- return generateRemoteEntry(getNormalizeModuleFederationOptions());
1498
- });
1499
- }
1500
- if (id === VIRTUAL_EXPOSES) {
1501
- return generateExposes();
1502
- }
1503
- if (id.includes(getHostAutoInitPath())) {
1504
- var options = getNormalizeModuleFederationOptions();
1505
- if (_command === 'serve') {
1506
- var _viteConfig$server, _viteConfig$server2;
1507
- var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
1508
- var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
1509
- return "\n const origin = (window && " + !options.ignoreOrigin + ") ? window.origin : \"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + "\"\n const remoteEntryPromise = await import(origin + " + publicPath + ")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ";
1510
- }
1511
- return code;
1512
- }
1513
- }();
1514
- return mapCodeToCodeWithSourcemap(transformedCode);
1515
- }
1516
- };
1517
- }
1518
-
1519
- pluginutils.createFilter();
1520
- function pluginProxyRemotes (options) {
1521
- var remotes = options.remotes;
1522
- return {
1523
- name: 'proxyRemotes',
1524
- config: function config(_config, _ref) {
1525
- var _command = _ref.command;
1526
- Object.keys(remotes).forEach(function (key) {
1527
- var remote = remotes[key];
1528
- _config.resolve.alias.push({
1529
- find: new RegExp("^(" + remote.name + "(/.*|$))"),
1530
- replacement: '$1',
1531
- customResolver: function customResolver(source) {
1532
- var remoteModule = getRemoteVirtualModule(source, _command);
1533
- addUsedRemote(remote.name, source);
1534
- return remoteModule.getPath();
1535
- }
1536
- });
1537
- });
1538
- }
1539
- };
1540
- }
1541
-
1542
- function _catch(body, recover) {
1543
- try {
1544
- var result = body();
1545
- } catch (e) {
1546
- return recover(e);
1547
- }
1548
- if (result && result.then) {
1549
- return result.then(void 0, recover);
1550
- }
1551
- return result;
1552
- }
1553
- var DEFAULT_DEV_OPTIONS = {
1554
- disableLiveReload: true,
1555
- disableHotTypesReload: false,
1556
- disableDynamicRemoteTypeHints: false
1557
- };
1558
- var DYNAMIC_HINTS_PLUGIN = '@module-federation/dts-plugin/dynamic-remote-type-hints-plugin';
1559
- var getIPv4 = function getIPv4() {
1560
- return process.env['FEDERATION_IPV4'] || '127.0.0.1';
1561
- };
1562
- var forkDevWorkerPath = function () {
1563
- // eslint-disable-next-line @typescript-eslint/no-var-requires
1564
- return require.resolve('@module-federation/dts-plugin/dist/fork-dev-worker.js');
1565
- }();
1566
- var DevWorker = /*#__PURE__*/function () {
1567
- function DevWorker(options) {
1568
- this.worker = core.rpc.createRpcWorker(forkDevWorkerPath, {}, undefined, false);
1569
- this.worker.connect(options);
1570
- }
1571
- var _proto = DevWorker.prototype;
1572
- _proto.update = function update() {
1573
- var _this$worker$process;
1574
- (_this$worker$process = this.worker.process) == null || _this$worker$process.send == null || _this$worker$process.send({
1575
- type: core.rpc.RpcGMCallTypes.CALL,
1576
- id: this.worker.id,
1577
- args: [undefined, 'update']
1578
- });
1579
- };
1580
- _proto.exit = function exit() {
1581
- this.worker.terminate();
1582
- };
1583
- return DevWorker;
1584
- }();
1585
- var normalizeDevOptions = function normalizeDevOptions(dev) {
1586
- if (dev === false) {
1587
- return false;
1588
- }
1589
- if (dev === true || typeof dev === 'undefined') {
1590
- return _extends({}, DEFAULT_DEV_OPTIONS);
1591
- }
1592
- return _extends({}, DEFAULT_DEV_OPTIONS, dev);
1593
- };
1594
- var buildDtsModuleFederationConfig = function buildDtsModuleFederationConfig(options) {
1595
- var exposes = {};
1596
- Object.entries(options.exposes).forEach(function (_ref) {
1597
- var key = _ref[0],
1598
- value = _ref[1];
1599
- if (typeof value === 'string') {
1600
- exposes[key] = value;
1601
- return;
1602
- }
1603
- var importValue = Array.isArray(value["import"]) ? value["import"][0] : value["import"];
1604
- if (importValue) {
1605
- exposes[key] = importValue;
1606
- }
1607
- });
1608
- var remotes = {};
1609
- Object.entries(options.remotes).forEach(function (_ref2) {
1610
- var _remote$entryGlobalNa, _remote$entryGlobalNa2;
1611
- var key = _ref2[0],
1612
- remote = _ref2[1];
1613
- if (typeof remote === 'string') {
1614
- remotes[key] = remote;
1615
- return;
1616
- }
1617
- if (!remote.entry) {
1618
- return;
1619
- }
1620
- var entryLooksLikeUrl = ((_remote$entryGlobalNa = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa.startsWith('http')) || ((_remote$entryGlobalNa2 = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa2.includes('.json'));
1621
- var entryGlobalName = entryLooksLikeUrl ? remote.name || key : remote.entryGlobalName || remote.name || key;
1622
- remotes[key] = entryGlobalName + "@" + remote.entry;
1623
- });
1624
- return _extends({}, options, {
1625
- exposes: exposes,
1626
- remotes: remotes
1627
- });
1628
- };
1629
- var resolveOutputDir = function resolveOutputDir(config) {
1630
- var outDir = config.build.outDir;
1631
- if (path__namespace.isAbsolute(outDir)) {
1632
- return path__namespace.relative(config.root, outDir);
1633
- }
1634
- return outDir;
1635
- };
1636
- var ensureRuntimePlugin = function ensureRuntimePlugin(options, pluginId) {
1637
- var hasPlugin = options.runtimePlugins.some(function (plugin) {
1638
- if (typeof plugin === 'string') {
1639
- return plugin === pluginId;
1640
- }
1641
- return plugin[0] === pluginId;
1642
- });
1643
- if (!hasPlugin) {
1644
- options.runtimePlugins.push(pluginId);
1645
- }
1646
- };
1647
- var normalizeDevDtsOptions = function normalizeDevDtsOptions(dts, context) {
1648
- var defaultGenerateTypes = {
1649
- compileInChildProcess: true
1650
- };
1651
- var defaultConsumeTypes = {
1652
- consumeAPITypes: true
1653
- };
1654
- return sdk.normalizeOptions(dtsPlugin.isTSProject(dts, context), {
1655
- generateTypes: defaultGenerateTypes,
1656
- consumeTypes: defaultConsumeTypes,
1657
- extraOptions: {},
1658
- displayErrorInTerminal: typeof dts === 'object' && dts ? dts.displayErrorInTerminal : undefined
1659
- }, 'mfOptions.dts')(dts);
1660
- };
1661
- var logDtsError = function logDtsError(error, dtsOptions) {
1662
- if (dtsOptions === false) {
1663
- return;
1664
- }
1665
- if (typeof dtsOptions === 'object' && dtsOptions && dtsOptions.displayErrorInTerminal === false) {
1666
- return;
1667
- }
1668
- console.error(error);
1669
- };
1670
- function pluginDts(options) {
1671
- if (options.dts === false) {
1672
- return [];
1673
- }
1674
- var dtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
1675
- var resolvedConfig;
1676
- var devWorker;
1677
- var normalizedDevOptions;
1678
- var hasGeneratedBundle = false;
1679
- var devPlugin = {
1680
- name: 'module-federation-dts-dev',
1681
- apply: 'serve',
1682
- config: function config(_config) {
1683
- normalizedDevOptions = normalizeDevOptions(options.dev);
1684
- if (!normalizedDevOptions) {
1685
- return;
1789
+ apply: 'build',
1790
+ moduleParsed: function moduleParsed(module) {
1791
+ var id = module.id;
1792
+ if (id === VIRTUAL_EXPOSES) {
1793
+ // When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
1794
+ exposesParseEnd = true;
1686
1795
  }
1687
- if (normalizedDevOptions.disableDynamicRemoteTypeHints) {
1796
+ if (excludeFn(id)) {
1688
1797
  return;
1689
1798
  }
1690
- ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
1691
- var define = _config.define ? _extends({}, _config.define) : {};
1692
- if (!('FEDERATION_IPV4' in define)) {
1693
- define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
1799
+ parseEndSet.add(id);
1800
+ if (exposesParseEnd && parseStartSet.size === parseEndSet.size) {
1801
+ _resolve(1);
1694
1802
  }
1695
- _config.define = define;
1696
- },
1803
+ }
1804
+ }];
1805
+ }
1806
+
1807
+ var filter = pluginutils.createFilter();
1808
+ function pluginProxyRemoteEntry () {
1809
+ var viteConfig, _command;
1810
+ return {
1811
+ name: 'proxyRemoteEntry',
1812
+ enforce: 'post',
1697
1813
  configResolved: function configResolved(config) {
1698
- resolvedConfig = config;
1814
+ viteConfig = config;
1699
1815
  },
1700
- configureServer: function configureServer(server) {
1701
- if (!normalizedDevOptions || !resolvedConfig) {
1702
- return;
1816
+ config: function config(_config, _ref) {
1817
+ var command = _ref.command;
1818
+ _command = command;
1819
+ },
1820
+ resolveId: function resolveId(id) {
1821
+ if (id === REMOTE_ENTRY_ID) {
1822
+ return REMOTE_ENTRY_ID;
1703
1823
  }
1704
- var devOptions = normalizedDevOptions;
1705
- if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) {
1706
- return;
1824
+ if (id === VIRTUAL_EXPOSES) {
1825
+ return VIRTUAL_EXPOSES;
1707
1826
  }
1708
- if (!options.name) {
1709
- throw new Error('name is required if you want to enable dev server!');
1827
+ if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
1828
+ return id;
1710
1829
  }
1711
- var outputDir = resolveOutputDir(resolvedConfig);
1712
- var normalizedDtsOptions = normalizeDevDtsOptions(options.dts, resolvedConfig.root);
1713
- if (typeof normalizedDtsOptions !== 'object') {
1714
- return;
1830
+ },
1831
+ load: function load(id) {
1832
+ if (id === REMOTE_ENTRY_ID) {
1833
+ return parsePromise.then(function (_) {
1834
+ return generateRemoteEntry(getNormalizeModuleFederationOptions());
1835
+ });
1715
1836
  }
1716
- var normalizedGenerateTypes = sdk.normalizeOptions(Boolean(normalizedDtsOptions), {
1717
- compileInChildProcess: true
1718
- }, 'mfOptions.dts.generateTypes')(normalizedDtsOptions.generateTypes);
1719
- var remote = normalizedGenerateTypes === false ? undefined : _extends({
1720
- implementation: normalizedDtsOptions.implementation,
1721
- context: resolvedConfig.root,
1722
- outputDir: outputDir,
1723
- moduleFederationConfig: _extends({}, dtsModuleFederationConfig),
1724
- hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || '@mf-types'
1725
- }, normalizedGenerateTypes, {
1726
- typesFolder: '.dev-server'
1727
- });
1728
- if (remote && !remote.tsConfigPath && typeof normalizedDtsOptions === 'object' && normalizedDtsOptions.tsConfigPath) {
1729
- remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
1837
+ if (id === VIRTUAL_EXPOSES) {
1838
+ return generateExposes();
1730
1839
  }
1731
- var normalizedConsumeTypes = sdk.normalizeOptions(Boolean(normalizedDtsOptions), {
1732
- consumeAPITypes: true
1733
- }, 'mfOptions.dts.consumeTypes')(normalizedDtsOptions.consumeTypes);
1734
- var host = normalizedConsumeTypes === false ? undefined : _extends({
1735
- implementation: normalizedDtsOptions.implementation,
1736
- context: resolvedConfig.root,
1737
- moduleFederationConfig: dtsModuleFederationConfig,
1738
- typesFolder: normalizedConsumeTypes.typesFolder || '@mf-types',
1739
- abortOnError: false
1740
- }, normalizedConsumeTypes);
1741
- var extraOptions = normalizedDtsOptions.extraOptions || {};
1742
- if (!remote && !host && devOptions.disableLiveReload) {
1743
- return;
1840
+ if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
1841
+ return id;
1744
1842
  }
1745
- var startDevWorker = function startDevWorker() {
1746
- try {
1747
- var _temp2 = function _temp2() {
1748
- var _server$httpServer;
1749
- devWorker = new DevWorker({
1750
- name: options.name,
1751
- remote: remote,
1752
- host: host ? _extends({}, host, {
1753
- remoteTypeUrls: remoteTypeUrls
1754
- }) : undefined,
1755
- extraOptions: extraOptions,
1756
- disableLiveReload: devOptions.disableLiveReload,
1757
- disableHotTypesReload: devOptions.disableHotTypesReload
1758
- });
1759
- var update = function update() {
1760
- var _devWorker;
1761
- return (_devWorker = devWorker) == null ? void 0 : _devWorker.update();
1762
- };
1763
- server.watcher.on('change', update);
1764
- server.watcher.on('add', update);
1765
- server.watcher.on('unlink', update);
1766
- (_server$httpServer = server.httpServer) == null || _server$httpServer.once('close', function () {
1767
- var _devWorker2;
1768
- (_devWorker2 = devWorker) == null || _devWorker2.exit();
1769
- server.watcher.off('change', update);
1770
- server.watcher.off('add', update);
1771
- server.watcher.off('unlink', update);
1772
- });
1773
- };
1774
- var remoteTypeUrls;
1775
- var _temp = function () {
1776
- if (host) {
1777
- return Promise.resolve(new Promise(function (resolve) {
1778
- dtsPlugin.consumeTypesAPI({
1779
- host: host,
1780
- extraOptions: extraOptions,
1781
- displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
1782
- }, resolve);
1783
- })).then(function (_Promise) {
1784
- remoteTypeUrls = _Promise;
1785
- });
1786
- }
1787
- }();
1788
- return Promise.resolve(_temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp));
1789
- } catch (e) {
1790
- return Promise.reject(e);
1791
- }
1792
- };
1793
- startDevWorker()["catch"](function (error) {
1794
- logDtsError(error, normalizedDtsOptions);
1795
- });
1796
- }
1797
- };
1798
- var buildPlugin = {
1799
- name: 'module-federation-dts-build',
1800
- apply: 'build',
1801
- configResolved: function configResolved(config) {
1802
- resolvedConfig = config;
1803
1843
  },
1804
- generateBundle: function generateBundle() {
1805
- try {
1806
- var _temp6 = function _temp6() {
1807
- var generateOptions;
1808
- try {
1809
- generateOptions = dtsPlugin.normalizeGenerateTypesOptions({
1810
- context: context,
1811
- outputDir: outputDir,
1812
- dtsOptions: normalizedDtsOptions,
1813
- pluginOptions: dtsModuleFederationConfig
1814
- });
1815
- } catch (error) {
1816
- logDtsError(error, normalizedDtsOptions);
1817
- return;
1818
- }
1819
- if (!generateOptions) {
1820
- return;
1821
- }
1822
- var _temp4 = _catch(function () {
1823
- return Promise.resolve(dtsPlugin.generateTypesAPI({
1824
- dtsManagerOptions: generateOptions
1825
- })).then(function () {});
1826
- }, function (error) {
1827
- logDtsError(error, normalizedDtsOptions);
1844
+ transform: function transform(code, id) {
1845
+ var transformedCode = function () {
1846
+ if (!filter(id)) return;
1847
+ if (id.includes(REMOTE_ENTRY_ID)) {
1848
+ return parsePromise.then(function (_) {
1849
+ return generateRemoteEntry(getNormalizeModuleFederationOptions());
1828
1850
  });
1829
- if (_temp4 && _temp4.then) return _temp4.then(function () {});
1830
- };
1831
- if (hasGeneratedBundle) {
1832
- return Promise.resolve();
1833
- }
1834
- hasGeneratedBundle = true;
1835
- if (!resolvedConfig) {
1836
- return Promise.resolve();
1837
- }
1838
- var normalizedDtsOptions;
1839
- try {
1840
- normalizedDtsOptions = dtsPlugin.normalizeDtsOptions(dtsModuleFederationConfig, resolvedConfig.root);
1841
- } catch (error) {
1842
- logDtsError(error, options.dts);
1843
- return Promise.resolve();
1844
1851
  }
1845
- if (typeof normalizedDtsOptions !== 'object') {
1846
- return Promise.resolve();
1852
+ if (id === VIRTUAL_EXPOSES) {
1853
+ return generateExposes();
1847
1854
  }
1848
- var context = resolvedConfig.root;
1849
- var outputDir = resolveOutputDir(resolvedConfig);
1850
- var consumeOptions;
1851
- try {
1852
- consumeOptions = dtsPlugin.normalizeConsumeTypesOptions({
1853
- context: context,
1854
- dtsOptions: normalizedDtsOptions,
1855
- pluginOptions: dtsModuleFederationConfig
1856
- });
1857
- } catch (error) {
1858
- logDtsError(error, normalizedDtsOptions);
1859
- return Promise.resolve();
1855
+ if (id.includes(getHostAutoInitPath())) {
1856
+ var options = getNormalizeModuleFederationOptions();
1857
+ if (_command === 'serve') {
1858
+ var _viteConfig$server, _viteConfig$server2;
1859
+ var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
1860
+ var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
1861
+ return "\n const origin = (window && " + !options.ignoreOrigin + ") ? window.origin : \"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + "\"\n const remoteEntryPromise = await import(origin + " + publicPath + ")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ";
1862
+ }
1863
+ return code;
1860
1864
  }
1861
- var _temp5 = function (_consumeOptions) {
1862
- if ((_consumeOptions = consumeOptions) != null && (_consumeOptions = _consumeOptions.host) != null && _consumeOptions.typesOnBuild) {
1863
- var _temp3 = _catch(function () {
1864
- return Promise.resolve(dtsPlugin.consumeTypesAPI(consumeOptions)).then(function () {});
1865
- }, function (error) {
1866
- logDtsError(error, normalizedDtsOptions);
1867
- });
1868
- if (_temp3 && _temp3.then) return _temp3.then(function () {});
1865
+ }();
1866
+ return mapCodeToCodeWithSourcemap(transformedCode);
1867
+ }
1868
+ };
1869
+ }
1870
+
1871
+ pluginutils.createFilter();
1872
+ function pluginProxyRemotes (options) {
1873
+ var remotes = options.remotes;
1874
+ return {
1875
+ name: 'proxyRemotes',
1876
+ config: function config(_config, _ref) {
1877
+ var _command = _ref.command;
1878
+ Object.keys(remotes).forEach(function (key) {
1879
+ var remote = remotes[key];
1880
+ _config.resolve.alias.push({
1881
+ find: new RegExp("^(" + remote.name + "(/.*|$))"),
1882
+ replacement: '$1',
1883
+ customResolver: function customResolver(source) {
1884
+ var remoteModule = getRemoteVirtualModule(source, _command);
1885
+ addUsedRemote(remote.name, source);
1886
+ return remoteModule.getPath();
1869
1887
  }
1870
- }();
1871
- return Promise.resolve(_temp5 && _temp5.then ? _temp5.then(_temp6) : _temp6(_temp5));
1872
- } catch (e) {
1873
- return Promise.reject(e);
1874
- }
1888
+ });
1889
+ });
1875
1890
  }
1876
1891
  };
1877
- return [devPlugin, buildPlugin];
1878
1892
  }
1879
1893
 
1880
1894
  /**
@@ -2067,6 +2081,8 @@ function federation(mfUserOptions) {
2067
2081
  entryPath: VIRTUAL_EXPOSES
2068
2082
  }), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
2069
2083
  return id.includes(getHostAutoInitImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(VIRTUAL_EXPOSES) || id.includes(getLocalSharedImportMapPath());
2084
+ }, {
2085
+ moduleParseTimeout: options.moduleParseTimeout
2070
2086
  }), proxySharedModule({
2071
2087
  shared: shared
2072
2088
  }), [PluginDevProxyModuleTopLevelAwait(), {