@module-federation/vite 1.0.0-alpha-80b7215 → 1.0.0-alpha-4a73cd4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.cjs +408 -349
- package/lib/index.esm.js +409 -348
- package/lib/index.modern.js +449 -337
- package/lib/index.umd.js +410 -351
- package/lib/plugins/pluginProxyRemoteEntry.d.ts +2 -0
- package/lib/plugins/pluginProxyRemotes.d.ts +3 -0
- package/lib/plugins/pluginProxySharedModule_preBuild.d.ts +7 -0
- package/lib/utils/normalizeModuleFederationOptions.d.ts +2 -1
- package/lib/virtualModules/virtualRemoteEntry.d.ts +11 -0
- package/lib/virtualModules/virtualRemotes.d.ts +5 -0
- package/lib/virtualModules/virtualShared_preBuild.d.ts +12 -0
- package/package.json +4 -2
- package/lib/utils/vitePluginOverrideModule.d.ts +0 -6
- /package/lib/{utils/vitePluginAddEntry.d.ts → plugins/pluginAddEntry.d.ts} +0 -0
package/lib/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var pluginutils = require('@rollup/pluginutils');
|
|
2
|
-
var path = require('pathe');
|
|
3
1
|
var fs = require('fs');
|
|
2
|
+
var path = require('pathe');
|
|
3
|
+
var pluginutils = require('@rollup/pluginutils');
|
|
4
4
|
var estreeWalker = require('estree-walker');
|
|
5
5
|
var MagicString = require('magic-string');
|
|
6
6
|
|
|
@@ -24,61 +24,80 @@ function _interopNamespace(e) {
|
|
|
24
24
|
return n;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
28
27
|
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
|
|
28
|
+
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
29
29
|
var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
30
30
|
|
|
31
|
-
var
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
};
|
|
31
|
+
var addEntry = function addEntry(_ref) {
|
|
32
|
+
var entryPath = _ref.entryPath,
|
|
33
|
+
fileName = _ref.fileName;
|
|
34
|
+
var entryFiles = [];
|
|
35
|
+
var htmlFilePath;
|
|
36
|
+
return [{
|
|
37
|
+
name: 'add-entry',
|
|
38
|
+
apply: "serve",
|
|
39
|
+
configureServer: function configureServer(server) {
|
|
40
|
+
var _server$httpServer;
|
|
41
|
+
(_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', function () {
|
|
42
|
+
var port = server.config.server.port;
|
|
43
|
+
fetch(path__namespace.join("http://localhost:" + port, "" + entryPath))["catch"](function (e) {});
|
|
45
44
|
});
|
|
45
|
+
server.middlewares.use(function (req, res, next) {
|
|
46
|
+
if (!fileName) {
|
|
47
|
+
next();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) {
|
|
51
|
+
req.url = entryPath;
|
|
52
|
+
}
|
|
53
|
+
next();
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
transformIndexHtml: function transformIndexHtml(c) {
|
|
57
|
+
return c.replace('<head>', "<head><script type=\"module\" src=" + JSON.stringify(entryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/')) + "></script>");
|
|
46
58
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (!output.manualChunks) output.manualChunks = {};
|
|
65
|
-
var wrapManualChunks = function wrapManualChunks(original) {
|
|
66
|
-
return function (id) {
|
|
67
|
-
var _ref2 = id.match(pattern) || [],
|
|
68
|
-
moduleName = _ref2[1];
|
|
69
|
-
if (moduleName) {
|
|
70
|
-
return moduleName;
|
|
59
|
+
}, {
|
|
60
|
+
name: "add-entry",
|
|
61
|
+
enforce: "post",
|
|
62
|
+
apply: "build",
|
|
63
|
+
configResolved: function configResolved(config) {
|
|
64
|
+
var inputOptions = config.build.rollupOptions.input;
|
|
65
|
+
if (!inputOptions) {
|
|
66
|
+
htmlFilePath = path__namespace.resolve(config.root, 'index.html');
|
|
67
|
+
} else if (typeof inputOptions === 'string') {
|
|
68
|
+
entryFiles = [inputOptions];
|
|
69
|
+
htmlFilePath = path__namespace.resolve(config.root, inputOptions);
|
|
70
|
+
} else if (Array.isArray(inputOptions)) {
|
|
71
|
+
entryFiles = inputOptions;
|
|
72
|
+
htmlFilePath = path__namespace.resolve(config.root, inputOptions[0]);
|
|
73
|
+
} else if (typeof inputOptions === 'object') {
|
|
74
|
+
entryFiles = Object.values(inputOptions);
|
|
75
|
+
htmlFilePath = path__namespace.resolve(config.root, Object.values(inputOptions)[0]);
|
|
71
76
|
}
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
},
|
|
78
|
+
buildStart: function buildStart() {
|
|
79
|
+
var _this$emitFile;
|
|
80
|
+
var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
|
|
81
|
+
this.emitFile((_this$emitFile = {}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
|
|
82
|
+
if (htmlFilePath) {
|
|
83
|
+
var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
|
|
84
|
+
var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
|
|
85
|
+
var match;
|
|
86
|
+
while ((match = scriptRegex.exec(htmlContent)) !== null) {
|
|
87
|
+
entryFiles.push(match[1]);
|
|
88
|
+
}
|
|
74
89
|
}
|
|
75
|
-
|
|
76
|
-
|
|
90
|
+
},
|
|
91
|
+
transform: function transform(code, id) {
|
|
92
|
+
if (entryFiles.some(function (file) {
|
|
93
|
+
return id.endsWith(file);
|
|
94
|
+
})) {
|
|
95
|
+
var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
|
|
96
|
+
return injection + code;
|
|
77
97
|
}
|
|
78
|
-
}
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
}
|
|
98
|
+
}
|
|
99
|
+
}];
|
|
100
|
+
};
|
|
82
101
|
|
|
83
102
|
function normalizeExposesItem(key, item) {
|
|
84
103
|
var importPath = '';
|
|
@@ -96,7 +115,7 @@ function normalizeExposes(exposes) {
|
|
|
96
115
|
if (!exposes) return {};
|
|
97
116
|
var res = {};
|
|
98
117
|
Object.keys(exposes).forEach(function (key) {
|
|
99
|
-
res[key
|
|
118
|
+
res[key] = normalizeExposesItem(key, exposes[key]);
|
|
100
119
|
});
|
|
101
120
|
return res;
|
|
102
121
|
}
|
|
@@ -181,8 +200,12 @@ function normalizeLibrary(library) {
|
|
|
181
200
|
if (!library) return undefined;
|
|
182
201
|
return library;
|
|
183
202
|
}
|
|
203
|
+
var config;
|
|
204
|
+
function getNormalizeModuleFederationOptions() {
|
|
205
|
+
return config;
|
|
206
|
+
}
|
|
184
207
|
function normalizeModuleFederationOptions(options) {
|
|
185
|
-
return {
|
|
208
|
+
return config = {
|
|
186
209
|
exposes: normalizeExposes(options.exposes),
|
|
187
210
|
filename: options.filename || 'remoteEntry-[hash]',
|
|
188
211
|
library: normalizeLibrary(options.library),
|
|
@@ -201,216 +224,63 @@ function normalizeModuleFederationOptions(options) {
|
|
|
201
224
|
};
|
|
202
225
|
}
|
|
203
226
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
var entryPath = _ref.entryPath,
|
|
219
|
-
fileName = _ref.fileName;
|
|
220
|
-
var entryFiles = [];
|
|
221
|
-
var htmlFilePath;
|
|
222
|
-
return [{
|
|
223
|
-
name: 'add-entry',
|
|
224
|
-
apply: "serve",
|
|
225
|
-
configureServer: function configureServer(server) {
|
|
226
|
-
var _server$httpServer;
|
|
227
|
-
(_server$httpServer = server.httpServer) == null || _server$httpServer.once == null || _server$httpServer.once('listening', function () {
|
|
228
|
-
var port = server.config.server.port;
|
|
229
|
-
fetch(path__namespace.join("http://localhost:" + port, "" + entryPath))["catch"](function (e) {});
|
|
230
|
-
});
|
|
231
|
-
server.middlewares.use(function (req, res, next) {
|
|
232
|
-
if (!fileName) {
|
|
233
|
-
next();
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
if (req.url && req.url.startsWith(fileName.replace(/^\/?/, '/'))) {
|
|
237
|
-
req.url = entryPath;
|
|
238
|
-
}
|
|
239
|
-
next();
|
|
240
|
-
});
|
|
241
|
-
},
|
|
242
|
-
transformIndexHtml: function transformIndexHtml(c) {
|
|
243
|
-
return c.replace('<head>', "<head><script type=\"module\" src=" + JSON.stringify(entryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/')) + "></script>");
|
|
244
|
-
}
|
|
245
|
-
}, {
|
|
246
|
-
name: "add-entry",
|
|
247
|
-
enforce: "post",
|
|
248
|
-
apply: "build",
|
|
249
|
-
configResolved: function configResolved(config) {
|
|
250
|
-
var inputOptions = config.build.rollupOptions.input;
|
|
251
|
-
if (!inputOptions) {
|
|
252
|
-
htmlFilePath = path__namespace.resolve(config.root, 'index.html');
|
|
253
|
-
} else if (typeof inputOptions === 'string') {
|
|
254
|
-
entryFiles = [inputOptions];
|
|
255
|
-
htmlFilePath = path__namespace.resolve(config.root, inputOptions);
|
|
256
|
-
} else if (Array.isArray(inputOptions)) {
|
|
257
|
-
entryFiles = inputOptions;
|
|
258
|
-
htmlFilePath = path__namespace.resolve(config.root, inputOptions[0]);
|
|
259
|
-
} else if (typeof inputOptions === 'object') {
|
|
260
|
-
entryFiles = Object.values(inputOptions);
|
|
261
|
-
htmlFilePath = path__namespace.resolve(config.root, Object.values(inputOptions)[0]);
|
|
262
|
-
}
|
|
263
|
-
},
|
|
264
|
-
buildStart: function buildStart() {
|
|
265
|
-
var _this$emitFile;
|
|
266
|
-
var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes("[hash");
|
|
267
|
-
this.emitFile((_this$emitFile = {}, _this$emitFile[hasHash ? "name" : "fileName"] = fileName, _this$emitFile.type = 'chunk', _this$emitFile.id = entryPath, _this$emitFile.preserveSignature = 'strict', _this$emitFile));
|
|
268
|
-
if (htmlFilePath) {
|
|
269
|
-
var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
|
|
270
|
-
var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
|
|
271
|
-
var match;
|
|
272
|
-
while ((match = scriptRegex.exec(htmlContent)) !== null) {
|
|
273
|
-
entryFiles.push(match[1]);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
transform: function transform(code, id) {
|
|
278
|
-
if (entryFiles.some(function (file) {
|
|
279
|
-
return id.endsWith(file);
|
|
280
|
-
})) {
|
|
281
|
-
var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
|
|
282
|
-
return injection + code;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}];
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
function _arrayLikeToArray(r, a) {
|
|
289
|
-
(null == a || a > r.length) && (a = r.length);
|
|
290
|
-
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
291
|
-
return n;
|
|
227
|
+
/**
|
|
228
|
+
* Even the resolveId hook cannot interfere with vite pre-build,
|
|
229
|
+
* and adding query parameter virtual modules will also fail.
|
|
230
|
+
* You can only proxy to the real file through alias
|
|
231
|
+
*/
|
|
232
|
+
var emptyNpmDir = path.resolve(require.resolve("an-empty-js-file"), "../");
|
|
233
|
+
/**
|
|
234
|
+
* The original shared module is proxied by getLoadShareModulePath, and the new shared module is prebuilt here
|
|
235
|
+
*/
|
|
236
|
+
var cacheMap2 = {};
|
|
237
|
+
function getPreBuildLibPath(pkg) {
|
|
238
|
+
if (!cacheMap2[pkg]) cacheMap2[pkg] = "__mf__prebuildwrap_" + npmPackageNameToFileName(pkg) + ".js";
|
|
239
|
+
var filename = cacheMap2[pkg];
|
|
240
|
+
return filename;
|
|
292
241
|
}
|
|
293
|
-
function
|
|
294
|
-
var
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
t && (r = t);
|
|
298
|
-
var o = 0;
|
|
299
|
-
return function () {
|
|
300
|
-
return o >= r.length ? {
|
|
301
|
-
done: !0
|
|
302
|
-
} : {
|
|
303
|
-
done: !1,
|
|
304
|
-
value: r[o++]
|
|
305
|
-
};
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
242
|
+
function getLocalSharedImportMapFileName() {
|
|
243
|
+
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
244
|
+
name = _getNormalizeModuleFe.name;
|
|
245
|
+
return npmPackageNameToFileName(name) + "_" + "__mf__localSharedImportMap.js";
|
|
309
246
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
var t = {}.toString.call(r).slice(8, -1);
|
|
314
|
-
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;
|
|
315
|
-
}
|
|
247
|
+
// Only npm package name import can trigger pre-build, absolute path cannot
|
|
248
|
+
function getLocalSharedImportMapId() {
|
|
249
|
+
return "an-empty-js-file/" + getLocalSharedImportMapFileName();
|
|
316
250
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if (options === void 0) {
|
|
320
|
-
options = {};
|
|
321
|
-
}
|
|
322
|
-
var _options = options,
|
|
323
|
-
_options$override = _options.override,
|
|
324
|
-
_override = _options$override === void 0 ? [] : _options$override,
|
|
325
|
-
include = _options.include,
|
|
326
|
-
exclude = _options.exclude;
|
|
327
|
-
var override = new Set(_override);
|
|
328
|
-
var filterFunction = pluginutils.createFilter(include, exclude);
|
|
329
|
-
var alias = {};
|
|
330
|
-
var keys = override.keys();
|
|
331
|
-
for (var _iterator = _createForOfIteratorHelperLoose(keys), _step; !(_step = _iterator()).done;) {
|
|
332
|
-
var key = _step.value;
|
|
333
|
-
alias['__overrideModule__' + key] = require.resolve("an-empty-js-file") + ("?__overrideModule__=" + encodeURIComponent(key));
|
|
334
|
-
}
|
|
335
|
-
return {
|
|
336
|
-
name: 'overrideModule',
|
|
337
|
-
enforce: 'post',
|
|
338
|
-
config: function config(_config) {
|
|
339
|
-
var _config$resolve$alias;
|
|
340
|
-
if (!_config.optimizeDeps) _config.optimizeDeps = {};
|
|
341
|
-
if (!_config.optimizeDeps.needsInterop) _config.optimizeDeps.needsInterop = [];
|
|
342
|
-
Object.keys(alias).forEach(function (key) {
|
|
343
|
-
var _config$optimizeDeps;
|
|
344
|
-
(_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.needsInterop) == null || _config$optimizeDeps.push(key);
|
|
345
|
-
});
|
|
346
|
-
_config.optimizeDeps.needsInterop.push('an-empty-js-file');
|
|
347
|
-
(_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(alias).map(function (key) {
|
|
348
|
-
return {
|
|
349
|
-
find: key,
|
|
350
|
-
replacement: alias[key]
|
|
351
|
-
};
|
|
352
|
-
}));
|
|
353
|
-
},
|
|
354
|
-
resolveId: function resolveId(id) {
|
|
355
|
-
return alias[id];
|
|
356
|
-
},
|
|
357
|
-
transform: {
|
|
358
|
-
handler: function handler(code, id) {
|
|
359
|
-
if (!filterFunction(id)) return null;
|
|
360
|
-
var ast;
|
|
361
|
-
try {
|
|
362
|
-
ast = this.parse(code, {
|
|
363
|
-
allowReturnOutsideFunction: true
|
|
364
|
-
});
|
|
365
|
-
} catch (e) {
|
|
366
|
-
throw new Error(id + ": " + e);
|
|
367
|
-
}
|
|
368
|
-
var s = new MagicString__default["default"](code);
|
|
369
|
-
estreeWalker.walk(ast, {
|
|
370
|
-
enter: function enter(node) {
|
|
371
|
-
var replaceIfMatch = function replaceIfMatch(sourceNode) {
|
|
372
|
-
if (sourceNode && sourceNode.value) {
|
|
373
|
-
if (override.has(sourceNode.value)) {
|
|
374
|
-
var start = sourceNode.start + 1; // Skip the opening quote
|
|
375
|
-
var end = sourceNode.end - 1; // Skip the closing quote
|
|
376
|
-
s.overwrite(start, end, '__overrideModule__' + sourceNode.value);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
if (node.type === 'ImportDeclaration') {
|
|
381
|
-
replaceIfMatch(node.source);
|
|
382
|
-
}
|
|
383
|
-
if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportAllDeclaration') {
|
|
384
|
-
replaceIfMatch(node.source);
|
|
385
|
-
}
|
|
386
|
-
if (node.type === 'ImportExpression') {
|
|
387
|
-
replaceIfMatch(node.source);
|
|
388
|
-
}
|
|
389
|
-
if (node.type === 'CallExpression' && node.callee.name === 'require' && node.arguments.length && node.arguments[0].type === 'Literal') {
|
|
390
|
-
replaceIfMatch(node.arguments[0]);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
});
|
|
394
|
-
return {
|
|
395
|
-
code: s.toString(),
|
|
396
|
-
map: s.generateMap({
|
|
397
|
-
hires: true
|
|
398
|
-
})
|
|
399
|
-
};
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
};
|
|
251
|
+
function getLocalSharedImportMapPath() {
|
|
252
|
+
return path.resolve(emptyNpmDir, getLocalSharedImportMapFileName());
|
|
403
253
|
}
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
function wrapRemoteEntry() {
|
|
409
|
-
return "\n import {init, get} from \"__mf__cwdRemoteEntry\"\n export {init, get}\n ";
|
|
254
|
+
function writeLocalSharedImportMap(pkgList) {
|
|
255
|
+
fs.writeFileSync(getLocalSharedImportMapPath(), "\n export default {\n " + pkgList.map(function (pkg) {
|
|
256
|
+
return "\n " + JSON.stringify(pkg) + ": async () => {\n let pkg = await import(\"" + getPreBuildLibPath(pkg) + "\")\n return pkg\n }\n ";
|
|
257
|
+
}).join(",") + "\n }\n ");
|
|
410
258
|
}
|
|
411
|
-
|
|
412
|
-
|
|
259
|
+
/**
|
|
260
|
+
* generate loadShare virtual module
|
|
261
|
+
*/
|
|
262
|
+
var cacheMap1 = {};
|
|
263
|
+
function getLoadShareModulePath(pkg) {
|
|
264
|
+
var _getNormalizeModuleFe2 = getNormalizeModuleFederationOptions(),
|
|
265
|
+
name = _getNormalizeModuleFe2.name;
|
|
266
|
+
if (!cacheMap1[pkg]) cacheMap1[pkg] = npmPackageNameToFileName(name) + "_" + ("__mf__loadShare_" + npmPackageNameToFileName(pkg) + ".js");
|
|
267
|
+
var filename = cacheMap1[pkg];
|
|
268
|
+
return path.resolve(emptyNpmDir, filename);
|
|
413
269
|
}
|
|
270
|
+
function writeLoadShareModule(pkg, shareItem, command) {
|
|
271
|
+
fs.writeFileSync(getLoadShareModulePath(pkg), "\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 ");
|
|
272
|
+
}
|
|
273
|
+
function npmPackageNameToFileName(packageName) {
|
|
274
|
+
// 1. 去掉包名前的 "@"
|
|
275
|
+
// 2. 将包名中的 "/" 替换为 "__" 以避免文件路径问题
|
|
276
|
+
// 3. 去掉不合法的文件名字符
|
|
277
|
+
return packageName.replace(/^@/, '') // 移除作用域前缀 "@"
|
|
278
|
+
.replace(/\//g, '__') // 将 "/" 替换为 "__"
|
|
279
|
+
.replace(/[^a-zA-Z0-9_.-]/g, '_'); // 替换其他非法字符为 "_"
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
var emptyPath = require.resolve('an-empty-js-file');
|
|
283
|
+
var REMOTE_ENTRY_ID = "REMOTE_ENTRY_ID";
|
|
414
284
|
function generateRemoteEntry(options) {
|
|
415
285
|
var pluginImportNames = options.runtimePlugins.map(function (p, i) {
|
|
416
286
|
return ["$runtimePlugin_" + i, "import $runtimePlugin_" + i + " from \"" + p + "\";"];
|
|
@@ -419,9 +289,9 @@ function generateRemoteEntry(options) {
|
|
|
419
289
|
return item[1];
|
|
420
290
|
}).join('\n') + "\n\n const exposesMap = {\n " + Object.keys(options.exposes).map(function (key) {
|
|
421
291
|
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 ";
|
|
422
|
-
}).join(',') + "\n }\n async function init(shared = {}) {\n const localShared = {\n " + Object.keys(options.shared).map(function (key) {
|
|
292
|
+
}).join(',') + "\n }\n import localSharedImportMap from \"" + getLocalSharedImportMapId() + "\"\n async function init(shared = {}) {\n const localShared = {\n " + Object.keys(options.shared).map(function (key) {
|
|
423
293
|
var shareItem = options.shared[key];
|
|
424
|
-
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(shareItem.name) + ",\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
|
|
294
|
+
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(shareItem.name) + ",\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 return function () {\n return res\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + "\n }\n }\n ";
|
|
425
295
|
}).join(',') + "\n }\n const initRes = runtimeInit({\n name: " + JSON.stringify(options.name) + ",\n remotes: [" + Object.keys(options.remotes).map(function (key) {
|
|
426
296
|
var remote = options.remotes[key];
|
|
427
297
|
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 ";
|
|
@@ -429,17 +299,55 @@ function generateRemoteEntry(options) {
|
|
|
429
299
|
return item[0] + "()";
|
|
430
300
|
}).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 ";
|
|
431
301
|
}
|
|
432
|
-
|
|
433
|
-
|
|
302
|
+
var WRAP_REMOTE_ENTRY_PATH = emptyPath + '?__mf__wrapRemoteEntry__';
|
|
303
|
+
function generateWrapRemoteEntry() {
|
|
304
|
+
return "\n import {init, get} from \"" + REMOTE_ENTRY_ID + "\"\n export {init, get}\n ";
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Inject entry file, automatically init when used as host,
|
|
308
|
+
* and will not inject remoteEntry
|
|
309
|
+
*/
|
|
310
|
+
var HOST_AUTO_INIT = emptyPath + '?__mf__isHostInit';
|
|
311
|
+
function generateWrapHostInit() {
|
|
312
|
+
return "\n import {init} from \"" + REMOTE_ENTRY_ID + "\"\n init()\n ";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
var filter$1 = pluginutils.createFilter();
|
|
316
|
+
function pluginProxyRemoteEntry () {
|
|
434
317
|
return {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
318
|
+
name: 'proxyRemoteEntry',
|
|
319
|
+
enforce: 'post',
|
|
320
|
+
resolveId: function resolveId(id) {
|
|
321
|
+
if (id === REMOTE_ENTRY_ID) {
|
|
322
|
+
return REMOTE_ENTRY_ID;
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
load: function load(id) {
|
|
326
|
+
if (id === REMOTE_ENTRY_ID) {
|
|
327
|
+
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
transform: function transform(code, id) {
|
|
331
|
+
try {
|
|
332
|
+
if (!filter$1(id)) return Promise.resolve();
|
|
333
|
+
if (id === REMOTE_ENTRY_ID) {
|
|
334
|
+
return Promise.resolve(generateRemoteEntry(getNormalizeModuleFederationOptions()));
|
|
335
|
+
}
|
|
336
|
+
if (id.includes(WRAP_REMOTE_ENTRY_PATH)) {
|
|
337
|
+
return Promise.resolve(generateWrapRemoteEntry());
|
|
338
|
+
}
|
|
339
|
+
if (id.includes(HOST_AUTO_INIT)) {
|
|
340
|
+
return Promise.resolve(generateWrapHostInit());
|
|
341
|
+
}
|
|
342
|
+
return Promise.resolve();
|
|
343
|
+
} catch (e) {
|
|
344
|
+
return Promise.reject(e);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
439
347
|
};
|
|
440
348
|
}
|
|
441
|
-
|
|
442
|
-
function
|
|
349
|
+
|
|
350
|
+
function generateRemotes(id, command) {
|
|
443
351
|
return {
|
|
444
352
|
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 ",
|
|
445
353
|
map: null,
|
|
@@ -447,108 +355,259 @@ function wrapRemote(id) {
|
|
|
447
355
|
syntheticNamedExports: '__mf__dynamicExports'
|
|
448
356
|
};
|
|
449
357
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
var
|
|
456
|
-
|
|
457
|
-
replacement: require.resolve('@module-federation/runtime')
|
|
458
|
-
}];
|
|
459
|
-
Object.keys(remotes).forEach(function (key) {
|
|
460
|
-
var remote = remotes[key];
|
|
461
|
-
alias.push({
|
|
462
|
-
find: new RegExp("(" + remote.name + "(/.*|$)?)"),
|
|
463
|
-
replacement: '$1',
|
|
464
|
-
customResolver: function customResolver(source) {
|
|
465
|
-
if (!con.optimizeDeps) con.optimizeDeps = {};
|
|
466
|
-
if (!con.optimizeDeps.needsInterop) con.optimizeDeps.needsInterop = [];
|
|
467
|
-
if (con.optimizeDeps.needsInterop.indexOf(source) === -1) con.optimizeDeps.needsInterop.push(source);
|
|
468
|
-
return this.resolve(require.resolve('an-empty-js-file') + '?__moduleRemote__=' + encodeURIComponent(source));
|
|
469
|
-
}
|
|
470
|
-
});
|
|
471
|
-
});
|
|
472
|
-
var remotePrefixList = Object.keys(remotes);
|
|
473
|
-
// pkgname will be escaped, and matching path also needs to be processed in the same way:
|
|
474
|
-
// @json2csv/plainjs --> .vite/deps/@json2csv_plainjs
|
|
475
|
-
var sharedKeyMatchList = Object.keys(shared).map(function (item) {
|
|
476
|
-
return "__overrideModule__" + item.replace('/', '_');
|
|
358
|
+
|
|
359
|
+
var filter = pluginutils.createFilter();
|
|
360
|
+
function pluginProxyRemotes (options) {
|
|
361
|
+
var command;
|
|
362
|
+
var remotes = options.remotes;
|
|
363
|
+
var matchRemotesList = Object.keys(remotes).map(function (item) {
|
|
364
|
+
return item.replace(/\//g, "_");
|
|
477
365
|
});
|
|
478
|
-
return
|
|
479
|
-
|
|
480
|
-
entryPath: emptyPath + '?__mf__wrapRemoteEntry__',
|
|
481
|
-
fileName: filename
|
|
482
|
-
}), addEntry({
|
|
483
|
-
entryName: 'hostInit',
|
|
484
|
-
entryPath: emptyPath + '?__mf__isHostInit'
|
|
485
|
-
}), [overrideModule({
|
|
486
|
-
override: Object.keys(shared)
|
|
487
|
-
}), {
|
|
488
|
-
name: 'module-federation-vite',
|
|
489
|
-
enforce: 'post',
|
|
366
|
+
return {
|
|
367
|
+
name: "proxyRemotes",
|
|
490
368
|
config: function config(_config, _ref) {
|
|
491
|
-
var _config$resolve$alias, _config$optimizeDeps;
|
|
492
369
|
var _command = _ref.command;
|
|
493
|
-
con = _config;
|
|
494
370
|
command = _command;
|
|
495
|
-
(
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
371
|
+
Object.keys(remotes).forEach(function (key) {
|
|
372
|
+
var remote = remotes[key];
|
|
373
|
+
_config.resolve.alias.push({
|
|
374
|
+
find: new RegExp("(" + remote.name + "(/.*|$)?)"),
|
|
375
|
+
replacement: '$1',
|
|
376
|
+
customResolver: function customResolver(source) {
|
|
377
|
+
if (!_config.optimizeDeps) _config.optimizeDeps = {};
|
|
378
|
+
if (!_config.optimizeDeps.needsInterop) _config.optimizeDeps.needsInterop = [];
|
|
379
|
+
if (_config.optimizeDeps.needsInterop.indexOf(source) === -1) _config.optimizeDeps.needsInterop.push(source);
|
|
380
|
+
return this.resolve(require.resolve('an-empty-js-file') + '?__moduleRemote__=' + encodeURIComponent(source));
|
|
381
|
+
}
|
|
382
|
+
});
|
|
500
383
|
});
|
|
501
384
|
},
|
|
502
|
-
resolveId: function resolveId(id) {
|
|
503
|
-
if (id === '__mf__cwdRemoteEntry') {
|
|
504
|
-
return '__mf__cwdRemoteEntry';
|
|
505
|
-
}
|
|
506
|
-
},
|
|
507
|
-
load: function load(id) {
|
|
508
|
-
if (id === '__mf__cwdRemoteEntry') {
|
|
509
|
-
return generateRemoteEntry(options);
|
|
510
|
-
}
|
|
511
|
-
},
|
|
512
385
|
transform: function transform(code, id) {
|
|
513
386
|
try {
|
|
514
387
|
if (!filter(id)) return Promise.resolve();
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
}
|
|
518
|
-
if (id.indexOf('__mf__wrapRemoteEntry__') > -1) {
|
|
519
|
-
return Promise.resolve(wrapRemoteEntry());
|
|
520
|
-
}
|
|
521
|
-
if (id.indexOf('__mf__isHostInit') > -1) {
|
|
522
|
-
return Promise.resolve(wrapHostInit());
|
|
523
|
-
}
|
|
524
|
-
if (id.indexOf('__mf__cwdRemoteEntry') > -1) {
|
|
525
|
-
return Promise.resolve(generateRemoteEntry(options));
|
|
526
|
-
}
|
|
527
|
-
var _ref2 = sharedKeyMatchList.length && id.match(new RegExp("/(" + sharedKeyMatchList.join('|') + ")(_.*.js|.js)")) || [],
|
|
528
|
-
devSharedModuleName = _ref2[0];
|
|
529
|
-
if (devSharedModuleName) {
|
|
530
|
-
return Promise.resolve(wrapShare(devSharedModuleName.replace('/__overrideModule__', '').replace(/_/g, '/').replace('.js', ''), shared));
|
|
531
|
-
}
|
|
532
|
-
var _ref3 = id.match(/\_\_overrideModule\_\_=[^&]+/) || [],
|
|
533
|
-
prodSharedName = _ref3[0];
|
|
534
|
-
if (prodSharedName) {
|
|
535
|
-
return Promise.resolve(wrapShare(decodeURIComponent(prodSharedName.replace('__overrideModule__=', '')), shared));
|
|
536
|
-
}
|
|
537
|
-
var _ref4 = remotePrefixList.length && id.match(new RegExp("/(" + remotePrefixList.join('|') + ")(_.*.js|.js)")) || [],
|
|
538
|
-
devRemoteModuleName = _ref4[0];
|
|
388
|
+
var _ref2 = matchRemotesList.length && id.match(new RegExp("/(" + matchRemotesList.join('|') + ")(_.*.js|.js)")) || [],
|
|
389
|
+
devRemoteModuleName = _ref2[0];
|
|
539
390
|
if (devRemoteModuleName) {
|
|
540
|
-
return Promise.resolve(
|
|
391
|
+
return Promise.resolve(generateRemotes(devRemoteModuleName.replace('/', '').replace(/_/g, '/').replace('.js', ''), command));
|
|
541
392
|
}
|
|
542
|
-
var
|
|
543
|
-
prodRemoteName =
|
|
393
|
+
var _ref3 = id.match(/\_\_moduleRemote\_\_=[^&]+/) || [],
|
|
394
|
+
prodRemoteName = _ref3[0];
|
|
544
395
|
if (prodRemoteName) {
|
|
545
|
-
return Promise.resolve(
|
|
396
|
+
return Promise.resolve(generateRemotes(decodeURIComponent(prodRemoteName.replace('__moduleRemote__=', '')), command));
|
|
546
397
|
}
|
|
547
398
|
return Promise.resolve();
|
|
548
399
|
} catch (e) {
|
|
549
400
|
return Promise.reject(e);
|
|
550
401
|
}
|
|
551
402
|
}
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function proxySharedModule(options) {
|
|
407
|
+
var _options$shared = options.shared,
|
|
408
|
+
shared = _options$shared === void 0 ? {} : _options$shared,
|
|
409
|
+
include = options.include,
|
|
410
|
+
exclude = options.exclude;
|
|
411
|
+
var filterFunction = pluginutils.createFilter(include, exclude);
|
|
412
|
+
writeLocalSharedImportMap(Object.keys(shared));
|
|
413
|
+
return [{
|
|
414
|
+
name: 'preBuildShared',
|
|
415
|
+
enforce: 'post',
|
|
416
|
+
config: function config(_config, _ref) {
|
|
417
|
+
var _config$resolve$alias, _config$resolve$alias2;
|
|
418
|
+
var command = _ref.command;
|
|
419
|
+
// config?.optimizeDeps?.include?.push?.("an-empty-js-file");
|
|
420
|
+
// config.optimizeDeps.needsInterop.push('an-empty-js-file');
|
|
421
|
+
(_config$resolve$alias = _config.resolve.alias).push.apply(_config$resolve$alias, Object.keys(shared).map(function (key) {
|
|
422
|
+
var _config$optimizeDeps;
|
|
423
|
+
_config == null || (_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push == null || _config$optimizeDeps.push(getPreBuildLibPath(key));
|
|
424
|
+
// write proxyFile
|
|
425
|
+
writeLoadShareModule(key, shared[key], command);
|
|
426
|
+
var preBuildLibPath = getLoadShareModulePath(key);
|
|
427
|
+
return {
|
|
428
|
+
// Intercept all dependency requests to the proxy module
|
|
429
|
+
// Dependency requests issued by localSharedImportMap are allowed without proxying.
|
|
430
|
+
find: new RegExp("^" + key + "$"),
|
|
431
|
+
replacement: preBuildLibPath,
|
|
432
|
+
customResolver: function customResolver(source, importer) {
|
|
433
|
+
if (importer.includes("node_modules/" + key + "/")) {
|
|
434
|
+
return this.resolve(key);
|
|
435
|
+
}
|
|
436
|
+
return this.resolve(preBuildLibPath);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}));
|
|
440
|
+
(_config$resolve$alias2 = _config.resolve.alias).push.apply(_config$resolve$alias2, Object.keys(shared).map(function (key) {
|
|
441
|
+
return {
|
|
442
|
+
find: new RegExp("^" + getPreBuildLibPath(key) + "$"),
|
|
443
|
+
customResolver: function customResolver(source, importer) {
|
|
444
|
+
return this.resolve(key);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
}));
|
|
448
|
+
}
|
|
449
|
+
}, {
|
|
450
|
+
name: "prebuild-top-level-await",
|
|
451
|
+
apply: "serve",
|
|
452
|
+
transform: function transform(code, id) {
|
|
453
|
+
if (!code.includes("/*mf top-level-await placeholder replacement mf*/")) {
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
if (!filterFunction(id)) return null;
|
|
457
|
+
var ast;
|
|
458
|
+
try {
|
|
459
|
+
ast = this.parse(code, {
|
|
460
|
+
allowReturnOutsideFunction: true
|
|
461
|
+
});
|
|
462
|
+
} catch (e) {
|
|
463
|
+
throw new Error(id + ": " + e);
|
|
464
|
+
}
|
|
465
|
+
var magicString = new MagicString__default["default"](code);
|
|
466
|
+
estreeWalker.walk(ast, {
|
|
467
|
+
enter: function enter(node) {
|
|
468
|
+
// 处理命名导出
|
|
469
|
+
if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
|
|
470
|
+
var exportSpecifiers = node.specifiers.map(function (specifier) {
|
|
471
|
+
return specifier.exported.name;
|
|
472
|
+
});
|
|
473
|
+
var proxyStatements = exportSpecifiers.map(function (name) {
|
|
474
|
+
return "\n const __mfproxy__await" + name + " = await " + name + "();\n const __mfproxy__" + name + " = () => __mfproxy__await" + name + ";\n ";
|
|
475
|
+
}).join('\n');
|
|
476
|
+
var exportStatements = exportSpecifiers.map(function (name) {
|
|
477
|
+
return "__mfproxy__" + name + " as " + name;
|
|
478
|
+
}).join(', ');
|
|
479
|
+
var start = node.start;
|
|
480
|
+
var end = node.end;
|
|
481
|
+
var replacement = proxyStatements + "\nexport { " + exportStatements + " };";
|
|
482
|
+
magicString.overwrite(start, end, replacement);
|
|
483
|
+
}
|
|
484
|
+
// 处理默认导出
|
|
485
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
486
|
+
var declaration = node.declaration;
|
|
487
|
+
var _start = node.start;
|
|
488
|
+
var _end = node.end;
|
|
489
|
+
var proxyStatement;
|
|
490
|
+
var exportStatement = 'default';
|
|
491
|
+
if (declaration.type === 'Identifier') {
|
|
492
|
+
// 处理标识符 (如: export default foo;)
|
|
493
|
+
proxyStatement = "\n const __mfproxy__awaitdefault = await " + declaration.name + "();\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
494
|
+
} else if (declaration.type === 'CallExpression' || declaration.type === 'FunctionDeclaration') {
|
|
495
|
+
// 处理调用表达式或函数声明 (如: export default someFunction();)
|
|
496
|
+
var declarationCode = code.slice(declaration.start, declaration.end);
|
|
497
|
+
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + declarationCode + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
498
|
+
} else {
|
|
499
|
+
// 其他类型 (可以根据需要添加更多处理逻辑)
|
|
500
|
+
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + code.slice(declaration.start, declaration.end) + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
501
|
+
}
|
|
502
|
+
var _replacement = proxyStatement + "\nexport { __mfproxy__default as " + exportStatement + " };";
|
|
503
|
+
magicString.overwrite(_start, _end, _replacement);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
return {
|
|
508
|
+
code: magicString.toString(),
|
|
509
|
+
map: magicString.generateMap({
|
|
510
|
+
hires: true
|
|
511
|
+
})
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
}];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
var aliasToArrayPlugin = {
|
|
518
|
+
name: 'alias-transform-plugin',
|
|
519
|
+
config: function config(_config, _ref) {
|
|
520
|
+
if (!_config.resolve) _config.resolve = {};
|
|
521
|
+
if (!_config.resolve.alias) _config.resolve.alias = [];
|
|
522
|
+
var alias = _config.resolve.alias;
|
|
523
|
+
if (typeof alias === 'object' && !Array.isArray(alias)) {
|
|
524
|
+
_config.resolve.alias = Object.entries(alias).map(function (_ref2) {
|
|
525
|
+
var find = _ref2[0],
|
|
526
|
+
replacement = _ref2[1];
|
|
527
|
+
return {
|
|
528
|
+
find: find,
|
|
529
|
+
replacement: replacement
|
|
530
|
+
};
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
var normalizeBuildPlugin = (function (singleChunkModules) {
|
|
537
|
+
return {
|
|
538
|
+
name: 'normalizeBuild',
|
|
539
|
+
config: function config(_config, _ref) {
|
|
540
|
+
if (!_config.build) _config.build = {};
|
|
541
|
+
if (!_config.build.rollupOptions) _config.build.rollupOptions = {};
|
|
542
|
+
var rollupOptions = _config.build.rollupOptions;
|
|
543
|
+
if (!rollupOptions.output) rollupOptions.output = {};
|
|
544
|
+
normalizeManualChunks(rollupOptions.output, singleChunkModules);
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
});
|
|
548
|
+
function normalizeManualChunks(output, singleChunkModules) {
|
|
549
|
+
var pattern = new RegExp("node_modules/(" + singleChunkModules.join('|') + ")/");
|
|
550
|
+
if (!output.manualChunks) output.manualChunks = {};
|
|
551
|
+
var wrapManualChunks = function wrapManualChunks(original) {
|
|
552
|
+
return function (id) {
|
|
553
|
+
var _ref2 = id.match(pattern) || [],
|
|
554
|
+
moduleName = _ref2[1];
|
|
555
|
+
if (moduleName) {
|
|
556
|
+
return moduleName;
|
|
557
|
+
}
|
|
558
|
+
if (typeof original === 'function') {
|
|
559
|
+
return original.apply(void 0, [id].concat([].slice.call(arguments, 1)));
|
|
560
|
+
}
|
|
561
|
+
if (typeof original === 'object' && original) {
|
|
562
|
+
return original[id];
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
};
|
|
566
|
+
output.manualChunks = wrapManualChunks(output.manualChunks);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
var normalizeOptimizeDepsPlugin = {
|
|
570
|
+
name: 'normalizeOptimizeDeps',
|
|
571
|
+
config: function config(_config, _ref) {
|
|
572
|
+
var optimizeDeps = _config.optimizeDeps;
|
|
573
|
+
if (!optimizeDeps) {
|
|
574
|
+
_config.optimizeDeps = {};
|
|
575
|
+
optimizeDeps = _config.optimizeDeps;
|
|
576
|
+
}
|
|
577
|
+
if (!optimizeDeps.include) optimizeDeps.include = [];
|
|
578
|
+
if (!optimizeDeps.needsInterop) optimizeDeps.needsInterop = [];
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
function federation(mfUserOptions) {
|
|
583
|
+
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
584
|
+
var name = options.name,
|
|
585
|
+
shared = options.shared,
|
|
586
|
+
filename = options.filename;
|
|
587
|
+
if (!name) throw new Error("name is required");
|
|
588
|
+
return [aliasToArrayPlugin, normalizeOptimizeDepsPlugin, normalizeBuildPlugin([].concat(Object.keys(shared), ["@module-federation/runtime"]))].concat(addEntry({
|
|
589
|
+
entryName: 'remoteEntry',
|
|
590
|
+
entryPath: WRAP_REMOTE_ENTRY_PATH,
|
|
591
|
+
fileName: filename
|
|
592
|
+
}), addEntry({
|
|
593
|
+
entryName: 'hostInit',
|
|
594
|
+
entryPath: HOST_AUTO_INIT
|
|
595
|
+
}), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], proxySharedModule({
|
|
596
|
+
shared: shared
|
|
597
|
+
}), [{
|
|
598
|
+
name: 'module-federation-vite',
|
|
599
|
+
enforce: 'post',
|
|
600
|
+
config: function config(_config, _ref) {
|
|
601
|
+
var _config$optimizeDeps;
|
|
602
|
+
_config.resolve.alias.push({
|
|
603
|
+
find: '@module-federation/runtime',
|
|
604
|
+
replacement: require.resolve('@module-federation/runtime')
|
|
605
|
+
});
|
|
606
|
+
(_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push('@module-federation/runtime');
|
|
607
|
+
// Object.keys(shared).forEach((key) => {
|
|
608
|
+
// config.optimizeDeps?.include?.push(key);
|
|
609
|
+
// });
|
|
610
|
+
}
|
|
552
611
|
}]);
|
|
553
612
|
}
|
|
554
613
|
|