@module-federation/vite 1.0.0-alpha-c13dc95 → 1.0.0-alpha-31ab3f9
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 +19 -6
- package/lib/index.cjs +664 -318
- package/lib/index.esm.js +666 -320
- package/lib/index.modern.js +646 -323
- package/lib/index.umd.js +667 -321
- package/lib/plugins/pluginDevProxyModuleTopLevelAwait.d.ts +2 -0
- package/lib/plugins/pluginMFManifest.d.ts +3 -0
- package/lib/utils/VirtualModule.d.ts +5 -3
- package/lib/utils/localSharedImportMap_temp.d.ts +2 -0
- package/lib/utils/normalizeModuleFederationOptions.d.ts +8 -2
- package/lib/utils/packageNameUtils.d.ts +7 -3
- package/lib/virtualModules/index.d.ts +4 -3
- package/lib/virtualModules/virtualRemoteEntry.d.ts +5 -0
- package/lib/virtualModules/virtualRemotes.d.ts +6 -8
- package/lib/virtualModules/virtualRuntimeInitStatus.d.ts +3 -0
- package/lib/virtualModules/virtualShared_preBuild.d.ts +0 -6
- package/package.json +2 -2
- package/lib/utils/localSharedImportMap_windows.d.ts +0 -2
package/lib/index.cjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
var fs = require('fs');
|
|
2
2
|
var path = require('pathe');
|
|
3
3
|
var pluginutils = require('@rollup/pluginutils');
|
|
4
|
-
var defu = require('defu');
|
|
5
4
|
var estreeWalker = require('estree-walker');
|
|
6
5
|
var MagicString = require('magic-string');
|
|
6
|
+
var defu = require('defu');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
9
|
|
|
@@ -37,9 +37,14 @@ var addEntry = function addEntry(_ref) {
|
|
|
37
37
|
var devEntryPath = entryPath.startsWith("virtual:mf") ? "/@id/" + entryPath : entryPath;
|
|
38
38
|
var entryFiles = [];
|
|
39
39
|
var htmlFilePath;
|
|
40
|
+
var _command;
|
|
40
41
|
return [{
|
|
41
42
|
name: 'add-entry',
|
|
42
43
|
apply: "serve",
|
|
44
|
+
config: function config(_config, _ref2) {
|
|
45
|
+
var command = _ref2.command;
|
|
46
|
+
_command = command;
|
|
47
|
+
},
|
|
43
48
|
configureServer: function configureServer(server) {
|
|
44
49
|
var _server$httpServer;
|
|
45
50
|
(_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', function () {
|
|
@@ -63,7 +68,6 @@ var addEntry = function addEntry(_ref) {
|
|
|
63
68
|
}, {
|
|
64
69
|
name: "add-entry",
|
|
65
70
|
enforce: "post",
|
|
66
|
-
apply: "build",
|
|
67
71
|
configResolved: function configResolved(config) {
|
|
68
72
|
var inputOptions = config.build.rollupOptions.input;
|
|
69
73
|
if (!inputOptions) {
|
|
@@ -81,6 +85,7 @@ var addEntry = function addEntry(_ref) {
|
|
|
81
85
|
},
|
|
82
86
|
buildStart: function buildStart() {
|
|
83
87
|
var _this$emitFile;
|
|
88
|
+
if (_command === "serve") return;
|
|
84
89
|
var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
|
|
85
90
|
this.emitFile((_this$emitFile = {
|
|
86
91
|
name: entryName
|
|
@@ -105,46 +110,105 @@ var addEntry = function addEntry(_ref) {
|
|
|
105
110
|
}];
|
|
106
111
|
};
|
|
107
112
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
var
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
function pluginModuleParseEnd (excludeFn) {
|
|
116
|
-
return [{
|
|
117
|
-
name: "_",
|
|
113
|
+
/**
|
|
114
|
+
* Solve the problem that dev mode dependency prebunding does not support top-level await syntax
|
|
115
|
+
*/
|
|
116
|
+
function PluginDevProxyModuleTopLevelAwait() {
|
|
117
|
+
var filterFunction = pluginutils.createFilter();
|
|
118
|
+
return {
|
|
119
|
+
name: "dev-proxy-module-top-level-await",
|
|
118
120
|
apply: "serve",
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
}, {
|
|
124
|
-
enforce: "pre",
|
|
125
|
-
name: "parseStart",
|
|
126
|
-
apply: "build",
|
|
127
|
-
load: function load(id) {
|
|
128
|
-
if (excludeFn(id)) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
parseStartSet.add(id);
|
|
132
|
-
}
|
|
133
|
-
}, {
|
|
134
|
-
enforce: "post",
|
|
135
|
-
name: "parseEnd",
|
|
136
|
-
apply: "build",
|
|
137
|
-
moduleParsed: function moduleParsed(module) {
|
|
138
|
-
var id = module.id;
|
|
139
|
-
if (excludeFn(id)) {
|
|
140
|
-
return;
|
|
121
|
+
transform: function transform(code, id) {
|
|
122
|
+
if (!code.includes("/*mf top-level-await placeholder replacement mf*/")) {
|
|
123
|
+
return null;
|
|
141
124
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
125
|
+
if (!filterFunction(id)) return null;
|
|
126
|
+
var ast;
|
|
127
|
+
try {
|
|
128
|
+
ast = this.parse(code, {
|
|
129
|
+
allowReturnOutsideFunction: true
|
|
130
|
+
});
|
|
131
|
+
} catch (e) {
|
|
132
|
+
throw new Error(id + ": " + e);
|
|
145
133
|
}
|
|
134
|
+
var magicString = new MagicString__default["default"](code);
|
|
135
|
+
estreeWalker.walk(ast, {
|
|
136
|
+
enter: function enter(node) {
|
|
137
|
+
if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
|
|
138
|
+
var exportSpecifiers = node.specifiers.map(function (specifier) {
|
|
139
|
+
return specifier.exported.name;
|
|
140
|
+
});
|
|
141
|
+
var proxyStatements = exportSpecifiers.map(function (name) {
|
|
142
|
+
return "\n const __mfproxy__await" + name + " = await " + name + "();\n const __mfproxy__" + name + " = () => __mfproxy__await" + name + ";\n ";
|
|
143
|
+
}).join('\n');
|
|
144
|
+
var exportStatements = exportSpecifiers.map(function (name) {
|
|
145
|
+
return "__mfproxy__" + name + " as " + name;
|
|
146
|
+
}).join(', ');
|
|
147
|
+
var start = node.start;
|
|
148
|
+
var end = node.end;
|
|
149
|
+
var replacement = proxyStatements + "\nexport { " + exportStatements + " };";
|
|
150
|
+
magicString.overwrite(start, end, replacement);
|
|
151
|
+
}
|
|
152
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
153
|
+
var declaration = node.declaration;
|
|
154
|
+
var _start = node.start;
|
|
155
|
+
var _end = node.end;
|
|
156
|
+
var proxyStatement;
|
|
157
|
+
var exportStatement = 'default';
|
|
158
|
+
if (declaration.type === 'Identifier') {
|
|
159
|
+
// example: export default foo;
|
|
160
|
+
proxyStatement = "\n const __mfproxy__awaitdefault = await " + declaration.name + "();\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
161
|
+
} else if (declaration.type === 'CallExpression' || declaration.type === 'FunctionDeclaration') {
|
|
162
|
+
// example: export default someFunction();
|
|
163
|
+
var declarationCode = code.slice(declaration.start, declaration.end);
|
|
164
|
+
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + declarationCode + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
165
|
+
} else {
|
|
166
|
+
// other
|
|
167
|
+
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + code.slice(declaration.start, declaration.end) + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
168
|
+
}
|
|
169
|
+
var _replacement = proxyStatement + "\nexport { __mfproxy__default as " + exportStatement + " };";
|
|
170
|
+
magicString.overwrite(_start, _end, _replacement);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
code: magicString.toString(),
|
|
176
|
+
map: magicString.generateMap({
|
|
177
|
+
hires: true
|
|
178
|
+
})
|
|
179
|
+
};
|
|
146
180
|
}
|
|
147
|
-
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function _arrayLikeToArray(r, a) {
|
|
185
|
+
(null == a || a > r.length) && (a = r.length);
|
|
186
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
187
|
+
return n;
|
|
188
|
+
}
|
|
189
|
+
function _createForOfIteratorHelperLoose(r, e) {
|
|
190
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
191
|
+
if (t) return (t = t.call(r)).next.bind(t);
|
|
192
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
|
|
193
|
+
t && (r = t);
|
|
194
|
+
var o = 0;
|
|
195
|
+
return function () {
|
|
196
|
+
return o >= r.length ? {
|
|
197
|
+
done: !0
|
|
198
|
+
} : {
|
|
199
|
+
done: !1,
|
|
200
|
+
value: r[o++]
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
205
|
+
}
|
|
206
|
+
function _unsupportedIterableToArray(r, a) {
|
|
207
|
+
if (r) {
|
|
208
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
209
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
210
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
211
|
+
}
|
|
148
212
|
}
|
|
149
213
|
|
|
150
214
|
function normalizeExposesItem(key, item) {
|
|
@@ -197,7 +261,7 @@ function normalizeRemoteItem(key, remote) {
|
|
|
197
261
|
entryGlobalName: key
|
|
198
262
|
}, remote);
|
|
199
263
|
}
|
|
200
|
-
function removePathFromNpmPackage
|
|
264
|
+
function removePathFromNpmPackage(packageString) {
|
|
201
265
|
// 匹配npm包名的正则表达式,忽略路径部分
|
|
202
266
|
var regex = /^(?:@[^/]+\/)?[^/]+/;
|
|
203
267
|
// 使用正则表达式匹配并提取包名
|
|
@@ -208,7 +272,7 @@ function removePathFromNpmPackage$1(packageString) {
|
|
|
208
272
|
function normalizeShareItem(key, shareItem) {
|
|
209
273
|
var version;
|
|
210
274
|
try {
|
|
211
|
-
version = require(path__namespace.join(removePathFromNpmPackage
|
|
275
|
+
version = require(path__namespace.join(removePathFromNpmPackage(key), 'package.json')).version;
|
|
212
276
|
} catch (e) {
|
|
213
277
|
console.log(e);
|
|
214
278
|
}
|
|
@@ -256,10 +320,28 @@ function normalizeLibrary(library) {
|
|
|
256
320
|
if (!library) return undefined;
|
|
257
321
|
return library;
|
|
258
322
|
}
|
|
323
|
+
function normalizeManifest(manifest) {
|
|
324
|
+
if (manifest === void 0) {
|
|
325
|
+
manifest = false;
|
|
326
|
+
}
|
|
327
|
+
if (typeof manifest === "boolean") {
|
|
328
|
+
return manifest;
|
|
329
|
+
}
|
|
330
|
+
return Object.assign({
|
|
331
|
+
filePath: "",
|
|
332
|
+
disableAssetsAnalyze: false,
|
|
333
|
+
fileName: "mf-manifest.json"
|
|
334
|
+
}, manifest);
|
|
335
|
+
}
|
|
259
336
|
var config;
|
|
260
337
|
function getNormalizeModuleFederationOptions() {
|
|
261
338
|
return config;
|
|
262
339
|
}
|
|
340
|
+
function getNormalizeShareItem(key) {
|
|
341
|
+
var options = getNormalizeModuleFederationOptions();
|
|
342
|
+
var shareItem = options.shared[removePathFromNpmPackage(key)] || options.shared[removePathFromNpmPackage(key) + "/"];
|
|
343
|
+
return shareItem;
|
|
344
|
+
}
|
|
263
345
|
function normalizeModuleFederationOptions(options) {
|
|
264
346
|
return config = {
|
|
265
347
|
exposes: normalizeExposes(options.exposes),
|
|
@@ -274,7 +356,7 @@ function normalizeModuleFederationOptions(options) {
|
|
|
274
356
|
runtimePlugins: options.runtimePlugins || [],
|
|
275
357
|
getPublicPath: options.getPublicPath,
|
|
276
358
|
implementation: options.implementation,
|
|
277
|
-
manifest: options.manifest,
|
|
359
|
+
manifest: normalizeManifest(options.manifest),
|
|
278
360
|
dev: options.dev,
|
|
279
361
|
dts: options.dts
|
|
280
362
|
};
|
|
@@ -289,33 +371,42 @@ function normalizeModuleFederationOptions(options) {
|
|
|
289
371
|
* . => 4
|
|
290
372
|
*/
|
|
291
373
|
/**
|
|
292
|
-
*
|
|
374
|
+
* Encodes a package name into a valid file name.
|
|
375
|
+
* @param {string} name - The package name, e.g., "@scope/xx-xx.xx".
|
|
376
|
+
* @returns {string} - The encoded file name.
|
|
293
377
|
*/
|
|
294
378
|
function packageNameEncode(name) {
|
|
295
379
|
if (typeof name !== "string") throw new Error("A string package name is required");
|
|
296
|
-
return name.replace(
|
|
380
|
+
return name.replace(/@/g, "_mf_0_").replace(/\//g, "_mf_1_").replace(/-/g, "_mf_2_").replace(/\./g, "_mf_3_");
|
|
297
381
|
}
|
|
298
382
|
/**
|
|
299
|
-
*
|
|
383
|
+
* Decodes an encoded file name back to the original package name.
|
|
384
|
+
* @param {string} encoded - The encoded file name, e.g., "_mf_0_scope_mf_1_xx_mf_2_xx_mf_3_xx".
|
|
385
|
+
* @returns {string} - The decoded package name.
|
|
300
386
|
*/
|
|
301
|
-
function packageNameDecode(
|
|
302
|
-
if (typeof
|
|
303
|
-
return
|
|
387
|
+
function packageNameDecode(encoded) {
|
|
388
|
+
if (typeof encoded !== "string") throw new Error("A string encoded file name is required");
|
|
389
|
+
return encoded.replace(/_mf_0_/g, "@").replace(/_mf_1_/g, "/").replace(/_mf_2_/g, "-").replace(/_mf_3_/g, ".");
|
|
304
390
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* https://github.com/module-federation/vite/issues/68
|
|
394
|
+
*/
|
|
395
|
+
function getLocalSharedImportMapPath_temp() {
|
|
396
|
+
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
397
|
+
name = _getNormalizeModuleFe.name;
|
|
398
|
+
return path__default["default"].resolve(".__mf__temp", packageNameEncode(name), "localSharedImportMap");
|
|
399
|
+
}
|
|
400
|
+
function writeLocalSharedImportMap_temp(content) {
|
|
401
|
+
var localSharedImportMapId = getLocalSharedImportMapPath_temp();
|
|
402
|
+
createFile(localSharedImportMapId + ".js", "\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n" + content);
|
|
312
403
|
}
|
|
313
|
-
function
|
|
314
|
-
var
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
404
|
+
function createFile(filePath, content) {
|
|
405
|
+
var dir = path__default["default"].dirname(filePath);
|
|
406
|
+
fs.mkdirSync(dir, {
|
|
407
|
+
recursive: true
|
|
408
|
+
});
|
|
409
|
+
fs.writeFileSync(filePath, content);
|
|
319
410
|
}
|
|
320
411
|
|
|
321
412
|
var nodeModulesDir = function findNodeModulesDir(startDir) {
|
|
@@ -341,28 +432,47 @@ fs.writeFileSync(path.resolve(nodeModulesDir, virtualPackageName, "package.json"
|
|
|
341
432
|
name: virtualPackageName,
|
|
342
433
|
main: "empty.js"
|
|
343
434
|
}));
|
|
435
|
+
var patternMap = {};
|
|
436
|
+
var cacheMap = {};
|
|
344
437
|
/**
|
|
345
438
|
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
346
439
|
*/
|
|
347
440
|
var VirtualModule = /*#__PURE__*/function () {
|
|
348
|
-
function VirtualModule(name,
|
|
349
|
-
|
|
350
|
-
|
|
441
|
+
function VirtualModule(name, tag, suffix) {
|
|
442
|
+
var _name$split$slice$pop;
|
|
443
|
+
if (tag === void 0) {
|
|
444
|
+
tag = '__mf_v__';
|
|
445
|
+
}
|
|
446
|
+
if (suffix === void 0) {
|
|
447
|
+
suffix = "";
|
|
351
448
|
}
|
|
352
|
-
this.
|
|
449
|
+
this.name = void 0;
|
|
450
|
+
this.tag = void 0;
|
|
451
|
+
this.suffix = void 0;
|
|
353
452
|
this.inited = false;
|
|
354
|
-
this.
|
|
355
|
-
this.
|
|
356
|
-
this.
|
|
453
|
+
this.name = name;
|
|
454
|
+
this.tag = tag;
|
|
455
|
+
this.suffix = suffix || ((_name$split$slice$pop = name.split(".").slice(1).pop()) == null ? void 0 : _name$split$slice$pop.replace(/(.)/, ".$1")) || ".js";
|
|
456
|
+
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
457
|
+
cacheMap[this.tag][this.name] = this;
|
|
357
458
|
}
|
|
459
|
+
VirtualModule.findModule = function findModule(tag, str) {
|
|
460
|
+
if (str === void 0) {
|
|
461
|
+
str = "";
|
|
462
|
+
}
|
|
463
|
+
if (!patternMap[tag]) patternMap[tag] = new RegExp("(.*" + packageNameEncode(tag) + "(.+?)" + packageNameEncode(tag) + ".*)");
|
|
464
|
+
var moduleName = (str.match(patternMap[tag]) || [])[2];
|
|
465
|
+
if (moduleName) return cacheMap[tag][packageNameDecode(moduleName)];
|
|
466
|
+
return undefined;
|
|
467
|
+
};
|
|
358
468
|
var _proto = VirtualModule.prototype;
|
|
359
469
|
_proto.getPath = function getPath() {
|
|
360
470
|
return path.resolve(nodeModulesDir, this.getImportId());
|
|
361
471
|
};
|
|
362
472
|
_proto.getImportId = function getImportId() {
|
|
363
473
|
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
364
|
-
|
|
365
|
-
return virtualPackageName + "/" + packageNameEncode(
|
|
474
|
+
mfName = _getNormalizeModuleFe.name;
|
|
475
|
+
return virtualPackageName + "/" + packageNameEncode("" + mfName + this.tag + this.name + this.tag) + this.suffix;
|
|
366
476
|
};
|
|
367
477
|
_proto.writeSync = function writeSync(code, force) {
|
|
368
478
|
if (!force && this.inited) return;
|
|
@@ -377,24 +487,33 @@ var VirtualModule = /*#__PURE__*/function () {
|
|
|
377
487
|
return VirtualModule;
|
|
378
488
|
}();
|
|
379
489
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
function getLocalSharedImportMapPath_windows() {
|
|
384
|
-
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
385
|
-
name = _getNormalizeModuleFe.name;
|
|
386
|
-
return path__default["default"].resolve(".__mf__win", packageNameEncode(name), "localSharedImportMap");
|
|
490
|
+
var virtualRuntimeInitStatus = new VirtualModule("runtimeInit");
|
|
491
|
+
function writeRuntimeInitStatus() {
|
|
492
|
+
virtualRuntimeInitStatus.writeSync("\n let initResolve, initReject\n const initPromise = new Promise((re, rj) => {\n initResolve = re\n initReject = rj\n })\n export {\n initPromise,\n initResolve,\n initReject\n }\n ");
|
|
387
493
|
}
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
494
|
+
|
|
495
|
+
var cacheRemoteMap = {};
|
|
496
|
+
var LOAD_REMOTE_TAG = '__loadRemote__';
|
|
497
|
+
function getRemoteVirtualModule(remote, command) {
|
|
498
|
+
if (!cacheRemoteMap[remote]) {
|
|
499
|
+
cacheRemoteMap[remote] = new VirtualModule(remote, LOAD_REMOTE_TAG, ".js");
|
|
500
|
+
cacheRemoteMap[remote].writeSync(generateRemotes(remote, command));
|
|
501
|
+
}
|
|
502
|
+
var virtual = cacheRemoteMap[remote];
|
|
503
|
+
return virtual;
|
|
391
504
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
505
|
+
var usedRemotesMap = {
|
|
506
|
+
// remote1: {remote1/App, remote1, remote1/Button}
|
|
507
|
+
};
|
|
508
|
+
function addUsedRemote(remoteKey, remoteModule) {
|
|
509
|
+
if (!usedRemotesMap[remoteKey]) usedRemotesMap[remoteKey] = new Set();
|
|
510
|
+
usedRemotesMap[remoteKey].add(remoteModule);
|
|
511
|
+
}
|
|
512
|
+
function getUsedRemotesMap() {
|
|
513
|
+
return usedRemotesMap;
|
|
514
|
+
}
|
|
515
|
+
function generateRemotes(id, command) {
|
|
516
|
+
return "\n const {loadRemote} = require(\"@module-federation/runtime\")\n const {initPromise} = require(\"" + virtualRuntimeInitStatus.getImportId() + "\")\n const res = initPromise.then(_ => loadRemote(" + JSON.stringify(id) + "))\n const exportModule = " + (command !== "build" ? "/*mf top-level-await placeholder replacement mf*/" : "await ") + "initPromise.then(_ => res)\n module.exports = exportModule\n ";
|
|
398
517
|
}
|
|
399
518
|
|
|
400
519
|
/**
|
|
@@ -403,99 +522,75 @@ function createFile(filePath, content) {
|
|
|
403
522
|
* You can only proxy to the real file through alias
|
|
404
523
|
*/
|
|
405
524
|
// *** __prebuild__
|
|
406
|
-
|
|
407
|
-
var generateLocalSharedImportMap = function generateLocalSharedImportMap() {
|
|
408
|
-
return Promise.resolve(parsePromise).then(function () {
|
|
409
|
-
var options = getNormalizeModuleFederationOptions();
|
|
410
|
-
return "\n const localSharedImportMap = {\n " + Array.from(shareds).map(function (pkg) {
|
|
411
|
-
return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\")\n return pkg\n }\n ";
|
|
412
|
-
}).join(",") + "\n }\n const localShared = {\n " + Array.from(shareds).map(function (key) {
|
|
413
|
-
var shareItem = options.shared[removePathFromNpmPackage(key)] || options.shared[removePathFromNpmPackage(key) + "/"];
|
|
414
|
-
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 localShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = localSharedImportMap \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 }\n }\n ";
|
|
415
|
-
}).join(',') + "\n }\n export default localShared\n ";
|
|
416
|
-
});
|
|
417
|
-
};
|
|
418
|
-
// *** __loadShare__
|
|
419
|
-
var writeLocalSharedImportMap = function writeLocalSharedImportMap() {
|
|
420
|
-
try {
|
|
421
|
-
var _exit;
|
|
422
|
-
var sharedCount = shareds.size;
|
|
423
|
-
return Promise.resolve(function () {
|
|
424
|
-
if (prevSharedCount !== sharedCount) {
|
|
425
|
-
var _temp2 = function _temp2(_result) {
|
|
426
|
-
if (_exit) return _result;
|
|
427
|
-
var _writeSync = localSharedImportMapModule.writeSync;
|
|
428
|
-
return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI2) {
|
|
429
|
-
return _writeSync.call(localSharedImportMapModule, _generateLocalSharedI2, true);
|
|
430
|
-
});
|
|
431
|
-
};
|
|
432
|
-
prevSharedCount = sharedCount;
|
|
433
|
-
var _temp = function () {
|
|
434
|
-
if (process.platform === "win32") {
|
|
435
|
-
return Promise.resolve(generateLocalSharedImportMap()).then(function (_generateLocalSharedI) {
|
|
436
|
-
var _writeLocalSharedImpo = writeLocalSharedImportMap_windows(_generateLocalSharedI);
|
|
437
|
-
_exit = 1;
|
|
438
|
-
return _writeLocalSharedImpo;
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
}();
|
|
442
|
-
return _temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp);
|
|
443
|
-
}
|
|
444
|
-
}());
|
|
445
|
-
} catch (e) {
|
|
446
|
-
return Promise.reject(e);
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
525
|
var preBuildCacheMap = {};
|
|
450
526
|
var PREBUILD_TAG = "__prebuild__";
|
|
451
527
|
function writePreBuildLibPath(pkg) {
|
|
452
|
-
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(
|
|
528
|
+
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(pkg, PREBUILD_TAG);
|
|
453
529
|
preBuildCacheMap[pkg].writeSync("");
|
|
454
530
|
}
|
|
455
531
|
function getPreBuildLibImportId(pkg) {
|
|
456
|
-
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(
|
|
532
|
+
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(pkg, PREBUILD_TAG);
|
|
457
533
|
var importId = preBuildCacheMap[pkg].getImportId();
|
|
458
534
|
return importId;
|
|
459
535
|
}
|
|
460
|
-
|
|
461
|
-
function addShare(pkg) {
|
|
462
|
-
shareds.add(pkg);
|
|
463
|
-
}
|
|
464
|
-
// *** Expose locally provided shared modules here
|
|
465
|
-
var localSharedImportMapModule = new VirtualModule("localSharedImportMap");
|
|
466
|
-
function getLocalSharedImportMapPath() {
|
|
467
|
-
if (process.platform === "win32") {
|
|
468
|
-
return getLocalSharedImportMapPath_windows();
|
|
469
|
-
}
|
|
470
|
-
return localSharedImportMapModule.getPath();
|
|
471
|
-
}
|
|
472
|
-
var prevSharedCount = 0;
|
|
536
|
+
// *** __loadShare__
|
|
473
537
|
var LOAD_SHARE_TAG = "__loadShare__";
|
|
474
538
|
var loadShareCacheMap = {};
|
|
475
539
|
function getLoadShareModulePath(pkg) {
|
|
476
|
-
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(LOAD_SHARE_TAG
|
|
540
|
+
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(pkg, LOAD_SHARE_TAG, ".js");
|
|
477
541
|
var filepath = loadShareCacheMap[pkg].getPath();
|
|
478
542
|
return filepath;
|
|
479
543
|
}
|
|
480
544
|
function writeLoadShareModule(pkg, shareItem, command) {
|
|
481
|
-
loadShareCacheMap[pkg].writeSync("\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 res = 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 ");
|
|
545
|
+
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 ");
|
|
482
546
|
}
|
|
483
547
|
|
|
548
|
+
var usedShares = new Set();
|
|
549
|
+
function getUsedShares() {
|
|
550
|
+
return usedShares;
|
|
551
|
+
}
|
|
552
|
+
function addUsedShares(pkg) {
|
|
553
|
+
usedShares.add(pkg);
|
|
554
|
+
}
|
|
555
|
+
// *** Expose locally provided shared modules here
|
|
556
|
+
new VirtualModule("localSharedImportMap");
|
|
557
|
+
function getLocalSharedImportMapPath() {
|
|
558
|
+
return getLocalSharedImportMapPath_temp();
|
|
559
|
+
// return localSharedImportMapModule.getPath()
|
|
560
|
+
}
|
|
561
|
+
var prevSharedCount;
|
|
562
|
+
function writeLocalSharedImportMap() {
|
|
563
|
+
var sharedCount = getUsedShares().size;
|
|
564
|
+
if (prevSharedCount !== sharedCount) {
|
|
565
|
+
prevSharedCount = sharedCount;
|
|
566
|
+
writeLocalSharedImportMap_temp(generateLocalSharedImportMap());
|
|
567
|
+
// localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true)
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
function generateLocalSharedImportMap() {
|
|
571
|
+
var options = getNormalizeModuleFederationOptions();
|
|
572
|
+
return "\n const importMap = {\n " + Array.from(getUsedShares()).map(function (pkg) {
|
|
573
|
+
return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\")\n return pkg\n }\n ";
|
|
574
|
+
}).join(",") + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).map(function (key) {
|
|
575
|
+
var shareItem = getNormalizeShareItem(key);
|
|
576
|
+
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 \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 }\n }\n ";
|
|
577
|
+
}).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
|
|
578
|
+
var remote = options.remotes[key];
|
|
579
|
+
return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
|
|
580
|
+
}).join(',') + "\n ]\n export {\n usedShared,\n usedRemotes\n }\n ";
|
|
581
|
+
}
|
|
484
582
|
var REMOTE_ENTRY_ID = 'virtual:mf-REMOTE_ENTRY_ID';
|
|
485
583
|
function generateRemoteEntry(options) {
|
|
486
584
|
var pluginImportNames = options.runtimePlugins.map(function (p, i) {
|
|
487
585
|
return ["$runtimePlugin_" + i, "import $runtimePlugin_" + i + " from \"" + p + "\";"];
|
|
488
586
|
});
|
|
489
|
-
return "\n import {init as runtimeInit, loadRemote} from \"@module-federation/runtime\";\n
|
|
587
|
+
return "\n import {init as runtimeInit, loadRemote} from \"@module-federation/runtime\";\n " + pluginImportNames.map(function (item) {
|
|
490
588
|
return item[1];
|
|
491
589
|
}).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
|
|
492
590
|
return "\n " + JSON.stringify(key) + ": async () => {\n const importModule = await import(" + JSON.stringify(options.exposes[key]["import"]) + ")\n const exportModule = {}\n Object.assign(exportModule, importModule)\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return exportModule\n }\n ";
|
|
493
|
-
}).join(',') + "\n }\n import
|
|
494
|
-
var remote = options.remotes[key];
|
|
495
|
-
return "\n {\n entryGlobalName: " + JSON.stringify(remote.entryGlobalName) + ",\n name: " + JSON.stringify(remote.name) + ",\n type: " + JSON.stringify(remote.type) + ",\n entry: " + JSON.stringify(remote.entry) + ",\n }\n ";
|
|
496
|
-
}).join(',') + "\n ],\n shared: localSharedImportMap,\n plugins: [" + pluginImportNames.map(function (item) {
|
|
591
|
+
}).join(',') + "\n }\n import {usedShared, usedRemotes} from \"" + getLocalSharedImportMapPath() + "\"\n import {\n initResolve\n } from \"" + virtualRuntimeInitStatus.getImportId() + "\"\n async function init(shared = {}) {\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: usedRemotes,\n shared: usedShared,\n plugins: [" + pluginImportNames.map(function (item) {
|
|
497
592
|
return item[0] + "()";
|
|
498
|
-
}).join(', ') + "]\n });\n initRes.initShareScopeMap('" + options.shareScope + "', shared);\n return initRes\n }\n\n function getExposes(moduleName) {\n if (!(moduleName in exposesMap)) throw new Error(`Module ${moduleName} does not exist in container.`)\n return (exposesMap[moduleName])().then(res => () => res)\n }\n export {\n init,\n getExposes as get\n }\n ";
|
|
593
|
+
}).join(', ') + "]\n });\n initRes.initShareScopeMap('" + options.shareScope + "', shared);\n initResolve(initRes)\n return initRes\n }\n\n function getExposes(moduleName) {\n if (!(moduleName in exposesMap)) throw new Error(`Module ${moduleName} does not exist in container.`)\n return (exposesMap[moduleName])().then(res => () => res)\n }\n export {\n init,\n getExposes as get\n }\n ";
|
|
499
594
|
}
|
|
500
595
|
/**
|
|
501
596
|
* Inject entry file, automatically init when used as host,
|
|
@@ -512,26 +607,401 @@ function getHostAutoInitPath() {
|
|
|
512
607
|
return hostAutoInitModule.getPath();
|
|
513
608
|
}
|
|
514
609
|
|
|
515
|
-
var remoteVirtualModule = new VirtualModule("remoteModule");
|
|
516
|
-
function writeRemote() {
|
|
517
|
-
remoteVirtualModule.writeSync("");
|
|
518
|
-
}
|
|
519
|
-
function generateRemotes(id, command) {
|
|
520
|
-
return {
|
|
521
|
-
code: "\n import {loadRemote} from \"@module-federation/runtime\"\n const exportModule = await loadRemote(" + JSON.stringify(id) + ")\n " + (command === "build" && "\n export default 'default' in (exportModule || {}) ? exportModule.default : undefined\n export const __mf__dynamicExports = exportModule\n " || "") + "\n " + (command !== "build" && "\n export default exportModule\n " || "") + "\n ",
|
|
522
|
-
map: null,
|
|
523
|
-
// TODO: vite dev mode invalid, use optimizeDeps.needsInterop
|
|
524
|
-
syntheticNamedExports: '__mf__dynamicExports'
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
|
|
528
610
|
function initVirtualModules() {
|
|
529
611
|
writeLocalSharedImportMap();
|
|
530
612
|
writeHostAutoInit();
|
|
531
|
-
|
|
613
|
+
writeRuntimeInitStatus();
|
|
532
614
|
}
|
|
533
615
|
|
|
534
|
-
var
|
|
616
|
+
var Manifest = function Manifest() {
|
|
617
|
+
var mfOptions = getNormalizeModuleFederationOptions();
|
|
618
|
+
var name = mfOptions.name,
|
|
619
|
+
filename = mfOptions.filename,
|
|
620
|
+
manifestOptions = mfOptions.manifest;
|
|
621
|
+
var mfManifestName = "";
|
|
622
|
+
if (manifestOptions === true) {
|
|
623
|
+
mfManifestName = "mf-manifest.json";
|
|
624
|
+
}
|
|
625
|
+
if (typeof manifestOptions !== "boolean") {
|
|
626
|
+
mfManifestName = path.join((manifestOptions == null ? void 0 : manifestOptions.filePath) || "", (manifestOptions == null ? void 0 : manifestOptions.fileName) || "");
|
|
627
|
+
}
|
|
628
|
+
var extensions;
|
|
629
|
+
var root;
|
|
630
|
+
var remoteEntryFile;
|
|
631
|
+
return [{
|
|
632
|
+
name: 'moddule-federation-manifest',
|
|
633
|
+
apply: 'serve',
|
|
634
|
+
configureServer: function configureServer(server) {
|
|
635
|
+
server.middlewares.use(function (req, res, next) {
|
|
636
|
+
if (!mfManifestName) {
|
|
637
|
+
next();
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (req.url === mfManifestName.replace(/^\/?/, "/")) {
|
|
641
|
+
res.setHeader('Content-Type', 'application/json');
|
|
642
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
643
|
+
res.end(JSON.stringify({
|
|
644
|
+
id: name,
|
|
645
|
+
name: name,
|
|
646
|
+
metaData: {
|
|
647
|
+
name: name,
|
|
648
|
+
type: 'app',
|
|
649
|
+
buildInfo: {
|
|
650
|
+
buildVersion: '1.0.0',
|
|
651
|
+
buildName: name
|
|
652
|
+
},
|
|
653
|
+
remoteEntry: {
|
|
654
|
+
name: filename,
|
|
655
|
+
path: '',
|
|
656
|
+
type: 'module'
|
|
657
|
+
},
|
|
658
|
+
ssrRemoteEntry: {
|
|
659
|
+
name: filename,
|
|
660
|
+
path: '',
|
|
661
|
+
type: 'module'
|
|
662
|
+
},
|
|
663
|
+
types: {
|
|
664
|
+
path: '',
|
|
665
|
+
name: ''
|
|
666
|
+
},
|
|
667
|
+
globalName: name,
|
|
668
|
+
pluginVersion: '0.2.5',
|
|
669
|
+
publicPath: 'auto'
|
|
670
|
+
},
|
|
671
|
+
shared: Array.from(getUsedShares()).map(function (shareKey) {
|
|
672
|
+
var shareItem = getNormalizeShareItem(shareKey);
|
|
673
|
+
return {
|
|
674
|
+
id: name + ":" + shareKey,
|
|
675
|
+
name: shareKey,
|
|
676
|
+
version: shareItem.version,
|
|
677
|
+
requiredVersion: shareItem.shareConfig.requiredVersion,
|
|
678
|
+
assets: {
|
|
679
|
+
js: {
|
|
680
|
+
async: [],
|
|
681
|
+
sync: []
|
|
682
|
+
},
|
|
683
|
+
css: {
|
|
684
|
+
async: [],
|
|
685
|
+
sync: []
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
};
|
|
689
|
+
}),
|
|
690
|
+
remotes: function () {
|
|
691
|
+
var remotes = [];
|
|
692
|
+
var usedRemotesMap = getUsedRemotesMap();
|
|
693
|
+
Object.keys(usedRemotesMap).forEach(function (remoteKey) {
|
|
694
|
+
var usedModules = Array.from(usedRemotesMap[remoteKey]);
|
|
695
|
+
usedModules.forEach(function (moduleKey) {
|
|
696
|
+
remotes.push({
|
|
697
|
+
federationContainerName: mfOptions.remotes[remoteKey].entry,
|
|
698
|
+
moduleName: moduleKey.replace(remoteKey, '').replace('/', ''),
|
|
699
|
+
alias: remoteKey,
|
|
700
|
+
entry: '*'
|
|
701
|
+
});
|
|
702
|
+
});
|
|
703
|
+
});
|
|
704
|
+
return remotes;
|
|
705
|
+
}(),
|
|
706
|
+
exposes: Object.keys(mfOptions.exposes).map(function (key) {
|
|
707
|
+
var formatKey = key.replace('./', '');
|
|
708
|
+
return {
|
|
709
|
+
id: name + ':' + formatKey,
|
|
710
|
+
name: formatKey,
|
|
711
|
+
assets: {
|
|
712
|
+
js: {
|
|
713
|
+
async: [],
|
|
714
|
+
sync: []
|
|
715
|
+
},
|
|
716
|
+
css: {
|
|
717
|
+
sync: [],
|
|
718
|
+
async: []
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
path: key
|
|
722
|
+
};
|
|
723
|
+
})
|
|
724
|
+
}));
|
|
725
|
+
} else {
|
|
726
|
+
next();
|
|
727
|
+
}
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}, {
|
|
731
|
+
name: 'moddule-federation-manifest',
|
|
732
|
+
enforce: 'post',
|
|
733
|
+
config: function config(_config) {
|
|
734
|
+
if (!_config.build) _config.build = {};
|
|
735
|
+
if (!_config.build.manifest) _config.build.manifest = _config.build.manifest || !!manifestOptions;
|
|
736
|
+
},
|
|
737
|
+
configResolved: function configResolved(config) {
|
|
738
|
+
root = config.root;
|
|
739
|
+
extensions = config.resolve.extensions || ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'];
|
|
740
|
+
},
|
|
741
|
+
generateBundle: function generateBundle(options, bundle) {
|
|
742
|
+
try {
|
|
743
|
+
var _this = this;
|
|
744
|
+
// 递归查找模块的同步导入文件
|
|
745
|
+
var _findSynchronousImports = function findSynchronousImports(fileName, array) {
|
|
746
|
+
var fileData = bundle[fileName];
|
|
747
|
+
if (fileData && fileData.type === 'chunk') {
|
|
748
|
+
array.push(fileName); // 将当前文件加入预加载列表
|
|
749
|
+
// 遍历该文件的同步导入文件
|
|
750
|
+
fileData.imports.forEach(function (importedFile) {
|
|
751
|
+
if (array.indexOf(importedFile) === -1) {
|
|
752
|
+
_findSynchronousImports(importedFile, array); // 递归查找同步导入的文件
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
};
|
|
757
|
+
if (!mfManifestName) return Promise.resolve();
|
|
758
|
+
var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
|
|
759
|
+
return mfOptions.exposes[item]["import"];
|
|
760
|
+
}); // 获取你提供的 moduleIds
|
|
761
|
+
var filesContainingModules = {};
|
|
762
|
+
// 帮助函数:检查模块路径是否匹配
|
|
763
|
+
var isModuleMatched = function isModuleMatched(relativeModulePath, preloadModule) {
|
|
764
|
+
// 先尝试直接匹配
|
|
765
|
+
if (relativeModulePath === preloadModule) return true;
|
|
766
|
+
// 如果 preloadModule 没有后缀,尝试添加可能的后缀进行匹配
|
|
767
|
+
for (var _iterator = _createForOfIteratorHelperLoose(extensions), _step; !(_step = _iterator()).done;) {
|
|
768
|
+
var ext = _step.value;
|
|
769
|
+
if (relativeModulePath === "" + preloadModule + ext) {
|
|
770
|
+
return true;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
return false;
|
|
774
|
+
};
|
|
775
|
+
// 遍历打包生成的每个文件
|
|
776
|
+
for (var _i = 0, _Object$entries = Object.entries(bundle); _i < _Object$entries.length; _i++) {
|
|
777
|
+
var _Object$entries$_i = _Object$entries[_i],
|
|
778
|
+
fileName = _Object$entries$_i[0],
|
|
779
|
+
fileData = _Object$entries$_i[1];
|
|
780
|
+
if (mfOptions.filename.replace(/[\[\]]/g, "_") === fileData.name) {
|
|
781
|
+
remoteEntryFile = fileData.fileName;
|
|
782
|
+
}
|
|
783
|
+
if (fileData.type === 'chunk') {
|
|
784
|
+
// 遍历该文件的所有模块
|
|
785
|
+
for (var _i2 = 0, _Object$keys = Object.keys(fileData.modules); _i2 < _Object$keys.length; _i2++) {
|
|
786
|
+
var modulePath = _Object$keys[_i2];
|
|
787
|
+
// 将绝对路径转换为相对于 Vite root 的相对路径
|
|
788
|
+
var relativeModulePath = path.relative(root, modulePath);
|
|
789
|
+
// 检查模块是否在 preloadModules 列表中
|
|
790
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(exposesModules), _step2; !(_step2 = _iterator2()).done;) {
|
|
791
|
+
var preloadModule = _step2.value;
|
|
792
|
+
var formatPreloadModule = preloadModule.replace("./", "");
|
|
793
|
+
if (isModuleMatched(relativeModulePath, formatPreloadModule)) {
|
|
794
|
+
var _filesContainingModul;
|
|
795
|
+
if (!filesContainingModules[preloadModule]) {
|
|
796
|
+
filesContainingModules[preloadModule] = {
|
|
797
|
+
sync: [],
|
|
798
|
+
async: []
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
console.log(Object.keys(fileData.modules));
|
|
802
|
+
filesContainingModules[preloadModule].sync.push(fileName);
|
|
803
|
+
(_filesContainingModul = filesContainingModules[preloadModule].async).push.apply(_filesContainingModul, fileData.dynamicImports || []);
|
|
804
|
+
_findSynchronousImports(fileName, filesContainingModules[preloadModule].sync);
|
|
805
|
+
break; // 如果找到匹配,跳出循环
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
;
|
|
812
|
+
var fileToShareKey = {};
|
|
813
|
+
return Promise.resolve(Promise.all(Array.from(getUsedShares()).map(function (shareKey) {
|
|
814
|
+
try {
|
|
815
|
+
return Promise.resolve(_this.resolve(getPreBuildLibImportId(shareKey))).then(function (_this$resolve) {
|
|
816
|
+
var file = _this$resolve.id.split("?")[0];
|
|
817
|
+
fileToShareKey[file] = shareKey;
|
|
818
|
+
});
|
|
819
|
+
} catch (e) {
|
|
820
|
+
return Promise.reject(e);
|
|
821
|
+
}
|
|
822
|
+
}))).then(function () {
|
|
823
|
+
// 遍历打包生成的每个文件
|
|
824
|
+
for (var _i3 = 0, _Object$entries2 = Object.entries(bundle); _i3 < _Object$entries2.length; _i3++) {
|
|
825
|
+
var _Object$entries2$_i = _Object$entries2[_i3],
|
|
826
|
+
_fileName = _Object$entries2$_i[0],
|
|
827
|
+
_fileData = _Object$entries2$_i[1];
|
|
828
|
+
if (_fileData.type === 'chunk') {
|
|
829
|
+
// 遍历该文件的所有模块
|
|
830
|
+
for (var _i4 = 0, _Object$keys2 = Object.keys(_fileData.modules); _i4 < _Object$keys2.length; _i4++) {
|
|
831
|
+
var _modulePath = _Object$keys2[_i4];
|
|
832
|
+
var sharedKey = fileToShareKey[_modulePath];
|
|
833
|
+
if (sharedKey) {
|
|
834
|
+
var _filesContainingModul2;
|
|
835
|
+
if (!filesContainingModules[sharedKey]) {
|
|
836
|
+
filesContainingModules[sharedKey] = {
|
|
837
|
+
sync: [],
|
|
838
|
+
async: []
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
filesContainingModules[sharedKey].sync.push(_fileName);
|
|
842
|
+
(_filesContainingModul2 = filesContainingModules[sharedKey].async).push.apply(_filesContainingModul2, _fileData.dynamicImports || []);
|
|
843
|
+
_findSynchronousImports(_fileName, filesContainingModules[sharedKey].sync);
|
|
844
|
+
break; // 如果找到匹配,跳出循环
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
Object.keys(filesContainingModules).forEach(function (key) {
|
|
850
|
+
filesContainingModules[key].sync = Array.from(new Set(filesContainingModules[key].sync));
|
|
851
|
+
filesContainingModules[key].async = Array.from(new Set(filesContainingModules[key].async));
|
|
852
|
+
});
|
|
853
|
+
_this.emitFile({
|
|
854
|
+
type: 'asset',
|
|
855
|
+
fileName: mfManifestName,
|
|
856
|
+
source: generateMFManifest(filesContainingModules)
|
|
857
|
+
});
|
|
858
|
+
});
|
|
859
|
+
} catch (e) {
|
|
860
|
+
return Promise.reject(e);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}];
|
|
864
|
+
function generateMFManifest(preloadMap) {
|
|
865
|
+
var options = getNormalizeModuleFederationOptions();
|
|
866
|
+
var name = options.name;
|
|
867
|
+
var remoteEntry = {
|
|
868
|
+
name: remoteEntryFile,
|
|
869
|
+
path: '',
|
|
870
|
+
type: 'module'
|
|
871
|
+
};
|
|
872
|
+
var remotes = [];
|
|
873
|
+
var usedRemotesMap = getUsedRemotesMap();
|
|
874
|
+
Object.keys(usedRemotesMap).forEach(function (remoteKey) {
|
|
875
|
+
var usedModules = Array.from(usedRemotesMap[remoteKey]);
|
|
876
|
+
usedModules.forEach(function (moduleKey) {
|
|
877
|
+
remotes.push({
|
|
878
|
+
federationContainerName: options.remotes[remoteKey].entry,
|
|
879
|
+
moduleName: moduleKey.replace(remoteKey, '').replace('/', ''),
|
|
880
|
+
alias: remoteKey,
|
|
881
|
+
entry: '*'
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
});
|
|
885
|
+
// @ts-ignore
|
|
886
|
+
var shared = Array.from(getUsedShares()).map(function (shareKey) {
|
|
887
|
+
// assets(.css, .jpg, .svg等)其他资源, 不重要, 暂未处理
|
|
888
|
+
if (!preloadMap[shareKey]) return;
|
|
889
|
+
var shareItem = getNormalizeShareItem(shareKey);
|
|
890
|
+
return {
|
|
891
|
+
id: name + ":" + shareKey,
|
|
892
|
+
name: shareKey,
|
|
893
|
+
version: shareItem.version,
|
|
894
|
+
requiredVersion: shareItem.shareConfig.requiredVersion,
|
|
895
|
+
assets: {
|
|
896
|
+
js: {
|
|
897
|
+
async: preloadMap[shareKey].async,
|
|
898
|
+
sync: preloadMap[shareKey].sync
|
|
899
|
+
},
|
|
900
|
+
css: {
|
|
901
|
+
async: [],
|
|
902
|
+
sync: []
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
}).filter(function (item) {
|
|
907
|
+
return item;
|
|
908
|
+
});
|
|
909
|
+
var exposes = Object.keys(options.exposes).map(function (key) {
|
|
910
|
+
// assets(.css, .jpg, .svg等)其他资源, 不重要, 暂未处理
|
|
911
|
+
var formatKey = key.replace('./', '');
|
|
912
|
+
var sourceFile = options.exposes[key]["import"];
|
|
913
|
+
if (!preloadMap[sourceFile]) return;
|
|
914
|
+
return {
|
|
915
|
+
id: name + ':' + formatKey,
|
|
916
|
+
name: formatKey,
|
|
917
|
+
assets: {
|
|
918
|
+
js: {
|
|
919
|
+
async: preloadMap[sourceFile].async,
|
|
920
|
+
sync: preloadMap[sourceFile].sync
|
|
921
|
+
},
|
|
922
|
+
css: {
|
|
923
|
+
sync: [],
|
|
924
|
+
async: []
|
|
925
|
+
}
|
|
926
|
+
},
|
|
927
|
+
path: key
|
|
928
|
+
};
|
|
929
|
+
}).filter(function (item) {
|
|
930
|
+
return item;
|
|
931
|
+
}); // Filter out any null values
|
|
932
|
+
var result = {
|
|
933
|
+
id: name,
|
|
934
|
+
name: name,
|
|
935
|
+
metaData: {
|
|
936
|
+
name: name,
|
|
937
|
+
type: 'app',
|
|
938
|
+
buildInfo: {
|
|
939
|
+
buildVersion: '1.0.0',
|
|
940
|
+
buildName: name
|
|
941
|
+
},
|
|
942
|
+
remoteEntry: remoteEntry,
|
|
943
|
+
ssrRemoteEntry: remoteEntry,
|
|
944
|
+
types: {
|
|
945
|
+
path: '',
|
|
946
|
+
name: ''
|
|
947
|
+
// "zip": "@mf-types.zip",
|
|
948
|
+
// "api": "@mf-types.d.ts"
|
|
949
|
+
},
|
|
950
|
+
globalName: name,
|
|
951
|
+
pluginVersion: '0.2.5',
|
|
952
|
+
publicPath: 'auto'
|
|
953
|
+
},
|
|
954
|
+
shared: shared,
|
|
955
|
+
remotes: remotes,
|
|
956
|
+
exposes: exposes
|
|
957
|
+
};
|
|
958
|
+
return JSON.stringify(result);
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
var _resolve,
|
|
963
|
+
promise = new Promise(function (resolve, reject) {
|
|
964
|
+
_resolve = resolve;
|
|
965
|
+
});
|
|
966
|
+
var parsePromise = promise;
|
|
967
|
+
var parseStartSet = new Set();
|
|
968
|
+
var parseEndSet = new Set();
|
|
969
|
+
function pluginModuleParseEnd (excludeFn) {
|
|
970
|
+
return [{
|
|
971
|
+
name: "_",
|
|
972
|
+
apply: "serve",
|
|
973
|
+
config: function config() {
|
|
974
|
+
// No waiting in development mode
|
|
975
|
+
_resolve(1);
|
|
976
|
+
}
|
|
977
|
+
}, {
|
|
978
|
+
enforce: "pre",
|
|
979
|
+
name: "parseStart",
|
|
980
|
+
apply: "build",
|
|
981
|
+
load: function load(id) {
|
|
982
|
+
if (excludeFn(id)) {
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
parseStartSet.add(id);
|
|
986
|
+
}
|
|
987
|
+
}, {
|
|
988
|
+
enforce: "post",
|
|
989
|
+
name: "parseEnd",
|
|
990
|
+
apply: "build",
|
|
991
|
+
moduleParsed: function moduleParsed(module) {
|
|
992
|
+
var id = module.id;
|
|
993
|
+
if (excludeFn(id)) {
|
|
994
|
+
return;
|
|
995
|
+
}
|
|
996
|
+
parseEndSet.add(id);
|
|
997
|
+
if (parseStartSet.size === parseEndSet.size) {
|
|
998
|
+
_resolve(1);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}];
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
var filter = pluginutils.createFilter();
|
|
535
1005
|
function pluginProxyRemoteEntry () {
|
|
536
1006
|
return {
|
|
537
1007
|
name: 'proxyRemoteEntry',
|
|
@@ -543,14 +1013,18 @@ function pluginProxyRemoteEntry () {
|
|
|
543
1013
|
},
|
|
544
1014
|
load: function load(id) {
|
|
545
1015
|
if (id === REMOTE_ENTRY_ID) {
|
|
546
|
-
return
|
|
1016
|
+
return parsePromise.then(function (_) {
|
|
1017
|
+
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1018
|
+
});
|
|
547
1019
|
}
|
|
548
1020
|
},
|
|
549
1021
|
transform: function transform(code, id) {
|
|
550
1022
|
try {
|
|
551
|
-
if (!filter
|
|
1023
|
+
if (!filter(id)) return Promise.resolve();
|
|
552
1024
|
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
553
|
-
return Promise.resolve(
|
|
1025
|
+
return Promise.resolve(parsePromise.then(function (_) {
|
|
1026
|
+
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1027
|
+
}));
|
|
554
1028
|
}
|
|
555
1029
|
return Promise.resolve();
|
|
556
1030
|
} catch (e) {
|
|
@@ -560,50 +1034,25 @@ function pluginProxyRemoteEntry () {
|
|
|
560
1034
|
};
|
|
561
1035
|
}
|
|
562
1036
|
|
|
563
|
-
|
|
1037
|
+
pluginutils.createFilter();
|
|
564
1038
|
function pluginProxyRemotes (options) {
|
|
565
|
-
var command;
|
|
566
1039
|
var remotes = options.remotes;
|
|
567
|
-
var matchRemotesList = Object.keys(remotes).map(function (item) {
|
|
568
|
-
return item.replace(/\//g, "_");
|
|
569
|
-
});
|
|
570
1040
|
return {
|
|
571
1041
|
name: "proxyRemotes",
|
|
572
1042
|
config: function config(_config, _ref) {
|
|
573
1043
|
var _command = _ref.command;
|
|
574
|
-
command = _command;
|
|
575
1044
|
Object.keys(remotes).forEach(function (key) {
|
|
576
1045
|
var remote = remotes[key];
|
|
577
1046
|
_config.resolve.alias.push({
|
|
578
|
-
find: new RegExp("(" + remote.name + "(/.*|$)
|
|
579
|
-
replacement:
|
|
1047
|
+
find: new RegExp("^(" + remote.name + "(/.*|$))"),
|
|
1048
|
+
replacement: "$1",
|
|
580
1049
|
customResolver: function customResolver(source) {
|
|
581
|
-
var
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
if (_config.optimizeDeps.needsInterop.indexOf(requestPath) === -1) _config.optimizeDeps.needsInterop.push(requestPath);
|
|
585
|
-
return this.resolve(requestPath);
|
|
1050
|
+
var remoteModule = getRemoteVirtualModule(source, _command);
|
|
1051
|
+
addUsedRemote(remote.name, source);
|
|
1052
|
+
return remoteModule.getPath();
|
|
586
1053
|
}
|
|
587
1054
|
});
|
|
588
1055
|
});
|
|
589
|
-
},
|
|
590
|
-
transform: function transform(code, id) {
|
|
591
|
-
try {
|
|
592
|
-
if (!filter(id)) return Promise.resolve();
|
|
593
|
-
var _ref2 = matchRemotesList.length && id.match(new RegExp("/(" + matchRemotesList.join('|') + ")(_.*.js|.js)")) || [],
|
|
594
|
-
devRemoteModuleName = _ref2[0];
|
|
595
|
-
if (devRemoteModuleName) {
|
|
596
|
-
return Promise.resolve(generateRemotes(devRemoteModuleName.replace('/', '').replace(/_/g, '/').replace('.js', ''), command));
|
|
597
|
-
}
|
|
598
|
-
var _ref3 = id.match(/\_\_moduleRemote\_\_=[^&]+/) || [],
|
|
599
|
-
prodRemoteName = _ref3[0];
|
|
600
|
-
if (prodRemoteName) {
|
|
601
|
-
return Promise.resolve(generateRemotes(decodeURIComponent(prodRemoteName.replace('__moduleRemote__=', '')), command));
|
|
602
|
-
}
|
|
603
|
-
return Promise.resolve();
|
|
604
|
-
} catch (e) {
|
|
605
|
-
return Promise.reject(e);
|
|
606
|
-
}
|
|
607
1056
|
}
|
|
608
1057
|
};
|
|
609
1058
|
}
|
|
@@ -645,44 +1094,24 @@ var PromiseStore = /*#__PURE__*/function () {
|
|
|
645
1094
|
return PromiseStore;
|
|
646
1095
|
}();
|
|
647
1096
|
|
|
648
|
-
function wrapManualChunks(output, manualChunksCb) {
|
|
649
|
-
if (!output.manualChunks) output.manualChunks = {};
|
|
650
|
-
var wrapManualChunks = function wrapManualChunks(original) {
|
|
651
|
-
return function (id) {
|
|
652
|
-
var args = [].slice.call(arguments, 1);
|
|
653
|
-
var customRes = manualChunksCb == null ? void 0 : manualChunksCb.apply(void 0, [id].concat(args));
|
|
654
|
-
if (customRes) return customRes;
|
|
655
|
-
if (typeof original === 'function') {
|
|
656
|
-
return original.apply(void 0, [id].concat(args));
|
|
657
|
-
}
|
|
658
|
-
if (typeof original === 'object' && original) {
|
|
659
|
-
return original[id];
|
|
660
|
-
}
|
|
661
|
-
};
|
|
662
|
-
};
|
|
663
|
-
output.manualChunks = wrapManualChunks(output.manualChunks);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
1097
|
function proxySharedModule(options) {
|
|
667
1098
|
var _options$shared = options.shared,
|
|
668
|
-
shared = _options$shared === void 0 ? {} : _options$shared
|
|
669
|
-
include = options.include,
|
|
670
|
-
exclude = options.exclude;
|
|
671
|
-
var filterFunction = pluginutils.createFilter(include, exclude);
|
|
1099
|
+
shared = _options$shared === void 0 ? {} : _options$shared;
|
|
672
1100
|
return [{
|
|
673
1101
|
name: "generateLocalSharedImportMap",
|
|
674
1102
|
enforce: "post",
|
|
675
|
-
resolveId: function resolveId(id) {
|
|
676
|
-
if (id.includes(getLocalSharedImportMapPath())) return id;
|
|
677
|
-
},
|
|
678
1103
|
load: function load(id) {
|
|
679
1104
|
if (id.includes(getLocalSharedImportMapPath())) {
|
|
680
|
-
return
|
|
1105
|
+
return parsePromise.then(function (_) {
|
|
1106
|
+
return generateLocalSharedImportMap();
|
|
1107
|
+
});
|
|
681
1108
|
}
|
|
682
1109
|
},
|
|
683
1110
|
transform: function transform(code, id) {
|
|
684
1111
|
if (id.includes(getLocalSharedImportMapPath())) {
|
|
685
|
-
return
|
|
1112
|
+
return parsePromise.then(function (_) {
|
|
1113
|
+
return generateLocalSharedImportMap();
|
|
1114
|
+
});
|
|
686
1115
|
}
|
|
687
1116
|
}
|
|
688
1117
|
}, {
|
|
@@ -691,19 +1120,6 @@ function proxySharedModule(options) {
|
|
|
691
1120
|
config: function config(_config, _ref) {
|
|
692
1121
|
var _config$resolve$alias, _config$resolve$alias2;
|
|
693
1122
|
var command = _ref.command;
|
|
694
|
-
if (!_config.build) _config.build = {};
|
|
695
|
-
if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
|
|
696
|
-
var rollupOptions = _config.build.rollupOptions;
|
|
697
|
-
if (!rollupOptions.output) rollupOptions.output = {};
|
|
698
|
-
wrapManualChunks(_config.build.rollupOptions.output, function (id) {
|
|
699
|
-
// https://github.com/module-federation/vite/issues/40#issuecomment-2311434503
|
|
700
|
-
if (id.includes('/preload-helper.js')) {
|
|
701
|
-
return "preload-helper";
|
|
702
|
-
}
|
|
703
|
-
if (id.includes("node_modules/@module-federation/runtime")) {
|
|
704
|
-
return "@module-federation/runtime";
|
|
705
|
-
}
|
|
706
|
-
});
|
|
707
1123
|
(_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
|
|
708
1124
|
var pattern = key.endsWith("/") ? "(^" + key.replace(/\/$/, "") + "(/.+)?$)" : "(^" + key + "$)";
|
|
709
1125
|
return {
|
|
@@ -711,12 +1127,10 @@ function proxySharedModule(options) {
|
|
|
711
1127
|
find: new RegExp(pattern),
|
|
712
1128
|
replacement: "$1",
|
|
713
1129
|
customResolver: function customResolver(source, importer) {
|
|
714
|
-
var _config$optimizeDeps;
|
|
715
1130
|
var loadSharePath = getLoadShareModulePath(source);
|
|
716
|
-
_config == null || (_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.needsInterop) == null || _config$optimizeDeps.push(loadSharePath);
|
|
717
1131
|
writeLoadShareModule(source, shared[key], command);
|
|
718
1132
|
writePreBuildLibPath(source);
|
|
719
|
-
|
|
1133
|
+
addUsedShares(source);
|
|
720
1134
|
writeLocalSharedImportMap();
|
|
721
1135
|
return this.resolve(loadSharePath);
|
|
722
1136
|
}
|
|
@@ -725,20 +1139,18 @@ function proxySharedModule(options) {
|
|
|
725
1139
|
var savePrebuild = new PromiseStore();
|
|
726
1140
|
(_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
|
|
727
1141
|
return command === "build" ? {
|
|
728
|
-
find: new RegExp("(.*" + PREBUILD_TAG + "
|
|
1142
|
+
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
729
1143
|
replacement: function replacement($1) {
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
return packageNameDecode(pkgName);
|
|
1144
|
+
var pkgName = VirtualModule.findModule(PREBUILD_TAG, $1).name;
|
|
1145
|
+
return pkgName;
|
|
733
1146
|
}
|
|
734
1147
|
} : {
|
|
735
|
-
find: new RegExp("(.*" + PREBUILD_TAG + "
|
|
1148
|
+
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
736
1149
|
replacement: "$1",
|
|
737
1150
|
customResolver: function customResolver(source, importer) {
|
|
738
1151
|
try {
|
|
739
1152
|
var _this = this;
|
|
740
|
-
|
|
741
|
-
var pkgName = packageNameDecode(source.split(PREBUILD_TAG)[1]);
|
|
1153
|
+
var pkgName = VirtualModule.findModule(PREBUILD_TAG, source).name;
|
|
742
1154
|
if (importer.includes(LOAD_SHARE_TAG)) {
|
|
743
1155
|
// save pre-bunding module id
|
|
744
1156
|
savePrebuild.set(pkgName, _this.resolve(pkgName).then(function (item) {
|
|
@@ -773,69 +1185,6 @@ function proxySharedModule(options) {
|
|
|
773
1185
|
watch.ignored = [].concat(watch.ignored);
|
|
774
1186
|
watch.ignored.push("!**" + getLocalSharedImportMapPath() + "**");
|
|
775
1187
|
}
|
|
776
|
-
}, {
|
|
777
|
-
name: "prebuild-top-level-await",
|
|
778
|
-
apply: "serve",
|
|
779
|
-
transform: function transform(code, id) {
|
|
780
|
-
if (!code.includes("/*mf top-level-await placeholder replacement mf*/")) {
|
|
781
|
-
return null;
|
|
782
|
-
}
|
|
783
|
-
if (!filterFunction(id)) return null;
|
|
784
|
-
var ast;
|
|
785
|
-
try {
|
|
786
|
-
ast = this.parse(code, {
|
|
787
|
-
allowReturnOutsideFunction: true
|
|
788
|
-
});
|
|
789
|
-
} catch (e) {
|
|
790
|
-
throw new Error(id + ": " + e);
|
|
791
|
-
}
|
|
792
|
-
var magicString = new MagicString__default["default"](code);
|
|
793
|
-
estreeWalker.walk(ast, {
|
|
794
|
-
enter: function enter(node) {
|
|
795
|
-
if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
|
|
796
|
-
var exportSpecifiers = node.specifiers.map(function (specifier) {
|
|
797
|
-
return specifier.exported.name;
|
|
798
|
-
});
|
|
799
|
-
var proxyStatements = exportSpecifiers.map(function (name) {
|
|
800
|
-
return "\n const __mfproxy__await" + name + " = await " + name + "();\n const __mfproxy__" + name + " = () => __mfproxy__await" + name + ";\n ";
|
|
801
|
-
}).join('\n');
|
|
802
|
-
var exportStatements = exportSpecifiers.map(function (name) {
|
|
803
|
-
return "__mfproxy__" + name + " as " + name;
|
|
804
|
-
}).join(', ');
|
|
805
|
-
var start = node.start;
|
|
806
|
-
var end = node.end;
|
|
807
|
-
var replacement = proxyStatements + "\nexport { " + exportStatements + " };";
|
|
808
|
-
magicString.overwrite(start, end, replacement);
|
|
809
|
-
}
|
|
810
|
-
if (node.type === 'ExportDefaultDeclaration') {
|
|
811
|
-
var declaration = node.declaration;
|
|
812
|
-
var _start = node.start;
|
|
813
|
-
var _end = node.end;
|
|
814
|
-
var proxyStatement;
|
|
815
|
-
var exportStatement = 'default';
|
|
816
|
-
if (declaration.type === 'Identifier') {
|
|
817
|
-
// example: export default foo;
|
|
818
|
-
proxyStatement = "\n const __mfproxy__awaitdefault = await " + declaration.name + "();\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
819
|
-
} else if (declaration.type === 'CallExpression' || declaration.type === 'FunctionDeclaration') {
|
|
820
|
-
// example: export default someFunction();
|
|
821
|
-
var declarationCode = code.slice(declaration.start, declaration.end);
|
|
822
|
-
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + declarationCode + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
823
|
-
} else {
|
|
824
|
-
// other
|
|
825
|
-
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + code.slice(declaration.start, declaration.end) + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
826
|
-
}
|
|
827
|
-
var _replacement = proxyStatement + "\nexport { __mfproxy__default as " + exportStatement + " };";
|
|
828
|
-
magicString.overwrite(_start, _end, _replacement);
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
});
|
|
832
|
-
return {
|
|
833
|
-
code: magicString.toString(),
|
|
834
|
-
map: magicString.generateMap({
|
|
835
|
-
hires: true
|
|
836
|
-
})
|
|
837
|
-
};
|
|
838
|
-
}
|
|
839
1188
|
}];
|
|
840
1189
|
}
|
|
841
1190
|
|
|
@@ -891,7 +1240,7 @@ function federation(mfUserOptions) {
|
|
|
891
1240
|
return id.includes(getHostAutoInitImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(getLocalSharedImportMapPath());
|
|
892
1241
|
}), proxySharedModule({
|
|
893
1242
|
shared: shared
|
|
894
|
-
}), [{
|
|
1243
|
+
}), [PluginDevProxyModuleTopLevelAwait(), {
|
|
895
1244
|
name: 'module-federation-vite',
|
|
896
1245
|
enforce: 'post',
|
|
897
1246
|
config: function config(_config, _ref) {
|
|
@@ -901,11 +1250,8 @@ function federation(mfUserOptions) {
|
|
|
901
1250
|
replacement: require.resolve('@module-federation/runtime')
|
|
902
1251
|
});
|
|
903
1252
|
(_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push('@module-federation/runtime');
|
|
904
|
-
// Object.keys(shared).forEach((key) => {
|
|
905
|
-
// config.optimizeDeps?.include?.push(key);
|
|
906
|
-
// });
|
|
907
1253
|
}
|
|
908
|
-
}]);
|
|
1254
|
+
}], Manifest());
|
|
909
1255
|
}
|
|
910
1256
|
|
|
911
1257
|
exports.federation = federation;
|