@module-federation/vite 1.2.2 → 1.2.3

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
@@ -496,11 +496,13 @@ function createFile(filePath, content) {
496
496
  fs.writeFileSync(filePath, content);
497
497
  }
498
498
 
499
- var nodeModulesDir = function findNodeModulesDir(startDir) {
500
- if (startDir === void 0) {
501
- startDir = process.cwd();
499
+ // Cache root path
500
+ var rootDir;
501
+ function findNodeModulesDir(root) {
502
+ if (root === void 0) {
503
+ root = process.cwd();
502
504
  }
503
- var currentDir = startDir;
505
+ var currentDir = root;
504
506
  while (currentDir !== path.parse(currentDir).root) {
505
507
  var nodeModulesPath = path.join(currentDir, 'node_modules');
506
508
  if (fs.existsSync(nodeModulesPath)) {
@@ -509,16 +511,16 @@ var nodeModulesDir = function findNodeModulesDir(startDir) {
509
511
  currentDir = path.dirname(currentDir);
510
512
  }
511
513
  return '';
512
- }();
513
- var virtualPackageName = '__mf__virtual';
514
- if (!fs.existsSync(path.resolve(nodeModulesDir, virtualPackageName))) {
515
- fs.mkdirSync(path.resolve(nodeModulesDir, virtualPackageName));
516
514
  }
517
- fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, 'empty.js'), '');
518
- fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, 'package.json'), JSON.stringify({
519
- name: virtualPackageName,
520
- main: 'empty.js'
521
- }));
515
+ // Cache nodeModulesDir result to avoid repeated calculations
516
+ var cachedNodeModulesDir;
517
+ function getNodeModulesDir() {
518
+ if (!cachedNodeModulesDir) {
519
+ cachedNodeModulesDir = findNodeModulesDir(rootDir);
520
+ }
521
+ return cachedNodeModulesDir;
522
+ }
523
+ var virtualPackageName = '__mf__virtual';
522
524
  var patternMap = {};
523
525
  var cacheMap = {};
524
526
  /**
@@ -543,6 +545,30 @@ var VirtualModule = /*#__PURE__*/function () {
543
545
  if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
544
546
  cacheMap[this.tag][this.name] = this;
545
547
  }
