@module-federation/vite 1.0.0-alpha-661e052 → 1.0.0-alpha-2f53b4e

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
@@ -27,6 +27,7 @@ function _interopNamespace(e) {
27
27
 
28
28
  var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
29
29
  var path__namespace = /*#__PURE__*/_interopNamespace(path);
30
+ var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
30
31
  var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
31
32
 
32
33
  var addEntry = function addEntry(_ref) {
@@ -308,6 +309,13 @@ function removePathFromNpmPackage(packageString) {
308
309
  // Return the matched package name or the original string if no match is found
309
310
  return match ? match[0] : packageString;
310
311
  }
312
+ function getExtFromNpmPackage(packageString) {
313
+ var pkgName = removePathFromNpmPackage(packageString);
314
+ var subpath = packageString.replace(pkgName, "");
315
+ var parts = subpath.split('.');
316
+ var ext = parts.length > 1 ? "." + parts.pop() : undefined;
317
+ return ext;
318
+ }
311
319
 
312
320
  var nodeModulesDir = function findNodeModulesDir(startDir) {
313
321
  if (startDir === void 0) {
@@ -336,26 +344,58 @@ fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "package.json"
336
344
  * Physically generate files as virtual modules under node_modules/__mf__virtual/*
337
345
  */
338
346
  var VirtualModule = /*#__PURE__*/function () {
339
- function VirtualModule(name) {
347
+ function VirtualModule(name, ext) {
348
+ if (ext === void 0) {
349
+ ext = ".js";
350
+ }
340
351
  this.originName = void 0;
352
+ this.inited = false;
353
+ this.ext = void 0;
341
354
  this.originName = name;
355
+ this.ext = ext;
342
356
  }
343
357
  var _proto = VirtualModule.prototype;
344
358
  _proto.getPath = function getPath() {
345
359
  return path.resolve(nodeModulesDir, this.getImportId());
346
360
  };
347
361
  _proto.getImportId = function getImportId() {
348
- return virtualPackageName + "/" + packageNameEncode(this.originName);
362
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
363
+ name = _getNormalizeModuleFe.name;
364
+ return virtualPackageName + "/" + packageNameEncode(name) + "-" + packageNameEncode(this.originName) + this.ext;
349
365
  };
350
- _proto.writeSync = function writeSync(code) {
351
- fs.writeFileSync(this.getPath() + ".js", code);
366
+ _proto.writeSync = function writeSync(code, force) {
367
+ if (!force && this.inited) return;
368
+ if (!this.inited) {
369
+ this.inited = true;
370
+ }
371
+ fs.writeFileSync(this.getPath(), code);
352
372
  };
353
373
  _proto.write = function write(code) {
354
- fs.writeFile(this.getPath() + ".js", code, function () {});
374
+ fs.writeFile(this.getPath(), code, function () {});
355
375
  };
356
376
  return VirtualModule;
357
377
  }();
358
378
 
379
+ /**
380
+ * https://github.com/module-federation/vite/issues/68
381
+ */
382
+ function getLocalSharedImportMapPath_windows() {
383
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
384
+ name = _getNormalizeModuleFe.name;
385
+ return path__default["default"].resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
386
+ }
387
+ function writeLocalSharedImportMap_windows(content) {
388
+ var localSharedImportMapId = getLocalSharedImportMapPath_windows();
389
+ createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
390
+ }
391
+ function createFile(filePath, content) {
392
+ var dir = path__default["default"].dirname(filePath);
393
+ fs.mkdirSync(dir, {
394
+ recursive: true
395
+ });
396
+ fs.writeFileSync(filePath, content);
397
+ }
398
+
359
399
  /**
360
400
  * Even the resolveId hook cannot interfere with vite pre-build,
361
401
  * and adding query parameter virtual modules will also fail.
@@ -377,14 +417,28 @@ var generateLocalSharedImportMap = function generateLocalSharedImportMap() {
377
417
  // *** __loadShare__
378
418
  var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
379
419
  try {
420
+ var _exit;
380
421
  var sharedCount = shareds.size;
381
422
  return Promise.resolve(function () {
382
423
  if (prevSharedCount !== sharedCount) {
424
+ var _temp2 = function _temp2(_result) {
425
+ if (_exit) return _result;
426
+ var _writeSync = localSharedImportMapModule.writeSync;
427
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI2) {
428
+ return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI2, true);
429
+ });
430
+ };
383
431
  prevSharedCount = sharedCount;
384
- var _writeSync = localSharedImportMapModule.writeSync;
385
- return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
386
- return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI);
387
- });
432
+ var _temp = function () {
433
+ if (process.platform === "win32") {
434
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
435
+ var _writeLocalSharedImpo = writeLocalSharedImportMap_windows(_generateLocalSharedI);
436
+ _exit = 1;
437
+ return _writeLocalSharedImpo;
438
+ });
439
+ }
440
+ }();
441
+ return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
388
442
  }
389
443
  }());
390
444
  } catch (e) {
@@ -394,11 +448,11 @@ var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
394
448
  var preBuildCacheMap = {};
395
449
  var PREBUILD_TAG = "__prebuild__";
396
450
  function writePreBuildLibPath(pkg) {
397
- if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg);
451
+ if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg, getExtFromNpmPackage(pkg));
398
452
  preBuildCacheMap[pkg].writeSync("");
399
453
  }
400
454
  function getPreBuildLibImportId(pkg) {
401
- if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg);
455
+ if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg, getExtFromNpmPackage(pkg));
402
456
  var importId = preBuildCacheMap[pkg].getImportId();
403
457
  return importId;
404
458
  }
@@ -408,8 +462,10 @@ function addShare(pkg) {
408
462
  }
409
463
  // *** Expose locally provided shared modules here
410
464
  var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
411
- localSharedImportMapModule.writeSync("");
412
- function getLocalSharedImportMapId() {
465
+ function getLocalSharedImportMapPath() {
466
+ if (process.platform === "win32") {
467
+ return getLocalSharedImportMapPath_windows();
468
+ }
413
469
  return localSharedImportMapModule.getPath();
414
470
  }
415
471
  var prevSharedCount = 0;
@@ -433,7 +489,7 @@ function generateRemoteEntry(options) {
433
489
  return item[1];
434
490
  }).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
435
491
  return "\n " + JSON.stringify(key) + ": async () => {\n const importModule = await import(" + JSON.stringify(options.exposes[key]["import"]) + ")\n const exportModule = {}\n Object.assign(exportModule, importModule)\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return exportModule\n }\n ";
436
- }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapId() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
492
+ }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapPath() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
437
493
  var remote = options.remotes[key];
438
494
  return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
439
495
  }).join(',') + "\n ],\n shared: localSharedImportMap,\n plugins: [" + pluginImportNames.map(function (item) {
@@ -441,17 +497,49 @@ function generateRemoteEntry(options) {
441
497
  }).join(', ') + "]\n });\n initRes.initShareScopeMap('" + options.shareScope + "', shared);\n return initRes\n }\n\n function getExposes(moduleName) {\n if (!(moduleName in exposesMap)) throw new Error(`Module ${moduleName} does not exist in container.`)\n return (exposesMap[moduleName])().then(res => () => res)\n }\n export {\n init,\n getExposes as get\n }\n ";
442
498
  }
443
499
  var wrapRemoteEntryModule = new VirtualModule("wrapRemoteEntry");
444
- wrapRemoteEntryModule.writeSync("\nimport {init, get} from \"" + REMOTE_ENTRY_ID + "\"\nexport {init, get}\n");
445
- var WRAP_REMOTE_ENTRY_QUERY_STR = wrapRemoteEntryModule.getImportId();
446
- var WRAP_REMOTE_ENTRY_PATH = wrapRemoteEntryModule.getPath();
500
+ function writeWrapRemoteEntry() {
501
+ wrapRemoteEntryModule.writeSync("\n import {init, get} from \"" + REMOTE_ENTRY_ID + "\"\n export {init, get}\n ");
502
+ }
503
+ function getWrapRemoteEntryImportId() {
504
+ return wrapRemoteEntryModule.getImportId();
505
+ }
506
+ function getWrapRemoteEntryPath() {
507
+ return wrapRemoteEntryModule.getPath();
508
+ }
447
509
  /**
448
510
  * Inject entry file, automatically init when used as host,
449
511
  * and will not inject remoteEntry
450
512
  */
451
513
  var hostAutoInitModule = new VirtualModule("hostAutoInit");
452
- hostAutoInitModule.writeSync("\n import {init} from \"" + REMOTE_ENTRY_ID + "\"\n init()\n ");
453
- var HOST_AUTO_INIT_QUERY_STR = hostAutoInitModule.getImportId();
454
- var HOST_AUTO_INIT_PATH = hostAutoInitModule.getPath();
514
+ function writeHostAutoInit() {
515
+ hostAutoInitModule.writeSync("\n import {init} from \"" + REMOTE_ENTRY_ID + "\"\n init()\n ");
516
+ }
517
+ function getHostAutoInitImportId() {
518
+ return hostAutoInitModule.getImportId();
519
+ }
520
+ function getHostAutoInitPath() {
521
+ return hostAutoInitModule.getPath();
522
+ }
523
+
524
+ var remoteVirtualModule = new VirtualModule("remoteModule");
525
+ function writeRemote() {
526
+ remoteVirtualModule.writeSync("");
527
+ }
528
+ function generateRemotes(id, command) {
529
+ return {
530
+ code: "\n import {loadRemote} from \"@module-federation/runtime\"\n const exportModule = await loadRemote(" + JSON.stringify(id) + ")\n " + (command === "build" && "\n export default 'default' in (exportModule || {}) ? exportModule.default : undefined\n export const __mf__dynamicExports = exportModule\n " || "") + "\n " + (command !== "build" && "\n export default exportModule\n " || "") + "\n ",
531
+ map: null,
532
+ // TODO: vite dev mode invalid, use optimizeDeps.needsInterop
533
+ syntheticNamedExports: '__mf__dynamicExports'
534
+ };
535
+ }
536
+
537
+ function initVirtualModules() {
538
+ writeLocalSharedImportMap();
539
+ writeWrapRemoteEntry();
540
+ writeHostAutoInit();
541
+ writeRemote();
542
+ }
455
543
 
456
544
  var filter$1 = pluginutils.createFilter();
457
545
  function pluginProxyRemoteEntry () {
@@ -482,17 +570,6 @@ function pluginProxyRemoteEntry () {
482
570
  };
483
571
  }
484
572
 
485
- var remoteVirtualModule = new VirtualModule("remoteModule");
486
- remoteVirtualModule.writeSync("");
487
- function generateRemotes(id, command) {
488
- return {
489
- code: "\n import {loadRemote} from \"@module-federation/runtime\"\n const exportModule = await loadRemote(" + JSON.stringify(id) + ")\n " + (command === "build" && "\n export default 'default' in (exportModule || {}) ? exportModule.default : undefined\n export const __mf__dynamicExports = exportModule\n " || "") + "\n " + (command !== "build" && "\n export default exportModule\n " || "") + "\n ",
490
- map: null,
491
- // TODO: vite dev mode invalid, use optimizeDeps.needsInterop
492
- syntheticNamedExports: '__mf__dynamicExports'
493
- };
494
- }
495
-
496
573
  var filter = pluginutils.createFilter();
497
574
  function pluginProxyRemotes (options) {
498
575
  var command;
@@ -606,15 +683,15 @@ function proxySharedModule(options) {
606
683
  name: "generateLocalSharedImportMap",
607
684
  enforce: "post",
608
685
  resolveId: function resolveId(id) {
609
- if (id.includes(getLocalSharedImportMapId())) return id;
686
+ if (id.includes(getLocalSharedImportMapPath())) return id;
610
687
  },
611
688
  load: function load(id) {
612
- if (id.includes(getLocalSharedImportMapId())) {
689
+ if (id.includes(getLocalSharedImportMapPath())) {
613
690
  return generateLocalSharedImportMap();
614
691
  }
615
692
  },
616
693
  transform: function transform(code, id) {
617
- if (id.includes(getLocalSharedImportMapId())) {
694
+ if (id.includes(getLocalSharedImportMapPath())) {
618
695
  return generateLocalSharedImportMap();
619
696
  }
620
697
  }
@@ -658,25 +735,29 @@ function proxySharedModule(options) {
658
735
  var savePrebuild = new PromiseStore();
659
736
  (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
660
737
  return command === "build" ? {
661
- find: new RegExp(virtualPackageName + "/" + PREBUILD_TAG + "(.+)"),
662
- replacement: function replacement(_, $1) {
663
- return packageNameDecode($1);
738
+ find: new RegExp("(.*" + PREBUILD_TAG + "(.+))"),
739
+ replacement: function replacement($1) {
740
+ $1 = $1.replace(/\.[^.]+$/, "");
741
+ var pkgName = packageNameDecode($1.split(PREBUILD_TAG)[1]);
742
+ return packageNameDecode(pkgName);
664
743
  }
665
744
  } : {
666
- find: new RegExp(virtualPackageName + "/" + PREBUILD_TAG + "(.+)"),
745
+ find: new RegExp("(.*" + PREBUILD_TAG + "(.+))"),
667
746
  replacement: "$1",
668
747
  customResolver: function customResolver(source, importer) {
669
748
  try {
670
749
  var _this = this;
750
+ source = source.replace(/\.[^.]+$/, "");
751
+ var pkgName = packageNameDecode(source.split(PREBUILD_TAG)[1]);
671
752
  if (importer.includes(LOAD_SHARE_TAG)) {
672
753
  // save pre-bunding module id
673
- savePrebuild.set(source, _this.resolve(packageNameDecode(source)).then(function (item) {
754
+ savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
674
755
  return item.id;
675
756
  }));
676
757
  }
677
758
  // Fix localSharedImportMap import id
678
759
  var _resolve = _this.resolve;
679
- return Promise.resolve(savePrebuild.get(source)).then(function (_savePrebuild$get) {
760
+ return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
680
761
  return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
681
762
  });
682
763
  } catch (e) {
@@ -690,6 +771,9 @@ function proxySharedModule(options) {
690
771
  name: "watchLocalSharedImportMap",
691
772
  apply: "serve",
692
773
  config: function config(_config2) {
774
+ _config2.optimizeDeps = defu.defu(_config2.optimizeDeps, {
775
+ exclude: [getLocalSharedImportMapPath()]
776
+ });
693
777
  _config2.server = defu.defu(_config2.server, {
694
778
  watch: {
695
779
  ignored: []
@@ -697,7 +781,7 @@ function proxySharedModule(options) {
697
781
  });
698
782
  var watch = _config2.server.watch;
699
783
  watch.ignored = [].concat(watch.ignored);
700
- watch.ignored.push("!**/node_modules/" + localSharedImportMapModule.getImportId() + ".js");
784
+ watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
701
785
  }
702
786
  }, {
703
787
  name: "prebuild-top-level-await",
@@ -792,6 +876,8 @@ var normalizeOptimizeDepsPlugin = {
792
876
  _config.optimizeDeps = {};
793
877
  optimizeDeps = _config.optimizeDeps;
794
878
  }
879
+ // todo: fix this workaround
880
+ optimizeDeps.force = true;
795
881
  if (!optimizeDeps.include) optimizeDeps.include = [];
796
882
  if (!optimizeDeps.needsInterop) optimizeDeps.needsInterop = [];
797
883
  }
@@ -799,19 +885,20 @@ var normalizeOptimizeDepsPlugin = {
799
885
 
800
886
  function federation(mfUserOptions) {
801
887
  var options = normalizeModuleFederationOptions(mfUserOptions);
888
+ initVirtualModules();
802
889
  var name = options.name,
803
890
  shared = options.shared,
804
891
  filename = options.filename;
805
892
  if (!name) throw new Error("name is required");
806
893
  return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
807
894
  entryName: 'remoteEntry',
808
- entryPath: WRAP_REMOTE_ENTRY_PATH,
895
+ entryPath: getWrapRemoteEntryPath(),
809
896
  fileName: filename
810
897
  }), addEntry({
811
898
  entryName: 'hostInit',
812
- entryPath: HOST_AUTO_INIT_PATH
899
+ entryPath: getHostAutoInitPath()
813
900
  }), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
814
- return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId());
901
+ return id.includes(getHostAutoInitImportId()) || id.includes(getWrapRemoteEntryImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
815
902
  }), proxySharedModule({
816
903
  shared: shared
817
904
  }), [{
package/lib/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as fs from 'fs';
2
2
  import { existsSync, mkdirSync, writeFileSync, writeFile } from 'fs';
3
3
  import * as path from 'pathe';
4
- import { parse, join, dirname, resolve } from 'pathe';
4
+ import path__default, { parse, join, dirname, resolve } from 'pathe';
5
5
  import { createFilter } from '@rollup/pluginutils';
6
6
  import { defu } from 'defu';
7
7
  import { walk } from 'estree-walker';
@@ -286,6 +286,13 @@ function removePathFromNpmPackage(packageString) {
286
286
  // Return the matched package name or the original string if no match is found
287
287
  return match ? match[0] : packageString;
288
288
  }
289
+ function getExtFromNpmPackage(packageString) {
290
+ var pkgName = removePathFromNpmPackage(packageString);
291
+ var subpath = packageString.replace(pkgName, "");
292
+ var parts = subpath.split('.');
293
+ var ext = parts.length > 1 ? "." + parts.pop() : undefined;
294
+ return ext;
295
+ }
289
296
 
290
297
  var nodeModulesDir = function findNodeModulesDir(startDir) {
291
298
  if (startDir === void 0) {
@@ -314,26 +321,58 @@ writeFileSync(resolve(nodeModulesDir, virtualPackageName, "package.json"), JSON.
314
321
  * Physically generate files as virtual modules under node_modules/__mf__virtual/*
315
322
  */
316
323
  var VirtualModule = /*#__PURE__*/function () {
317
- function VirtualModule(name) {
324
+ function VirtualModule(name, ext) {
325
+ if (ext === void 0) {
326
+ ext = ".js";
327
+ }
318
328
  this.originName = void 0;
329
+ this.inited = false;
330
+ this.ext = void 0;
319
331
  this.originName = name;
332
+ this.ext = ext;
320
333
  }
321
334
  var _proto = VirtualModule.prototype;
322
335
  _proto.getPath = function getPath() {
323
336
  return resolve(nodeModulesDir, this.getImportId());
324
337
  };
325
338
  _proto.getImportId = function getImportId() {
326
- return virtualPackageName + "/" + packageNameEncode(this.originName);
339
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
340
+ name = _getNormalizeModuleFe.name;
341
+ return virtualPackageName + "/" + packageNameEncode(name) + "-" + packageNameEncode(this.originName) + this.ext;
327
342
  };
328
- _proto.writeSync = function writeSync(code) {
329
- writeFileSync(this.getPath() + ".js", code);
343
+ _proto.writeSync = function writeSync(code, force) {
344
+ if (!force && this.inited) return;
345
+ if (!this.inited) {
346
+ this.inited = true;
347
+ }
348
+ writeFileSync(this.getPath(), code);
330
349
  };
331
350
  _proto.write = function write(code) {
332
- writeFile(this.getPath() + ".js", code, function () {});
351
+ writeFile(this.getPath(), code, function () {});
333
352
  };
334
353
  return VirtualModule;
335
354
  }();
336
355
 
356
+ /**
357
+ * https://github.com/module-federation/vite/issues/68
358
+ */
359
+ function getLocalSharedImportMapPath_windows() {
360
+ var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
361
+ name = _getNormalizeModuleFe.name;
362
+ return path__default.resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
363
+ }
364
+ function writeLocalSharedImportMap_windows(content) {
365
+ var localSharedImportMapId = getLocalSharedImportMapPath_windows();
366
+ createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
367
+ }
368
+ function createFile(filePath, content) {
369
+ var dir = path__default.dirname(filePath);
370
+ mkdirSync(dir, {
371
+ recursive: true
372
+ });
373
+ writeFileSync(filePath, content);
374
+ }
375
+
337
376
  /**
338
377
  * Even the resolveId hook cannot interfere with vite pre-build,
339
378
  * and adding query parameter virtual modules will also fail.
@@ -355,14 +394,28 @@ var generateLocalSharedImportMap = function generateLocalSharedImportMap() {
355
394
  // *** __loadShare__
356
395
  var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
357
396
  try {
397
+ var _exit;
358
398
  var sharedCount = shareds.size;
359
399
  return Promise.resolve(function () {
360
400
  if (prevSharedCount !== sharedCount) {
401
+ var _temp2 = function _temp2(_result) {
402
+ if (_exit) return _result;
403
+ var _writeSync = localSharedImportMapModule.writeSync;
404
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI2) {
405
+ return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI2, true);
406
+ });
407
+ };
361
408
  prevSharedCount = sharedCount;
362
- var _writeSync = localSharedImportMapModule.writeSync;
363
- return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
364
- return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI);
365
- });
409
+ var _temp = function () {
410
+ if (process.platform === "win32") {
411
+ return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
412
+ var _writeLocalSharedImpo = writeLocalSharedImportMap_windows(_generateLocalSharedI);
413
+ _exit = 1;
414
+ return _writeLocalSharedImpo;
415
+ });
416
+ }
417
+ }();
418
+ return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
366
419
  }
367
420
  }());
368
421
  } catch (e) {
@@ -372,11 +425,11 @@ var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
372
425
  var preBuildCacheMap = {};
373
426
  var PREBUILD_TAG = "__prebuild__";
374
427
  function writePreBuildLibPath(pkg) {
375
- if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg);
428
+ if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg, getExtFromNpmPackage(pkg));
376
429
  preBuildCacheMap[pkg].writeSync("");
377
430
  }
378
431
  function getPreBuildLibImportId(pkg) {
379
- if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg);
432
+ if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(PREBUILD_TAG + pkg, getExtFromNpmPackage(pkg));
380
433
  var importId = preBuildCacheMap[pkg].getImportId();
381
434
  return importId;
382
435
  }
@@ -386,8 +439,10 @@ function addShare(pkg) {
386
439
  }
387
440
  // *** Expose locally provided shared modules here
388
441
  var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
389
- localSharedImportMapModule.writeSync("");
390
- function getLocalSharedImportMapId() {
442
+ function getLocalSharedImportMapPath() {
443
+ if (process.platform === "win32") {
444
+ return getLocalSharedImportMapPath_windows();
445
+ }
391
446
  return localSharedImportMapModule.getPath();
392
447
  }
393
448
  var prevSharedCount = 0;
@@ -411,7 +466,7 @@ function generateRemoteEntry(options) {
411
466
  return item[1];
412
467
  }).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
413
468
  return "\n " + JSON.stringify(key) + ": async () => {\n const importModule = await import(" + JSON.stringify(options.exposes[key]["import"]) + ")\n const exportModule = {}\n Object.assign(exportModule, importModule)\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return exportModule\n }\n ";
414
- }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapId() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
469
+ }).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapPath() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
415
470
  var remote = options.remotes[key];
416
471
  return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
417
472
  }).join(',') + "\n ],\n shared: localSharedImportMap,\n plugins: [" + pluginImportNames.map(function (item) {
@@ -419,17 +474,49 @@ function generateRemoteEntry(options) {
419
474
  }).join(', ') + "]\n });\n initRes.initShareScopeMap('" + options.shareScope + "', shared);\n return initRes\n }\n\n function getExposes(moduleName) {\n if (!(moduleName in exposesMap)) throw new Error(`Module ${moduleName} does not exist in container.`)\n return (exposesMap[moduleName])().then(res => () => res)\n }\n export {\n init,\n getExposes as get\n }\n ";
420
475
  }
421
476
  var wrapRemoteEntryModule = new VirtualModule("wrapRemoteEntry");
422
- wrapRemoteEntryModule.writeSync("\nimport {init, get} from \"" + REMOTE_ENTRY_ID + "\"\nexport {init, get}\n");
423
- var WRAP_REMOTE_ENTRY_QUERY_STR = wrapRemoteEntryModule.getImportId();
424
- var WRAP_REMOTE_ENTRY_PATH = wrapRemoteEntryModule.getPath();
477
+ function writeWrapRemoteEntry() {
478
+ wrapRemoteEntryModule.writeSync("\n import {init, get} from \"" + REMOTE_ENTRY_ID + "\"\n export {init, get}\n ");
479
+ }
480
+ function getWrapRemoteEntryImportId() {
481
+ return wrapRemoteEntryModule.getImportId();
482
+ }
483
+ function getWrapRemoteEntryPath() {
484
+ return wrapRemoteEntryModule.getPath();
485
+ }
425
486
  /**
426
487
  * Inject entry file, automatically init when used as host,
427
488
  * and will not inject remoteEntry
428
489
  */
429
490
  var hostAutoInitModule = new VirtualModule("hostAutoInit");
430
- hostAutoInitModule.writeSync("\n import {init} from \"" + REMOTE_ENTRY_ID + "\"\n init()\n ");
431
- var HOST_AUTO_INIT_QUERY_STR = hostAutoInitModule.getImportId();
432
- var HOST_AUTO_INIT_PATH = hostAutoInitModule.getPath();
491
+ function writeHostAutoInit() {
492
+ hostAutoInitModule.writeSync("\n import {init} from \"" + REMOTE_ENTRY_ID + "\"\n init()\n ");
493
+ }
494
+ function getHostAutoInitImportId() {
495
+ return hostAutoInitModule.getImportId();
496
+ }
497
+ function getHostAutoInitPath() {
498
+ return hostAutoInitModule.getPath();
499
+ }
500
+
501
+ var remoteVirtualModule = new VirtualModule("remoteModule");
502
+ function writeRemote() {
503
+ remoteVirtualModule.writeSync("");
504
+ }
505
+ function generateRemotes(id, command) {
506
+ return {
507
+ code: "\n import {loadRemote} from \"@module-federation/runtime\"\n const exportModule = await loadRemote(" + JSON.stringify(id) + ")\n " + (command === "build" && "\n export default 'default' in (exportModule || {}) ? exportModule.default : undefined\n export const __mf__dynamicExports = exportModule\n " || "") + "\n " + (command !== "build" && "\n export default exportModule\n " || "") + "\n ",
508
+ map: null,
509
+ // TODO: vite dev mode invalid, use optimizeDeps.needsInterop
510
+ syntheticNamedExports: '__mf__dynamicExports'
511
+ };
512
+ }
513
+
514
+ function initVirtualModules() {
515
+ writeLocalSharedImportMap();
516
+ writeWrapRemoteEntry();
517
+ writeHostAutoInit();
518
+ writeRemote();
519
+ }
433
520
 
434
521
  var filter$1 = createFilter();
435
522
  function pluginProxyRemoteEntry () {
@@ -460,17 +547,6 @@ function pluginProxyRemoteEntry () {
460
547
  };
461
548
  }
462
549
 
463
- var remoteVirtualModule = new VirtualModule("remoteModule");
464
- remoteVirtualModule.writeSync("");
465
- function generateRemotes(id, command) {
466
- return {
467
- code: "\n import {loadRemote} from \"@module-federation/runtime\"\n const exportModule = await loadRemote(" + JSON.stringify(id) + ")\n " + (command === "build" && "\n export default 'default' in (exportModule || {}) ? exportModule.default : undefined\n export const __mf__dynamicExports = exportModule\n " || "") + "\n " + (command !== "build" && "\n export default exportModule\n " || "") + "\n ",
468
- map: null,
469
- // TODO: vite dev mode invalid, use optimizeDeps.needsInterop
470
- syntheticNamedExports: '__mf__dynamicExports'
471
- };
472
- }
473
-
474
550
  var filter = createFilter();
475
551
  function pluginProxyRemotes (options) {
476
552
  var command;
@@ -584,15 +660,15 @@ function proxySharedModule(options) {
584
660
  name: "generateLocalSharedImportMap",
585
661
  enforce: "post",
586
662
  resolveId: function resolveId(id) {
587
- if (id.includes(getLocalSharedImportMapId())) return id;
663
+ if (id.includes(getLocalSharedImportMapPath())) return id;
588
664
  },
589
665
  load: function load(id) {
590
- if (id.includes(getLocalSharedImportMapId())) {
666
+ if (id.includes(getLocalSharedImportMapPath())) {
591
667
  return generateLocalSharedImportMap();
592
668
  }
593
669
  },
594
670
  transform: function transform(code, id) {
595
- if (id.includes(getLocalSharedImportMapId())) {
671
+ if (id.includes(getLocalSharedImportMapPath())) {
596
672
  return generateLocalSharedImportMap();
597
673
  }
598
674
  }
@@ -636,25 +712,29 @@ function proxySharedModule(options) {
636
712
  var savePrebuild = new PromiseStore();
637
713
  (_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
638
714
  return command === "build" ? {
639
- find: new RegExp(virtualPackageName + "/" + PREBUILD_TAG + "(.+)"),
640
- replacement: function replacement(_, $1) {
641
- return packageNameDecode($1);
715
+ find: new RegExp("(.*" + PREBUILD_TAG + "(.+))"),
716
+ replacement: function replacement($1) {
717
+ $1 = $1.replace(/\.[^.]+$/, "");
718
+ var pkgName = packageNameDecode($1.split(PREBUILD_TAG)[1]);
719
+ return packageNameDecode(pkgName);
642
720
  }
643
721
  } : {
644
- find: new RegExp(virtualPackageName + "/" + PREBUILD_TAG + "(.+)"),
722
+ find: new RegExp("(.*" + PREBUILD_TAG + "(.+))"),
645
723
  replacement: "$1",
646
724
  customResolver: function customResolver(source, importer) {
647
725
  try {
648
726
  var _this = this;
727
+ source = source.replace(/\.[^.]+$/, "");
728
+ var pkgName = packageNameDecode(source.split(PREBUILD_TAG)[1]);
649
729
  if (importer.includes(LOAD_SHARE_TAG)) {
650
730
  // save pre-bunding module id
651
- savePrebuild.set(source, _this.resolve(packageNameDecode(source)).then(function (item) {
731
+ savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
652
732
  return item.id;
653
733
  }));
654
734
  }
655
735
  // Fix localSharedImportMap import id
656
736
  var _resolve = _this.resolve;
657
- return Promise.resolve(savePrebuild.get(source)).then(function (_savePrebuild$get) {
737
+ return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
658
738
  return Promise.resolve(_resolve.call(_this, _savePrebuild$get));
659
739
  });
660
740
  } catch (e) {
@@ -668,6 +748,9 @@ function proxySharedModule(options) {
668
748
  name: "watchLocalSharedImportMap",
669
749
  apply: "serve",
670
750
  config: function config(_config2) {
751
+ _config2.optimizeDeps = defu(_config2.optimizeDeps, {
752
+ exclude: [getLocalSharedImportMapPath()]
753
+ });
671
754
  _config2.server = defu(_config2.server, {
672
755
  watch: {
673
756
  ignored: []
@@ -675,7 +758,7 @@ function proxySharedModule(options) {
675
758
  });
676
759
  var watch = _config2.server.watch;
677
760
  watch.ignored = [].concat(watch.ignored);
678
- watch.ignored.push("!**/node_modules/" + localSharedImportMapModule.getImportId() + ".js");
761
+ watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
679
762
  }
680
763
  }, {
681
764
  name: "prebuild-top-level-await",
@@ -770,6 +853,8 @@ var normalizeOptimizeDepsPlugin = {
770
853
  _config.optimizeDeps = {};
771
854
  optimizeDeps = _config.optimizeDeps;
772
855
  }
856
+ // todo: fix this workaround
857
+ optimizeDeps.force = true;
773
858
  if (!optimizeDeps.include) optimizeDeps.include = [];
774
859
  if (!optimizeDeps.needsInterop) optimizeDeps.needsInterop = [];
775
860
  }
@@ -777,19 +862,20 @@ var normalizeOptimizeDepsPlugin = {
777
862
 
778
863
  function federation(mfUserOptions) {
779
864
  var options = normalizeModuleFederationOptions(mfUserOptions);
865
+ initVirtualModules();
780
866
  var name = options.name,
781
867
  shared = options.shared,
782
868
  filename = options.filename;
783
869
  if (!name) throw new Error("name is required");
784
870
  return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
785
871
  entryName: 'remoteEntry',
786
- entryPath: WRAP_REMOTE_ENTRY_PATH,
872
+ entryPath: getWrapRemoteEntryPath(),
787
873
  fileName: filename
788
874
  }), addEntry({
789
875
  entryName: 'hostInit',
790
- entryPath: HOST_AUTO_INIT_PATH
876
+ entryPath: getHostAutoInitPath()
791
877
  }), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
792
- return id.includes(HOST_AUTO_INIT_QUERY_STR) || id.includes(WRAP_REMOTE_ENTRY_QUERY_STR) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapId());
878
+ return id.includes(getHostAutoInitImportId()) || id.includes(getWrapRemoteEntryImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
793
879
  }), proxySharedModule({
794
880
  shared: shared
795
881
  }), [{