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