@module-federation/vite 1.7.1 → 1.8.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
@@ -122,7 +122,7 @@ var addEntry = function addEntry(_ref) {
122
122
  emitFileOptions.fileName = fileName;
123
123
  }
124
124
  emitFileId = this.emitFile(emitFileOptions);
125
- if (htmlFilePath) {
125
+ if (htmlFilePath && fs__namespace.existsSync(htmlFilePath)) {
126
126
  var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
127
127
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
128
128
  var match;
@@ -161,10 +161,14 @@ var addEntry = function addEntry(_ref) {
161
161
  */
162
162
  function PluginDevProxyModuleTopLevelAwait() {
163
163
  var filterFunction = pluginutils.createFilter();
164
+ var processedFlag = '/* already-processed-by-dev-proxy-module-top-level-await */';
164
165
  return {
165
166
  name: 'dev-proxy-module-top-level-await',
166
167
  apply: 'serve',
167
168
  transform: function transform(code, id) {
169
+ if (code.includes(processedFlag)) {
170
+ return null;
171
+ }
168
172
  if (!code.includes('/*mf top-level-await placeholder replacement mf*/')) {
169
173
  return null;
170
174
  }
@@ -217,8 +221,9 @@ function PluginDevProxyModuleTopLevelAwait() {
217
221
  }
218
222
  }
219
223
  });
224
+ var transformedCode = magicString.toString();
220
225
  return {
221
- code: magicString.toString(),
226
+ code: processedFlag + "\n" + transformedCode,
222
227
  map: magicString.generateMap({
223
228
  hires: true
224
229
  })
@@ -460,6 +465,7 @@ function normalizeModuleFederationOptions(options) {
460
465
  dev: options.dev,
461
466
  dts: options.dts,
462
467
  getPublicPath: options.getPublicPath,
468
+ publicPath: options.publicPath,
463
469
  shareStrategy: options.shareStrategy || 'version-first',
464
470
  ignoreOrigin: options.ignoreOrigin || false,
465
471
  virtualModuleDir: options.virtualModuleDir || '__mf__virtual'
@@ -716,9 +722,9 @@ function writeLocalSharedImportMap() {
716
722
  }
717
723
  function generateLocalSharedImportMap() {
718
724
  var options = getNormalizeModuleFederationOptions();
719
- return "\n const importMap = {\n " + Array.from(getUsedShares()).map(function (pkg) {
725
+ return "\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
720
726
  return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\")\n return pkg\n }\n ";
721
- }).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).map(function (key) {
727
+ }).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).sort().map(function (key) {
722
728
  var shareItem = getNormalizeShareItem(key);
723
729
  if (!shareItem) return null;
724
730
  return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap \n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }\n }\n ";
@@ -766,6 +772,178 @@ function initVirtualModules() {
766
772
  writeRuntimeInitStatus();
767
773
  }
768
774
 
775
+ var ASSET_TYPES = ['js', 'css'];
776
+ var LOAD_TIMINGS = ['sync', 'async'];
777
+ var JS_EXTENSIONS = ['.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
778
+ /**
779
+ * Creates an empty asset map structure for tracking JS and CSS assets
780
+ * @returns Initialized asset map with sync/async arrays for JS and CSS
781
+ */
782
+ var createEmptyAssetMap = function createEmptyAssetMap() {
783
+ return {
784
+ js: {
785
+ sync: [],
786
+ async: []
787
+ },
788
+ css: {
789
+ sync: [],
790
+ async: []
791
+ }
792
+ };
793
+ };
794
+ /**
795
+ * Tracks an asset in the preload map with deduplication
796
+ * @param map - The preload map to update
797
+ * @param key - The module key to track under
798
+ * @param fileName - The asset filename to track
799
+ * @param isAsync - Whether the asset is loaded async
800
+ * @param type - The asset type ('js' or 'css')
801
+ */
802
+ var trackAsset = function trackAsset(map, key, fileName, isAsync, type) {
803
+ if (!map[key]) {
804
+ map[key] = createEmptyAssetMap();
805
+ }
806
+ var target = isAsync ? map[key][type].async : map[key][type].sync;
807
+ if (!target.includes(fileName)) {
808
+ target.push(fileName);
809
+ }
810
+ };
811
+ /**
812
+ * Checks if a file is a CSS file by extension
813
+ * @param fileName - The filename to check
814
+ * @returns True if file has a CSS extension (.css, .scss, .less)
815
+ */
816
+ var isCSSFile = function isCSSFile(fileName) {
817
+ return fileName.endsWith('.css') || fileName.endsWith('.scss') || fileName.endsWith('.less');
818
+ };
819
+ /**
820
+ * Collects all CSS assets from the bundle
821
+ * @param bundle - The Rollup output bundle
822
+ * @returns Set of CSS asset filenames
823
+ */
824
+ var collectCssAssets = function collectCssAssets(bundle) {
825
+ var cssAssets = new Set();
826
+ for (var _i = 0, _Object$entries = Object.entries(bundle); _i < _Object$entries.length; _i++) {
827
+ var _Object$entries$_i = _Object$entries[_i],
828
+ fileName = _Object$entries$_i[0],
829
+ fileData = _Object$entries$_i[1];
830
+ if (fileData.type === 'asset' && isCSSFile(fileName)) {
831
+ cssAssets.add(fileName);
832
+ }
833
+ }
834
+ return cssAssets;
835
+ };
836
+ /**
837
+ * Processes module assets and tracks them in the files map
838
+ * @param bundle - The Rollup output bundle
839
+ * @param filesMap - The preload map to populate
840
+ * @param moduleMatcher - Function that matches module paths to keys
841
+ */
842
+ var processModuleAssets = function processModuleAssets(bundle, filesMap, moduleMatcher) {
843
+ for (var _i2 = 0, _Object$entries2 = Object.entries(bundle); _i2 < _Object$entries2.length; _i2++) {
844
+ var _Object$entries2$_i = _Object$entries2[_i2],
845
+ fileName = _Object$entries2$_i[0],
846
+ fileData = _Object$entries2$_i[1];
847
+ if (fileData.type !== 'chunk') continue;
848
+ if (!fileData.modules) continue;
849
+ for (var _i3 = 0, _Object$keys = Object.keys(fileData.modules); _i3 < _Object$keys.length; _i3++) {
850
+ var modulePath = _Object$keys[_i3];
851
+ var matchKey = moduleMatcher(modulePath);
852
+ if (!matchKey) continue;
853
+ // Track main JS chunk
854
+ trackAsset(filesMap, matchKey, fileName, false, 'js');
855
+ // Handle dynamic imports
856
+ if (fileData.dynamicImports) {
857
+ for (var _iterator = _createForOfIteratorHelperLoose(fileData.dynamicImports), _step; !(_step = _iterator()).done;) {
858
+ var dynamicImport = _step.value;
859
+ var importData = bundle[dynamicImport];
860
+ if (!importData) continue;
861
+ var isCss = isCSSFile(dynamicImport);
862
+ trackAsset(filesMap, matchKey, dynamicImport, true, isCss ? 'css' : 'js');
863
+ }
864
+ }
865
+ }
866
+ }
867
+ };
868
+ /**
869
+ * Deduplicates assets in the files map
870
+ * @param filesMap - The preload map to deduplicate
871
+ * @returns New deduplicated preload map
872
+ */
873
+ var deduplicateAssets = function deduplicateAssets(filesMap) {
874
+ var result = {};
875
+ for (var _i4 = 0, _Object$entries3 = Object.entries(filesMap); _i4 < _Object$entries3.length; _i4++) {
876
+ var _Object$entries3$_i = _Object$entries3[_i4],
877
+ key = _Object$entries3$_i[0],
878
+ assetMaps = _Object$entries3$_i[1];
879
+ result[key] = createEmptyAssetMap();
880
+ for (var _i5 = 0, _ASSET_TYPES = ASSET_TYPES; _i5 < _ASSET_TYPES.length; _i5++) {
881
+ var type = _ASSET_TYPES[_i5];
882
+ for (var _i6 = 0, _LOAD_TIMINGS = LOAD_TIMINGS; _i6 < _LOAD_TIMINGS.length; _i6++) {
883
+ var timing = _LOAD_TIMINGS[_i6];
884
+ result[key][type][timing] = Array.from(new Set(assetMaps[type][timing]));
885
+ }
886
+ }
887
+ }
888
+ return result;
889
+ };
890
+ /**
891
+ * Builds a mapping between module files and their share keys
892
+ * @param shareKeys - Set of share keys to map
893
+ * @param resolveFn - Function to resolve module paths
894
+ * @returns Map of file paths to their corresponding share keys
895
+ */
896
+ var buildFileToShareKeyMap = function buildFileToShareKeyMap(shareKeys, resolveFn) {
897
+ try {
898
+ var fileToShareKey = new Map();
899
+ return Promise.resolve(Promise.all(Array.from(shareKeys).map(function (shareKey) {
900
+ return resolveFn(getPreBuildLibImportId(shareKey)).then(function (resolution) {
901
+ var _resolution$id;
902
+ return {
903
+ shareKey: shareKey,
904
+ file: resolution == null || (_resolution$id = resolution.id) == null ? void 0 : _resolution$id.split('?')[0]
905
+ };
906
+ })["catch"](function () {
907
+ return null;
908
+ });
909
+ }))).then(function (resolutions) {
910
+ for (var _iterator2 = _createForOfIteratorHelperLoose(resolutions), _step2; !(_step2 = _iterator2()).done;) {
911
+ var resolution = _step2.value;
912
+ if (resolution != null && resolution.file) {
913
+ fileToShareKey.set(resolution.file, resolution.shareKey);
914
+ }
915
+ }
916
+ return fileToShareKey;
917
+ });
918
+ } catch (e) {
919
+ return Promise.reject(e);
920
+ }
921
+ };
922
+
923
+ /**
924
+ * Resolves the public path for remote entries
925
+ * @param options - Module Federation options
926
+ * @param viteBase - Vite's base config value
927
+ * @param originalBase - Original base config before any transformations
928
+ * @returns The resolved public path
929
+ */
930
+ function resolvePublicPath(options, viteBase, originalBase) {
931
+ // Use explicitly set publicPath if provided
932
+ if (options.publicPath) {
933
+ return options.publicPath;
934
+ }
935
+ // Handle empty original base case
936
+ if (originalBase === '') {
937
+ return 'auto';
938
+ }
939
+ // Use viteBase if available, ensuring it ends with a slash
940
+ if (viteBase) {
941
+ return viteBase.replace(/\/?$/, '/');
942
+ }
943
+ // Fallback to auto if no base is specified
944
+ return 'auto';
945
+ }
946
+
769
947
  var Manifest = function Manifest() {
770
948
  var mfOptions = getNormalizeModuleFederationOptions();
771
949
  var name = mfOptions.name,
@@ -777,21 +955,43 @@ var Manifest = function Manifest() {
777
955
  mfManifestName = 'mf-manifest.json';
778
956
  }
779
957
  if (typeof manifestOptions !== 'boolean') {
780
- mfManifestName = path.join((manifestOptions == null ? void 0 : manifestOptions.filePath) || '', (manifestOptions == null ? void 0 : manifestOptions.fileName) || '');
958
+ mfManifestName = path__namespace.join((manifestOptions == null ? void 0 : manifestOptions.filePath) || '', (manifestOptions == null ? void 0 : manifestOptions.fileName) || '');
781
959
  }
782
- var extensions;
783
960
  var root;
784
961
  var remoteEntryFile;
785
962
  var publicPath;
786
963
  var _command;
787
964
  var _originalConfigBase;
788
965
  var viteConfig;
966
+ /**
967
+ * Adds global CSS assets to all module exports
968
+ * @param filesMap - The preload map to update
969
+ * @param cssAssets - Set of CSS asset filenames to add
970
+ */
971
+ var addCssAssetsToAllExports = function addCssAssetsToAllExports(filesMap, cssAssets) {
972
+ Object.keys(filesMap).forEach(function (key) {
973
+ cssAssets.forEach(function (cssAsset) {
974
+ trackAsset(filesMap, key, cssAsset, false, 'css');
975
+ });
976
+ });
977
+ };
789
978
  return [{
790
979
  name: 'module-federation-manifest',
791
980
  apply: 'serve',
981
+ /**
982
+ * Stores resolved Vite config for later use
983
+ */
984
+ /**
985
+ * Finalizes configuration after all plugins are resolved
986
+ * @param config - Fully resolved Vite config
987
+ */
792
988
  configResolved: function configResolved(config) {
793
989
  viteConfig = config;
794
990
  },
991
+ /**
992
+ * Configures dev server middleware to handle manifest requests
993
+ * @param server - Vite dev server instance
994
+ */
795
995
  configureServer: function configureServer(server) {
796
996
  server.middlewares.use(function (req, res, next) {
797
997
  var _req$url;
@@ -839,137 +1039,85 @@ var Manifest = function Manifest() {
839
1039
  }, {
840
1040
  name: 'module-federation-manifest',
841
1041
  enforce: 'post',
1042
+ /**
1043
+ * Initial plugin configuration
1044
+ * @param config - Vite config object
1045
+ * @param command - Current Vite command (serve/build)
1046
+ */
842
1047
  config: function config(_config, _ref) {
843
1048
  var command = _ref.command;
844
1049
  if (!_config.build) _config.build = {};
845
- if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
1050
+ if (!_config.build.manifest) {
1051
+ _config.build.manifest = _config.build.manifest || !!manifestOptions;
1052
+ }
846
1053
  _command = command;
847
1054
  _originalConfigBase = _config.base;
848
1055
  },
849
1056
  configResolved: function configResolved(config) {
850
1057
  root = config.root;
851
- extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
852
1058
  var base = config.base;
853
1059
  if (_command === 'serve') {
854
1060
  base = (config.server.origin || '') + config.base;
855
1061
  }
856
- publicPath = _originalConfigBase === '' ? 'auto' : base ? base.replace(/\/?$/, '/') : 'auto';
1062
+ publicPath = resolvePublicPath(mfOptions, base, _originalConfigBase);
857
1063
  },
1064
+ /**
1065
+ * Generates the module federation manifest file
1066
+ * @param options - Rollup output options
1067
+ * @param bundle - Generated bundle assets
1068
+ */
858
1069
  generateBundle: function generateBundle(options, bundle) {
859
1070
  try {
860
1071
  var _this = this;
861
- // 递归查找模块的同步导入文件
862
- var _findSynchronousImports = function findSynchronousImports(fileName, array) {
863
- var fileData = bundle[fileName];
864
- if (fileData && fileData.type === 'chunk') {
865
- array.push(fileName); // 将当前文件加入预加载列表
866
- // 遍历该文件的同步导入文件
867
- fileData.imports.forEach(function (importedFile) {
868
- if (array.indexOf(importedFile) === -1) {
869
- _findSynchronousImports(importedFile, array); // 递归查找同步导入的文件
870
- }
871
- });
872
- }
873
- };
874
1072
  if (!mfManifestName) return Promise.resolve();
875
- var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
876
- return mfOptions.exposes[item]["import"];
877
- }); // 获取你提供的 moduleIds
878
- var filesContainingModules = {};
879
- // 帮助函数:检查模块路径是否匹配
880
- var isModuleMatched = function isModuleMatched(relativeModulePath, preloadModule) {
881
- // 先尝试直接匹配
882
- if (relativeModulePath === preloadModule) return true;
883
- // 如果 preloadModule 没有后缀,尝试添加可能的后缀进行匹配
884
- for (var _iterator = _createForOfIteratorHelperLoose(extensions), _step; !(_step = _iterator()).done;) {
885
- var ext = _step.value;
886
- if (relativeModulePath === "" + preloadModule + ext) {
887
- return true;
888
- }
889
- }
890
- return false;
891
- };
892
- // 遍历打包生成的每个文件
1073
+ var filesMap = {};
1074
+ // First pass: Find remoteEntry file
893
1075
  for (var _i = 0, _Object$entries = Object.entries(bundle); _i < _Object$entries.length; _i++) {
894
1076
  var _Object$entries$_i = _Object$entries[_i],
895
- fileName = _Object$entries$_i[0],
1077
+ _ = _Object$entries$_i[0],
896
1078
  fileData = _Object$entries$_i[1];
897
1079
  if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
898
1080
  remoteEntryFile = fileData.fileName;
899
- }
900
- if (fileData.type === 'chunk') {
901
- // 遍历该文件的所有模块
902
- for (var _i2 = 0, _Object$keys = Object.keys(fileData.modules); _i2 < _Object$keys.length; _i2++) {
903
- var modulePath = _Object$keys[_i2];
904
- // 将绝对路径转换为相对于 Vite root 的相对路径
905
- var relativeModulePath = path.relative(root, modulePath);
906
- // 检查模块是否在 preloadModules 列表中
907
- for (var _iterator2 = _createForOfIteratorHelperLoose(exposesModules), _step2; !(_step2 = _iterator2()).done;) {
908
- var preloadModule = _step2.value;
909
- var formatPreloadModule = preloadModule.replace('./', '');
910
- if (isModuleMatched(relativeModulePath, formatPreloadModule)) {
911
- var _filesContainingModul;
912
- if (!filesContainingModules[preloadModule]) {
913
- filesContainingModules[preloadModule] = {
914
- sync: [],
915
- async: []
916
- };
917
- }
918
- console.log(Object.keys(fileData.modules));
919
- filesContainingModules[preloadModule].sync.push(fileName);
920
- (_filesContainingModul = filesContainingModules[preloadModule].async).push.apply(_filesContainingModul, fileData.dynamicImports || []);
921
- _findSynchronousImports(fileName, filesContainingModules[preloadModule].sync);
922
- break; // 如果找到匹配,跳出循环
923
- }
924
- }
925
- }
1081
+ break; // We can break early since we only need to find remoteEntry once
926
1082
  }
927
1083
  }
928
- var fileToShareKey = {};
929
- return Promise.resolve(Promise.all(Array.from(getUsedShares()).map(function (shareKey) {
930
- try {
931
- return Promise.resolve(_this.resolve(getPreBuildLibImportId(shareKey))).then(function (_this$resolve) {
932
- var file = _this$resolve.id.split('?')[0];
933
- fileToShareKey[file] = shareKey;
934
- });
935
- } catch (e) {
936
- return Promise.reject(e);
937
- }
938
- }))).then(function () {
939
- // 遍历打包生成的每个文件
940
- for (var _i3 = 0, _Object$entries2 = Object.entries(bundle); _i3 < _Object$entries2.length; _i3++) {
941
- var _Object$entries2$_i = _Object$entries2[_i3],
942
- _fileName = _Object$entries2$_i[0],
943
- _fileData = _Object$entries2$_i[1];
944
- if (_fileData.type === 'chunk') {
945
- // 遍历该文件的所有模块
946
- for (var _i4 = 0, _Object$keys2 = Object.keys(_fileData.modules); _i4 < _Object$keys2.length; _i4++) {
947
- var _modulePath = _Object$keys2[_i4];
948
- var sharedKey = fileToShareKey[_modulePath];
949
- if (sharedKey) {
950
- var _filesContainingModul2;
951
- if (!filesContainingModules[sharedKey]) {
952
- filesContainingModules[sharedKey] = {
953
- sync: [],
954
- async: []
955
- };
956
- }
957
- filesContainingModules[sharedKey].sync.push(_fileName);
958
- (_filesContainingModul2 = filesContainingModules[sharedKey].async).push.apply(_filesContainingModul2, _fileData.dynamicImports || []);
959
- _findSynchronousImports(_fileName, filesContainingModules[sharedKey].sync);
960
- break; // 如果找到匹配,跳出循环
961
- }
962
- }
1084
+ // Second pass: Collect all CSS assets
1085
+ var allCssAssets = collectCssAssets(bundle);
1086
+ var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
1087
+ return mfOptions.exposes[item]["import"];
1088
+ });
1089
+ // Process exposed modules
1090
+ processModuleAssets(bundle, filesMap, function (modulePath) {
1091
+ var absoluteModulePath = path__namespace.resolve(root, modulePath);
1092
+ return exposesModules.find(function (exposeModule) {
1093
+ var exposePath = path__namespace.resolve(root, exposeModule);
1094
+ // First try exact path match
1095
+ if (absoluteModulePath === exposePath) {
1096
+ return true;
963
1097
  }
964
- }
965
- Object.keys(filesContainingModules).forEach(function (key) {
966
- filesContainingModules[key].sync = Array.from(new Set(filesContainingModules[key].sync));
967
- filesContainingModules[key].async = Array.from(new Set(filesContainingModules[key].async));
1098
+ // Then try path match without known extensions
1099
+ var getPathWithoutKnownExt = function getPathWithoutKnownExt(filePath) {
1100
+ var ext = path__namespace.extname(filePath);
1101
+ return JS_EXTENSIONS.includes(ext) ? path__namespace.join(path__namespace.dirname(filePath), path__namespace.basename(filePath, ext)) : filePath;
1102
+ };
1103
+ var modulePathNoExt = getPathWithoutKnownExt(absoluteModulePath);
1104
+ var exposePathNoExt = getPathWithoutKnownExt(exposePath);
1105
+ return modulePathNoExt === exposePathNoExt;
1106
+ });
1107
+ });
1108
+ // Process shared modules
1109
+ return Promise.resolve(buildFileToShareKeyMap(getUsedShares(), _this.resolve.bind(_this))).then(function (fileToShareKey) {
1110
+ processModuleAssets(bundle, filesMap, function (modulePath) {
1111
+ return fileToShareKey.get(modulePath);
968
1112
  });
1113
+ // Add all CSS assets to every export
1114
+ addCssAssetsToAllExports(filesMap, allCssAssets);
1115
+ // Final deduplication of all assets
1116
+ filesMap = deduplicateAssets(filesMap);
969
1117
  _this.emitFile({
970
1118
  type: 'asset',
971
1119
  fileName: mfManifestName,
972
- source: JSON.stringify(generateMFManifest(filesContainingModules))
1120
+ source: JSON.stringify(generateMFManifest(filesMap))
973
1121
  });
974
1122
  });
975
1123
  } catch (e) {
@@ -977,6 +1125,11 @@ var Manifest = function Manifest() {
977
1125
  }
978
1126
  }
979
1127
  }];
1128
+ /**
1129
+ * Generates the final manifest JSON structure
1130
+ * @param preloadMap - Map of module assets to include
1131
+ * @returns Complete manifest object
1132
+ */
980
1133
  function generateMFManifest(preloadMap) {
981
1134
  var options = getNormalizeModuleFederationOptions();
982
1135
  var name = options.name;
@@ -985,23 +1138,23 @@ var Manifest = function Manifest() {
985
1138
  path: '',
986
1139
  type: 'module'
987
1140
  };
988
- var remotes = [];
989
- var usedRemotesMap = getUsedRemotesMap();
990
- Object.keys(usedRemotesMap).forEach(function (remoteKey) {
991
- var usedModules = Array.from(usedRemotesMap[remoteKey]);
992
- usedModules.forEach(function (moduleKey) {
993
- remotes.push({
1141
+ // Process remotes
1142
+ var remotes = Array.from(Object.entries(getUsedRemotesMap())).flatMap(function (_ref2) {
1143
+ var remoteKey = _ref2[0],
1144
+ modules = _ref2[1];
1145
+ return Array.from(modules).map(function (moduleKey) {
1146
+ return {
994
1147
  federationContainerName: options.remotes[remoteKey].entry,
995
1148
  moduleName: moduleKey.replace(remoteKey, '').replace('/', ''),
996
1149
  alias: remoteKey,
997
1150
  entry: '*'
998
- });
1151
+ };
999
1152
  });
1000
1153
  });
1001
- // @ts-ignore
1154
+ // Process shared dependencies
1002
1155
  var shared = Array.from(getUsedShares()).map(function (shareKey) {
1003
- var _preloadMap$shareKey, _preloadMap$shareKey2;
1004
1156
  var shareItem = getNormalizeShareItem(shareKey);
1157
+ var assets = preloadMap[shareKey] || createEmptyAssetMap();
1005
1158
  return {
1006
1159
  id: name + ":" + shareKey,
1007
1160
  name: shareKey,
@@ -1009,42 +1162,40 @@ var Manifest = function Manifest() {
1009
1162
  requiredVersion: shareItem.shareConfig.requiredVersion,
1010
1163
  assets: {
1011
1164
  js: {
1012
- async: (preloadMap == null || (_preloadMap$shareKey = preloadMap[shareKey]) == null ? void 0 : _preloadMap$shareKey.async) || [],
1013
- sync: (preloadMap == null || (_preloadMap$shareKey2 = preloadMap[shareKey]) == null ? void 0 : _preloadMap$shareKey2.sync) || []
1165
+ async: assets.js.async,
1166
+ sync: assets.js.sync
1014
1167
  },
1015
1168
  css: {
1016
- async: [],
1017
- sync: []
1169
+ async: assets.css.async,
1170
+ sync: assets.css.sync
1018
1171
  }
1019
1172
  }
1020
1173
  };
1021
- }).filter(function (item) {
1022
- return item;
1023
- });
1024
- var exposes = Object.keys(options.exposes).map(function (key) {
1025
- var _preloadMap$sourceFil, _preloadMap$sourceFil2;
1026
- // assets(.css, .jpg, .svg等)其他资源, 不重要, 暂未处理
1174
+ }).filter(Boolean);
1175
+ // Process exposed modules
1176
+ var exposes = Object.entries(options.exposes).map(function (_ref3) {
1177
+ var key = _ref3[0],
1178
+ value = _ref3[1];
1027
1179
  var formatKey = key.replace('./', '');
1028
- var sourceFile = options.exposes[key]["import"];
1180
+ var sourceFile = value["import"];
1181
+ var assets = preloadMap[sourceFile] || createEmptyAssetMap();
1029
1182
  return {
1030
- id: name + ':' + formatKey,
1183
+ id: name + ":" + formatKey,
1031
1184
  name: formatKey,
1032
1185
  assets: {
1033
1186
  js: {
1034
- async: (preloadMap == null || (_preloadMap$sourceFil = preloadMap[sourceFile]) == null ? void 0 : _preloadMap$sourceFil.async) || [],
1035
- sync: (preloadMap == null || (_preloadMap$sourceFil2 = preloadMap[sourceFile]) == null ? void 0 : _preloadMap$sourceFil2.sync) || []
1187
+ async: assets.js.async,
1188
+ sync: assets.js.sync
1036
1189
  },
1037
1190
  css: {
1038
- sync: [],
1039
- async: []
1191
+ async: assets.css.async,
1192
+ sync: assets.css.sync
1040
1193
  }
1041
1194
  },
1042
1195
  path: key
1043
1196
  };
1044
- }).filter(function (item) {
1045
- return item;
1046
- }); // Filter out any null values
1047
- var result = {
1197
+ }).filter(Boolean);
1198
+ return {
1048
1199
  id: name,
1049
1200
  name: name,
1050
1201
  metaData: _extends({
@@ -1059,8 +1210,6 @@ var Manifest = function Manifest() {
1059
1210
  types: {
1060
1211
  path: '',
1061
1212
  name: ''
1062
- // "zip": "@mf-types.zip",
1063
- // "api": "@mf-types.d.ts"
1064
1213
  },
1065
1214
  globalName: name,
1066
1215
  pluginVersion: '0.2.5'
@@ -1073,7 +1222,6 @@ var Manifest = function Manifest() {
1073
1222
  remotes: remotes,
1074
1223
  exposes: exposes
1075
1224
  };
1076
- return result;
1077
1225
  }
1078
1226
  };
1079
1227
 
@@ -1177,7 +1325,8 @@ function pluginProxyRemoteEntry () {
1177
1325
  if (_command === 'serve') {
1178
1326
  var _viteConfig$server, _viteConfig$server2;
1179
1327
  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';
1180
- return Promise.resolve("\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 + \"" + (viteConfig.base + options.filename) + "\")\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 ");
1328
+ var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
1329
+ return Promise.resolve("\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 ");
1181
1330
  }
1182
1331
  return Promise.resolve(code);
1183
1332
  }