@module-federation/vite 1.2.2 → 1.2.4
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 +52 -17
- package/lib/index.esm.js +53 -18
- package/lib/index.modern.js +51 -16
- package/lib/index.umd.js +52 -17
- package/lib/utils/VirtualModule.d.ts +9 -0
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -496,11 +496,13 @@ function createFile(filePath, content) {
|
|
|
496
496
|
fs.writeFileSync(filePath, content);
|
|
497
497
|
}
|
|
498
498
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
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 =
|
|
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
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
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(
|
|
583
|
+
return path.resolve(getNodeModulesDir(), this.getImportId());
|
|
558
584
|
};
|
|
559
585
|
_proto.getImportId = function getImportId() {
|
|
560
586
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
@@ -838,7 +864,7 @@ var Manifest = function Manifest() {
|
|
|
838
864
|
var _Object$entries$_i = _Object$entries[_i],
|
|
839
865
|
fileName = _Object$entries$_i[0],
|
|
840
866
|
fileData = _Object$entries$_i[1];
|
|
841
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
867
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
842
868
|
remoteEntryFile = fileData.fileName;
|
|
843
869
|
}
|
|
844
870
|
if (fileData.type === 'chunk') {
|
|
@@ -1314,12 +1340,21 @@ var normalizeOptimizeDepsPlugin = {
|
|
|
1314
1340
|
|
|
1315
1341
|
function federation(mfUserOptions) {
|
|
1316
1342
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1317
|
-
initVirtualModules();
|
|
1318
1343
|
var name = options.name,
|
|
1319
1344
|
shared = options.shared,
|
|
1320
1345
|
filename = options.filename;
|
|
1321
1346
|
if (!name) throw new Error('name is required');
|
|
1322
|
-
return [
|
|
1347
|
+
return [{
|
|
1348
|
+
name: 'vite:module-federation-config',
|
|
1349
|
+
enforce: 'pre',
|
|
1350
|
+
configResolved: function configResolved(config) {
|
|
1351
|
+
// Set root path
|
|
1352
|
+
VirtualModule.setRoot(config.root);
|
|
1353
|
+
// Ensure virtual package directory exists
|
|
1354
|
+
VirtualModule.ensureVirtualPackageExists();
|
|
1355
|
+
initVirtualModules();
|
|
1356
|
+
}
|
|
1357
|
+
}, aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
|
|
1323
1358
|
entryName: 'remoteEntry',
|
|
1324
1359
|
entryPath: REMOTE_ENTRY_ID,
|
|
1325
1360
|
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,
|
|
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
|
-
|
|
476
|
-
|
|
477
|
-
|
|
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 =
|
|
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
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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(
|
|
559
|
+
return resolve(getNodeModulesDir(), this.getImportId());
|
|
534
560
|
};
|
|
535
561
|
_proto.getImportId = function getImportId() {
|
|
536
562
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
@@ -814,7 +840,7 @@ var Manifest = function Manifest() {
|
|
|
814
840
|
var _Object$entries$_i = _Object$entries[_i],
|
|
815
841
|
fileName = _Object$entries$_i[0],
|
|
816
842
|
fileData = _Object$entries$_i[1];
|
|
817
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
843
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
818
844
|
remoteEntryFile = fileData.fileName;
|
|
819
845
|
}
|
|
820
846
|
if (fileData.type === 'chunk') {
|
|
@@ -1290,12 +1316,21 @@ var normalizeOptimizeDepsPlugin = {
|
|
|
1290
1316
|
|
|
1291
1317
|
function federation(mfUserOptions) {
|
|
1292
1318
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1293
|
-
initVirtualModules();
|
|
1294
1319
|
var name = options.name,
|
|
1295
1320
|
shared = options.shared,
|
|
1296
1321
|
filename = options.filename;
|
|
1297
1322
|
if (!name) throw new Error('name is required');
|
|
1298
|
-
return [
|
|
1323
|
+
return [{
|
|
1324
|
+
name: 'vite:module-federation-config',
|
|
1325
|
+
enforce: 'pre',
|
|
1326
|
+
configResolved: function configResolved(config) {
|
|
1327
|
+
// Set root path
|
|
1328
|
+
VirtualModule.setRoot(config.root);
|
|
1329
|
+
// Ensure virtual package directory exists
|
|
1330
|
+
VirtualModule.ensureVirtualPackageExists();
|
|
1331
|
+
initVirtualModules();
|
|
1332
|
+
}
|
|
1333
|
+
}, aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
|
|
1299
1334
|
entryName: 'remoteEntry',
|
|
1300
1335
|
entryPath: REMOTE_ENTRY_ID,
|
|
1301
1336
|
fileName: filename
|
package/lib/index.modern.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,
|
|
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
|
-
|
|
447
|
-
|
|
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
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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(
|
|
517
|
+
return resolve(getNodeModulesDir(), this.getImportId());
|
|
492
518
|
}
|
|
493
519
|
getImportId() {
|
|
494
520
|
const {
|
|
@@ -895,7 +921,7 @@ const Manifest = () => {
|
|
|
895
921
|
};
|
|
896
922
|
// 遍历打包生成的每个文件
|
|
897
923
|
for (const [fileName, fileData] of Object.entries(bundle)) {
|
|
898
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
924
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
899
925
|
remoteEntryFile = fileData.fileName;
|
|
900
926
|
}
|
|
901
927
|
if (fileData.type === 'chunk') {
|
|
@@ -1357,7 +1383,6 @@ var normalizeOptimizeDepsPlugin = {
|
|
|
1357
1383
|
|
|
1358
1384
|
function federation(mfUserOptions) {
|
|
1359
1385
|
const options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1360
|
-
initVirtualModules();
|
|
1361
1386
|
const {
|
|
1362
1387
|
name,
|
|
1363
1388
|
remotes,
|
|
@@ -1365,7 +1390,17 @@ function federation(mfUserOptions) {
|
|
|
1365
1390
|
filename
|
|
1366
1391
|
} = options;
|
|
1367
1392
|
if (!name) throw new Error('name is required');
|
|
1368
|
-
return [
|
|
1393
|
+
return [{
|
|
1394
|
+
name: 'vite:module-federation-config',
|
|
1395
|
+
enforce: 'pre',
|
|
1396
|
+
configResolved(config) {
|
|
1397
|
+
// Set root path
|
|
1398
|
+
VirtualModule.setRoot(config.root);
|
|
1399
|
+
// Ensure virtual package directory exists
|
|
1400
|
+
VirtualModule.ensureVirtualPackageExists();
|
|
1401
|
+
initVirtualModules();
|
|
1402
|
+
}
|
|
1403
|
+
}, aliasToArrayPlugin, normalizeOptimizeDepsPlugin, ...addEntry({
|
|
1369
1404
|
entryName: 'remoteEntry',
|
|
1370
1405
|
entryPath: REMOTE_ENTRY_ID,
|
|
1371
1406
|
fileName: filename
|
package/lib/index.umd.js
CHANGED
|
@@ -494,11 +494,13 @@
|
|
|
494
494
|
fs.writeFileSync(filePath, content);
|
|
495
495
|
}
|
|
496
496
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
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 =
|
|
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
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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(
|
|
581
|
+
return path.resolve(getNodeModulesDir(), this.getImportId());
|
|
556
582
|
};
|
|
557
583
|
_proto.getImportId = function getImportId() {
|
|
558
584
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
@@ -836,7 +862,7 @@
|
|
|
836
862
|
var _Object$entries$_i = _Object$entries[_i],
|
|
837
863
|
fileName = _Object$entries$_i[0],
|
|
838
864
|
fileData = _Object$entries$_i[1];
|
|
839
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
865
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
840
866
|
remoteEntryFile = fileData.fileName;
|
|
841
867
|
}
|
|
842
868
|
if (fileData.type === 'chunk') {
|
|
@@ -1312,12 +1338,21 @@
|
|
|
1312
1338
|
|
|
1313
1339
|
function federation(mfUserOptions) {
|
|
1314
1340
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1315
|
-
initVirtualModules();
|
|
1316
1341
|
var name = options.name,
|
|
1317
1342
|
shared = options.shared,
|
|
1318
1343
|
filename = options.filename;
|
|
1319
1344
|
if (!name) throw new Error('name is required');
|
|
1320
|
-
return [
|
|
1345
|
+
return [{
|
|
1346
|
+
name: 'vite:module-federation-config',
|
|
1347
|
+
enforce: 'pre',
|
|
1348
|
+
configResolved: function configResolved(config) {
|
|
1349
|
+
// Set root path
|
|
1350
|
+
VirtualModule.setRoot(config.root);
|
|
1351
|
+
// Ensure virtual package directory exists
|
|
1352
|
+
VirtualModule.ensureVirtualPackageExists();
|
|
1353
|
+
initVirtualModules();
|
|
1354
|
+
}
|
|
1355
|
+
}, aliasToArrayPlugin, normalizeOptimizeDepsPlugin].concat(addEntry({
|
|
1321
1356
|
entryName: 'remoteEntry',
|
|
1322
1357
|
entryPath: REMOTE_ENTRY_ID,
|
|
1323
1358
|
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;
|