@module-federation/vite 1.0.0-alpha-b1bba84 → 1.0.0-alpha-f2257a4

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/README.md CHANGED
@@ -47,15 +47,17 @@ export default defineConfig({
47
47
  remote3:
48
48
  'remote1@https://unpkg.com/react-manifest-example_remote1@1.0.6/dist/mf-manifest.json',
49
49
  // "remote4": {
50
- // entry: "http://localhost:5174/dd/remoteEntry.js",
51
- // globalEntryName: "bb",
52
- // type: "esm"
50
+ // entry: "http://localhost:xxxx/remoteEntry.js",
51
+ // globalEntryName: "xxxx",
52
+ // type: "module"
53
53
  // }
54
54
  },
55
55
  exposes: {
56
56
  './App': './src/App.vue',
57
57
  },
58
- filename: 'dd/remoteEntry.js',
58
+ filename: 'remoteEntry-[hash].js',
59
+ // https://github.com/module-federation/vite/issues/87
60
+ manifest: true,
59
61
  shared: {
60
62
  vue: {},
61
63
  react: {
package/lib/index.cjs CHANGED
@@ -84,12 +84,18 @@ var addEntry = function addEntry(_ref) {
84
84
  }
85
85
  },
86
86
  buildStart: function buildStart() {
87
- var _this$emitFile;
88
87
  if (_command === "serve") return;
89
88
  var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
90
- this.emitFile((_this$emitFile = {
91
- name: entryName
92
- }, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
89
+ var emitFileOptions = {
90
+ name: entryName,
91
+ type: 'chunk',
92
+ id: entryPath,
93
+ preserveSignature: 'strict'
94
+ };
95
+ if (!hasHash) {
96
+ emitFileOptions.fileName = fileName;
97
+ }
98
+ this.emitFile(emitFileOptions);
93
99
  if (htmlFilePath) {
94
100
  var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
95
101
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -392,13 +398,13 @@ function packageNameDecode(encoded) {
392
398
  /**
393
399
  * https://github.com/module-federation/vite/issues/68
394
400
  */
395
- function getLocalSharedImportMapPath_windows() {
401
+ function getLocalSharedImportMapPath_temp() {
396
402
  var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
397
403
  name = _getNormalizeModuleFe.name;
398
- return path__default["default"].resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
404
+ return path__default["default"].resolve(".__mf__temp", packageNameEncode(name), "localSharedImportMap");
399
405
  }
400
- function writeLocalSharedImportMap_windows(content) {
401
- var localSharedImportMapId = getLocalSharedImportMapPath_windows();
406
+ function writeLocalSharedImportMap_temp(content) {
407
+ var localSharedImportMapId = getLocalSharedImportMapPath_temp();
402
408
  createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
403
409
  }
404
410
  function createFile(filePath, content) {
@@ -553,23 +559,18 @@ function addUsedShares(pkg) {
553
559
  usedShares.add(pkg);
554
560
  }
555
561
  // *** Expose locally provided shared modules here
556
- var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
562
+ new VirtualModule("localSharedImportMap");
557
563
  function getLocalSharedImportMapPath() {
558
- if (process.platform === "win32") {
559
- return getLocalSharedImportMapPath_windows();
560
- }
561
- return localSharedImportMapModule.getPath();
564
+ return getLocalSharedImportMapPath_temp();
565
+ // return localSharedImportMapModule.getPath()
562
566
  }
563
567
  var prevSharedCount;
564
568
  function writeLocalSharedImportMap() {
565
569
  var sharedCount = getUsedShares().size;
566
570
  if (prevSharedCount !== sharedCount) {
567
571
  prevSharedCount = sharedCount;
568
- if (process.platform === "win32") {
569
- writeLocalSharedImportMap_windows(generateLocalSharedImportMap());
570
- } else {
571
- localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true);
572
- }
572
+ writeLocalSharedImportMap_temp(generateLocalSharedImportMap());
573
+ // localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true)
573
574
  }
574
575
  }
575
576
  function generateLocalSharedImportMap() {
@@ -633,6 +634,8 @@ var Manifest = function Manifest() {
633
634
  var extensions;
634
635
  var root;
635
636
  var remoteEntryFile;
637
+ var publicPath;
638
+ var _command;
636
639
  return [{
637
640
  name: 'moddule-federation-manifest',
638
641
  apply: 'serve',
@@ -671,7 +674,7 @@ var Manifest = function Manifest() {
671
674
  },
672
675
  globalName: name,
673
676
  pluginVersion: '0.2.5',
674
- publicPath: 'auto'
677
+ publicPath: publicPath
675
678
  },
676
679
  shared: Array.from(getUsedShares()).map(function (shareKey) {
677
680
  var shareItem = getNormalizeShareItem(shareKey);
@@ -735,13 +738,20 @@ var Manifest = function Manifest() {
735
738
  }, {
736
739
  name: 'moddule-federation-manifest',
737
740
  enforce: 'post',
738
- config: function config(_config) {
741
+ config: function config(_config, _ref) {
742
+ var command = _ref.command;
739
743
  if (!_config.build) _config.build = {};
740
744
  if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
745
+ _command = command;
741
746
  },
742
747
  configResolved: function configResolved(config) {
743
748
  root = config.root;
744
749
  extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
750
+ publicPath = config.base ? config.base.replace(/\/?$/, "/") : "auto";
751
+ if (_command === "serve") {
752
+ var origin = config.server.origin;
753
+ publicPath = origin ? origin.replace(/\/?$/, "/") : "auto";
754
+ }
745
755
  },
746
756
  generateBundle: function generateBundle(options, bundle) {
747
757
  try {
@@ -782,7 +792,7 @@ var Manifest = function Manifest() {
782
792
  var _Object$entries$_i = _Object$entries[_i],
783
793
  fileName = _Object$entries$_i[0],
784
794
  fileData = _Object$entries$_i[1];
785
- if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
795
+ if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name || fileData.name === "remoteEntry") {
786
796
  remoteEntryFile = fileData.fileName;
787
797
  }
788
798
  if (fileData.type === 'chunk') {
@@ -954,7 +964,7 @@ var Manifest = function Manifest() {
954
964
  },
955
965
  globalName: name,
956
966
  pluginVersion: '0.2.5',
957
- publicPath: 'auto'
967
+ publicPath: publicPath
958
968
  },
959
969
  shared: shared,
960
970
  remotes: remotes,
@@ -1102,6 +1112,7 @@ var PromiseStore = /*#__PURE__*/function () {
1102
1112
  function proxySharedModule(options) {
1103
1113
  var _options$shared = options.shared,
1104
1114
  shared = _options$shared === void 0 ? {} : _options$shared;
1115
+ var _config;
1105
1116
  return [{
1106
1117
  name: "generateLocalSharedImportMap",
1107
1118
  enforce: "post",
@@ -1122,16 +1133,20 @@ function proxySharedModule(options) {
1122
1133
  }, {
1123
1134
  name: 'proxyPreBuildShared',
1124
1135
  enforce: 'post',
1125
- config: function config(_config, _ref) {
1126
- var _config$resolve$alias, _config$resolve$alias2;
1136
+ configResolved: function configResolved(config) {
1137
+ _config = config;
1138
+ },
1139
+ config: function config(_config2, _ref) {
1140
+ var _config2$resolve$alia, _config2$resolve$alia2;
1127
1141
  var command = _ref.command;
1128
- (_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
1142
+ (_config2$resolve$alia = _config2.resolve.alias).push.apply(_config2$resolve$alia, Object.keys(shared).map(function (key) {
1129
1143
  var pattern = key.endsWith("/") ? "(^" + key.replace(/\/$/, "") + "(/.+)?$)" : "(^" + key + "$)";
1130
1144
  return {
1131
1145
  // Intercept all shared requests and proxy them to loadShare
1132
1146
  find: new RegExp(pattern),
1133
1147
  replacement: "$1",
1134
1148
  customResolver: function customResolver(source, importer) {
1149
+ if (/\.css$/.test(source)) return;
1135
1150
  var loadSharePath = getLoadShareModulePath(source);
1136
1151
  writeLoadShareModule(source, shared[key], command);
1137
1152
  writePreBuildLibPath(source);
@@ -1142,7 +1157,7 @@ function proxySharedModule(options) {
1142
1157
  };
1143
1158
  }));
1144
1159
  var savePrebuild = new PromiseStore();
1145
- (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
1160
+ (_config2$resolve$alia2 = _config2.resolve.alias).push.apply(_config2$resolve$alia2, Object.keys(shared).map(function (key) {
1146
1161
  return command === "build" ? {
1147
1162
  find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
1148
1163
  replacement: function replacement($1) {
@@ -1156,16 +1171,18 @@ function proxySharedModule(options) {
1156
1171
  try {
1157
1172
  var _this = this;
1158
1173
  var pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
1159
- if (importer.includes(LOAD_SHARE_TAG)) {
1160
- // save pre-bunding module id
1161
- savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
1162
- return item.id;
1163
- }));
1164
- }
1165
- // Fix localSharedImportMap import id
1166
- var _resolve = _this.resolve;
1167
- return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
1168
- return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
1174
+ return Promise.resolve(_this.resolve(pkgName).then(function (item) {
1175
+ return item.id;
1176
+ })).then(function (result) {
1177
+ if (!result.includes(_config.cacheDir)) {
1178
+ // save pre-bunding module id
1179
+ savePrebuild.set(pkgName, Promise.resolve(result));
1180
+ }
1181
+ // Fix localSharedImportMap import id
1182
+ var _resolve = _this.resolve;
1183
+ return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
1184
+ return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
1185
+ });
1169
1186
  });
1170
1187
  } catch (e) {
1171
1188
  return Promise.reject(e);
@@ -1177,16 +1194,16 @@ function proxySharedModule(options) {
1177
1194
  }, {
1178
1195
  name: "watchLocalSharedImportMap",
1179
1196
  apply: "serve",
1180
- config: function config(_config2) {
1181
- _config2.optimizeDeps = defu.defu(_config2.optimizeDeps, {
1197
+ config: function config(_config3) {
1198
+ _config3.optimizeDeps = defu.defu(_config3.optimizeDeps, {
1182
1199
  exclude: [getLocalSharedImportMapPath()]
1183
1200
  });
1184
- _config2.server = defu.defu(_config2.server, {
1201
+ _config3.server = defu.defu(_config3.server, {
1185
1202
  watch: {
1186
1203
  ignored: []
1187
1204
  }
1188
1205
  });
1189
- var watch = _config2.server.watch;
1206
+ var watch = _config3.server.watch;
1190
1207
  watch.ignored = [].concat(watch.ignored);
1191
1208
  watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
1192
1209
  }
package/lib/index.esm.js CHANGED
@@ -61,12 +61,18 @@ var addEntry = function addEntry(_ref) {
61
61
  }
62
62
  },
63
63
  buildStart: function buildStart() {
64
- var _this$emitFile;
65
64
  if (_command === "serve") return;
66
65
  var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
67
- this.emitFile((_this$emitFile = {
68
- name: entryName
69
- }, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
66
+ var emitFileOptions = {
67
+ name: entryName,
68
+ type: 'chunk',
69
+ id: entryPath,
70
+ preserveSignature: 'strict'
71
+ };
72
+ if (!hasHash) {
73
+ emitFileOptions.fileName = fileName;
74
+ }
75
+ this.emitFile(emitFileOptions);
70
76
  if (htmlFilePath) {
71
77
  var htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
72
78
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -369,13 +375,13 @@ function packageNameDecode(encoded) {
369
375
  /**
370
376
  * https://github.com/module-federation/vite/issues/68
371
377
  */
372
- function getLocalSharedImportMapPath_windows() {
378
+ function getLocalSharedImportMapPath_temp() {
373
379
  var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
374
380
  name = _getNormalizeModuleFe.name;
375
- return path__default.resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
381
+ return path__default.resolve(".__mf__temp", packageNameEncode(name), "localSharedImportMap");
376
382
  }
377
- function writeLocalSharedImportMap_windows(content) {
378
- var localSharedImportMapId = getLocalSharedImportMapPath_windows();
383
+ function writeLocalSharedImportMap_temp(content) {
384
+ var localSharedImportMapId = getLocalSharedImportMapPath_temp();
379
385
  createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
380
386
  }
381
387
  function createFile(filePath, content) {
@@ -530,23 +536,18 @@ function addUsedShares(pkg) {
530
536
  usedShares.add(pkg);
531
537
  }
532
538
  // *** Expose locally provided shared modules here
533
- var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
539
+ new VirtualModule("localSharedImportMap");
534
540
  function getLocalSharedImportMapPath() {
535
- if (process.platform === "win32") {
536
- return getLocalSharedImportMapPath_windows();
537
- }
538
- return localSharedImportMapModule.getPath();
541
+ return getLocalSharedImportMapPath_temp();
542
+ // return localSharedImportMapModule.getPath()
539
543
  }
540
544
  var prevSharedCount;
541
545
  function writeLocalSharedImportMap() {
542
546
  var sharedCount = getUsedShares().size;
543
547
  if (prevSharedCount !== sharedCount) {
544
548
  prevSharedCount = sharedCount;
545
- if (process.platform === "win32") {
546
- writeLocalSharedImportMap_windows(generateLocalSharedImportMap());
547
- } else {
548
- localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true);
549
- }
549
+ writeLocalSharedImportMap_temp(generateLocalSharedImportMap());
550
+ // localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true)
550
551
  }
551
552
  }
552
553
  function generateLocalSharedImportMap() {
@@ -610,6 +611,8 @@ var Manifest = function Manifest() {
610
611
  var extensions;
611
612
  var root;
612
613
  var remoteEntryFile;
614
+ var publicPath;
615
+ var _command;
613
616
  return [{
614
617
  name: 'moddule-federation-manifest',
615
618
  apply: 'serve',
@@ -648,7 +651,7 @@ var Manifest = function Manifest() {
648
651
  },
649
652
  globalName: name,
650
653
  pluginVersion: '0.2.5',
651
- publicPath: 'auto'
654
+ publicPath: publicPath
652
655
  },
653
656
  shared: Array.from(getUsedShares()).map(function (shareKey) {
654
657
  var shareItem = getNormalizeShareItem(shareKey);
@@ -712,13 +715,20 @@ var Manifest = function Manifest() {
712
715
  }, {
713
716
  name: 'moddule-federation-manifest',
714
717
  enforce: 'post',
715
- config: function config(_config) {
718
+ config: function config(_config, _ref) {
719
+ var command = _ref.command;
716
720
  if (!_config.build) _config.build = {};
717
721
  if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
722
+ _command = command;
718
723
  },
719
724
  configResolved: function configResolved(config) {
720
725
  root = config.root;
721
726
  extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
727
+ publicPath = config.base ? config.base.replace(/\/?$/, "/") : "auto";
728
+ if (_command === "serve") {
729
+ var origin = config.server.origin;
730
+ publicPath = origin ? origin.replace(/\/?$/, "/") : "auto";
731
+ }
722
732
  },
723
733
  generateBundle: function generateBundle(options, bundle) {
724
734
  try {
@@ -759,7 +769,7 @@ var Manifest = function Manifest() {
759
769
  var _Object$entries$_i = _Object$entries[_i],
760
770
  fileName = _Object$entries$_i[0],
761
771
  fileData = _Object$entries$_i[1];
762
- if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
772
+ if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name || fileData.name === "remoteEntry") {
763
773
  remoteEntryFile = fileData.fileName;
764
774
  }
765
775
  if (fileData.type === 'chunk') {
@@ -931,7 +941,7 @@ var Manifest = function Manifest() {
931
941
  },
932
942
  globalName: name,
933
943
  pluginVersion: '0.2.5',
934
- publicPath: 'auto'
944
+ publicPath: publicPath
935
945
  },
936
946
  shared: shared,
937
947
  remotes: remotes,
@@ -1079,6 +1089,7 @@ var PromiseStore = /*#__PURE__*/function () {
1079
1089
  function proxySharedModule(options) {
1080
1090
  var _options$shared = options.shared,
1081
1091
  shared = _options$shared === void 0 ? {} : _options$shared;
1092
+ var _config;
1082
1093
  return [{
1083
1094
  name: "generateLocalSharedImportMap",
1084
1095
  enforce: "post",
@@ -1099,16 +1110,20 @@ function proxySharedModule(options) {
1099
1110
  }, {
1100
1111
  name: 'proxyPreBuildShared',
1101
1112
  enforce: 'post',
1102
- config: function config(_config, _ref) {
1103
- var _config$resolve$alias, _config$resolve$alias2;
1113
+ configResolved: function configResolved(config) {
1114
+ _config = config;
1115
+ },
1116
+ config: function config(_config2, _ref) {
1117
+ var _config2$resolve$alia, _config2$resolve$alia2;
1104
1118
  var command = _ref.command;
1105
- (_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
1119
+ (_config2$resolve$alia = _config2.resolve.alias).push.apply(_config2$resolve$alia, Object.keys(shared).map(function (key) {
1106
1120
  var pattern = key.endsWith("/") ? "(^" + key.replace(/\/$/, "") + "(/.+)?$)" : "(^" + key + "$)";
1107
1121
  return {
1108
1122
  // Intercept all shared requests and proxy them to loadShare
1109
1123
  find: new RegExp(pattern),
1110
1124
  replacement: "$1",
1111
1125
  customResolver: function customResolver(source, importer) {
1126
+ if (/\.css$/.test(source)) return;
1112
1127
  var loadSharePath = getLoadShareModulePath(source);
1113
1128
  writeLoadShareModule(source, shared[key], command);
1114
1129
  writePreBuildLibPath(source);
@@ -1119,7 +1134,7 @@ function proxySharedModule(options) {
1119
1134
  };
1120
1135
  }));
1121
1136
  var savePrebuild = new PromiseStore();
1122
- (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
1137
+ (_config2$resolve$alia2 = _config2.resolve.alias).push.apply(_config2$resolve$alia2, Object.keys(shared).map(function (key) {
1123
1138
  return command === "build" ? {
1124
1139
  find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
1125
1140
  replacement: function replacement($1) {
@@ -1133,16 +1148,18 @@ function proxySharedModule(options) {
1133
1148
  try {
1134
1149
  var _this = this;
1135
1150
  var pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
1136
- if (importer.includes(LOAD_SHARE_TAG)) {
1137
- // save pre-bunding module id
1138
- savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
1139
- return item.id;
1140
- }));
1141
- }
1142
- // Fix localSharedImportMap import id
1143
- var _resolve = _this.resolve;
1144
- return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
1145
- return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
1151
+ return Promise.resolve(_this.resolve(pkgName).then(function (item) {
1152
+ return item.id;
1153
+ })).then(function (result) {
1154
+ if (!result.includes(_config.cacheDir)) {
1155
+ // save pre-bunding module id
1156
+ savePrebuild.set(pkgName, Promise.resolve(result));
1157
+ }
1158
+ // Fix localSharedImportMap import id
1159
+ var _resolve = _this.resolve;
1160
+ return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
1161
+ return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
1162
+ });
1146
1163
  });
1147
1164
  } catch (e) {
1148
1165
  return Promise.reject(e);
@@ -1154,16 +1171,16 @@ function proxySharedModule(options) {
1154
1171
  }, {
1155
1172
  name: "watchLocalSharedImportMap",
1156
1173
  apply: "serve",
1157
- config: function config(_config2) {
1158
- _config2.optimizeDeps = defu(_config2.optimizeDeps, {
1174
+ config: function config(_config3) {
1175
+ _config3.optimizeDeps = defu(_config3.optimizeDeps, {
1159
1176
  exclude: [getLocalSharedImportMapPath()]
1160
1177
  });
1161
- _config2.server = defu(_config2.server, {
1178
+ _config3.server = defu(_config3.server, {
1162
1179
  watch: {
1163
1180
  ignored: []
1164
1181
  }
1165
1182
  });
1166
- var watch = _config2.server.watch;
1183
+ var watch = _config3.server.watch;
1167
1184
  watch.ignored = [].concat(watch.ignored);
1168
1185
  watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
1169
1186
  }
@@ -67,13 +67,16 @@ const addEntry = ({
67
67
  buildStart() {
68
68
  if (_command === "serve") return;
69
69
  const hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
70
- this.emitFile({
70
+ const emitFileOptions = {
71
71
  name: entryName,
72
- [hasHash ? "name" : "fileName"]: fileName,
73
72
  type: 'chunk',
74
73
  id: entryPath,
75
74
  preserveSignature: 'strict'
76
- });
75
+ };
76
+ if (!hasHash) {
77
+ emitFileOptions.fileName = fileName;
78
+ }
79
+ this.emitFile(emitFileOptions);
77
80
  if (htmlFilePath) {
78
81
  const htmlContent = fs.readFileSync(htmlFilePath, 'utf-8');
79
82
  const scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -348,14 +351,14 @@ function packageNameDecode(encoded) {
348
351
  /**
349
352
  * https://github.com/module-federation/vite/issues/68
350
353
  */
351
- function getLocalSharedImportMapPath_windows() {
354
+ function getLocalSharedImportMapPath_temp() {
352
355
  const {
353
356
  name
354
357
  } = getNormalizeModuleFederationOptions();
355
- return path__default.resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
358
+ return path__default.resolve(".__mf__temp", packageNameEncode(name), "localSharedImportMap");
356
359
  }
357
- function writeLocalSharedImportMap_windows(content) {
358
- const localSharedImportMapId = getLocalSharedImportMapPath_windows();
360
+ function writeLocalSharedImportMap_temp(content) {
361
+ const localSharedImportMapId = getLocalSharedImportMapPath_temp();
359
362
  createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
360
363
  }
361
364
  function createFile(filePath, content) {
@@ -529,23 +532,18 @@ function addUsedShares(pkg) {
529
532
  usedShares.add(pkg);
530
533
  }
531
534
  // *** Expose locally provided shared modules here
532
- const localSharedImportMapModule = new VirtualModule("localSharedImportMap");
535
+ new VirtualModule("localSharedImportMap");
533
536
  function getLocalSharedImportMapPath() {
534
- if (process.platform === "win32") {
535
- return getLocalSharedImportMapPath_windows();
536
- }
537
- return localSharedImportMapModule.getPath();
537
+ return getLocalSharedImportMapPath_temp();
538
+ // return localSharedImportMapModule.getPath()
538
539
  }
539
540
  let prevSharedCount;
540
541
  function writeLocalSharedImportMap() {
541
542
  const sharedCount = getUsedShares().size;
542
543
  if (prevSharedCount !== sharedCount) {
543
544
  prevSharedCount = sharedCount;
544
- if (process.platform === "win32") {
545
- writeLocalSharedImportMap_windows(generateLocalSharedImportMap());
546
- } else {
547
- localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true);
548
- }
545
+ writeLocalSharedImportMap_temp(generateLocalSharedImportMap());
546
+ // localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true)
549
547
  }
550
548
  }
551
549
  function generateLocalSharedImportMap() {
@@ -699,6 +697,8 @@ const Manifest = () => {
699
697
  let extensions;
700
698
  let root;
701
699
  let remoteEntryFile;
700
+ let publicPath;
701
+ let _command;
702
702
  return [{
703
703
  name: 'moddule-federation-manifest',
704
704
  apply: 'serve',
@@ -737,7 +737,7 @@ const Manifest = () => {
737
737
  },
738
738
  globalName: name,
739
739
  pluginVersion: '0.2.5',
740
- publicPath: 'auto'
740
+ publicPath
741
741
  },
742
742
  shared: Array.from(getUsedShares()).map(shareKey => {
743
743
  const shareItem = getNormalizeShareItem(shareKey);
@@ -801,13 +801,21 @@ const Manifest = () => {
801
801
  }, {
802
802
  name: 'moddule-federation-manifest',
803
803
  enforce: 'post',
804
- config(config) {
804
+ config(config, {
805
+ command
806
+ }) {
805
807
  if (!config.build) config.build = {};
806
808
  if (!config.build.manifest) config.build.manifest = config.build.manifest || !!manifestOptions;
809
+ _command = command;
807
810
  },
808
811
  configResolved(config) {
809
812
  root = config.root;
810
813
  extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
814
+ publicPath = config.base ? config.base.replace(/\/?$/, "/") : "auto";
815
+ if (_command === "serve") {
816
+ const origin = config.server.origin;
817
+ publicPath = origin ? origin.replace(/\/?$/, "/") : "auto";
818
+ }
811
819
  },
812
820
  async generateBundle(options, bundle) {
813
821
  if (!mfManifestName) return;
@@ -827,7 +835,7 @@ const Manifest = () => {
827
835
  };
828
836
  // 遍历打包生成的每个文件
829
837
  for (const [fileName, fileData] of Object.entries(bundle)) {
830
- if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
838
+ if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name || fileData.name === "remoteEntry") {
831
839
  remoteEntryFile = fileData.fileName;
832
840
  }
833
841
  if (fileData.type === 'chunk') {
@@ -991,7 +999,7 @@ const Manifest = () => {
991
999
  },
992
1000
  globalName: name,
993
1001
  pluginVersion: '0.2.5',
994
- publicPath: 'auto'
1002
+ publicPath
995
1003
  },
996
1004
  shared,
997
1005
  remotes,
@@ -1133,6 +1141,7 @@ function proxySharedModule(options) {
1133
1141
  include,
1134
1142
  exclude
1135
1143
  } = options;
1144
+ let _config;
1136
1145
  return [{
1137
1146
  name: "generateLocalSharedImportMap",
1138
1147
  enforce: "post",
@@ -1149,6 +1158,9 @@ function proxySharedModule(options) {
1149
1158
  }, {
1150
1159
  name: 'proxyPreBuildShared',
1151
1160
  enforce: 'post',
1161
+ configResolved(config) {
1162
+ _config = config;
1163
+ },
1152
1164
  config(config, {
1153
1165
  command
1154
1166
  }) {
@@ -1159,6 +1171,7 @@ function proxySharedModule(options) {
1159
1171
  find: new RegExp(pattern),
1160
1172
  replacement: "$1",
1161
1173
  customResolver(source, importer) {
1174
+ if (/\.css$/.test(source)) return;
1162
1175
  const loadSharePath = getLoadShareModulePath(source);
1163
1176
  writeLoadShareModule(source, shared[key], command);
1164
1177
  writePreBuildLibPath(source);
@@ -1181,9 +1194,10 @@ function proxySharedModule(options) {
1181
1194
  replacement: "$1",
1182
1195
  async customResolver(source, importer) {
1183
1196
  const pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
1184
- if (importer.includes(LOAD_SHARE_TAG)) {
1197
+ const result = await this.resolve(pkgName).then(item => item.id);
1198
+ if (!result.includes(_config.cacheDir)) {
1185
1199
  // save pre-bunding module id
1186
- savePrebuild.set(pkgName, this.resolve(pkgName).then(item => item.id));
1200
+ savePrebuild.set(pkgName, Promise.resolve(result));
1187
1201
  }
1188
1202
  // Fix localSharedImportMap import id
1189
1203
  return await this.resolve(await savePrebuild.get(pkgName));
package/lib/index.umd.js CHANGED
@@ -82,12 +82,18 @@
82
82
  }
83
83
  },
84
84
  buildStart: function buildStart() {
85
- var _this$emitFile;
86
85
  if (_command === "serve") return;
87
86
  var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
88
- this.emitFile((_this$emitFile = {
89
- name: entryName
90
- }, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
87
+ var emitFileOptions = {
88
+ name: entryName,
89
+ type: 'chunk',
90
+ id: entryPath,
91
+ preserveSignature: 'strict'
92
+ };
93
+ if (!hasHash) {
94
+ emitFileOptions.fileName = fileName;
95
+ }
96
+ this.emitFile(emitFileOptions);
91
97
  if (htmlFilePath) {
92
98
  var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
93
99
  var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
@@ -390,13 +396,13 @@
390
396
  /**
391
397
  * https://github.com/module-federation/vite/issues/68
392
398
  */
393
- function getLocalSharedImportMapPath_windows() {
399
+ function getLocalSharedImportMapPath_temp() {
394
400
  var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
395
401
  name = _getNormalizeModuleFe.name;
396
- return path__default["default"].resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
402
+ return path__default["default"].resolve(".__mf__temp", packageNameEncode(name), "localSharedImportMap");
397
403
  }
398
- function writeLocalSharedImportMap_windows(content) {
399
- var localSharedImportMapId = getLocalSharedImportMapPath_windows();
404
+ function writeLocalSharedImportMap_temp(content) {
405
+ var localSharedImportMapId = getLocalSharedImportMapPath_temp();
400
406
  createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
401
407
  }
402
408
  function createFile(filePath, content) {
@@ -551,23 +557,18 @@
551
557
  usedShares.add(pkg);
552
558
  }
553
559
  // *** Expose locally provided shared modules here
554
- var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
560
+ new VirtualModule("localSharedImportMap");
555
561
  function getLocalSharedImportMapPath() {
556
- if (process.platform === "win32") {
557
- return getLocalSharedImportMapPath_windows();
558
- }
559
- return localSharedImportMapModule.getPath();
562
+ return getLocalSharedImportMapPath_temp();
563
+ // return localSharedImportMapModule.getPath()
560
564
  }
561
565
  var prevSharedCount;
562
566
  function writeLocalSharedImportMap() {
563
567
  var sharedCount = getUsedShares().size;
564
568
  if (prevSharedCount !== sharedCount) {
565
569
  prevSharedCount = sharedCount;
566
- if (process.platform === "win32") {
567
- writeLocalSharedImportMap_windows(generateLocalSharedImportMap());
568
- } else {
569
- localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true);
570
- }
570
+ writeLocalSharedImportMap_temp(generateLocalSharedImportMap());
571
+ // localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true)
571
572
  }
572
573
  }
573
574
  function generateLocalSharedImportMap() {
@@ -631,6 +632,8 @@
631
632
  var extensions;
632
633
  var root;
633
634
  var remoteEntryFile;
635
+ var publicPath;
636
+ var _command;
634
637
  return [{
635
638
  name: 'moddule-federation-manifest',
636
639
  apply: 'serve',
@@ -669,7 +672,7 @@
669
672
  },
670
673
  globalName: name,
671
674
  pluginVersion: '0.2.5',
672
- publicPath: 'auto'
675
+ publicPath: publicPath
673
676
  },
674
677
  shared: Array.from(getUsedShares()).map(function (shareKey) {
675
678
  var shareItem = getNormalizeShareItem(shareKey);
@@ -733,13 +736,20 @@
733
736
  }, {
734
737
  name: 'moddule-federation-manifest',
735
738
  enforce: 'post',
736
- config: function config(_config) {
739
+ config: function config(_config, _ref) {
740
+ var command = _ref.command;
737
741
  if (!_config.build) _config.build = {};
738
742
  if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
743
+ _command = command;
739
744
  },
740
745
  configResolved: function configResolved(config) {
741
746
  root = config.root;
742
747
  extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
748
+ publicPath = config.base ? config.base.replace(/\/?$/, "/") : "auto";
749
+ if (_command === "serve") {
750
+ var origin = config.server.origin;
751
+ publicPath = origin ? origin.replace(/\/?$/, "/") : "auto";
752
+ }
743
753
  },
744
754
  generateBundle: function generateBundle(options, bundle) {
745
755
  try {
@@ -780,7 +790,7 @@
780
790
  var _Object$entries$_i = _Object$entries[_i],
781
791
  fileName = _Object$entries$_i[0],
782
792
  fileData = _Object$entries$_i[1];
783
- if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
793
+ if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name || fileData.name === "remoteEntry") {
784
794
  remoteEntryFile = fileData.fileName;
785
795
  }
786
796
  if (fileData.type === 'chunk') {
@@ -952,7 +962,7 @@
952
962
  },
953
963
  globalName: name,
954
964
  pluginVersion: '0.2.5',
955
- publicPath: 'auto'
965
+ publicPath: publicPath
956
966
  },
957
967
  shared: shared,
958
968
  remotes: remotes,
@@ -1100,6 +1110,7 @@
1100
1110
  function proxySharedModule(options) {
1101
1111
  var _options$shared = options.shared,
1102
1112
  shared = _options$shared === void 0 ? {} : _options$shared;
1113
+ var _config;
1103
1114
  return [{
1104
1115
  name: "generateLocalSharedImportMap",
1105
1116
  enforce: "post",
@@ -1120,16 +1131,20 @@
1120
1131
  }, {
1121
1132
  name: 'proxyPreBuildShared',
1122
1133
  enforce: 'post',
1123
- config: function config(_config, _ref) {
1124
- var _config$resolve$alias, _config$resolve$alias2;
1134
+ configResolved: function configResolved(config) {
1135
+ _config = config;
1136
+ },
1137
+ config: function config(_config2, _ref) {
1138
+ var _config2$resolve$alia, _config2$resolve$alia2;
1125
1139
  var command = _ref.command;
1126
- (_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
1140
+ (_config2$resolve$alia = _config2.resolve.alias).push.apply(_config2$resolve$alia, Object.keys(shared).map(function (key) {
1127
1141
  var pattern = key.endsWith("/") ? "(^" + key.replace(/\/$/, "") + "(/.+)?$)" : "(^" + key + "$)";
1128
1142
  return {
1129
1143
  // Intercept all shared requests and proxy them to loadShare
1130
1144
  find: new RegExp(pattern),
1131
1145
  replacement: "$1",
1132
1146
  customResolver: function customResolver(source, importer) {
1147
+ if (/\.css$/.test(source)) return;
1133
1148
  var loadSharePath = getLoadShareModulePath(source);
1134
1149
  writeLoadShareModule(source, shared[key], command);
1135
1150
  writePreBuildLibPath(source);
@@ -1140,7 +1155,7 @@
1140
1155
  };
1141
1156
  }));
1142
1157
  var savePrebuild = new PromiseStore();
1143
- (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
1158
+ (_config2$resolve$alia2 = _config2.resolve.alias).push.apply(_config2$resolve$alia2, Object.keys(shared).map(function (key) {
1144
1159
  return command === "build" ? {
1145
1160
  find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
1146
1161
  replacement: function replacement($1) {
@@ -1154,16 +1169,18 @@
1154
1169
  try {
1155
1170
  var _this = this;
1156
1171
  var pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
1157
- if (importer.includes(LOAD_SHARE_TAG)) {
1158
- // save pre-bunding module id
1159
- savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
1160
- return item.id;
1161
- }));
1162
- }
1163
- // Fix localSharedImportMap import id
1164
- var _resolve = _this.resolve;
1165
- return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
1166
- return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
1172
+ return Promise.resolve(_this.resolve(pkgName).then(function (item) {
1173
+ return item.id;
1174
+ })).then(function (result) {
1175
+ if (!result.includes(_config.cacheDir)) {
1176
+ // save pre-bunding module id
1177
+ savePrebuild.set(pkgName, Promise.resolve(result));
1178
+ }
1179
+ // Fix localSharedImportMap import id
1180
+ var _resolve = _this.resolve;
1181
+ return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
1182
+ return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
1183
+ });
1167
1184
  });
1168
1185
  } catch (e) {
1169
1186
  return Promise.reject(e);
@@ -1175,16 +1192,16 @@
1175
1192
  }, {
1176
1193
  name: "watchLocalSharedImportMap",
1177
1194
  apply: "serve",
1178
- config: function config(_config2) {
1179
- _config2.optimizeDeps = defu.defu(_config2.optimizeDeps, {
1195
+ config: function config(_config3) {
1196
+ _config3.optimizeDeps = defu.defu(_config3.optimizeDeps, {
1180
1197
  exclude: [getLocalSharedImportMapPath()]
1181
1198
  });
1182
- _config2.server = defu.defu(_config2.server, {
1199
+ _config3.server = defu.defu(_config3.server, {
1183
1200
  watch: {
1184
1201
  ignored: []
1185
1202
  }
1186
1203
  });
1187
- var watch = _config2.server.watch;
1204
+ var watch = _config3.server.watch;
1188
1205
  watch.ignored = [].concat(watch.ignored);
1189
1206
  watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
1190
1207
  }
@@ -0,0 +1,2 @@
1
+ export declare function getLocalSharedImportMapPath_temp(): string;
2
+ export declare function writeLocalSharedImportMap_temp(content: string): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.0.0-alpha-b1bba84",
3
+ "version": "1.0.0-alpha-f2257a4",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "source": "src/index.ts",
@@ -10,6 +10,7 @@
10
10
  "lib/**/*"
11
11
  ],
12
12
  "scripts": {
13
+ "postinstall": "pnpm run build",
13
14
  "prepare": "husky install",
14
15
  "format": "pretty-quick",
15
16
  "dev": "microbundle watch --no-sourcemap --compress=false",
@@ -1,2 +0,0 @@
1
- export declare function getLocalSharedImportMapPath_windows(): string;
2
- export declare function writeLocalSharedImportMap_windows(content: string): void;