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