@module-federation/vite 1.2.1 → 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 +82 -22
- package/lib/index.esm.js +83 -23
- package/lib/index.modern.js +81 -21
- package/lib/index.umd.js +82 -22
- package/lib/utils/VirtualModule.d.ts +9 -0
- package/package.json +17 -19
package/lib/index.cjs
CHANGED
|
@@ -330,12 +330,34 @@ function removePathFromNpmPackage(packageString) {
|
|
|
330
330
|
// 返回匹配到的包名,如果没有匹配到则返回原字符串
|
|
331
331
|
return match ? match[0] : packageString;
|
|
332
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Tries to find the package.json's version of a shared package
|
|
335
|
+
* if `package.json` is not declared in `exports`
|
|
336
|
+
* @param {string} sharedName
|
|
337
|
+
* @returns {string | undefined}
|
|
338
|
+
*/
|
|
339
|
+
function searchPackageVersion(sharedName) {
|
|
340
|
+
try {
|
|
341
|
+
var sharedPath = require.resolve(sharedName);
|
|
342
|
+
var potentialPackageJsonDir = path__namespace.dirname(sharedPath);
|
|
343
|
+
var rootDir = path__namespace.parse(potentialPackageJsonDir).root;
|
|
344
|
+
while (path__namespace.parse(potentialPackageJsonDir).base !== 'node_modules' && potentialPackageJsonDir !== rootDir) {
|
|
345
|
+
var potentialPackageJson = path__namespace.join(potentialPackageJsonDir, 'package.json');
|
|
346
|
+
if (fs__namespace.existsSync(potentialPackageJson)) {
|
|
347
|
+
return require(potentialPackageJson).version;
|
|
348
|
+
}
|
|
349
|
+
potentialPackageJsonDir = path__namespace.dirname(potentialPackageJsonDir);
|
|
350
|
+
}
|
|
351
|
+
} catch (_) {}
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
333
354
|
function normalizeShareItem(key, shareItem) {
|
|
334
355
|
var version;
|
|
335
356
|
try {
|
|
336
357
|
version = require(path__namespace.join(removePathFromNpmPackage(key), 'package.json')).version;
|
|
337
358
|
} catch (e) {
|
|
338
|
-
|
|
359
|
+
version = searchPackageVersion(key);
|
|
360
|
+
if (!version) console.error(e);
|
|
339
361
|
}
|
|
340
362
|
if (typeof shareItem === 'string') {
|
|
341
363
|
return {
|
|
@@ -474,11 +496,13 @@ function createFile(filePath, content) {
|
|
|
474
496
|
fs.writeFileSync(filePath, content);
|
|
475
497
|
}
|
|
476
498
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
499
|
+
// Cache root path
|
|
500
|
+
var rootDir;
|
|
501
|
+
function findNodeModulesDir(root) {
|
|
502
|
+
if (root === void 0) {
|
|
503
|
+
root = process.cwd();
|
|
480
504
|
}
|
|
481
|
-
var currentDir =
|
|
505
|
+
var currentDir = root;
|
|
482
506
|
while (currentDir !== path.parse(currentDir).root) {
|
|
483
507
|
var nodeModulesPath = path.join(currentDir, 'node_modules');
|
|
484
508
|
if (fs.existsSync(nodeModulesPath)) {
|
|
@@ -487,16 +511,16 @@ var nodeModulesDir = function findNodeModulesDir(startDir) {
|
|
|
487
511
|
currentDir = path.dirname(currentDir);
|
|
488
512
|
}
|
|
489
513
|
return '';
|
|
490
|
-
}();
|
|
491
|
-
var virtualPackageName = '__mf__virtual';
|
|
492
|
-
if (!fs.existsSync(path.resolve(nodeModulesDir, virtualPackageName))) {
|
|
493
|
-
fs.mkdirSync(path.resolve(nodeModulesDir, virtualPackageName));
|
|
494
514
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
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';
|
|
500
524
|
var patternMap = {};
|
|
501
525
|
var cacheMap = {};
|
|
502
526
|
/**
|
|
@@ -521,6 +545,30 @@ var VirtualModule = /*#__PURE__*/function () {
|
|
|
521
545
|
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
522
546
|
cacheMap[this.tag][this.name] = this;
|
|
523
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
|
+
};
|
|
524
572
|
VirtualModule.findModule = function findModule(tag, str) {
|
|
525
573
|
if (str === void 0) {
|
|
526
574
|
str = '';
|
|
@@ -532,7 +580,7 @@ var VirtualModule = /*#__PURE__*/function () {
|
|
|
532
580
|
};
|
|
533
581
|
var _proto = VirtualModule.prototype;
|
|
534
582
|
_proto.getPath = function getPath() {
|
|
535
|
-
return path.resolve(
|
|
583
|
+
return path.resolve(getNodeModulesDir(), this.getImportId());
|
|
536
584
|
};
|
|
537
585
|
_proto.getImportId = function getImportId() {
|
|
538
586
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
@@ -728,7 +776,7 @@ var Manifest = function Manifest() {
|
|
|
728
776
|
res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
|
|
729
777
|
id: name,
|
|
730
778
|
name: name,
|
|
731
|
-
metaData: {
|
|
779
|
+
metaData: _extends({
|
|
732
780
|
name: name,
|
|
733
781
|
type: 'app',
|
|
734
782
|
buildInfo: {
|
|
@@ -750,9 +798,12 @@ var Manifest = function Manifest() {
|
|
|
750
798
|
name: ''
|
|
751
799
|
},
|
|
752
800
|
globalName: name,
|
|
753
|
-
pluginVersion: '0.2.5'
|
|
801
|
+
pluginVersion: '0.2.5'
|
|
802
|
+
}, !!getPublicPath ? {
|
|
803
|
+
publicPath: getPublicPath
|
|
804
|
+
} : {
|
|
754
805
|
publicPath: publicPath
|
|
755
|
-
}
|
|
806
|
+
})
|
|
756
807
|
})));
|
|
757
808
|
} else {
|
|
758
809
|
next();
|
|
@@ -816,7 +867,7 @@ var Manifest = function Manifest() {
|
|
|
816
867
|
var _Object$entries$_i = _Object$entries[_i],
|
|
817
868
|
fileName = _Object$entries$_i[0],
|
|
818
869
|
fileData = _Object$entries$_i[1];
|
|
819
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
870
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
820
871
|
remoteEntryFile = fileData.fileName;
|
|
821
872
|
}
|
|
822
873
|
if (fileData.type === 'chunk') {
|
|
@@ -987,7 +1038,7 @@ var Manifest = function Manifest() {
|
|
|
987
1038
|
globalName: name,
|
|
988
1039
|
pluginVersion: '0.2.5'
|
|
989
1040
|
}, !!getPublicPath ? {
|
|
990
|
-
|
|
1041
|
+
publicPath: getPublicPath
|
|
991
1042
|
} : {
|
|
992
1043
|
publicPath: publicPath
|
|
993
1044
|
}),
|
|
@@ -1292,12 +1343,21 @@ var normalizeOptimizeDepsPlugin = {
|
|
|
1292
1343
|
|
|
1293
1344
|
function federation(mfUserOptions) {
|
|
1294
1345
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1295
|
-
initVirtualModules();
|
|
1296
1346
|
var name = options.name,
|
|
1297
1347
|
shared = options.shared,
|
|
1298
1348
|
filename = options.filename;
|
|
1299
1349
|
if (!name) throw new Error('name is required');
|
|
1300
|
-
return [
|
|
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({
|
|
1301
1361
|
entryName: 'remoteEntry',
|
|
1302
1362
|
entryPath: REMOTE_ENTRY_ID,
|
|
1303
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,
|
|
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';
|
|
@@ -306,12 +306,34 @@ function removePathFromNpmPackage(packageString) {
|
|
|
306
306
|
// 返回匹配到的包名,如果没有匹配到则返回原字符串
|
|
307
307
|
return match ? match[0] : packageString;
|
|
308
308
|
}
|
|
309
|
+
/**
|
|
310
|
+
* Tries to find the package.json's version of a shared package
|
|
311
|
+
* if `package.json` is not declared in `exports`
|
|
312
|
+
* @param {string} sharedName
|
|
313
|
+
* @returns {string | undefined}
|
|
314
|
+
*/
|
|
315
|
+
function searchPackageVersion(sharedName) {
|
|
316
|
+
try {
|
|
317
|
+
var sharedPath = require.resolve(sharedName);
|
|
318
|
+
var potentialPackageJsonDir = path.dirname(sharedPath);
|
|
319
|
+
var rootDir = path.parse(potentialPackageJsonDir).root;
|
|
320
|
+
while (path.parse(potentialPackageJsonDir).base !== 'node_modules' && potentialPackageJsonDir !== rootDir) {
|
|
321
|
+
var potentialPackageJson = path.join(potentialPackageJsonDir, 'package.json');
|
|
322
|
+
if (fs.existsSync(potentialPackageJson)) {
|
|
323
|
+
return require(potentialPackageJson).version;
|
|
324
|
+
}
|
|
325
|
+
potentialPackageJsonDir = path.dirname(potentialPackageJsonDir);
|
|
326
|
+
}
|
|
327
|
+
} catch (_) {}
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
309
330
|
function normalizeShareItem(key, shareItem) {
|
|
310
331
|
var version;
|
|
311
332
|
try {
|
|
312
333
|
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
|
|
313
334
|
} catch (e) {
|
|
314
|
-
|
|
335
|
+
version = searchPackageVersion(key);
|
|
336
|
+
if (!version) console.error(e);
|
|
315
337
|
}
|
|
316
338
|
if (typeof shareItem === 'string') {
|
|
317
339
|
return {
|
|
@@ -450,11 +472,13 @@ function createFile(filePath, content) {
|
|
|
450
472
|
writeFileSync(filePath, content);
|
|
451
473
|
}
|
|
452
474
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
475
|
+
// Cache root path
|
|
476
|
+
var rootDir;
|
|
477
|
+
function findNodeModulesDir(root) {
|
|
478
|
+
if (root === void 0) {
|
|
479
|
+
root = process.cwd();
|
|
456
480
|
}
|
|
457
|
-
var currentDir =
|
|
481
|
+
var currentDir = root;
|
|
458
482
|
while (currentDir !== parse(currentDir).root) {
|
|
459
483
|
var nodeModulesPath = join(currentDir, 'node_modules');
|
|
460
484
|
if (existsSync(nodeModulesPath)) {
|
|
@@ -463,16 +487,16 @@ var nodeModulesDir = function findNodeModulesDir(startDir) {
|
|
|
463
487
|
currentDir = dirname(currentDir);
|
|
464
488
|
}
|
|
465
489
|
return '';
|
|
466
|
-
}();
|
|
467
|
-
var virtualPackageName = '__mf__virtual';
|
|
468
|
-
if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
|
|
469
|
-
mkdirSync(resolve(nodeModulesDir, virtualPackageName));
|
|
470
490
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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';
|
|
476
500
|
var patternMap = {};
|
|
477
501
|
var cacheMap = {};
|
|
478
502
|
/**
|
|
@@ -497,6 +521,30 @@ var VirtualModule = /*#__PURE__*/function () {
|
|
|
497
521
|
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
498
522
|
cacheMap[this.tag][this.name] = this;
|
|
499
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
|
+
};
|
|
500
548
|
VirtualModule.findModule = function findModule(tag, str) {
|
|
501
549
|
if (str === void 0) {
|
|
502
550
|
str = '';
|
|
@@ -508,7 +556,7 @@ var VirtualModule = /*#__PURE__*/function () {
|
|
|
508
556
|
};
|
|
509
557
|
var _proto = VirtualModule.prototype;
|
|
510
558
|
_proto.getPath = function getPath() {
|
|
511
|
-
return resolve(
|
|
559
|
+
return resolve(getNodeModulesDir(), this.getImportId());
|
|
512
560
|
};
|
|
513
561
|
_proto.getImportId = function getImportId() {
|
|
514
562
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
@@ -704,7 +752,7 @@ var Manifest = function Manifest() {
|
|
|
704
752
|
res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
|
|
705
753
|
id: name,
|
|
706
754
|
name: name,
|
|
707
|
-
metaData: {
|
|
755
|
+
metaData: _extends({
|
|
708
756
|
name: name,
|
|
709
757
|
type: 'app',
|
|
710
758
|
buildInfo: {
|
|
@@ -726,9 +774,12 @@ var Manifest = function Manifest() {
|
|
|
726
774
|
name: ''
|
|
727
775
|
},
|
|
728
776
|
globalName: name,
|
|
729
|
-
pluginVersion: '0.2.5'
|
|
777
|
+
pluginVersion: '0.2.5'
|
|
778
|
+
}, !!getPublicPath ? {
|
|
779
|
+
publicPath: getPublicPath
|
|
780
|
+
} : {
|
|
730
781
|
publicPath: publicPath
|
|
731
|
-
}
|
|
782
|
+
})
|
|
732
783
|
})));
|
|
733
784
|
} else {
|
|
734
785
|
next();
|
|
@@ -792,7 +843,7 @@ var Manifest = function Manifest() {
|
|
|
792
843
|
var _Object$entries$_i = _Object$entries[_i],
|
|
793
844
|
fileName = _Object$entries$_i[0],
|
|
794
845
|
fileData = _Object$entries$_i[1];
|
|
795
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
846
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
796
847
|
remoteEntryFile = fileData.fileName;
|
|
797
848
|
}
|
|
798
849
|
if (fileData.type === 'chunk') {
|
|
@@ -963,7 +1014,7 @@ var Manifest = function Manifest() {
|
|
|
963
1014
|
globalName: name,
|
|
964
1015
|
pluginVersion: '0.2.5'
|
|
965
1016
|
}, !!getPublicPath ? {
|
|
966
|
-
|
|
1017
|
+
publicPath: getPublicPath
|
|
967
1018
|
} : {
|
|
968
1019
|
publicPath: publicPath
|
|
969
1020
|
}),
|
|
@@ -1268,12 +1319,21 @@ var normalizeOptimizeDepsPlugin = {
|
|
|
1268
1319
|
|
|
1269
1320
|
function federation(mfUserOptions) {
|
|
1270
1321
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1271
|
-
initVirtualModules();
|
|
1272
1322
|
var name = options.name,
|
|
1273
1323
|
shared = options.shared,
|
|
1274
1324
|
filename = options.filename;
|
|
1275
1325
|
if (!name) throw new Error('name is required');
|
|
1276
|
-
return [
|
|
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({
|
|
1277
1337
|
entryName: 'remoteEntry',
|
|
1278
1338
|
entryPath: REMOTE_ENTRY_ID,
|
|
1279
1339
|
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';
|
|
@@ -279,12 +279,34 @@ function removePathFromNpmPackage(packageString) {
|
|
|
279
279
|
// 返回匹配到的包名,如果没有匹配到则返回原字符串
|
|
280
280
|
return match ? match[0] : packageString;
|
|
281
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Tries to find the package.json's version of a shared package
|
|
284
|
+
* if `package.json` is not declared in `exports`
|
|
285
|
+
* @param {string} sharedName
|
|
286
|
+
* @returns {string | undefined}
|
|
287
|
+
*/
|
|
288
|
+
function searchPackageVersion(sharedName) {
|
|
289
|
+
try {
|
|
290
|
+
const sharedPath = require.resolve(sharedName);
|
|
291
|
+
let potentialPackageJsonDir = path.dirname(sharedPath);
|
|
292
|
+
const rootDir = path.parse(potentialPackageJsonDir).root;
|
|
293
|
+
while (path.parse(potentialPackageJsonDir).base !== 'node_modules' && potentialPackageJsonDir !== rootDir) {
|
|
294
|
+
const potentialPackageJson = path.join(potentialPackageJsonDir, 'package.json');
|
|
295
|
+
if (fs.existsSync(potentialPackageJson)) {
|
|
296
|
+
return require(potentialPackageJson).version;
|
|
297
|
+
}
|
|
298
|
+
potentialPackageJsonDir = path.dirname(potentialPackageJsonDir);
|
|
299
|
+
}
|
|
300
|
+
} catch (_) {}
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
282
303
|
function normalizeShareItem(key, shareItem) {
|
|
283
304
|
let version;
|
|
284
305
|
try {
|
|
285
306
|
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
|
|
286
307
|
} catch (e) {
|
|
287
|
-
|
|
308
|
+
version = searchPackageVersion(key);
|
|
309
|
+
if (!version) console.error(e);
|
|
288
310
|
}
|
|
289
311
|
if (typeof shareItem === 'string') {
|
|
290
312
|
return {
|
|
@@ -421,8 +443,10 @@ function createFile(filePath, content) {
|
|
|
421
443
|
writeFileSync(filePath, content);
|
|
422
444
|
}
|
|
423
445
|
|
|
424
|
-
|
|
425
|
-
|
|
446
|
+
// Cache root path
|
|
447
|
+
let rootDir;
|
|
448
|
+
function findNodeModulesDir(root = process.cwd()) {
|
|
449
|
+
let currentDir = root;
|
|
426
450
|
while (currentDir !== parse(currentDir).root) {
|
|
427
451
|
const nodeModulesPath = join(currentDir, 'node_modules');
|
|
428
452
|
if (existsSync(nodeModulesPath)) {
|
|
@@ -431,22 +455,46 @@ const nodeModulesDir = function findNodeModulesDir(startDir = process.cwd()) {
|
|
|
431
455
|
currentDir = dirname(currentDir);
|
|
432
456
|
}
|
|
433
457
|
return '';
|
|
434
|
-
}();
|
|
435
|
-
const virtualPackageName = '__mf__virtual';
|
|
436
|
-
if (!existsSync(resolve(nodeModulesDir, virtualPackageName))) {
|
|
437
|
-
mkdirSync(resolve(nodeModulesDir, virtualPackageName));
|
|
438
458
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
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';
|
|
444
468
|
const patternMap = {};
|
|
445
469
|
const cacheMap = {};
|
|
446
470
|
/**
|
|
447
471
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
448
472
|
*/
|
|
449
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
|
+
}
|
|
450
498
|
static findModule(tag, str = '') {
|
|
451
499
|
if (!patternMap[tag]) patternMap[tag] = new RegExp(`(.*${packageNameEncode(tag)}(.+?)${packageNameEncode(tag)}.*)`);
|
|
452
500
|
const moduleName = (str.match(patternMap[tag]) || [])[2];
|
|
@@ -466,7 +514,7 @@ class VirtualModule {
|
|
|
466
514
|
cacheMap[this.tag][this.name] = this;
|
|
467
515
|
}
|
|
468
516
|
getPath() {
|
|
469
|
-
return resolve(
|
|
517
|
+
return resolve(getNodeModulesDir(), this.getImportId());
|
|
470
518
|
}
|
|
471
519
|
getImportId() {
|
|
472
520
|
const {
|
|
@@ -805,7 +853,7 @@ const Manifest = () => {
|
|
|
805
853
|
res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
|
|
806
854
|
id: name,
|
|
807
855
|
name: name,
|
|
808
|
-
metaData: {
|
|
856
|
+
metaData: _extends({
|
|
809
857
|
name: name,
|
|
810
858
|
type: 'app',
|
|
811
859
|
buildInfo: {
|
|
@@ -827,9 +875,12 @@ const Manifest = () => {
|
|
|
827
875
|
name: ''
|
|
828
876
|
},
|
|
829
877
|
globalName: name,
|
|
830
|
-
pluginVersion: '0.2.5'
|
|
878
|
+
pluginVersion: '0.2.5'
|
|
879
|
+
}, !!getPublicPath ? {
|
|
880
|
+
publicPath: getPublicPath
|
|
881
|
+
} : {
|
|
831
882
|
publicPath
|
|
832
|
-
}
|
|
883
|
+
})
|
|
833
884
|
})));
|
|
834
885
|
} else {
|
|
835
886
|
next();
|
|
@@ -873,7 +924,7 @@ const Manifest = () => {
|
|
|
873
924
|
};
|
|
874
925
|
// 遍历打包生成的每个文件
|
|
875
926
|
for (const [fileName, fileData] of Object.entries(bundle)) {
|
|
876
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
927
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
877
928
|
remoteEntryFile = fileData.fileName;
|
|
878
929
|
}
|
|
879
930
|
if (fileData.type === 'chunk') {
|
|
@@ -1037,7 +1088,7 @@ const Manifest = () => {
|
|
|
1037
1088
|
globalName: name,
|
|
1038
1089
|
pluginVersion: '0.2.5'
|
|
1039
1090
|
}, !!getPublicPath ? {
|
|
1040
|
-
getPublicPath
|
|
1091
|
+
publicPath: getPublicPath
|
|
1041
1092
|
} : {
|
|
1042
1093
|
publicPath
|
|
1043
1094
|
}),
|
|
@@ -1335,7 +1386,6 @@ var normalizeOptimizeDepsPlugin = {
|
|
|
1335
1386
|
|
|
1336
1387
|
function federation(mfUserOptions) {
|
|
1337
1388
|
const options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1338
|
-
initVirtualModules();
|
|
1339
1389
|
const {
|
|
1340
1390
|
name,
|
|
1341
1391
|
remotes,
|
|
@@ -1343,7 +1393,17 @@ function federation(mfUserOptions) {
|
|
|
1343
1393
|
filename
|
|
1344
1394
|
} = options;
|
|
1345
1395
|
if (!name) throw new Error('name is required');
|
|
1346
|
-
return [
|
|
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({
|
|
1347
1407
|
entryName: 'remoteEntry',
|
|
1348
1408
|
entryPath: REMOTE_ENTRY_ID,
|
|
1349
1409
|
fileName: filename
|
package/lib/index.umd.js
CHANGED
|
@@ -328,12 +328,34 @@
|
|
|
328
328
|
// 返回匹配到的包名,如果没有匹配到则返回原字符串
|
|
329
329
|
return match ? match[0] : packageString;
|
|
330
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Tries to find the package.json's version of a shared package
|
|
333
|
+
* if `package.json` is not declared in `exports`
|
|
334
|
+
* @param {string} sharedName
|
|
335
|
+
* @returns {string | undefined}
|
|
336
|
+
*/
|
|
337
|
+
function searchPackageVersion(sharedName) {
|
|
338
|
+
try {
|
|
339
|
+
var sharedPath = require.resolve(sharedName);
|
|
340
|
+
var potentialPackageJsonDir = path__namespace.dirname(sharedPath);
|
|
341
|
+
var rootDir = path__namespace.parse(potentialPackageJsonDir).root;
|
|
342
|
+
while (path__namespace.parse(potentialPackageJsonDir).base !== 'node_modules' && potentialPackageJsonDir !== rootDir) {
|
|
343
|
+
var potentialPackageJson = path__namespace.join(potentialPackageJsonDir, 'package.json');
|
|
344
|
+
if (fs__namespace.existsSync(potentialPackageJson)) {
|
|
345
|
+
return require(potentialPackageJson).version;
|
|
346
|
+
}
|
|
347
|
+
potentialPackageJsonDir = path__namespace.dirname(potentialPackageJsonDir);
|
|
348
|
+
}
|
|
349
|
+
} catch (_) {}
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
331
352
|
function normalizeShareItem(key, shareItem) {
|
|
332
353
|
var version;
|
|
333
354
|
try {
|
|
334
355
|
version = require(path__namespace.join(removePathFromNpmPackage(key), 'package.json')).version;
|
|
335
356
|
} catch (e) {
|
|
336
|
-
|
|
357
|
+
version = searchPackageVersion(key);
|
|
358
|
+
if (!version) console.error(e);
|
|
337
359
|
}
|
|
338
360
|
if (typeof shareItem === 'string') {
|
|
339
361
|
return {
|
|
@@ -472,11 +494,13 @@
|
|
|
472
494
|
fs.writeFileSync(filePath, content);
|
|
473
495
|
}
|
|
474
496
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
497
|
+
// Cache root path
|
|
498
|
+
var rootDir;
|
|
499
|
+
function findNodeModulesDir(root) {
|
|
500
|
+
if (root === void 0) {
|
|
501
|
+
root = process.cwd();
|
|
478
502
|
}
|
|
479
|
-
var currentDir =
|
|
503
|
+
var currentDir = root;
|
|
480
504
|
while (currentDir !== path.parse(currentDir).root) {
|
|
481
505
|
var nodeModulesPath = path.join(currentDir, 'node_modules');
|
|
482
506
|
if (fs.existsSync(nodeModulesPath)) {
|
|
@@ -485,16 +509,16 @@
|
|
|
485
509
|
currentDir = path.dirname(currentDir);
|
|
486
510
|
}
|
|
487
511
|
return '';
|
|
488
|
-
}();
|
|
489
|
-
var virtualPackageName = '__mf__virtual';
|
|
490
|
-
if (!fs.existsSync(path.resolve(nodeModulesDir, virtualPackageName))) {
|
|
491
|
-
fs.mkdirSync(path.resolve(nodeModulesDir, virtualPackageName));
|
|
492
512
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
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';
|
|
498
522
|
var patternMap = {};
|
|
499
523
|
var cacheMap = {};
|
|
500
524
|
/**
|
|
@@ -519,6 +543,30 @@
|
|
|
519
543
|
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
520
544
|
cacheMap[this.tag][this.name] = this;
|
|
521
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
|
+
};
|
|
522
570
|
VirtualModule.findModule = function findModule(tag, str) {
|
|
523
571
|
if (str === void 0) {
|
|
524
572
|
str = '';
|
|
@@ -530,7 +578,7 @@
|
|
|
530
578
|
};
|
|
531
579
|
var _proto = VirtualModule.prototype;
|
|
532
580
|
_proto.getPath = function getPath() {
|
|
533
|
-
return path.resolve(
|
|
581
|
+
return path.resolve(getNodeModulesDir(), this.getImportId());
|
|
534
582
|
};
|
|
535
583
|
_proto.getImportId = function getImportId() {
|
|
536
584
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
@@ -726,7 +774,7 @@
|
|
|
726
774
|
res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
|
|
727
775
|
id: name,
|
|
728
776
|
name: name,
|
|
729
|
-
metaData: {
|
|
777
|
+
metaData: _extends({
|
|
730
778
|
name: name,
|
|
731
779
|
type: 'app',
|
|
732
780
|
buildInfo: {
|
|
@@ -748,9 +796,12 @@
|
|
|
748
796
|
name: ''
|
|
749
797
|
},
|
|
750
798
|
globalName: name,
|
|
751
|
-
pluginVersion: '0.2.5'
|
|
799
|
+
pluginVersion: '0.2.5'
|
|
800
|
+
}, !!getPublicPath ? {
|
|
801
|
+
publicPath: getPublicPath
|
|
802
|
+
} : {
|
|
752
803
|
publicPath: publicPath
|
|
753
|
-
}
|
|
804
|
+
})
|
|
754
805
|
})));
|
|
755
806
|
} else {
|
|
756
807
|
next();
|
|
@@ -814,7 +865,7 @@
|
|
|
814
865
|
var _Object$entries$_i = _Object$entries[_i],
|
|
815
866
|
fileName = _Object$entries$_i[0],
|
|
816
867
|
fileData = _Object$entries$_i[1];
|
|
817
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_') === fileData.name || fileData.name === 'remoteEntry') {
|
|
868
|
+
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
818
869
|
remoteEntryFile = fileData.fileName;
|
|
819
870
|
}
|
|
820
871
|
if (fileData.type === 'chunk') {
|
|
@@ -985,7 +1036,7 @@
|
|
|
985
1036
|
globalName: name,
|
|
986
1037
|
pluginVersion: '0.2.5'
|
|
987
1038
|
}, !!getPublicPath ? {
|
|
988
|
-
|
|
1039
|
+
publicPath: getPublicPath
|
|
989
1040
|
} : {
|
|
990
1041
|
publicPath: publicPath
|
|
991
1042
|
}),
|
|
@@ -1290,12 +1341,21 @@
|
|
|
1290
1341
|
|
|
1291
1342
|
function federation(mfUserOptions) {
|
|
1292
1343
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1293
|
-
initVirtualModules();
|
|
1294
1344
|
var name = options.name,
|
|
1295
1345
|
shared = options.shared,
|
|
1296
1346
|
filename = options.filename;
|
|
1297
1347
|
if (!name) throw new Error('name is required');
|
|
1298
|
-
return [
|
|
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({
|
|
1299
1359
|
entryName: 'remoteEntry',
|
|
1300
1360
|
entryPath: REMOTE_ENTRY_ID,
|
|
1301
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.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Vite plugin for Module Federation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -9,22 +9,6 @@
|
|
|
9
9
|
"files": [
|
|
10
10
|
"lib/**/*"
|
|
11
11
|
],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"prepare": "husky install",
|
|
14
|
-
"fmt": "prettier --write src",
|
|
15
|
-
"fmt.check": "prettier --check src",
|
|
16
|
-
"format": "pretty-quick",
|
|
17
|
-
"dev": "microbundle watch --no-sourcemap --compress=false",
|
|
18
|
-
"build": "rimraf lib && microbundle --no-sourcemap --compress=false",
|
|
19
|
-
"dev-rv": "pnpm -filter 'examples-rust-vite*' run dev",
|
|
20
|
-
"preview-rv": "pnpm -filter 'examples-rust-vite*' run preview",
|
|
21
|
-
"dev-vv": "pnpm -filter 'examples-vite-vite*' run dev",
|
|
22
|
-
"dev-nv": "pnpm -filter 'examples-nuxt-vite-host' -filter 'examples-vite-vite-remote' run dev",
|
|
23
|
-
"preview-vv": "pnpm -filter 'examples-vite-vite*' run preview",
|
|
24
|
-
"multi-example": "pnpm --filter \"multi-example-*\" --parallel run start",
|
|
25
|
-
"test": "vitest",
|
|
26
|
-
"e2e": "playwright test"
|
|
27
|
-
},
|
|
28
12
|
"repository": {
|
|
29
13
|
"type": "git",
|
|
30
14
|
"url": "git+https://github.com/module-federation/vite.git"
|
|
@@ -44,7 +28,6 @@
|
|
|
44
28
|
"url": "https://github.com/module-federation/vite/issues"
|
|
45
29
|
},
|
|
46
30
|
"homepage": "https://github.com/module-federation/vite#readme",
|
|
47
|
-
"packageManager": "pnpm@9.1.3",
|
|
48
31
|
"dependencies": {
|
|
49
32
|
"@module-federation/runtime": "^0.8.0",
|
|
50
33
|
"@rollup/pluginutils": "^5.1.0",
|
|
@@ -65,5 +48,20 @@
|
|
|
65
48
|
"vite": "^5.4.3",
|
|
66
49
|
"vitest": "^2.1.1",
|
|
67
50
|
"wait-on": "^8.0.1"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"fmt": "prettier --write src",
|
|
54
|
+
"fmt.check": "prettier --check src",
|
|
55
|
+
"format": "pretty-quick",
|
|
56
|
+
"dev": "microbundle watch --no-sourcemap --compress=false",
|
|
57
|
+
"build": "rimraf lib && microbundle --no-sourcemap --compress=false",
|
|
58
|
+
"dev-rv": "pnpm -filter 'examples-rust-vite*' run dev",
|
|
59
|
+
"preview-rv": "pnpm -filter 'examples-rust-vite*' run preview",
|
|
60
|
+
"dev-vv": "pnpm -filter 'examples-vite-vite*' run dev",
|
|
61
|
+
"dev-nv": "pnpm -filter 'examples-nuxt-vite-host' -filter 'examples-vite-vite-remote' run dev",
|
|
62
|
+
"preview-vv": "pnpm -filter 'examples-vite-vite*' run preview",
|
|
63
|
+
"multi-example": "pnpm --filter \"multi-example-*\" --parallel run start",
|
|
64
|
+
"test": "vitest",
|
|
65
|
+
"e2e": "playwright test"
|
|
68
66
|
}
|
|
69
|
-
}
|
|
67
|
+
}
|