548
+ /**
549
+ * Set the root path for finding node_modules
550
+ * @param root - Root path
551
+ */
552
+ VirtualModule.setRoot = function setRoot(root) {
553
+ rootDir = root;
554
+ // Reset cache to ensure using the new root path
555
+ cachedNodeModulesDir = undefined;
556
+ }
557
+ /**
558
+ * Ensure virtual package directory exists
559
+ */;
560
+ VirtualModule.ensureVirtualPackageExists = function ensureVirtualPackageExists() {
561
+ var nodeModulesDir = getNodeModulesDir();
562
+ var virtualPackagePath = path.resolve(nodeModulesDir, virtualPackageName);
563
+ if (!fs.existsSync(virtualPackagePath)) {
564
+ fs.mkdirSync(virtualPackagePath);
565
+ fs.writeFileSync(path.resolve(virtualPackagePath, 'empty.js'), '');
566
+ fs.writeFileSync(path.resolve(virtualPackagePath, 'package.json'), JSON.stringify({
567
+ name: virtualPackageName,
568
+ main: 'empty.js'
569
+ }));
570
+ }
571
+ };
546
572
  VirtualModule.findModule = function findModule(tag, str) {
547
573
  if (str === void 0) {
548
574
  str = '';
@@ -554,7 +580,7 @@ var VirtualModule = /*#__PURE__*/function () {
554
580
  };
555
581
  var _proto = VirtualModule.prototype;
556
582
  _proto.getPath = function getPath() {
557
- return path.resolve(nodeModulesDir, this.getImportId());
583
+ return path.resolve(getNodeModulesDir(), this.getImportId());
558
584
  };
559
585
  _proto.getImportId = function getImportId() {
560
586
  var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
@@ -750,7 +776,7 @@ var Manifest = function Manifest() {
750
776
  res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
751
777
  id: name,
752
778
  name: name,
753
- metaData: {
779
+ metaData: _extends({
754
780
  name: name,
755
781
  type: 'app',
756
782
  buildInfo: {
@@ -772,9 +798,12 @@ var Manifest = function Manifest() {
772
798
  name: ''
773
799
  },
774
800
  globalName: name,
775
- pluginVersion: '0.2.5',
801
+ pluginVersion: '0.2.5'
802
+ }, !!getPublicPath ? {
803
+ publicPath: getPublicPath
804
+ } : {
776
805
  publicPath: publicPath
777
- }
806
+ })
778
807
  })));
779
808
  } else {
780
809
  next();
@@ -838,7 +867,7 @@ var Manifest = function Manifest() {
838
867
  var _Object$entries$_i = _Object$entries[_i],
839
868
  fileName = _Object$entries$_i[0],
840
869
  fileData = _Object$entries$_i[1];
841
- if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
870
+ if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
842
871
  remoteEntryFile = fileData.fileName;
843
872
  }
844
873
  if (fileData.type === 'chunk') {
@@ -1009,7 +1038,7 @@ var Manifest = function Manifest() {
1009
1038
  globalName: name,
1010
1039
  pluginVersion: '0.2.5'
1011
1040
  }, !!getPublicPath ? {
1012
- getPublicPath: getPublicPath
1041
+ publicPath: getPublicPath
1013
1042
  } : {
1014
1043
  publicPath: publicPath
1015
1044
  }),
@@ -1314,12 +1343,21 @@ var normalizeOptimizeDepsPlugin = {
1314
1343
 
1315
1344
  function federation(mfUserOptions) {
1316
1345
  var options = normalizeModuleFederationOptions(mfUserOptions);
1317
- initVirtualModules();
1318
1346
  var name = options.name,
1319
1347
  shared = options.shared,
1320
1348
  filename = options.filename;
1321
1349
  if (!name) throw new Error('name is required');
1322
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
1350
+ return [{
1351
+ name: 'vite:module-federation-config',
1352
+ enforce: 'pre',
1353
+ configResolved: function configResolved(config) {
1354
+ // Set root path
1355
+ VirtualModule.setRoot(config.root);
1356
+ // Ensure virtual package directory exists
1357
+ VirtualModule.ensureVirtualPackageExists();
1358
+ initVirtualModules();
1359
+ }
1360
+ }, aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
1323
1361
  entryName: 'remoteEntry',
1324
1362
  entryPath: REMOTE_ENTRY_ID,
1325
1363
  fileName: filename
package/lib/index.esm.js CHANGED
@@ -2,7 +2,7 @@ import defu from 'defu';
2
2
  import * as fs from 'fs';
3
3
  import { mkdirSync, writeFileSync, existsSync, writeFile } from 'fs';
4
4
  import * as path from 'pathe';
5
- import path__default, { parse, join, dirname, resolve, relative } from 'pathe';
5
+ import path__default, { resolve, parse, join, dirname, relative } from 'pathe';
6
6
  import { createFilter } from '@rollup/pluginutils';
7
7
  import { walk } from 'estree-walker';
8
8
  import MagicString from 'magic-string';
@@ -472,11 +472,13 @@ function createFile(filePath, content) {
472
472
  writeFileSync(filePath, content);
473
473
  }
474
474
 
475
- var nodeModulesDir = function findNodeModulesDir(startDir) {
476
- if (startDir === void 0) {
477
- startDir = process.cwd();
475
+ // Cache root path
476
+ var rootDir;
477
+ function findNodeModulesDir(root) {
478
+ if (root === void 0) {
479
+ root = process.cwd();
478
480
  }
479
- var currentDir = startDir;
481
+ var currentDir = root;
480
482
  while (currentDir !== parse(currentDir).root) {
481
483
  var nodeModulesPath = join(currentDir, 'node_modules');
482
484
  if (existsSync(nodeModulesPath)) {
@@ -485,16 +487,16 @@ var nodeModulesDir = function findNodeModulesDir(startDir) {
485
487
  currentDir = dirname(currentDir);
486
488
  }
487
489
  return '';
488
- }();
489
- var virtualPackageName = '__mf__virtual';
490
- if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
491
- mkdirSync(resolve(nodeModulesDir, virtualPackageName));
492
490
  }
493
- writeFileSync(resolve(nodeModulesDir, virtualPackageName, 'empty.js'), '');
494
- writeFileSync(resolve(nodeModulesDir, virtualPackageName, 'package.json'), JSON.stringify({
495
- name: virtualPackageName,
496
- main: 'empty.js'
497
- }));
491
+ // Cache nodeModulesDir result to avoid repeated calculations
492
+ var cachedNodeModulesDir;
493
+ function getNodeModulesDir() {
494
+ if (!cachedNodeModulesDir) {
495
+ cachedNodeModulesDir = findNodeModulesDir(rootDir);
496
+ }
497
+ return cachedNodeModulesDir;
498
+ }
499
+ var virtualPackageName = '__mf__virtual';
498
500
  var patternMap = {};
499
501
  var cacheMap = {};
500
502
  /**
@@ -519,6 +521,30 @@ var VirtualModule = /*#__PURE__*/function () {
519
521
  if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
520
522
  cacheMap[this.tag][this.name] = this;
521
523
  }
524
+ /**
525
+ * Set the root path for finding node_modules
526
+ * @param root - Root path
527
+ */
528
+ VirtualModule.setRoot = function setRoot(root) {
529
+ rootDir = root;
530
+ // Reset cache to ensure using the new root path
531
+ cachedNodeModulesDir = undefined;
532
+ }
533
+ /**
534
+ * Ensure virtual package directory exists
535
+ */;
536
+ VirtualModule.ensureVirtualPackageExists = function ensureVirtualPackageExists() {
537
+ var nodeModulesDir = getNodeModulesDir();
538
+ var virtualPackagePath = resolve(nodeModulesDir, virtualPackageName);
539
+ if (!existsSync(virtualPackagePath)) {
540
+ mkdirSync(virtualPackagePath);
541
+ writeFileSync(resolve(virtualPackagePath, 'empty.js'), '');
542
+ writeFileSync(resolve(virtualPackagePath, 'package.json'), JSON.stringify({
543
+ name: virtualPackageName,
544
+ main: 'empty.js'
545
+ }));
546
+ }
547
+ };
522
548
  VirtualModule.findModule = function findModule(tag, str) {
523
549
  if (str === void 0) {
524
550
  str = '';
@@ -530,7 +556,7 @@ var VirtualModule = /*#__PURE__*/function () {
530
556
  };
531
557
  var _proto = VirtualModule.prototype;
532
558
  _proto.getPath = function getPath() {
533
- return resolve(nodeModulesDir, this.getImportId());
559
+ return resolve(getNodeModulesDir(), this.getImportId());
534
560
  };
535
561
  _proto.getImportId = function getImportId() {
536
562
  var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
@@ -726,7 +752,7 @@ var Manifest = function Manifest() {
726
752
  res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
727
753
  id: name,
728
754
  name: name,
729
- metaData: {
755
+ metaData: _extends({
730
756
  name: name,
731
757
  type: 'app',
732
758
  buildInfo: {
@@ -748,9 +774,12 @@ var Manifest = function Manifest() {
748
774
  name: ''
749
775
  },
750
776
  globalName: name,
751
- pluginVersion: '0.2.5',
777
+ pluginVersion: '0.2.5'
778
+ }, !!getPublicPath ? {
779
+ publicPath: getPublicPath
780
+ } : {
752
781
  publicPath: publicPath
753
- }
782
+ })
754
783
  })));
755
784
  } else {
756
785
  next();
@@ -814,7 +843,7 @@ var Manifest = function Manifest() {
814
843
  var _Object$entries$_i = _Object$entries[_i],
815
844
  fileName = _Object$entries$_i[0],
816
845
  fileData = _Object$entries$_i[1];
817
- if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
846
+ if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
818
847
  remoteEntryFile = fileData.fileName;
819
848
  }
820
849
  if (fileData.type === 'chunk') {
@@ -985,7 +1014,7 @@ var Manifest = function Manifest() {
985
1014
  globalName: name,
986
1015
  pluginVersion: '0.2.5'
987
1016
  }, !!getPublicPath ? {
988
- getPublicPath: getPublicPath
1017
+ publicPath: getPublicPath
989
1018
  } : {
990
1019
  publicPath: publicPath
991
1020
  }),
@@ -1290,12 +1319,21 @@ var normalizeOptimizeDepsPlugin = {
1290
1319
 
1291
1320
  function federation(mfUserOptions) {
1292
1321
  var options = normalizeModuleFederationOptions(mfUserOptions);
1293
- initVirtualModules();
1294
1322
  var name = options.name,
1295
1323
  shared = options.shared,
1296
1324
  filename = options.filename;
1297
1325
  if (!name) throw new Error('name is required');
1298
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
1326
+ return [{
1327
+ name: 'vite:module-federation-config',
1328
+ enforce: 'pre',
1329
+ configResolved: function configResolved(config) {
1330
+ // Set root path
1331
+ VirtualModule.setRoot(config.root);
1332
+ // Ensure virtual package directory exists
1333
+ VirtualModule.ensureVirtualPackageExists();
1334
+ initVirtualModules();
1335
+ }
1336
+ }, aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
1299
1337
  entryName: 'remoteEntry',
1300
1338
  entryPath: REMOTE_ENTRY_ID,
1301
1339
  fileName: filename
@@ -2,7 +2,7 @@ import defu from 'defu';
2
2
  import * as fs from 'fs';
3
3
  import { mkdirSync, writeFileSync, existsSync, writeFile } from 'fs';
4
4
  import * as path from 'pathe';
5
- import path__default, { parse, join, dirname, resolve, relative } from 'pathe';
5
+ import path__default, { resolve, parse, join, dirname, relative } from 'pathe';
6
6
  import { createFilter } from '@rollup/pluginutils';
7
7
  import { walk } from 'estree-walker';
8
8
  import MagicString from 'magic-string';
@@ -443,8 +443,10 @@ function createFile(filePath, content) {
443
443
  writeFileSync(filePath, content);
444
444
  }
445
445
 
446
- const nodeModulesDir = function findNodeModulesDir(startDir = process.cwd()) {
447
- let currentDir = startDir;
446
+ // Cache root path
447
+ let rootDir;
448
+ function findNodeModulesDir(root = process.cwd()) {
449
+ let currentDir = root;
448
450
  while (currentDir !== parse(currentDir).root) {
449
451
  const nodeModulesPath = join(currentDir, 'node_modules');
450
452
  if (existsSync(nodeModulesPath)) {
@@ -453,22 +455,46 @@ const nodeModulesDir = function findNodeModulesDir(startDir = process.cwd()) {
453
455
  currentDir = dirname(currentDir);
454
456
  }
455
457
  return '';
456
- }();
457
- const virtualPackageName = '__mf__virtual';
458
- if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
459
- mkdirSync(resolve(nodeModulesDir, virtualPackageName));
460
458
  }
461
- writeFileSync(resolve(nodeModulesDir, virtualPackageName, 'empty.js'), '');
462
- writeFileSync(resolve(nodeModulesDir, virtualPackageName, 'package.json'), JSON.stringify({
463
- name: virtualPackageName,
464
- main: 'empty.js'
465
- }));
459
+ // Cache nodeModulesDir result to avoid repeated calculations
460
+ let cachedNodeModulesDir;
461
+ function getNodeModulesDir() {
462
+ if (!cachedNodeModulesDir) {
463
+ cachedNodeModulesDir = findNodeModulesDir(rootDir);
464
+ }
465
+ return cachedNodeModulesDir;
466
+ }
467
+ const virtualPackageName = '__mf__virtual';
466
468
  const patternMap = {};
467
469
  const cacheMap = {};
468
470
  /**
469
471
  * Physically generate files as virtual modules under node_modules/__mf__virtual/*
470
472
  */
471
473
  class VirtualModule {
474
+ /**
475
+ * Set the root path for finding node_modules
476
+ * @param root - Root path
477
+ */
478
+ static setRoot(root) {
479
+ rootDir = root;
480
+ // Reset cache to ensure using the new root path
481
+ cachedNodeModulesDir = undefined;
482
+ }
483
+ /**
484
+ * Ensure virtual package directory exists
485
+ */
486
+ static ensureVirtualPackageExists() {
487
+ const nodeModulesDir = getNodeModulesDir();
488
+ const virtualPackagePath = resolve(nodeModulesDir, virtualPackageName);
489
+ if (!existsSync(virtualPackagePath)) {
490
+ mkdirSync(virtualPackagePath);
491
+ writeFileSync(resolve(virtualPackagePath, 'empty.js'), '');
492
+ writeFileSync(resolve(virtualPackagePath, 'package.json'), JSON.stringify({
493
+ name: virtualPackageName,
494
+ main: 'empty.js'
495
+ }));
496
+ }
497
+ }
472
498
  static findModule(tag, str = '') {
473
499
  if (!patternMap[tag]) patternMap[tag] = new RegExp(`(.*${packageNameEncode(tag)}(.+?)${packageNameEncode(tag)}.*)`);
474
500
  const moduleName = (str.match(patternMap[tag]) || [])[2];
@@ -488,7 +514,7 @@ class VirtualModule {
488
514
  cacheMap[this.tag][this.name] = this;
489
515
  }
490
516
  getPath() {
491
- return resolve(nodeModulesDir, this.getImportId());
517
+ return resolve(getNodeModulesDir(), this.getImportId());
492
518
  }
493
519
  getImportId() {
494
520
  const {
@@ -827,7 +853,7 @@ const Manifest = () => {
827
853
  res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
828
854
  id: name,
829
855
  name: name,
830
- metaData: {
856
+ metaData: _extends({
831
857
  name: name,
832
858
  type: 'app',
833
859
  buildInfo: {
@@ -849,9 +875,12 @@ const Manifest = () => {
849
875
  name: ''
850
876
  },
851
877
  globalName: name,
852
- pluginVersion: '0.2.5',
878
+ pluginVersion: '0.2.5'
879
+ }, !!getPublicPath ? {
880
+ publicPath: getPublicPath
881
+ } : {
853
882
  publicPath
854
- }
883
+ })
855
884
  })));
856
885
  } else {
857
886
  next();
@@ -895,7 +924,7 @@ const Manifest = () => {
895
924
  };
896
925
  // 遍历打包生成的每个文件
897
926
  for (const [fileName, fileData] of Object.entries(bundle)) {
898
- if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
927
+ if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
899
928
  remoteEntryFile = fileData.fileName;
900
929
  }
901
930
  if (fileData.type === 'chunk') {
@@ -1059,7 +1088,7 @@ const Manifest = () => {
1059
1088
  globalName: name,
1060
1089
  pluginVersion: '0.2.5'
1061
1090
  }, !!getPublicPath ? {
1062
- getPublicPath
1091
+ publicPath: getPublicPath
1063
1092
  } : {
1064
1093
  publicPath
1065
1094
  }),
@@ -1357,7 +1386,6 @@ var normalizeOptimizeDepsPlugin = {
1357
1386
 
1358
1387
  function federation(mfUserOptions) {
1359
1388
  const options = normalizeModuleFederationOptions(mfUserOptions);
1360
- initVirtualModules();
1361
1389
  const {
1362
1390
  name,
1363
1391
  remotes,
@@ -1365,7 +1393,17 @@ function federation(mfUserOptions) {
1365
1393
  filename
1366
1394
  } = options;
1367
1395
  if (!name) throw new Error('name is required');
1368
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, ...addEntry({
1396
+ return [{
1397
+ name: 'vite:module-federation-config',
1398
+ enforce: 'pre',
1399
+ configResolved(config) {
1400
+ // Set root path
1401
+ VirtualModule.setRoot(config.root);
1402
+ // Ensure virtual package directory exists
1403
+ VirtualModule.ensureVirtualPackageExists();
1404
+ initVirtualModules();
1405
+ }
1406
+ }, aliasToArrayPlugin, normalizeOptimizeDepsPlugin, ...addEntry({
1369
1407
  entryName: 'remoteEntry',
1370
1408
  entryPath: REMOTE_ENTRY_ID,
1371
1409
  fileName: filename
package/lib/index.umd.js CHANGED
@@ -494,11 +494,13 @@
494
494
  fs.writeFileSync(filePath, content);
495
495
  }
496
496
 
497
- var nodeModulesDir = function findNodeModulesDir(startDir) {
498
- if (startDir === void 0) {
499
- startDir = process.cwd();
497
+ // Cache root path
498
+ var rootDir;
499
+ function findNodeModulesDir(root) {
500
+ if (root === void 0) {
501
+ root = process.cwd();
500
502
  }
501
- var currentDir = startDir;
503
+ var currentDir = root;
502
504
  while (currentDir !== path.parse(currentDir).root) {
503
505
  var nodeModulesPath = path.join(currentDir, 'node_modules');
504
506
  if (fs.existsSync(nodeModulesPath)) {
@@ -507,16 +509,16 @@
507
509
  currentDir = path.dirname(currentDir);
508
510
  }
509
511
  return '';
510
- }();
511
- var virtualPackageName = '__mf__virtual';
512
- if (!fs.existsSync(path.resolve(nodeModulesDir, virtualPackageName))) {
513
- fs.mkdirSync(path.resolve(nodeModulesDir, virtualPackageName));
514
512
  }
515
- fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, 'empty.js'), '');
516
- fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, 'package.json'), JSON.stringify({
517
- name: virtualPackageName,
518
- main: 'empty.js'
519
- }));
513
+ // Cache nodeModulesDir result to avoid repeated calculations
514
+ var cachedNodeModulesDir;
515
+ function getNodeModulesDir() {
516
+ if (!cachedNodeModulesDir) {
517
+ cachedNodeModulesDir = findNodeModulesDir(rootDir);
518
+ }
519
+ return cachedNodeModulesDir;
520
+ }
521
+ var virtualPackageName = '__mf__virtual';
520
522
  var patternMap = {};
521
523
  var cacheMap = {};
522
524
  /**
@@ -541,6 +543,30 @@
541
543
  if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
542
544
  cacheMap[this.tag][this.name] = this;
543
545
  }
546
+ /**
547
+ * Set the root path for finding node_modules
548
+ * @param root - Root path
549
+ */
550
+ VirtualModule.setRoot = function setRoot(root) {
551
+ rootDir = root;
552
+ // Reset cache to ensure using the new root path
553
+ cachedNodeModulesDir = undefined;
554
+ }
555
+ /**
556
+ * Ensure virtual package directory exists
557
+ */;
558
+ VirtualModule.ensureVirtualPackageExists = function ensureVirtualPackageExists() {
559
+ var nodeModulesDir = getNodeModulesDir();
560
+ var virtualPackagePath = path.resolve(nodeModulesDir, virtualPackageName);
561
+ if (!fs.existsSync(virtualPackagePath)) {
562
+ fs.mkdirSync(virtualPackagePath);
563
+ fs.writeFileSync(path.resolve(virtualPackagePath, 'empty.js'), '');
564
+ fs.writeFileSync(path.resolve(virtualPackagePath, 'package.json'), JSON.stringify({
565
+ name: virtualPackageName,
566
+ main: 'empty.js'
567
+ }));
568
+ }
569
+ };
544
570
  VirtualModule.findModule = function findModule(tag, str) {
545
571
  if (str === void 0) {
546
572
  str = '';
@@ -552,7 +578,7 @@
552
578
  };
553
579
  var _proto = VirtualModule.prototype;
554
580
  _proto.getPath = function getPath() {
555
- return path.resolve(nodeModulesDir, this.getImportId());
581
+ return path.resolve(getNodeModulesDir(), this.getImportId());
556
582
  };
557
583
  _proto.getImportId = function getImportId() {
558
584
  var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
@@ -748,7 +774,7 @@
748
774
  res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
749
775
  id: name,
750
776
  name: name,
751
- metaData: {
777
+ metaData: _extends({
752
778
  name: name,
753
779
  type: 'app',
754
780
  buildInfo: {
@@ -770,9 +796,12 @@
770
796
  name: ''
771
797
  },
772
798
  globalName: name,
773
- pluginVersion: '0.2.5',
799
+ pluginVersion: '0.2.5'
800
+ }, !!getPublicPath ? {
801
+ publicPath: getPublicPath
802
+ } : {
774
803
  publicPath: publicPath
775
- }
804
+ })
776
805
  })));
777
806
  } else {
778
807
  next();
@@ -836,7 +865,7 @@
836
865
  var _Object$entries$_i = _Object$entries[_i],
837
866
  fileName = _Object$entries$_i[0],
838
867
  fileData = _Object$entries$_i[1];
839
- if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
868
+ if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
840
869
  remoteEntryFile = fileData.fileName;
841
870
  }
842
871
  if (fileData.type === 'chunk') {
@@ -1007,7 +1036,7 @@
1007
1036
  globalName: name,
1008
1037
  pluginVersion: '0.2.5'
1009
1038
  }, !!getPublicPath ? {
1010
- getPublicPath: getPublicPath
1039
+ publicPath: getPublicPath
1011
1040
  } : {
1012
1041
  publicPath: publicPath
1013
1042
  }),
@@ -1312,12 +1341,21 @@
1312
1341
 
1313
1342
  function federation(mfUserOptions) {
1314
1343
  var options = normalizeModuleFederationOptions(mfUserOptions);
1315
- initVirtualModules();
1316
1344
  var name = options.name,
1317
1345
  shared = options.shared,
1318
1346
  filename = options.filename;
1319
1347
  if (!name) throw new Error('name is required');
1320
- return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
1348
+ return [{
1349
+ name: 'vite:module-federation-config',
1350
+ enforce: 'pre',
1351
+ configResolved: function configResolved(config) {
1352
+ // Set root path
1353
+ VirtualModule.setRoot(config.root);
1354
+ // Ensure virtual package directory exists
1355
+ VirtualModule.ensureVirtualPackageExists();
1356
+ initVirtualModules();
1357
+ }
1358
+ }, aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
1321
1359
  entryName: 'remoteEntry',
1322
1360
  entryPath: REMOTE_ENTRY_ID,
1323
1361
  fileName: filename
@@ -7,6 +7,15 @@ export default class VirtualModule {
7
7
  tag: string;
8
8
  suffix: string;
9
9
  inited: boolean;
10
+ /**
11
+ * Set the root path for finding node_modules
12
+ * @param root - Root path
13
+ */
14
+ static setRoot(root: string): void;
15
+ /**
16
+ * Ensure virtual package directory exists
17
+ */
18
+ static ensureVirtualPackageExists(): void;
10
19
  static findModule(tag: string, str?: string): VirtualModule | undefined;
11
20
  constructor(name: string, tag?: string, suffix?: string);
12
21
  getPath(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/vite",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Vite plugin for Module Federation",
5
5
  "type": "module",
6
6
  "source": "src/index.ts",