@module-federation/vite 1.8.0 → 1.9.0
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/README.md +10 -0
- package/lib/index.cjs +62 -29
- package/lib/index.esm.js +62 -29
- package/lib/index.modern.js +78 -37
- package/lib/index.umd.js +65 -32
- package/lib/plugins/pluginAddEntry.d.ts +2 -1
- package/lib/utils/VirtualModule.d.ts +1 -0
- package/lib/utils/mapCodeToCodeWithSourcemap.d.ts +4 -0
- package/lib/utils/normalizeModuleFederationOptions.d.ts +18 -1
- package/package.json +21 -18
package/README.md
CHANGED
|
@@ -109,6 +109,15 @@ export default defineConfig({
|
|
|
109
109
|
},
|
|
110
110
|
filename: "remoteEntry.js",
|
|
111
111
|
shared: ["vue"],
|
|
112
|
+
// Optional parameter that controls where the host initialization script is injected.
|
|
113
|
+
// By default, it is injected into the index.html file.
|
|
114
|
+
// You can set this to "entry" to inject it into the entry script instead.
|
|
115
|
+
// Useful if your application does not load from index.html.
|
|
116
|
+
hostInitInjectLocation: "html", // or "entry"
|
|
117
|
+
// Controls whether all CSS assets from the bundle should be added to every exposed module.
|
|
118
|
+
// When false (default), the plugin will not process any CSS assets.
|
|
119
|
+
// When true, all CSS assets are bundled into every exposed module.
|
|
120
|
+
bundleAllCSS: false, // or true
|
|
112
121
|
}),
|
|
113
122
|
],
|
|
114
123
|
server: {
|
|
@@ -124,6 +133,7 @@ export default defineConfig({
|
|
|
124
133
|
```
|
|
125
134
|
|
|
126
135
|
The host app configuration specifies its name, the filename of its exposed remote entry remoteEntry.js, and importantly, the configuration of the remote application to load.
|
|
136
|
+
You can specify the place the host initialization file is injected with the **hostInitInjectLocation** option, which is described in the example code above.
|
|
127
137
|
|
|
128
138
|
## Load the Remote App
|
|
129
139
|
|
package/lib/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
var defu = require('defu');
|
|
2
2
|
var fs = require('fs');
|
|
3
3
|
var path = require('pathe');
|
|
4
|
+
var MagicString = require('magic-string');
|
|
4
5
|
var pluginutils = require('@rollup/pluginutils');
|
|
5
6
|
var estreeWalker = require('estree-walker');
|
|
6
|
-
var MagicString = require('magic-string');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
9
|
|
|
@@ -31,6 +31,21 @@ var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
|
31
31
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
32
32
|
var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
33
33
|
|
|
34
|
+
var mapCodeToCodeWithSourcemap = function mapCodeToCodeWithSourcemap(code) {
|
|
35
|
+
return Promise.resolve(code).then(function (resolvedCode) {
|
|
36
|
+
if (resolvedCode === undefined) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
var s = new MagicString__default["default"](resolvedCode);
|
|
40
|
+
return {
|
|
41
|
+
code: s.toString(),
|
|
42
|
+
map: s.generateMap({
|
|
43
|
+
hires: true
|
|
44
|
+
})
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
34
49
|
function getFirstHtmlEntryFile(entryFiles) {
|
|
35
50
|
return entryFiles.find(function (file) {
|
|
36
51
|
return file.endsWith('.html');
|
|
@@ -150,7 +165,7 @@ var addEntry = function addEntry(_ref) {
|
|
|
150
165
|
return id.endsWith(file);
|
|
151
166
|
})) {
|
|
152
167
|
var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
|
|
153
|
-
return injection + code;
|
|
168
|
+
return mapCodeToCodeWithSourcemap(injection + code);
|
|
154
169
|
}
|
|
155
170
|
}
|
|
156
171
|
}];
|
|
@@ -383,6 +398,7 @@ function normalizeShareItem(key, shareItem) {
|
|
|
383
398
|
scope: 'default',
|
|
384
399
|
from: '',
|
|
385
400
|
shareConfig: {
|
|
401
|
+
"import": undefined,
|
|
386
402
|
singleton: false,
|
|
387
403
|
requiredVersion: version ? "^" + version : '*'
|
|
388
404
|
}
|
|
@@ -394,6 +410,7 @@ function normalizeShareItem(key, shareItem) {
|
|
|
394
410
|
version: shareItem.version || version,
|
|
395
411
|
scope: shareItem.shareScope || 'default',
|
|
396
412
|
shareConfig: {
|
|
413
|
+
"import": typeof shareItem === 'object' ? shareItem["import"] : undefined,
|
|
397
414
|
singleton: shareItem.singleton || false,
|
|
398
415
|
requiredVersion: shareItem.requiredVersion || (version ? "^" + version : '*'),
|
|
399
416
|
strictVersion: !!shareItem.strictVersion
|
|
@@ -468,7 +485,9 @@ function normalizeModuleFederationOptions(options) {
|
|
|
468
485
|
publicPath: options.publicPath,
|
|
469
486
|
shareStrategy: options.shareStrategy || 'version-first',
|
|
470
487
|
ignoreOrigin: options.ignoreOrigin || false,
|
|
471
|
-
virtualModuleDir: options.virtualModuleDir || '__mf__virtual'
|
|
488
|
+
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
|
|
489
|
+
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
|
|
490
|
+
bundleAllCSS: options.bundleAllCSS || false
|
|
472
491
|
};
|
|
473
492
|
}
|
|
474
493
|
|
|
@@ -556,6 +575,16 @@ var cacheMap = {};
|
|
|
556
575
|
/**
|
|
557
576
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
558
577
|
*/
|
|
578
|
+
function assertModuleFound(tag, str) {
|
|
579
|
+
if (str === void 0) {
|
|
580
|
+
str = '';
|
|
581
|
+
}
|
|
582
|
+
var module = VirtualModule.findModule(tag, str);
|
|
583
|
+
if (!module) {
|
|
584
|
+
throw new Error("Module Federation shared module '" + str + "' not found. Please ensure it's installed as a dependency in your package.json.");
|
|
585
|
+
}
|
|
586
|
+
return module;
|
|
587
|
+
}
|
|
559
588
|
var VirtualModule = /*#__PURE__*/function () {
|
|
560
589
|
function VirtualModule(name, tag, suffix) {
|
|
561
590
|
if (tag === void 0) {
|
|
@@ -695,7 +724,7 @@ function getLoadShareModulePath(pkg) {
|
|
|
695
724
|
return filepath;
|
|
696
725
|
}
|
|
697
726
|
function writeLoadShareModule(pkg, shareItem, command) {
|
|
698
|
-
loadShareCacheMap[pkg].writeSync("\n
|
|
727
|
+
loadShareCacheMap[pkg].writeSync("\n\n ;() => import(" + JSON.stringify(getPreBuildLibImportId(pkg)) + ").catch(() => {});\n // dev uses dynamic import to separate chunks\n " + (command !== 'build' ? ";() => import(" + JSON.stringify(pkg) + ").catch(() => {});" : '') + "\n const {loadShare} = require(\"@module-federation/runtime\")\n const {initPromise} = require(\"" + virtualRuntimeInitStatus.getImportId() + "\")\n const res = initPromise.then(_ => loadShare(" + JSON.stringify(pkg) + ", {\n customShareInfo: {shareConfig:{\n singleton: " + shareItem.shareConfig.singleton + ",\n strictVersion: " + shareItem.shareConfig.strictVersion + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }}}))\n const exportModule = " + (command !== 'build' ? '/*mf top-level-await placeholder replacement mf*/' : 'await ') + "res.then(factory => factory())\n module.exports = exportModule\n ");
|
|
699
728
|
}
|
|
700
729
|
|
|
701
730
|
var usedShares = new Set();
|
|
@@ -722,12 +751,13 @@ function writeLocalSharedImportMap() {
|
|
|
722
751
|
}
|
|
723
752
|
function generateLocalSharedImportMap() {
|
|
724
753
|
var options = getNormalizeModuleFederationOptions();
|
|
725
|
-
return "\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
726
|
-
|
|
754
|
+
return "\n import {loadShare} from \"@module-federation/runtime\";\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
755
|
+
var shareItem = getNormalizeShareItem(pkg);
|
|
756
|
+
return "\n " + JSON.stringify(pkg) + ": async () => {\n " + ((shareItem == null ? void 0 : shareItem.shareConfig["import"]) === false ? "throw new Error(`Shared module '${" + JSON.stringify(pkg) + "}' must be provided by host`);" : "let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\");\n return pkg;") + "\n }\n ";
|
|
727
757
|
}).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).sort().map(function (key) {
|
|
728
758
|
var shareItem = getNormalizeShareItem(key);
|
|
729
759
|
if (!shareItem) return null;
|
|
730
|
-
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap
|
|
760
|
+
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n if (" + (shareItem.shareConfig["import"] === false) + ") {\n throw new Error(`Shared module '${" + JSON.stringify(key) + "}' must be provided by host`);\n }\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap\n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + ",\n " + (shareItem.shareConfig["import"] === false ? 'import: false,' : '') + "\n }\n }\n ";
|
|
731
761
|
}).filter(function (x) {
|
|
732
762
|
return x !== null;
|
|
733
763
|
}).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
|
|
@@ -757,7 +787,7 @@ function generateRemoteEntry(options) {
|
|
|
757
787
|
var HOST_AUTO_INIT_TAG = '__H_A_I__';
|
|
758
788
|
var hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG);
|
|
759
789
|
function writeHostAutoInit() {
|
|
760
|
-
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await
|
|
790
|
+
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ");
|
|
761
791
|
}
|
|
762
792
|
function getHostAutoInitImportId() {
|
|
763
793
|
return hostAutoInitModule.getImportId();
|
|
@@ -1082,7 +1112,7 @@ var Manifest = function Manifest() {
|
|
|
1082
1112
|
}
|
|
1083
1113
|
}
|
|
1084
1114
|
// Second pass: Collect all CSS assets
|
|
1085
|
-
var allCssAssets = collectCssAssets(bundle);
|
|
1115
|
+
var allCssAssets = mfOptions.bundleAllCSS ? collectCssAssets(bundle) : new Set();
|
|
1086
1116
|
var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
|
|
1087
1117
|
return mfOptions.exposes[item]["import"];
|
|
1088
1118
|
});
|
|
@@ -1110,8 +1140,10 @@ var Manifest = function Manifest() {
|
|
|
1110
1140
|
processModuleAssets(bundle, filesMap, function (modulePath) {
|
|
1111
1141
|
return fileToShareKey.get(modulePath);
|
|
1112
1142
|
});
|
|
1113
|
-
// Add all CSS assets to every export
|
|
1114
|
-
|
|
1143
|
+
// Add all CSS assets to every export if bundleAllCSS is enabled
|
|
1144
|
+
if (mfOptions.bundleAllCSS) {
|
|
1145
|
+
addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
1146
|
+
}
|
|
1115
1147
|
// Final deduplication of all assets
|
|
1116
1148
|
filesMap = deduplicateAssets(filesMap);
|
|
1117
1149
|
_this.emitFile({
|
|
@@ -1310,15 +1342,15 @@ function pluginProxyRemoteEntry () {
|
|
|
1310
1342
|
}
|
|
1311
1343
|
},
|
|
1312
1344
|
transform: function transform(code, id) {
|
|
1313
|
-
|
|
1314
|
-
if (!filter(id)) return
|
|
1345
|
+
var transformedCode = function () {
|
|
1346
|
+
if (!filter(id)) return;
|
|
1315
1347
|
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1316
|
-
return
|
|
1348
|
+
return parsePromise.then(function (_) {
|
|
1317
1349
|
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1318
|
-
})
|
|
1350
|
+
});
|
|
1319
1351
|
}
|
|
1320
1352
|
if (id === VIRTUAL_EXPOSES) {
|
|
1321
|
-
return
|
|
1353
|
+
return generateExposes();
|
|
1322
1354
|
}
|
|
1323
1355
|
if (id.includes(getHostAutoInitPath())) {
|
|
1324
1356
|
var options = getNormalizeModuleFederationOptions();
|
|
@@ -1326,14 +1358,12 @@ function pluginProxyRemoteEntry () {
|
|
|
1326
1358
|
var _viteConfig$server, _viteConfig$server2;
|
|
1327
1359
|
var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1328
1360
|
var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1329
|
-
return
|
|
1361
|
+
return "\n const origin = (window && " + !options.ignoreOrigin + ") ? window.origin : \"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + "\"\n const remoteEntryPromise = await import(origin + " + publicPath + ")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ";
|
|
1330
1362
|
}
|
|
1331
|
-
return
|
|
1363
|
+
return code;
|
|
1332
1364
|
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
return Promise.reject(e);
|
|
1336
|
-
}
|
|
1365
|
+
}();
|
|
1366
|
+
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1337
1367
|
}
|
|
1338
1368
|
};
|
|
1339
1369
|
}
|
|
@@ -1412,11 +1442,11 @@ function proxySharedModule(options) {
|
|
|
1412
1442
|
});
|
|
1413
1443
|
}
|
|
1414
1444
|
},
|
|
1415
|
-
transform: function transform(
|
|
1445
|
+
transform: function transform(_, id) {
|
|
1416
1446
|
if (id.includes(getLocalSharedImportMapPath())) {
|
|
1417
|
-
return parsePromise.then(function (_) {
|
|
1447
|
+
return mapCodeToCodeWithSourcemap(parsePromise.then(function (_) {
|
|
1418
1448
|
return generateLocalSharedImportMap();
|
|
1419
|
-
});
|
|
1449
|
+
}));
|
|
1420
1450
|
}
|
|
1421
1451
|
}
|
|
1422
1452
|
}, {
|
|
@@ -1450,7 +1480,8 @@ function proxySharedModule(options) {
|
|
|
1450
1480
|
return command === 'build' ? {
|
|
1451
1481
|
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
1452
1482
|
replacement: function replacement($1) {
|
|
1453
|
-
var
|
|
1483
|
+
var module = assertModuleFound(PREBUILD_TAG, $1);
|
|
1484
|
+
var pkgName = module.name;
|
|
1454
1485
|
return pkgName;
|
|
1455
1486
|
}
|
|
1456
1487
|
} : {
|
|
@@ -1459,7 +1490,8 @@ function proxySharedModule(options) {
|
|
|
1459
1490
|
customResolver: function customResolver(source, importer) {
|
|
1460
1491
|
try {
|
|
1461
1492
|
var _this = this;
|
|
1462
|
-
var
|
|
1493
|
+
var module = assertModuleFound(PREBUILD_TAG, source);
|
|
1494
|
+
var pkgName = module.name;
|
|
1463
1495
|
return Promise.resolve(_this.resolve(pkgName, importer).then(function (item) {
|
|
1464
1496
|
return item.id;
|
|
1465
1497
|
})).then(function (result) {
|
|
@@ -1521,7 +1553,8 @@ function federation(mfUserOptions) {
|
|
|
1521
1553
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1522
1554
|
var name = options.name,
|
|
1523
1555
|
shared = options.shared,
|
|
1524
|
-
filename = options.filename
|
|
1556
|
+
filename = options.filename,
|
|
1557
|
+
hostInitInjectLocation = options.hostInitInjectLocation;
|
|
1525
1558
|
if (!name) throw new Error('name is required');
|
|
1526
1559
|
return [{
|
|
1527
1560
|
name: 'vite:module-federation-config',
|
|
@@ -1540,7 +1573,7 @@ function federation(mfUserOptions) {
|
|
|
1540
1573
|
}), addEntry({
|
|
1541
1574
|
entryName: 'hostInit',
|
|
1542
1575
|
entryPath: getHostAutoInitPath(),
|
|
1543
|
-
inject:
|
|
1576
|
+
inject: hostInitInjectLocation
|
|
1544
1577
|
}), addEntry({
|
|
1545
1578
|
entryName: 'virtualExposes',
|
|
1546
1579
|
entryPath: VIRTUAL_EXPOSES
|
package/lib/index.esm.js
CHANGED
|
@@ -3,9 +3,24 @@ import * as fs from 'fs';
|
|
|
3
3
|
import { mkdirSync, writeFileSync, existsSync, writeFile } from 'fs';
|
|
4
4
|
import * as path from 'pathe';
|
|
5
5
|
import path__default, { resolve, basename, parse, join, dirname } from 'pathe';
|
|
6
|
+
import MagicString from 'magic-string';
|
|
6
7
|
import { createFilter } from '@rollup/pluginutils';
|
|
7
8
|
import { walk } from 'estree-walker';
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
var mapCodeToCodeWithSourcemap = function mapCodeToCodeWithSourcemap(code) {
|
|
11
|
+
return Promise.resolve(code).then(function (resolvedCode) {
|
|
12
|
+
if (resolvedCode === undefined) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
var s = new MagicString(resolvedCode);
|
|
16
|
+
return {
|
|
17
|
+
code: s.toString(),
|
|
18
|
+
map: s.generateMap({
|
|
19
|
+
hires: true
|
|
20
|
+
})
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
};
|
|
9
24
|
|
|
10
25
|
function getFirstHtmlEntryFile(entryFiles) {
|
|
11
26
|
return entryFiles.find(function (file) {
|
|
@@ -126,7 +141,7 @@ var addEntry = function addEntry(_ref) {
|
|
|
126
141
|
return id.endsWith(file);
|
|
127
142
|
})) {
|
|
128
143
|
var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
|
|
129
|
-
return injection + code;
|
|
144
|
+
return mapCodeToCodeWithSourcemap(injection + code);
|
|
130
145
|
}
|
|
131
146
|
}
|
|
132
147
|
}];
|
|
@@ -359,6 +374,7 @@ function normalizeShareItem(key, shareItem) {
|
|
|
359
374
|
scope: 'default',
|
|
360
375
|
from: '',
|
|
361
376
|
shareConfig: {
|
|
377
|
+
"import": undefined,
|
|
362
378
|
singleton: false,
|
|
363
379
|
requiredVersion: version ? "^" + version : '*'
|
|
364
380
|
}
|
|
@@ -370,6 +386,7 @@ function normalizeShareItem(key, shareItem) {
|
|
|
370
386
|
version: shareItem.version || version,
|
|
371
387
|
scope: shareItem.shareScope || 'default',
|
|
372
388
|
shareConfig: {
|
|
389
|
+
"import": typeof shareItem === 'object' ? shareItem["import"] : undefined,
|
|
373
390
|
singleton: shareItem.singleton || false,
|
|
374
391
|
requiredVersion: shareItem.requiredVersion || (version ? "^" + version : '*'),
|
|
375
392
|
strictVersion: !!shareItem.strictVersion
|
|
@@ -444,7 +461,9 @@ function normalizeModuleFederationOptions(options) {
|
|
|
444
461
|
publicPath: options.publicPath,
|
|
445
462
|
shareStrategy: options.shareStrategy || 'version-first',
|
|
446
463
|
ignoreOrigin: options.ignoreOrigin || false,
|
|
447
|
-
virtualModuleDir: options.virtualModuleDir || '__mf__virtual'
|
|
464
|
+
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
|
|
465
|
+
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
|
|
466
|
+
bundleAllCSS: options.bundleAllCSS || false
|
|
448
467
|
};
|
|
449
468
|
}
|
|
450
469
|
|
|
@@ -532,6 +551,16 @@ var cacheMap = {};
|
|
|
532
551
|
/**
|
|
533
552
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
534
553
|
*/
|
|
554
|
+
function assertModuleFound(tag, str) {
|
|
555
|
+
if (str === void 0) {
|
|
556
|
+
str = '';
|
|
557
|
+
}
|
|
558
|
+
var module = VirtualModule.findModule(tag, str);
|
|
559
|
+
if (!module) {
|
|
560
|
+
throw new Error("Module Federation shared module '" + str + "' not found. Please ensure it's installed as a dependency in your package.json.");
|
|
561
|
+
}
|
|
562
|
+
return module;
|
|
563
|
+
}
|
|
535
564
|
var VirtualModule = /*#__PURE__*/function () {
|
|
536
565
|
function VirtualModule(name, tag, suffix) {
|
|
537
566
|
if (tag === void 0) {
|
|
@@ -671,7 +700,7 @@ function getLoadShareModulePath(pkg) {
|
|
|
671
700
|
return filepath;
|
|
672
701
|
}
|
|
673
702
|
function writeLoadShareModule(pkg, shareItem, command) {
|
|
674
|
-
loadShareCacheMap[pkg].writeSync("\n
|
|
703
|
+
loadShareCacheMap[pkg].writeSync("\n\n ;() => import(" + JSON.stringify(getPreBuildLibImportId(pkg)) + ").catch(() => {});\n // dev uses dynamic import to separate chunks\n " + (command !== 'build' ? ";() => import(" + JSON.stringify(pkg) + ").catch(() => {});" : '') + "\n const {loadShare} = require(\"@module-federation/runtime\")\n const {initPromise} = require(\"" + virtualRuntimeInitStatus.getImportId() + "\")\n const res = initPromise.then(_ => loadShare(" + JSON.stringify(pkg) + ", {\n customShareInfo: {shareConfig:{\n singleton: " + shareItem.shareConfig.singleton + ",\n strictVersion: " + shareItem.shareConfig.strictVersion + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }}}))\n const exportModule = " + (command !== 'build' ? '/*mf top-level-await placeholder replacement mf*/' : 'await ') + "res.then(factory => factory())\n module.exports = exportModule\n ");
|
|
675
704
|
}
|
|
676
705
|
|
|
677
706
|
var usedShares = new Set();
|
|
@@ -698,12 +727,13 @@ function writeLocalSharedImportMap() {
|
|
|
698
727
|
}
|
|
699
728
|
function generateLocalSharedImportMap() {
|
|
700
729
|
var options = getNormalizeModuleFederationOptions();
|
|
701
|
-
return "\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
702
|
-
|
|
730
|
+
return "\n import {loadShare} from \"@module-federation/runtime\";\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
731
|
+
var shareItem = getNormalizeShareItem(pkg);
|
|
732
|
+
return "\n " + JSON.stringify(pkg) + ": async () => {\n " + ((shareItem == null ? void 0 : shareItem.shareConfig["import"]) === false ? "throw new Error(`Shared module '${" + JSON.stringify(pkg) + "}' must be provided by host`);" : "let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\");\n return pkg;") + "\n }\n ";
|
|
703
733
|
}).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).sort().map(function (key) {
|
|
704
734
|
var shareItem = getNormalizeShareItem(key);
|
|
705
735
|
if (!shareItem) return null;
|
|
706
|
-
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap
|
|
736
|
+
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n if (" + (shareItem.shareConfig["import"] === false) + ") {\n throw new Error(`Shared module '${" + JSON.stringify(key) + "}' must be provided by host`);\n }\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap\n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + ",\n " + (shareItem.shareConfig["import"] === false ? 'import: false,' : '') + "\n }\n }\n ";
|
|
707
737
|
}).filter(function (x) {
|
|
708
738
|
return x !== null;
|
|
709
739
|
}).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
|
|
@@ -733,7 +763,7 @@ function generateRemoteEntry(options) {
|
|
|
733
763
|
var HOST_AUTO_INIT_TAG = '__H_A_I__';
|
|
734
764
|
var hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG);
|
|
735
765
|
function writeHostAutoInit() {
|
|
736
|
-
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await
|
|
766
|
+
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ");
|
|
737
767
|
}
|
|
738
768
|
function getHostAutoInitImportId() {
|
|
739
769
|
return hostAutoInitModule.getImportId();
|
|
@@ -1058,7 +1088,7 @@ var Manifest = function Manifest() {
|
|
|
1058
1088
|
}
|
|
1059
1089
|
}
|
|
1060
1090
|
// Second pass: Collect all CSS assets
|
|
1061
|
-
var allCssAssets = collectCssAssets(bundle);
|
|
1091
|
+
var allCssAssets = mfOptions.bundleAllCSS ? collectCssAssets(bundle) : new Set();
|
|
1062
1092
|
var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
|
|
1063
1093
|
return mfOptions.exposes[item]["import"];
|
|
1064
1094
|
});
|
|
@@ -1086,8 +1116,10 @@ var Manifest = function Manifest() {
|
|
|
1086
1116
|
processModuleAssets(bundle, filesMap, function (modulePath) {
|
|
1087
1117
|
return fileToShareKey.get(modulePath);
|
|
1088
1118
|
});
|
|
1089
|
-
// Add all CSS assets to every export
|
|
1090
|
-
|
|
1119
|
+
// Add all CSS assets to every export if bundleAllCSS is enabled
|
|
1120
|
+
if (mfOptions.bundleAllCSS) {
|
|
1121
|
+
addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
1122
|
+
}
|
|
1091
1123
|
// Final deduplication of all assets
|
|
1092
1124
|
filesMap = deduplicateAssets(filesMap);
|
|
1093
1125
|
_this.emitFile({
|
|
@@ -1286,15 +1318,15 @@ function pluginProxyRemoteEntry () {
|
|
|
1286
1318
|
}
|
|
1287
1319
|
},
|
|
1288
1320
|
transform: function transform(code, id) {
|
|
1289
|
-
|
|
1290
|
-
if (!filter(id)) return
|
|
1321
|
+
var transformedCode = function () {
|
|
1322
|
+
if (!filter(id)) return;
|
|
1291
1323
|
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1292
|
-
return
|
|
1324
|
+
return parsePromise.then(function (_) {
|
|
1293
1325
|
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1294
|
-
})
|
|
1326
|
+
});
|
|
1295
1327
|
}
|
|
1296
1328
|
if (id === VIRTUAL_EXPOSES) {
|
|
1297
|
-
return
|
|
1329
|
+
return generateExposes();
|
|
1298
1330
|
}
|
|
1299
1331
|
if (id.includes(getHostAutoInitPath())) {
|
|
1300
1332
|
var options = getNormalizeModuleFederationOptions();
|
|
@@ -1302,14 +1334,12 @@ function pluginProxyRemoteEntry () {
|
|
|
1302
1334
|
var _viteConfig$server, _viteConfig$server2;
|
|
1303
1335
|
var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1304
1336
|
var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1305
|
-
return
|
|
1337
|
+
return "\n const origin = (window && " + !options.ignoreOrigin + ") ? window.origin : \"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + "\"\n const remoteEntryPromise = await import(origin + " + publicPath + ")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ";
|
|
1306
1338
|
}
|
|
1307
|
-
return
|
|
1339
|
+
return code;
|
|
1308
1340
|
}
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
return Promise.reject(e);
|
|
1312
|
-
}
|
|
1341
|
+
}();
|
|
1342
|
+
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1313
1343
|
}
|
|
1314
1344
|
};
|
|
1315
1345
|
}
|
|
@@ -1388,11 +1418,11 @@ function proxySharedModule(options) {
|
|
|
1388
1418
|
});
|
|
1389
1419
|
}
|
|
1390
1420
|
},
|
|
1391
|
-
transform: function transform(
|
|
1421
|
+
transform: function transform(_, id) {
|
|
1392
1422
|
if (id.includes(getLocalSharedImportMapPath())) {
|
|
1393
|
-
return parsePromise.then(function (_) {
|
|
1423
|
+
return mapCodeToCodeWithSourcemap(parsePromise.then(function (_) {
|
|
1394
1424
|
return generateLocalSharedImportMap();
|
|
1395
|
-
});
|
|
1425
|
+
}));
|
|
1396
1426
|
}
|
|
1397
1427
|
}
|
|
1398
1428
|
}, {
|
|
@@ -1426,7 +1456,8 @@ function proxySharedModule(options) {
|
|
|
1426
1456
|
return command === 'build' ? {
|
|
1427
1457
|
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
1428
1458
|
replacement: function replacement($1) {
|
|
1429
|
-
var
|
|
1459
|
+
var module = assertModuleFound(PREBUILD_TAG, $1);
|
|
1460
|
+
var pkgName = module.name;
|
|
1430
1461
|
return pkgName;
|
|
1431
1462
|
}
|
|
1432
1463
|
} : {
|
|
@@ -1435,7 +1466,8 @@ function proxySharedModule(options) {
|
|
|
1435
1466
|
customResolver: function customResolver(source, importer) {
|
|
1436
1467
|
try {
|
|
1437
1468
|
var _this = this;
|
|
1438
|
-
var
|
|
1469
|
+
var module = assertModuleFound(PREBUILD_TAG, source);
|
|
1470
|
+
var pkgName = module.name;
|
|
1439
1471
|
return Promise.resolve(_this.resolve(pkgName, importer).then(function (item) {
|
|
1440
1472
|
return item.id;
|
|
1441
1473
|
})).then(function (result) {
|
|
@@ -1497,7 +1529,8 @@ function federation(mfUserOptions) {
|
|
|
1497
1529
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1498
1530
|
var name = options.name,
|
|
1499
1531
|
shared = options.shared,
|
|
1500
|
-
filename = options.filename
|
|
1532
|
+
filename = options.filename,
|
|
1533
|
+
hostInitInjectLocation = options.hostInitInjectLocation;
|
|
1501
1534
|
if (!name) throw new Error('name is required');
|
|
1502
1535
|
return [{
|
|
1503
1536
|
name: 'vite:module-federation-config',
|
|
@@ -1516,7 +1549,7 @@ function federation(mfUserOptions) {
|
|
|
1516
1549
|
}), addEntry({
|
|
1517
1550
|
entryName: 'hostInit',
|
|
1518
1551
|
entryPath: getHostAutoInitPath(),
|
|
1519
|
-
inject:
|
|
1552
|
+
inject: hostInitInjectLocation
|
|
1520
1553
|
}), addEntry({
|
|
1521
1554
|
entryName: 'virtualExposes',
|
|
1522
1555
|
entryPath: VIRTUAL_EXPOSES
|
package/lib/index.modern.js
CHANGED
|
@@ -3,9 +3,23 @@ import * as fs from 'fs';
|
|
|
3
3
|
import { mkdirSync, writeFileSync, existsSync, writeFile } from 'fs';
|
|
4
4
|
import * as path from 'pathe';
|
|
5
5
|
import path__default, { resolve, basename, parse, join, dirname } from 'pathe';
|
|
6
|
+
import MagicString from 'magic-string';
|
|
6
7
|
import { createFilter } from '@rollup/pluginutils';
|
|
7
8
|
import { walk } from 'estree-walker';
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
async function mapCodeToCodeWithSourcemap(code) {
|
|
11
|
+
const resolvedCode = await code;
|
|
12
|
+
if (resolvedCode === undefined) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const s = new MagicString(resolvedCode);
|
|
16
|
+
return {
|
|
17
|
+
code: s.toString(),
|
|
18
|
+
map: s.generateMap({
|
|
19
|
+
hires: true
|
|
20
|
+
})
|
|
21
|
+
};
|
|
22
|
+
}
|
|
9
23
|
|
|
10
24
|
function getFirstHtmlEntryFile(entryFiles) {
|
|
11
25
|
return entryFiles.find(file => file.endsWith('.html'));
|
|
@@ -127,7 +141,7 @@ const addEntry = ({
|
|
|
127
141
|
const injection = `
|
|
128
142
|
import ${JSON.stringify(entryPath)};
|
|
129
143
|
`;
|
|
130
|
-
return injection + code;
|
|
144
|
+
return mapCodeToCodeWithSourcemap(injection + code);
|
|
131
145
|
}
|
|
132
146
|
}
|
|
133
147
|
}];
|
|
@@ -332,6 +346,7 @@ function normalizeShareItem(key, shareItem) {
|
|
|
332
346
|
scope: 'default',
|
|
333
347
|
from: '',
|
|
334
348
|
shareConfig: {
|
|
349
|
+
import: undefined,
|
|
335
350
|
singleton: false,
|
|
336
351
|
requiredVersion: version ? `^${version}` : '*'
|
|
337
352
|
}
|
|
@@ -343,6 +358,7 @@ function normalizeShareItem(key, shareItem) {
|
|
|
343
358
|
version: shareItem.version || version,
|
|
344
359
|
scope: shareItem.shareScope || 'default',
|
|
345
360
|
shareConfig: {
|
|
361
|
+
import: typeof shareItem === 'object' ? shareItem.import : undefined,
|
|
346
362
|
singleton: shareItem.singleton || false,
|
|
347
363
|
requiredVersion: shareItem.requiredVersion || (version ? `^${version}` : '*'),
|
|
348
364
|
strictVersion: !!shareItem.strictVersion
|
|
@@ -414,7 +430,9 @@ function normalizeModuleFederationOptions(options) {
|
|
|
414
430
|
publicPath: options.publicPath,
|
|
415
431
|
shareStrategy: options.shareStrategy || 'version-first',
|
|
416
432
|
ignoreOrigin: options.ignoreOrigin || false,
|
|
417
|
-
virtualModuleDir: options.virtualModuleDir || '__mf__virtual'
|
|
433
|
+
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
|
|
434
|
+
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
|
|
435
|
+
bundleAllCSS: options.bundleAllCSS || false
|
|
418
436
|
};
|
|
419
437
|
}
|
|
420
438
|
|
|
@@ -500,6 +518,13 @@ const cacheMap = {};
|
|
|
500
518
|
/**
|
|
501
519
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
502
520
|
*/
|
|
521
|
+
function assertModuleFound(tag, str = '') {
|
|
522
|
+
const module = VirtualModule.findModule(tag, str);
|
|
523
|
+
if (!module) {
|
|
524
|
+
throw new Error(`Module Federation shared module '${str}' not found. Please ensure it's installed as a dependency in your package.json.`);
|
|
525
|
+
}
|
|
526
|
+
return module;
|
|
527
|
+
}
|
|
503
528
|
class VirtualModule {
|
|
504
529
|
/**
|
|
505
530
|
* Set the root path for finding node_modules
|
|
@@ -663,7 +688,7 @@ function getLoadShareModulePath(pkg) {
|
|
|
663
688
|
}
|
|
664
689
|
function writeLoadShareModule(pkg, shareItem, command) {
|
|
665
690
|
loadShareCacheMap[pkg].writeSync(`
|
|
666
|
-
|
|
691
|
+
|
|
667
692
|
;() => import(${JSON.stringify(getPreBuildLibImportId(pkg))}).catch(() => {});
|
|
668
693
|
// dev uses dynamic import to separate chunks
|
|
669
694
|
${command !== 'build' ? `;() => import(${JSON.stringify(pkg)}).catch(() => {});` : ''}
|
|
@@ -705,13 +730,17 @@ function writeLocalSharedImportMap() {
|
|
|
705
730
|
function generateLocalSharedImportMap() {
|
|
706
731
|
const options = getNormalizeModuleFederationOptions();
|
|
707
732
|
return `
|
|
733
|
+
import {loadShare} from "@module-federation/runtime";
|
|
708
734
|
const importMap = {
|
|
709
|
-
${Array.from(getUsedShares()).sort().map(pkg =>
|
|
735
|
+
${Array.from(getUsedShares()).sort().map(pkg => {
|
|
736
|
+
const shareItem = getNormalizeShareItem(pkg);
|
|
737
|
+
return `
|
|
710
738
|
${JSON.stringify(pkg)}: async () => {
|
|
711
|
-
let pkg = await import("${getPreBuildLibImportId(pkg)}")
|
|
712
|
-
|
|
739
|
+
${(shareItem == null ? void 0 : shareItem.shareConfig.import) === false ? `throw new Error(\`Shared module '\${${JSON.stringify(pkg)}}' must be provided by host\`);` : `let pkg = await import("${getPreBuildLibImportId(pkg)}");
|
|
740
|
+
return pkg;`}
|
|
713
741
|
}
|
|
714
|
-
|
|
742
|
+
`;
|
|
743
|
+
}).join(',')}
|
|
715
744
|
}
|
|
716
745
|
const usedShared = {
|
|
717
746
|
${Array.from(getUsedShares()).sort().map(key => {
|
|
@@ -725,8 +754,11 @@ function generateLocalSharedImportMap() {
|
|
|
725
754
|
loaded: false,
|
|
726
755
|
from: ${JSON.stringify(options.name)},
|
|
727
756
|
async get () {
|
|
757
|
+
if (${shareItem.shareConfig.import === false}) {
|
|
758
|
+
throw new Error(\`Shared module '\${${JSON.stringify(key)}}' must be provided by host\`);
|
|
759
|
+
}
|
|
728
760
|
usedShared[${JSON.stringify(key)}].loaded = true
|
|
729
|
-
const {${JSON.stringify(key)}: pkgDynamicImport} = importMap
|
|
761
|
+
const {${JSON.stringify(key)}: pkgDynamicImport} = importMap
|
|
730
762
|
const res = await pkgDynamicImport()
|
|
731
763
|
const exportModule = {...res}
|
|
732
764
|
// All npm packages pre-built by vite will be converted to esm
|
|
@@ -740,7 +772,8 @@ function generateLocalSharedImportMap() {
|
|
|
740
772
|
},
|
|
741
773
|
shareConfig: {
|
|
742
774
|
singleton: ${shareItem.shareConfig.singleton},
|
|
743
|
-
requiredVersion: ${JSON.stringify(shareItem.shareConfig.requiredVersion)}
|
|
775
|
+
requiredVersion: ${JSON.stringify(shareItem.shareConfig.requiredVersion)},
|
|
776
|
+
${shareItem.shareConfig.import === false ? 'import: false,' : ''}
|
|
744
777
|
}
|
|
745
778
|
}
|
|
746
779
|
`;
|
|
@@ -828,7 +861,7 @@ const hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG)
|
|
|
828
861
|
function writeHostAutoInit() {
|
|
829
862
|
hostAutoInitModule.writeSync(`
|
|
830
863
|
const remoteEntryPromise = import("${REMOTE_ENTRY_ID}")
|
|
831
|
-
// __tla only serves as a hack for vite-plugin-top-level-await.
|
|
864
|
+
// __tla only serves as a hack for vite-plugin-top-level-await.
|
|
832
865
|
Promise.resolve(remoteEntryPromise)
|
|
833
866
|
.then(remoteEntry => {
|
|
834
867
|
return Promise.resolve(remoteEntry.__tla)
|
|
@@ -1132,7 +1165,7 @@ const Manifest = () => {
|
|
|
1132
1165
|
}
|
|
1133
1166
|
}
|
|
1134
1167
|
// Second pass: Collect all CSS assets
|
|
1135
|
-
const allCssAssets = collectCssAssets(bundle);
|
|
1168
|
+
const allCssAssets = mfOptions.bundleAllCSS ? collectCssAssets(bundle) : new Set();
|
|
1136
1169
|
const exposesModules = Object.keys(mfOptions.exposes).map(item => mfOptions.exposes[item].import);
|
|
1137
1170
|
// Process exposed modules
|
|
1138
1171
|
processModuleAssets(bundle, filesMap, modulePath => {
|
|
@@ -1156,8 +1189,10 @@ const Manifest = () => {
|
|
|
1156
1189
|
// Process shared modules
|
|
1157
1190
|
const fileToShareKey = await buildFileToShareKeyMap(getUsedShares(), this.resolve.bind(this));
|
|
1158
1191
|
processModuleAssets(bundle, filesMap, modulePath => fileToShareKey.get(modulePath));
|
|
1159
|
-
// Add all CSS assets to every export
|
|
1160
|
-
|
|
1192
|
+
// Add all CSS assets to every export if bundleAllCSS is enabled
|
|
1193
|
+
if (mfOptions.bundleAllCSS) {
|
|
1194
|
+
addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
1195
|
+
}
|
|
1161
1196
|
// Final deduplication of all assets
|
|
1162
1197
|
filesMap = deduplicateAssets(filesMap);
|
|
1163
1198
|
this.emitFile({
|
|
@@ -1344,21 +1379,22 @@ function pluginProxyRemoteEntry () {
|
|
|
1344
1379
|
return id;
|
|
1345
1380
|
}
|
|
1346
1381
|
},
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1382
|
+
transform(code, id) {
|
|
1383
|
+
const transformedCode = (() => {
|
|
1384
|
+
if (!filter(id)) return;
|
|
1385
|
+
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1386
|
+
return parsePromise.then(_ => generateRemoteEntry(getNormalizeModuleFederationOptions()));
|
|
1387
|
+
}
|
|
1388
|
+
if (id === VIRTUAL_EXPOSES) {
|
|
1389
|
+
return generateExposes();
|
|
1390
|
+
}
|
|
1391
|
+
if (id.includes(getHostAutoInitPath())) {
|
|
1392
|
+
const options = getNormalizeModuleFederationOptions();
|
|
1393
|
+
if (_command === 'serve') {
|
|
1394
|
+
var _viteConfig$server, _viteConfig$server2;
|
|
1395
|
+
const host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1396
|
+
const publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1397
|
+
return `
|
|
1362
1398
|
const origin = (window && ${!options.ignoreOrigin}) ? window.origin : "//${host}:${(_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port}"
|
|
1363
1399
|
const remoteEntryPromise = await import(origin + ${publicPath})
|
|
1364
1400
|
// __tla only serves as a hack for vite-plugin-top-level-await.
|
|
@@ -1368,9 +1404,11 @@ function pluginProxyRemoteEntry () {
|
|
|
1368
1404
|
.then(remoteEntry.init).catch(remoteEntry.init)
|
|
1369
1405
|
})
|
|
1370
1406
|
`;
|
|
1407
|
+
}
|
|
1408
|
+
return code;
|
|
1371
1409
|
}
|
|
1372
|
-
|
|
1373
|
-
|
|
1410
|
+
})();
|
|
1411
|
+
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1374
1412
|
}
|
|
1375
1413
|
};
|
|
1376
1414
|
}
|
|
@@ -1450,9 +1488,9 @@ function proxySharedModule(options) {
|
|
|
1450
1488
|
return parsePromise.then(_ => generateLocalSharedImportMap());
|
|
1451
1489
|
}
|
|
1452
1490
|
},
|
|
1453
|
-
transform(
|
|
1491
|
+
transform(_, id) {
|
|
1454
1492
|
if (id.includes(getLocalSharedImportMapPath())) {
|
|
1455
|
-
return parsePromise.then(_ => generateLocalSharedImportMap());
|
|
1493
|
+
return mapCodeToCodeWithSourcemap(parsePromise.then(_ => generateLocalSharedImportMap()));
|
|
1456
1494
|
}
|
|
1457
1495
|
}
|
|
1458
1496
|
}, {
|
|
@@ -1486,14 +1524,16 @@ function proxySharedModule(options) {
|
|
|
1486
1524
|
return command === 'build' ? {
|
|
1487
1525
|
find: new RegExp(`(.*${PREBUILD_TAG}.*)`),
|
|
1488
1526
|
replacement: function ($1) {
|
|
1489
|
-
const
|
|
1527
|
+
const module = assertModuleFound(PREBUILD_TAG, $1);
|
|
1528
|
+
const pkgName = module.name;
|
|
1490
1529
|
return pkgName;
|
|
1491
1530
|
}
|
|
1492
1531
|
} : {
|
|
1493
1532
|
find: new RegExp(`(.*${PREBUILD_TAG}.*)`),
|
|
1494
1533
|
replacement: '$1',
|
|
1495
1534
|
async customResolver(source, importer) {
|
|
1496
|
-
const
|
|
1535
|
+
const module = assertModuleFound(PREBUILD_TAG, source);
|
|
1536
|
+
const pkgName = module.name;
|
|
1497
1537
|
const result = await this.resolve(pkgName, importer).then(item => item.id);
|
|
1498
1538
|
if (!result.includes(_config.cacheDir)) {
|
|
1499
1539
|
// save pre-bunding module id
|
|
@@ -1552,7 +1592,8 @@ function federation(mfUserOptions) {
|
|
|
1552
1592
|
name,
|
|
1553
1593
|
remotes,
|
|
1554
1594
|
shared,
|
|
1555
|
-
filename
|
|
1595
|
+
filename,
|
|
1596
|
+
hostInitInjectLocation
|
|
1556
1597
|
} = options;
|
|
1557
1598
|
if (!name) throw new Error('name is required');
|
|
1558
1599
|
return [{
|
|
@@ -1572,7 +1613,7 @@ function federation(mfUserOptions) {
|
|
|
1572
1613
|
}), ...addEntry({
|
|
1573
1614
|
entryName: 'hostInit',
|
|
1574
1615
|
entryPath: getHostAutoInitPath(),
|
|
1575
|
-
inject:
|
|
1616
|
+
inject: hostInitInjectLocation
|
|
1576
1617
|
}), ...addEntry({
|
|
1577
1618
|
entryName: 'virtualExposes',
|
|
1578
1619
|
entryPath: VIRTUAL_EXPOSES
|
package/lib/index.umd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('defu'), require('fs'), require('pathe'), require('
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'defu', 'fs', 'pathe', '@rollup/pluginutils', 'estree-walker'
|
|
4
|
-
(global = global || self, factory(global.vite = {}, global.defu, global.fs, global.pathe, global.
|
|
5
|
-
})(this, (function (exports, defu, fs, path, pluginutils, estreeWalker
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('defu'), require('fs'), require('pathe'), require('magic-string'), require('@rollup/pluginutils'), require('estree-walker')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'defu', 'fs', 'pathe', 'magic-string', '@rollup/pluginutils', 'estree-walker'], factory) :
|
|
4
|
+
(global = global || self, factory(global.vite = {}, global.defu, global.fs, global.pathe, global.magicString, global.pluginutils, global.estreeWalker));
|
|
5
|
+
})(this, (function (exports, defu, fs, path, MagicString, pluginutils, estreeWalker) {
|
|
6
6
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
7
|
|
|
8
8
|
function _interopNamespace(e) {
|
|
@@ -29,6 +29,21 @@
|
|
|
29
29
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
30
30
|
var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
31
31
|
|
|
32
|
+
var mapCodeToCodeWithSourcemap = function mapCodeToCodeWithSourcemap(code) {
|
|
33
|
+
return Promise.resolve(code).then(function (resolvedCode) {
|
|
34
|
+
if (resolvedCode === undefined) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
var s = new MagicString__default["default"](resolvedCode);
|
|
38
|
+
return {
|
|
39
|
+
code: s.toString(),
|
|
40
|
+
map: s.generateMap({
|
|
41
|
+
hires: true
|
|
42
|
+
})
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
32
47
|
function getFirstHtmlEntryFile(entryFiles) {
|
|
33
48
|
return entryFiles.find(function (file) {
|
|
34
49
|
return file.endsWith('.html');
|
|
@@ -148,7 +163,7 @@
|
|
|
148
163
|
return id.endsWith(file);
|
|
149
164
|
})) {
|
|
150
165
|
var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
|
|
151
|
-
return injection + code;
|
|
166
|
+
return mapCodeToCodeWithSourcemap(injection + code);
|
|
152
167
|
}
|
|
153
168
|
}
|
|
154
169
|
}];
|
|
@@ -381,6 +396,7 @@
|
|
|
381
396
|
scope: 'default',
|
|
382
397
|
from: '',
|
|
383
398
|
shareConfig: {
|
|
399
|
+
"import": undefined,
|
|
384
400
|
singleton: false,
|
|
385
401
|
requiredVersion: version ? "^" + version : '*'
|
|
386
402
|
}
|
|
@@ -392,6 +408,7 @@
|
|
|
392
408
|
version: shareItem.version || version,
|
|
393
409
|
scope: shareItem.shareScope || 'default',
|
|
394
410
|
shareConfig: {
|
|
411
|
+
"import": typeof shareItem === 'object' ? shareItem["import"] : undefined,
|
|
395
412
|
singleton: shareItem.singleton || false,
|
|
396
413
|
requiredVersion: shareItem.requiredVersion || (version ? "^" + version : '*'),
|
|
397
414
|
strictVersion: !!shareItem.strictVersion
|
|
@@ -466,7 +483,9 @@
|
|
|
466
483
|
publicPath: options.publicPath,
|
|
467
484
|
shareStrategy: options.shareStrategy || 'version-first',
|
|
468
485
|
ignoreOrigin: options.ignoreOrigin || false,
|
|
469
|
-
virtualModuleDir: options.virtualModuleDir || '__mf__virtual'
|
|
486
|
+
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
|
|
487
|
+
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
|
|
488
|
+
bundleAllCSS: options.bundleAllCSS || false
|
|
470
489
|
};
|
|
471
490
|
}
|
|
472
491
|
|
|
@@ -554,6 +573,16 @@
|
|
|
554
573
|
/**
|
|
555
574
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
556
575
|
*/
|
|
576
|
+
function assertModuleFound(tag, str) {
|
|
577
|
+
if (str === void 0) {
|
|
578
|
+
str = '';
|
|
579
|
+
}
|
|
580
|
+
var module = VirtualModule.findModule(tag, str);
|
|
581
|
+
if (!module) {
|
|
582
|
+
throw new Error("Module Federation shared module '" + str + "' not found. Please ensure it's installed as a dependency in your package.json.");
|
|
583
|
+
}
|
|
584
|
+
return module;
|
|
585
|
+
}
|
|
557
586
|
var VirtualModule = /*#__PURE__*/function () {
|
|
558
587
|
function VirtualModule(name, tag, suffix) {
|
|
559
588
|
if (tag === void 0) {
|
|
@@ -693,7 +722,7 @@
|
|
|
693
722
|
return filepath;
|
|
694
723
|
}
|
|
695
724
|
function writeLoadShareModule(pkg, shareItem, command) {
|
|
696
|
-
loadShareCacheMap[pkg].writeSync("\n
|
|
725
|
+
loadShareCacheMap[pkg].writeSync("\n\n ;() => import(" + JSON.stringify(getPreBuildLibImportId(pkg)) + ").catch(() => {});\n // dev uses dynamic import to separate chunks\n " + (command !== 'build' ? ";() => import(" + JSON.stringify(pkg) + ").catch(() => {});" : '') + "\n const {loadShare} = require(\"@module-federation/runtime\")\n const {initPromise} = require(\"" + virtualRuntimeInitStatus.getImportId() + "\")\n const res = initPromise.then(_ => loadShare(" + JSON.stringify(pkg) + ", {\n customShareInfo: {shareConfig:{\n singleton: " + shareItem.shareConfig.singleton + ",\n strictVersion: " + shareItem.shareConfig.strictVersion + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }}}))\n const exportModule = " + (command !== 'build' ? '/*mf top-level-await placeholder replacement mf*/' : 'await ') + "res.then(factory => factory())\n module.exports = exportModule\n ");
|
|
697
726
|
}
|
|
698
727
|
|
|
699
728
|
var usedShares = new Set();
|
|
@@ -720,12 +749,13 @@
|
|
|
720
749
|
}
|
|
721
750
|
function generateLocalSharedImportMap() {
|
|
722
751
|
var options = getNormalizeModuleFederationOptions();
|
|
723
|
-
return "\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
724
|
-
|
|
752
|
+
return "\n import {loadShare} from \"@module-federation/runtime\";\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
753
|
+
var shareItem = getNormalizeShareItem(pkg);
|
|
754
|
+
return "\n " + JSON.stringify(pkg) + ": async () => {\n " + ((shareItem == null ? void 0 : shareItem.shareConfig["import"]) === false ? "throw new Error(`Shared module '${" + JSON.stringify(pkg) + "}' must be provided by host`);" : "let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\");\n return pkg;") + "\n }\n ";
|
|
725
755
|
}).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).sort().map(function (key) {
|
|
726
756
|
var shareItem = getNormalizeShareItem(key);
|
|
727
757
|
if (!shareItem) return null;
|
|
728
|
-
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap
|
|
758
|
+
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n if (" + (shareItem.shareConfig["import"] === false) + ") {\n throw new Error(`Shared module '${" + JSON.stringify(key) + "}' must be provided by host`);\n }\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap\n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + ",\n " + (shareItem.shareConfig["import"] === false ? 'import: false,' : '') + "\n }\n }\n ";
|
|
729
759
|
}).filter(function (x) {
|
|
730
760
|
return x !== null;
|
|
731
761
|
}).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
|
|
@@ -755,7 +785,7 @@
|
|
|
755
785
|
var HOST_AUTO_INIT_TAG = '__H_A_I__';
|
|
756
786
|
var hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG);
|
|
757
787
|
function writeHostAutoInit() {
|
|
758
|
-
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await
|
|
788
|
+
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ");
|
|
759
789
|
}
|
|
760
790
|
function getHostAutoInitImportId() {
|
|
761
791
|
return hostAutoInitModule.getImportId();
|
|
@@ -1080,7 +1110,7 @@
|
|
|
1080
1110
|
}
|
|
1081
1111
|
}
|
|
1082
1112
|
// Second pass: Collect all CSS assets
|
|
1083
|
-
var allCssAssets = collectCssAssets(bundle);
|
|
1113
|
+
var allCssAssets = mfOptions.bundleAllCSS ? collectCssAssets(bundle) : new Set();
|
|
1084
1114
|
var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
|
|
1085
1115
|
return mfOptions.exposes[item]["import"];
|
|
1086
1116
|
});
|
|
@@ -1108,8 +1138,10 @@
|
|
|
1108
1138
|
processModuleAssets(bundle, filesMap, function (modulePath) {
|
|
1109
1139
|
return fileToShareKey.get(modulePath);
|
|
1110
1140
|
});
|
|
1111
|
-
// Add all CSS assets to every export
|
|
1112
|
-
|
|
1141
|
+
// Add all CSS assets to every export if bundleAllCSS is enabled
|
|
1142
|
+
if (mfOptions.bundleAllCSS) {
|
|
1143
|
+
addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
1144
|
+
}
|
|
1113
1145
|
// Final deduplication of all assets
|
|
1114
1146
|
filesMap = deduplicateAssets(filesMap);
|
|
1115
1147
|
_this.emitFile({
|
|
@@ -1308,15 +1340,15 @@
|
|
|
1308
1340
|
}
|
|
1309
1341
|
},
|
|
1310
1342
|
transform: function transform(code, id) {
|
|
1311
|
-
|
|
1312
|
-
if (!filter(id)) return
|
|
1343
|
+
var transformedCode = function () {
|
|
1344
|
+
if (!filter(id)) return;
|
|
1313
1345
|
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1314
|
-
return
|
|
1346
|
+
return parsePromise.then(function (_) {
|
|
1315
1347
|
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1316
|
-
})
|
|
1348
|
+
});
|
|
1317
1349
|
}
|
|
1318
1350
|
if (id === VIRTUAL_EXPOSES) {
|
|
1319
|
-
return
|
|
1351
|
+
return generateExposes();
|
|
1320
1352
|
}
|
|
1321
1353
|
if (id.includes(getHostAutoInitPath())) {
|
|
1322
1354
|
var options = getNormalizeModuleFederationOptions();
|
|
@@ -1324,14 +1356,12 @@
|
|
|
1324
1356
|
var _viteConfig$server, _viteConfig$server2;
|
|
1325
1357
|
var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1326
1358
|
var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1327
|
-
return
|
|
1359
|
+
return "\n const origin = (window && " + !options.ignoreOrigin + ") ? window.origin : \"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + "\"\n const remoteEntryPromise = await import(origin + " + publicPath + ")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ";
|
|
1328
1360
|
}
|
|
1329
|
-
return
|
|
1361
|
+
return code;
|
|
1330
1362
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
return Promise.reject(e);
|
|
1334
|
-
}
|
|
1363
|
+
}();
|
|
1364
|
+
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1335
1365
|
}
|
|
1336
1366
|
};
|
|
1337
1367
|
}
|
|
@@ -1410,11 +1440,11 @@
|
|
|
1410
1440
|
});
|
|
1411
1441
|
}
|
|
1412
1442
|
},
|
|
1413
|
-
transform: function transform(
|
|
1443
|
+
transform: function transform(_, id) {
|
|
1414
1444
|
if (id.includes(getLocalSharedImportMapPath())) {
|
|
1415
|
-
return parsePromise.then(function (_) {
|
|
1445
|
+
return mapCodeToCodeWithSourcemap(parsePromise.then(function (_) {
|
|
1416
1446
|
return generateLocalSharedImportMap();
|
|
1417
|
-
});
|
|
1447
|
+
}));
|
|
1418
1448
|
}
|
|
1419
1449
|
}
|
|
1420
1450
|
}, {
|
|
@@ -1448,7 +1478,8 @@
|
|
|
1448
1478
|
return command === 'build' ? {
|
|
1449
1479
|
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
1450
1480
|
replacement: function replacement($1) {
|
|
1451
|
-
var
|
|
1481
|
+
var module = assertModuleFound(PREBUILD_TAG, $1);
|
|
1482
|
+
var pkgName = module.name;
|
|
1452
1483
|
return pkgName;
|
|
1453
1484
|
}
|
|
1454
1485
|
} : {
|
|
@@ -1457,7 +1488,8 @@
|
|
|
1457
1488
|
customResolver: function customResolver(source, importer) {
|
|
1458
1489
|
try {
|
|
1459
1490
|
var _this = this;
|
|
1460
|
-
var
|
|
1491
|
+
var module = assertModuleFound(PREBUILD_TAG, source);
|
|
1492
|
+
var pkgName = module.name;
|
|
1461
1493
|
return Promise.resolve(_this.resolve(pkgName, importer).then(function (item) {
|
|
1462
1494
|
return item.id;
|
|
1463
1495
|
})).then(function (result) {
|
|
@@ -1519,7 +1551,8 @@
|
|
|
1519
1551
|
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
1520
1552
|
var name = options.name,
|
|
1521
1553
|
shared = options.shared,
|
|
1522
|
-
filename = options.filename
|
|
1554
|
+
filename = options.filename,
|
|
1555
|
+
hostInitInjectLocation = options.hostInitInjectLocation;
|
|
1523
1556
|
if (!name) throw new Error('name is required');
|
|
1524
1557
|
return [{
|
|
1525
1558
|
name: 'vite:module-federation-config',
|
|
@@ -1538,7 +1571,7 @@
|
|
|
1538
1571
|
}), addEntry({
|
|
1539
1572
|
entryName: 'hostInit',
|
|
1540
1573
|
entryPath: getHostAutoInitPath(),
|
|
1541
|
-
inject:
|
|
1574
|
+
inject: hostInitInjectLocation
|
|
1542
1575
|
}), addEntry({
|
|
1543
1576
|
entryName: 'virtualExposes',
|
|
1544
1577
|
entryPath: VIRTUAL_EXPOSES
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
|
+
import { NormalizedModuleFederationOptions } from '../utils/normalizeModuleFederationOptions';
|
|
2
3
|
interface AddEntryOptions {
|
|
3
4
|
entryName: string;
|
|
4
5
|
entryPath: string;
|
|
5
6
|
fileName?: string;
|
|
6
|
-
inject?: '
|
|
7
|
+
inject?: NormalizedModuleFederationOptions['hostInitInjectLocation'];
|
|
7
8
|
}
|
|
8
9
|
declare const addEntry: ({ entryName, entryPath, fileName, inject, }: AddEntryOptions) => Plugin[];
|
|
9
10
|
export default addEntry;
|
|
@@ -2,6 +2,7 @@ export declare function getSuffix(name: string): string;
|
|
|
2
2
|
/**
|
|
3
3
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
4
4
|
*/
|
|
5
|
+
export declare function assertModuleFound(tag: string, str?: string): VirtualModule;
|
|
5
6
|
export default class VirtualModule {
|
|
6
7
|
name: string;
|
|
7
8
|
tag: string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SharedConfig, ShareStrategy } from '@module-federation/runtime/types';
|
|
2
|
+
import type { sharePlugin } from '@module-federation/sdk';
|
|
2
3
|
export type RemoteEntryType = 'var' | 'module' | 'assign' | 'assign-properties' | 'this' | 'window' | 'self' | 'global' | 'commonjs' | 'commonjs2' | 'commonjs-module' | 'commonjs-static' | 'amd' | 'amd-require' | 'umd' | 'umd2' | 'jsonp' | 'system' | string;
|
|
3
4
|
interface ExposesItem {
|
|
4
5
|
import: string;
|
|
@@ -19,7 +20,7 @@ export interface ShareItem {
|
|
|
19
20
|
version: string | undefined;
|
|
20
21
|
scope: string;
|
|
21
22
|
from: string;
|
|
22
|
-
shareConfig: SharedConfig;
|
|
23
|
+
shareConfig: SharedConfig & sharePlugin.SharedConfig;
|
|
23
24
|
}
|
|
24
25
|
interface ManifestOptions {
|
|
25
26
|
filePath?: string;
|
|
@@ -41,6 +42,12 @@ export type ModuleFederationOptions = {
|
|
|
41
42
|
* Defaults to Vite's base config or "auto" if base is empty
|
|
42
43
|
*/
|
|
43
44
|
publicPath?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Controls whether all CSS assets from the bundle should be added to every exposed module.
|
|
47
|
+
* When false (default), the plugin will not process any CSS assets.
|
|
48
|
+
* When true, all CSS assets are bundled into every exposed module.
|
|
49
|
+
*/
|
|
50
|
+
bundleAllCSS?: boolean;
|
|
44
51
|
shared?: string[] | Record<string, string | {
|
|
45
52
|
name?: string;
|
|
46
53
|
version?: string;
|
|
@@ -48,6 +55,7 @@ export type ModuleFederationOptions = {
|
|
|
48
55
|
singleton?: boolean;
|
|
49
56
|
requiredVersion?: string;
|
|
50
57
|
strictVersion?: boolean;
|
|
58
|
+
import?: sharePlugin.SharedConfig['import'];
|
|
51
59
|
}> | undefined;
|
|
52
60
|
runtimePlugins?: string[];
|
|
53
61
|
getPublicPath?: string;
|
|
@@ -58,6 +66,7 @@ export type ModuleFederationOptions = {
|
|
|
58
66
|
shareStrategy?: ShareStrategy;
|
|
59
67
|
ignoreOrigin?: boolean;
|
|
60
68
|
virtualModuleDir?: string;
|
|
69
|
+
hostInitInjectLocation?: HostInitInjectLocationOptions;
|
|
61
70
|
};
|
|
62
71
|
export interface NormalizedModuleFederationOptions {
|
|
63
72
|
exposes: Record<string, ExposesItem>;
|
|
@@ -78,7 +87,15 @@ export interface NormalizedModuleFederationOptions {
|
|
|
78
87
|
publicPath?: string;
|
|
79
88
|
ignoreOrigin?: boolean;
|
|
80
89
|
virtualModuleDir: string;
|
|
90
|
+
hostInitInjectLocation: HostInitInjectLocationOptions;
|
|
91
|
+
/**
|
|
92
|
+
* Controls whether all CSS assets from the bundle should be added to every exposed module.
|
|
93
|
+
* When false (default), the plugin will not process any CSS assets.
|
|
94
|
+
* When true, all CSS assets are bundled into every exposed module.
|
|
95
|
+
*/
|
|
96
|
+
bundleAllCSS: boolean;
|
|
81
97
|
}
|
|
98
|
+
type HostInitInjectLocationOptions = 'entry' | 'html';
|
|
82
99
|
interface PluginDevOptions {
|
|
83
100
|
disableLiveReload?: boolean;
|
|
84
101
|
disableHotTypesReload?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/vite",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "Vite plugin for Module Federation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -9,6 +9,22 @@
|
|
|
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
|
+
},
|
|
12
28
|
"repository": {
|
|
13
29
|
"type": "git",
|
|
14
30
|
"url": "git+https://github.com/module-federation/vite.git"
|
|
@@ -28,8 +44,10 @@
|
|
|
28
44
|
"url": "https://github.com/module-federation/vite/issues"
|
|
29
45
|
},
|
|
30
46
|
"homepage": "https://github.com/module-federation/vite#readme",
|
|
47
|
+
"packageManager": "pnpm@9.1.3",
|
|
31
48
|
"dependencies": {
|
|
32
|
-
"@module-federation/runtime": "^0.
|
|
49
|
+
"@module-federation/runtime": "^0.18.3",
|
|
50
|
+
"@module-federation/sdk": "^0.18.3",
|
|
33
51
|
"@rollup/pluginutils": "^5.1.0",
|
|
34
52
|
"defu": "^6.1.4",
|
|
35
53
|
"estree-walker": "^2",
|
|
@@ -49,20 +67,5 @@
|
|
|
49
67
|
"vite": "^5.4.3",
|
|
50
68
|
"vitest": "^2.1.1",
|
|
51
69
|
"wait-on": "^8.0.1"
|
|
52
|
-
},
|
|
53
|
-
"scripts": {
|
|
54
|
-
"fmt": "prettier --write src",
|
|
55
|
-
"fmt.check": "prettier --check src",
|
|
56
|
-
"format": "pretty-quick",
|
|
57
|
-
"dev": "microbundle watch --no-sourcemap --compress=false",
|
|
58
|
-
"build": "rimraf lib && microbundle --no-sourcemap --compress=false",
|
|
59
|
-
"dev-rv": "pnpm -filter 'examples-rust-vite*' run dev",
|
|
60
|
-
"preview-rv": "pnpm -filter 'examples-rust-vite*' run preview",
|
|
61
|
-
"dev-vv": "pnpm -filter 'examples-vite-vite*' run dev",
|
|
62
|
-
"dev-nv": "pnpm -filter 'examples-nuxt-vite-host' -filter 'examples-vite-vite-remote' run dev",
|
|
63
|
-
"preview-vv": "pnpm -filter 'examples-vite-vite*' run preview",
|
|
64
|
-
"multi-example": "pnpm --filter \"multi-example-*\" --parallel run start",
|
|
65
|
-
"test": "vitest",
|
|
66
|
-
"e2e": "playwright test"
|
|
67
70
|
}
|
|
68
|
-
}
|
|
71
|
+
}
|