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