@module-federation/vite 1.10.0 → 1.11.1
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 +4 -1
- package/lib/index.cjs +1819 -1963
- package/lib/index.d.cts +131 -0
- package/lib/index.d.mts +131 -0
- package/lib/index.mjs +1946 -0
- package/package.json +29 -18
- package/lib/index.d.ts +0 -4
- package/lib/index.esm.js +0 -2091
- package/lib/index.modern.js +0 -2123
- package/lib/index.umd.js +0 -2112
- package/lib/plugins/__tests__/pluginCheckAliasConflicts.test.d.ts +0 -1
- package/lib/plugins/__tests__/pluginDts.test.d.ts +0 -1
- package/lib/plugins/pluginAddEntry.d.ts +0 -10
- package/lib/plugins/pluginCheckAliasConflicts.d.ts +0 -9
- package/lib/plugins/pluginDevProxyModuleTopLevelAwait.d.ts +0 -2
- package/lib/plugins/pluginDts.d.ts +0 -3
- package/lib/plugins/pluginMFManifest.d.ts +0 -3
- package/lib/plugins/pluginModuleParseEnd.d.ts +0 -10
- package/lib/plugins/pluginProxyRemoteEntry.d.ts +0 -2
- package/lib/plugins/pluginProxyRemotes.d.ts +0 -3
- package/lib/plugins/pluginProxySharedModule_preBuild.d.ts +0 -7
- package/lib/utils/PromiseStore.d.ts +0 -16
- package/lib/utils/VirtualModule.d.ts +0 -26
- package/lib/utils/__tests__/VirtualModule.test.d.ts +0 -1
- package/lib/utils/__tests__/cssModuleHelpers.test.d.ts +0 -1
- package/lib/utils/__tests__/helpers.d.ts +0 -2
- package/lib/utils/__tests__/normalizeModuleFederationOption.test.d.ts +0 -1
- package/lib/utils/__tests__/publicPath.test.d.ts +0 -1
- package/lib/utils/__tests__/serializeRuntimeOptions.test.d.ts +0 -1
- package/lib/utils/aliasToArrayPlugin.d.ts +0 -10
- package/lib/utils/cssModuleHelpers.d.ts +0 -75
- package/lib/utils/localSharedImportMap_temp.d.ts +0 -2
- package/lib/utils/mapCodeToCodeWithSourcemap.d.ts +0 -4
- package/lib/utils/normalizeModuleFederationOptions.d.ts +0 -159
- package/lib/utils/normalizeOptimizeDeps.d.ts +0 -9
- package/lib/utils/packageNameUtils.d.ts +0 -22
- package/lib/utils/publicPath.d.ts +0 -9
- package/lib/utils/serializeRuntimeOptions.d.ts +0 -10
- package/lib/utils/wrapManualChunks.d.ts +0 -1
- package/lib/virtualModules/index.d.ts +0 -6
- package/lib/virtualModules/virtualExposes.d.ts +0 -2
- package/lib/virtualModules/virtualRemoteEntry.d.ts +0 -16
- package/lib/virtualModules/virtualRemotes.d.ts +0 -6
- package/lib/virtualModules/virtualRuntimeInitStatus.d.ts +0 -3
- package/lib/virtualModules/virtualShared_preBuild.d.ts +0 -17
package/lib/index.umd.js
DELETED
|
@@ -1,2112 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('defu'), require('fs'), require('pathe'), require('magic-string'), require('@rollup/pluginutils'), require('estree-walker'), require('@module-federation/sdk'), require('@module-federation/dts-plugin'), require('@module-federation/dts-plugin/core')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'defu', 'fs', 'pathe', 'magic-string', '@rollup/pluginutils', 'estree-walker', '@module-federation/sdk', '@module-federation/dts-plugin', '@module-federation/dts-plugin/core'], factory) :
|
|
4
|
-
(global = global || self, factory(global.vite = {}, global.defu, global.fs, global.pathe, global.magicString, global.pluginutils, global.estreeWalker, global.sdk, global.dtsPlugin, global.core));
|
|
5
|
-
})(this, (function (exports, defu, fs, path, MagicString, pluginutils, estreeWalker, sdk, dtsPlugin, core) {
|
|
6
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
-
|
|
8
|
-
function _interopNamespace(e) {
|
|
9
|
-
if (e && e.__esModule) return e;
|
|
10
|
-
var n = Object.create(null);
|
|
11
|
-
if (e) {
|
|
12
|
-
Object.keys(e).forEach(function (k) {
|
|
13
|
-
if (k !== 'default') {
|
|
14
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: function () { return e[k]; }
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
n["default"] = e;
|
|
23
|
-
return n;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
var defu__default = /*#__PURE__*/_interopDefaultLegacy(defu);
|
|
27
|
-
var fs__namespace = /*#__PURE__*/_interopNamespace(fs);
|
|
28
|
-
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
29
|
-
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
30
|
-
var MagicString__default = /*#__PURE__*/_interopDefaultLegacy(MagicString);
|
|
31
|
-
|
|
32
|
-
var mapCodeToCodeWithSourcemap = function mapCodeToCodeWithSourcemap(code) {
|
|
33
|
-
return Promise.resolve(code).then(function (resolvedCode) {
|
|
34
|
-
if (resolvedCode === undefined) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
var s = new MagicString__default["default"](resolvedCode);
|
|
38
|
-
return {
|
|
39
|
-
code: s.toString(),
|
|
40
|
-
map: s.generateMap({
|
|
41
|
-
hires: true
|
|
42
|
-
})
|
|
43
|
-
};
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
function getFirstHtmlEntryFile(entryFiles) {
|
|
48
|
-
return entryFiles.find(function (file) {
|
|
49
|
-
return file.endsWith('.html');
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
var addEntry = function addEntry(_ref) {
|
|
53
|
-
var entryName = _ref.entryName,
|
|
54
|
-
entryPath = _ref.entryPath,
|
|
55
|
-
fileName = _ref.fileName,
|
|
56
|
-
_ref$inject = _ref.inject,
|
|
57
|
-
inject = _ref$inject === void 0 ? 'entry' : _ref$inject;
|
|
58
|
-
var devEntryPath = entryPath.startsWith('virtual:mf') ? '@id/' + entryPath : entryPath;
|
|
59
|
-
var entryFiles = [];
|
|
60
|
-
var htmlFilePath;
|
|
61
|
-
var _command;
|
|
62
|
-
var emitFileId;
|
|
63
|
-
var viteConfig;
|
|
64
|
-
function injectHtml() {
|
|
65
|
-
return inject === 'html' && htmlFilePath;
|
|
66
|
-
}
|
|
67
|
-
function injectEntry() {
|
|
68
|
-
return inject === 'entry' || !htmlFilePath;
|
|
69
|
-
}
|
|
70
|
-
return [{
|
|
71
|
-
name: 'add-entry',
|
|
72
|
-
apply: 'serve',
|
|
73
|
-
config: function config(_config, _ref2) {
|
|
74
|
-
var command = _ref2.command;
|
|
75
|
-
_command = command;
|
|
76
|
-
},
|
|
77
|
-
configResolved: function configResolved(config) {
|
|
78
|
-
viteConfig = config;
|
|
79
|
-
devEntryPath = config.base + devEntryPath.replace(/\\\\?/g, '/').replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/^\//, '');
|
|
80
|
-
},
|
|
81
|
-
configureServer: function configureServer(server) {
|
|
82
|
-
server.middlewares.use(function (req, res, next) {
|
|
83
|
-
if (!fileName) {
|
|
84
|
-
next();
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (req.url && req.url.startsWith((viteConfig.base + fileName).replace(/^\/?/, '/'))) {
|
|
88
|
-
req.url = devEntryPath;
|
|
89
|
-
}
|
|
90
|
-
next();
|
|
91
|
-
});
|
|
92
|
-
},
|
|
93
|
-
transformIndexHtml: function transformIndexHtml(c) {
|
|
94
|
-
if (!injectHtml()) return;
|
|
95
|
-
return c.replace('<head>', "<head><script type=\"module\" src=" + JSON.stringify(devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/')) + "></script>");
|
|
96
|
-
},
|
|
97
|
-
transform: function transform(code, id) {
|
|
98
|
-
if (id.includes('node_modules') || inject !== 'html' || htmlFilePath) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
if (id.includes('.svelte-kit') && id.includes('internal.js')) {
|
|
102
|
-
var src = devEntryPath.replace(/.+?\:([/\\])[/\\]?/, '$1').replace(/\\\\?/g, '/');
|
|
103
|
-
return code.replace(/<head>/g, '<head><script type=\\"module\\" src=\\"' + src + '\\"></script>');
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}, {
|
|
107
|
-
name: 'add-entry',
|
|
108
|
-
enforce: 'post',
|
|
109
|
-
configResolved: function configResolved(config) {
|
|
110
|
-
viteConfig = config;
|
|
111
|
-
var inputOptions = config.build.rollupOptions.input;
|
|
112
|
-
if (!inputOptions) {
|
|
113
|
-
htmlFilePath = path__namespace.resolve(config.root, 'index.html');
|
|
114
|
-
} else if (typeof inputOptions === 'string') {
|
|
115
|
-
entryFiles = [inputOptions];
|
|
116
|
-
} else if (Array.isArray(inputOptions)) {
|
|
117
|
-
entryFiles = inputOptions;
|
|
118
|
-
} else if (typeof inputOptions === 'object') {
|
|
119
|
-
entryFiles = Object.values(inputOptions);
|
|
120
|
-
}
|
|
121
|
-
if (entryFiles && entryFiles.length > 0) {
|
|
122
|
-
htmlFilePath = getFirstHtmlEntryFile(entryFiles);
|
|
123
|
-
}
|
|
124
|
-
},
|
|
125
|
-
buildStart: function buildStart() {
|
|
126
|
-
if (_command === 'serve') return;
|
|
127
|
-
var hasHash = fileName == null || fileName.includes == null ? void 0 : fileName.includes('[hash');
|
|
128
|
-
var emitFileOptions = {
|
|
129
|
-
name: entryName,
|
|
130
|
-
type: 'chunk',
|
|
131
|
-
id: entryPath,
|
|
132
|
-
preserveSignature: 'strict'
|
|
133
|
-
};
|
|
134
|
-
if (!hasHash) {
|
|
135
|
-
emitFileOptions.fileName = fileName;
|
|
136
|
-
}
|
|
137
|
-
emitFileId = this.emitFile(emitFileOptions);
|
|
138
|
-
if (htmlFilePath && fs__namespace.existsSync(htmlFilePath)) {
|
|
139
|
-
var htmlContent = fs__namespace.readFileSync(htmlFilePath, 'utf-8');
|
|
140
|
-
var scriptRegex = /<script\s+[^>]*src=["']([^"']+)["'][^>]*>/gi;
|
|
141
|
-
var match;
|
|
142
|
-
while ((match = scriptRegex.exec(htmlContent)) !== null) {
|
|
143
|
-
entryFiles.push(match[1]);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
generateBundle: function generateBundle(options, bundle) {
|
|
148
|
-
var _viteConfig$experimen, _viteConfig$experimen2;
|
|
149
|
-
if (!injectHtml()) return;
|
|
150
|
-
var file = this.getFileName(emitFileId);
|
|
151
|
-
var path = (_viteConfig$experimen = viteConfig.experimental) != null && _viteConfig$experimen.renderBuiltUrl ? (_viteConfig$experimen2 = viteConfig.experimental) == null ? void 0 : _viteConfig$experimen2.renderBuiltUrl(file) : viteConfig.base + file;
|
|
152
|
-
var scriptContent = "\n <script type=\"module\" src=\"" + path + "\"></script>\n ";
|
|
153
|
-
for (var _fileName in bundle) {
|
|
154
|
-
if (_fileName.endsWith('.html')) {
|
|
155
|
-
var htmlAsset = bundle[_fileName];
|
|
156
|
-
if (htmlAsset.type === 'chunk') return;
|
|
157
|
-
var htmlContent = htmlAsset.source.toString() || '';
|
|
158
|
-
htmlContent = htmlContent.replace('<head>', "<head>" + scriptContent);
|
|
159
|
-
htmlAsset.source = htmlContent;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
},
|
|
163
|
-
transform: function transform(code, id) {
|
|
164
|
-
if (injectEntry() && entryFiles.some(function (file) {
|
|
165
|
-
return id.endsWith(file);
|
|
166
|
-
})) {
|
|
167
|
-
var injection = "\n import " + JSON.stringify(entryPath) + ";\n ";
|
|
168
|
-
return mapCodeToCodeWithSourcemap(injection + code);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}];
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
function _arrayLikeToArray(r, a) {
|
|
175
|
-
(null == a || a > r.length) && (a = r.length);
|
|
176
|
-
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
177
|
-
return n;
|
|
178
|
-
}
|
|
179
|
-
function _createForOfIteratorHelperLoose(r, e) {
|
|
180
|
-
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
181
|
-
if (t) return (t = t.call(r)).next.bind(t);
|
|
182
|
-
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
|
|
183
|
-
t && (r = t);
|
|
184
|
-
var o = 0;
|
|
185
|
-
return function () {
|
|
186
|
-
return o >= r.length ? {
|
|
187
|
-
done: !0
|
|
188
|
-
} : {
|
|
189
|
-
done: !1,
|
|
190
|
-
value: r[o++]
|
|
191
|
-
};
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
195
|
-
}
|
|
196
|
-
function _extends() {
|
|
197
|
-
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
198
|
-
for (var e = 1; e < arguments.length; e++) {
|
|
199
|
-
var t = arguments[e];
|
|
200
|
-
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
201
|
-
}
|
|
202
|
-
return n;
|
|
203
|
-
}, _extends.apply(null, arguments);
|
|
204
|
-
}
|
|
205
|
-
function _unsupportedIterableToArray(r, a) {
|
|
206
|
-
if (r) {
|
|
207
|
-
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
208
|
-
var t = {}.toString.call(r).slice(8, -1);
|
|
209
|
-
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;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Check if user-defined alias conflicts with shared modules
|
|
215
|
-
* This should run after aliasToArrayPlugin to ensure alias is an array
|
|
216
|
-
*/
|
|
217
|
-
function checkAliasConflicts(options) {
|
|
218
|
-
var _options$shared = options.shared,
|
|
219
|
-
shared = _options$shared === void 0 ? {} : _options$shared;
|
|
220
|
-
var sharedKeys = Object.keys(shared);
|
|
221
|
-
return {
|
|
222
|
-
name: 'check-alias-conflicts',
|
|
223
|
-
configResolved: function configResolved(config) {
|
|
224
|
-
var _config$resolve;
|
|
225
|
-
if (sharedKeys.length === 0) return;
|
|
226
|
-
var userAliases = ((_config$resolve = config.resolve) == null ? void 0 : _config$resolve.alias) || [];
|
|
227
|
-
var conflicts = [];
|
|
228
|
-
for (var _i = 0, _sharedKeys = sharedKeys; _i < _sharedKeys.length; _i++) {
|
|
229
|
-
var sharedKey = _sharedKeys[_i];
|
|
230
|
-
for (var _iterator = _createForOfIteratorHelperLoose(userAliases), _step; !(_step = _iterator()).done;) {
|
|
231
|
-
var aliasEntry = _step.value;
|
|
232
|
-
var findPattern = aliasEntry.find;
|
|
233
|
-
var replacement = aliasEntry.replacement;
|
|
234
|
-
// Skip if replacement is not a string (e.g., customResolver)
|
|
235
|
-
if (typeof replacement !== 'string') continue;
|
|
236
|
-
// Skip Module Federation internal aliases (used for proxying shared modules)
|
|
237
|
-
// These are generated with replacement '$1' and should not trigger warnings
|
|
238
|
-
if (replacement === '$1') continue;
|
|
239
|
-
// Check if alias pattern matches the shared module
|
|
240
|
-
var isMatch = false;
|
|
241
|
-
if (typeof findPattern === 'string') {
|
|
242
|
-
isMatch = findPattern === sharedKey || sharedKey.startsWith(findPattern + '/');
|
|
243
|
-
} else if (findPattern instanceof RegExp) {
|
|
244
|
-
isMatch = findPattern.test(sharedKey);
|
|
245
|
-
}
|
|
246
|
-
if (isMatch) {
|
|
247
|
-
conflicts.push({
|
|
248
|
-
sharedModule: sharedKey,
|
|
249
|
-
alias: String(findPattern),
|
|
250
|
-
target: replacement
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (conflicts.length > 0) {
|
|
256
|
-
config.logger.warn('\n[Module Federation] Detected alias conflicts with shared modules:');
|
|
257
|
-
conflicts.forEach(function (_ref) {
|
|
258
|
-
var sharedModule = _ref.sharedModule,
|
|
259
|
-
alias = _ref.alias,
|
|
260
|
-
target = _ref.target;
|
|
261
|
-
config.logger.warn(" - Shared module \"" + sharedModule + "\" is aliased by \"" + alias + "\" to \"" + target + "\"");
|
|
262
|
-
});
|
|
263
|
-
config.logger.warn(" This may cause runtime errors as the shared module will bypass Module Federation's sharing mechanism.");
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Solve the problem that dev mode dependency prebunding does not support top-level await syntax
|
|
271
|
-
*/
|
|
272
|
-
function PluginDevProxyModuleTopLevelAwait() {
|
|
273
|
-
var filterFunction = pluginutils.createFilter();
|
|
274
|
-
var processedFlag = '/* already-processed-by-dev-proxy-module-top-level-await */';
|
|
275
|
-
return {
|
|
276
|
-
name: 'dev-proxy-module-top-level-await',
|
|
277
|
-
apply: 'serve',
|
|
278
|
-
transform: function transform(code, id) {
|
|
279
|
-
if (code.includes(processedFlag)) {
|
|
280
|
-
return null;
|
|
281
|
-
}
|
|
282
|
-
if (!code.includes('/*mf top-level-await placeholder replacement mf*/')) {
|
|
283
|
-
return null;
|
|
284
|
-
}
|
|
285
|
-
if (!filterFunction(id)) return null;
|
|
286
|
-
var ast;
|
|
287
|
-
try {
|
|
288
|
-
ast = this.parse(code, {
|
|
289
|
-
allowReturnOutsideFunction: true
|
|
290
|
-
});
|
|
291
|
-
} catch (e) {
|
|
292
|
-
throw new Error(id + ": " + e);
|
|
293
|
-
}
|
|
294
|
-
var magicString = new MagicString__default["default"](code);
|
|
295
|
-
estreeWalker.walk(ast, {
|
|
296
|
-
enter: function enter(node) {
|
|
297
|
-
if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
|
|
298
|
-
var exportSpecifiers = node.specifiers.map(function (specifier) {
|
|
299
|
-
return specifier.exported.name;
|
|
300
|
-
});
|
|
301
|
-
var proxyStatements = exportSpecifiers.map(function (name) {
|
|
302
|
-
return "\n const __mfproxy__await" + name + " = await " + name + "();\n const __mfproxy__" + name + " = () => __mfproxy__await" + name + ";\n ";
|
|
303
|
-
}).join('\n');
|
|
304
|
-
var exportStatements = exportSpecifiers.map(function (name) {
|
|
305
|
-
return "__mfproxy__" + name + " as " + name;
|
|
306
|
-
}).join(', ');
|
|
307
|
-
var start = node.start;
|
|
308
|
-
var end = node.end;
|
|
309
|
-
var replacement = proxyStatements + "\nexport { " + exportStatements + " };";
|
|
310
|
-
magicString.overwrite(start, end, replacement);
|
|
311
|
-
}
|
|
312
|
-
if (node.type === 'ExportDefaultDeclaration') {
|
|
313
|
-
var declaration = node.declaration;
|
|
314
|
-
var _start = node.start;
|
|
315
|
-
var _end = node.end;
|
|
316
|
-
var proxyStatement;
|
|
317
|
-
var exportStatement = 'default';
|
|
318
|
-
if (declaration.type === 'Identifier') {
|
|
319
|
-
// example: export default foo;
|
|
320
|
-
proxyStatement = "\n const __mfproxy__awaitdefault = await " + declaration.name + "();\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
321
|
-
} else if (declaration.type === 'CallExpression' || declaration.type === 'FunctionDeclaration') {
|
|
322
|
-
// example: export default someFunction();
|
|
323
|
-
var declarationCode = code.slice(declaration.start, declaration.end);
|
|
324
|
-
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + declarationCode + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
325
|
-
} else {
|
|
326
|
-
// other
|
|
327
|
-
proxyStatement = "\n const __mfproxy__awaitdefault = await (" + code.slice(declaration.start, declaration.end) + ");\n const __mfproxy__default = __mfproxy__awaitdefault;\n ";
|
|
328
|
-
}
|
|
329
|
-
var _replacement = proxyStatement + "\nexport { __mfproxy__default as " + exportStatement + " };";
|
|
330
|
-
magicString.overwrite(_start, _end, _replacement);
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
var transformedCode = magicString.toString();
|
|
335
|
-
return {
|
|
336
|
-
code: processedFlag + "\n" + transformedCode,
|
|
337
|
-
map: magicString.generateMap({
|
|
338
|
-
hires: true
|
|
339
|
-
})
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
function _catch(body, recover) {
|
|
346
|
-
try {
|
|
347
|
-
var result = body();
|
|
348
|
-
} catch (e) {
|
|
349
|
-
return recover(e);
|
|
350
|
-
}
|
|
351
|
-
if (result && result.then) {
|
|
352
|
-
return result.then(void 0, recover);
|
|
353
|
-
}
|
|
354
|
-
return result;
|
|
355
|
-
}
|
|
356
|
-
var DEFAULT_DEV_OPTIONS = {
|
|
357
|
-
disableLiveReload: true,
|
|
358
|
-
disableHotTypesReload: false,
|
|
359
|
-
disableDynamicRemoteTypeHints: false
|
|
360
|
-
};
|
|
361
|
-
var DYNAMIC_HINTS_PLUGIN = '@module-federation/dts-plugin/dynamic-remote-type-hints-plugin';
|
|
362
|
-
var getIPv4 = function getIPv4() {
|
|
363
|
-
return process.env['FEDERATION_IPV4'] || '127.0.0.1';
|
|
364
|
-
};
|
|
365
|
-
var forkDevWorkerPath = function () {
|
|
366
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
367
|
-
return require.resolve('@module-federation/dts-plugin/dist/fork-dev-worker.js');
|
|
368
|
-
}();
|
|
369
|
-
var DevWorker = /*#__PURE__*/function () {
|
|
370
|
-
function DevWorker(options) {
|
|
371
|
-
this.worker = core.rpc.createRpcWorker(forkDevWorkerPath, {}, undefined, false);
|
|
372
|
-
this.worker.connect(options);
|
|
373
|
-
}
|
|
374
|
-
var _proto = DevWorker.prototype;
|
|
375
|
-
_proto.update = function update() {
|
|
376
|
-
var _this$worker$process;
|
|
377
|
-
(_this$worker$process = this.worker.process) == null || _this$worker$process.send == null || _this$worker$process.send({
|
|
378
|
-
type: core.rpc.RpcGMCallTypes.CALL,
|
|
379
|
-
id: this.worker.id,
|
|
380
|
-
args: [undefined, 'update']
|
|
381
|
-
});
|
|
382
|
-
};
|
|
383
|
-
_proto.exit = function exit() {
|
|
384
|
-
this.worker.terminate();
|
|
385
|
-
};
|
|
386
|
-
return DevWorker;
|
|
387
|
-
}();
|
|
388
|
-
var normalizeDevOptions = function normalizeDevOptions(dev) {
|
|
389
|
-
if (dev === false) {
|
|
390
|
-
return false;
|
|
391
|
-
}
|
|
392
|
-
if (dev === true || typeof dev === 'undefined') {
|
|
393
|
-
return _extends({}, DEFAULT_DEV_OPTIONS);
|
|
394
|
-
}
|
|
395
|
-
return _extends({}, DEFAULT_DEV_OPTIONS, dev);
|
|
396
|
-
};
|
|
397
|
-
var buildDtsModuleFederationConfig = function buildDtsModuleFederationConfig(options) {
|
|
398
|
-
var exposes = {};
|
|
399
|
-
Object.entries(options.exposes).forEach(function (_ref) {
|
|
400
|
-
var key = _ref[0],
|
|
401
|
-
value = _ref[1];
|
|
402
|
-
if (typeof value === 'string') {
|
|
403
|
-
exposes[key] = value;
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
var importValue = Array.isArray(value["import"]) ? value["import"][0] : value["import"];
|
|
407
|
-
if (importValue) {
|
|
408
|
-
exposes[key] = importValue;
|
|
409
|
-
}
|
|
410
|
-
});
|
|
411
|
-
var remotes = {};
|
|
412
|
-
Object.entries(options.remotes).forEach(function (_ref2) {
|
|
413
|
-
var _remote$entryGlobalNa, _remote$entryGlobalNa2;
|
|
414
|
-
var key = _ref2[0],
|
|
415
|
-
remote = _ref2[1];
|
|
416
|
-
if (typeof remote === 'string') {
|
|
417
|
-
remotes[key] = remote;
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
if (!remote.entry) {
|
|
421
|
-
return;
|
|
422
|
-
}
|
|
423
|
-
var entryLooksLikeUrl = ((_remote$entryGlobalNa = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa.startsWith('http')) || ((_remote$entryGlobalNa2 = remote.entryGlobalName) == null ? void 0 : _remote$entryGlobalNa2.includes('.json'));
|
|
424
|
-
var entryGlobalName = entryLooksLikeUrl ? remote.name || key : remote.entryGlobalName || remote.name || key;
|
|
425
|
-
remotes[key] = entryGlobalName + "@" + remote.entry;
|
|
426
|
-
});
|
|
427
|
-
return _extends({}, options, {
|
|
428
|
-
exposes: exposes,
|
|
429
|
-
remotes: remotes
|
|
430
|
-
});
|
|
431
|
-
};
|
|
432
|
-
var resolveOutputDir = function resolveOutputDir(config) {
|
|
433
|
-
var outDir = config.build.outDir;
|
|
434
|
-
if (path__namespace.isAbsolute(outDir)) {
|
|
435
|
-
return path__namespace.relative(config.root, outDir);
|
|
436
|
-
}
|
|
437
|
-
return outDir;
|
|
438
|
-
};
|
|
439
|
-
var ensureRuntimePlugin = function ensureRuntimePlugin(options, pluginId) {
|
|
440
|
-
var hasPlugin = options.runtimePlugins.some(function (plugin) {
|
|
441
|
-
if (typeof plugin === 'string') {
|
|
442
|
-
return plugin === pluginId;
|
|
443
|
-
}
|
|
444
|
-
return plugin[0] === pluginId;
|
|
445
|
-
});
|
|
446
|
-
if (!hasPlugin) {
|
|
447
|
-
options.runtimePlugins.push(pluginId);
|
|
448
|
-
}
|
|
449
|
-
};
|
|
450
|
-
var normalizeDevDtsOptions = function normalizeDevDtsOptions(dts, context) {
|
|
451
|
-
var defaultGenerateTypes = {
|
|
452
|
-
compileInChildProcess: true
|
|
453
|
-
};
|
|
454
|
-
var defaultConsumeTypes = {
|
|
455
|
-
consumeAPITypes: true
|
|
456
|
-
};
|
|
457
|
-
return sdk.normalizeOptions(dtsPlugin.isTSProject(dts, context), {
|
|
458
|
-
generateTypes: defaultGenerateTypes,
|
|
459
|
-
consumeTypes: defaultConsumeTypes,
|
|
460
|
-
extraOptions: {},
|
|
461
|
-
displayErrorInTerminal: typeof dts === 'object' && dts ? dts.displayErrorInTerminal : undefined
|
|
462
|
-
}, 'mfOptions.dts')(dts);
|
|
463
|
-
};
|
|
464
|
-
var logDtsError = function logDtsError(error, dtsOptions) {
|
|
465
|
-
if (dtsOptions === false) {
|
|
466
|
-
return;
|
|
467
|
-
}
|
|
468
|
-
if (typeof dtsOptions === 'object' && dtsOptions && dtsOptions.displayErrorInTerminal === false) {
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
471
|
-
console.error(error);
|
|
472
|
-
};
|
|
473
|
-
function pluginDts(options) {
|
|
474
|
-
if (options.dts === false) {
|
|
475
|
-
return [];
|
|
476
|
-
}
|
|
477
|
-
var dtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
|
|
478
|
-
var resolvedConfig;
|
|
479
|
-
var devWorker;
|
|
480
|
-
var normalizedDevOptions;
|
|
481
|
-
var hasGeneratedBundle = false;
|
|
482
|
-
var devPlugin = {
|
|
483
|
-
name: 'module-federation-dts-dev',
|
|
484
|
-
apply: 'serve',
|
|
485
|
-
config: function config(_config) {
|
|
486
|
-
normalizedDevOptions = normalizeDevOptions(options.dev);
|
|
487
|
-
if (!normalizedDevOptions) {
|
|
488
|
-
return;
|
|
489
|
-
}
|
|
490
|
-
if (normalizedDevOptions.disableDynamicRemoteTypeHints) {
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
|
|
494
|
-
var define = _config.define ? _extends({}, _config.define) : {};
|
|
495
|
-
if (!('FEDERATION_IPV4' in define)) {
|
|
496
|
-
define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
|
|
497
|
-
}
|
|
498
|
-
_config.define = define;
|
|
499
|
-
},
|
|
500
|
-
configResolved: function configResolved(config) {
|
|
501
|
-
resolvedConfig = config;
|
|
502
|
-
},
|
|
503
|
-
configureServer: function configureServer(server) {
|
|
504
|
-
if (!normalizedDevOptions || !resolvedConfig) {
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
var devOptions = normalizedDevOptions;
|
|
508
|
-
if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) {
|
|
509
|
-
return;
|
|
510
|
-
}
|
|
511
|
-
if (!options.name) {
|
|
512
|
-
throw new Error('name is required if you want to enable dev server!');
|
|
513
|
-
}
|
|
514
|
-
var outputDir = resolveOutputDir(resolvedConfig);
|
|
515
|
-
var normalizedDtsOptions = normalizeDevDtsOptions(options.dts, resolvedConfig.root);
|
|
516
|
-
if (typeof normalizedDtsOptions !== 'object') {
|
|
517
|
-
return;
|
|
518
|
-
}
|
|
519
|
-
var normalizedGenerateTypes = sdk.normalizeOptions(Boolean(normalizedDtsOptions), {
|
|
520
|
-
compileInChildProcess: true
|
|
521
|
-
}, 'mfOptions.dts.generateTypes')(normalizedDtsOptions.generateTypes);
|
|
522
|
-
var remote = normalizedGenerateTypes === false ? undefined : _extends({
|
|
523
|
-
implementation: normalizedDtsOptions.implementation,
|
|
524
|
-
context: resolvedConfig.root,
|
|
525
|
-
outputDir: outputDir,
|
|
526
|
-
moduleFederationConfig: _extends({}, dtsModuleFederationConfig),
|
|
527
|
-
hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || '@mf-types'
|
|
528
|
-
}, normalizedGenerateTypes, {
|
|
529
|
-
typesFolder: '.dev-server'
|
|
530
|
-
});
|
|
531
|
-
if (remote && !remote.tsConfigPath && typeof normalizedDtsOptions === 'object' && normalizedDtsOptions.tsConfigPath) {
|
|
532
|
-
remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
533
|
-
}
|
|
534
|
-
var normalizedConsumeTypes = sdk.normalizeOptions(Boolean(normalizedDtsOptions), {
|
|
535
|
-
consumeAPITypes: true
|
|
536
|
-
}, 'mfOptions.dts.consumeTypes')(normalizedDtsOptions.consumeTypes);
|
|
537
|
-
var host = normalizedConsumeTypes === false ? undefined : _extends({
|
|
538
|
-
implementation: normalizedDtsOptions.implementation,
|
|
539
|
-
context: resolvedConfig.root,
|
|
540
|
-
moduleFederationConfig: dtsModuleFederationConfig,
|
|
541
|
-
typesFolder: normalizedConsumeTypes.typesFolder || '@mf-types',
|
|
542
|
-
abortOnError: false
|
|
543
|
-
}, normalizedConsumeTypes);
|
|
544
|
-
var extraOptions = normalizedDtsOptions.extraOptions || {};
|
|
545
|
-
if (!remote && !host && devOptions.disableLiveReload) {
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
var startDevWorker = function startDevWorker() {
|
|
549
|
-
try {
|
|
550
|
-
var _temp2 = function _temp2() {
|
|
551
|
-
var _server$httpServer;
|
|
552
|
-
devWorker = new DevWorker({
|
|
553
|
-
name: options.name,
|
|
554
|
-
remote: remote,
|
|
555
|
-
host: host ? _extends({}, host, {
|
|
556
|
-
remoteTypeUrls: remoteTypeUrls
|
|
557
|
-
}) : undefined,
|
|
558
|
-
extraOptions: extraOptions,
|
|
559
|
-
disableLiveReload: devOptions.disableLiveReload,
|
|
560
|
-
disableHotTypesReload: devOptions.disableHotTypesReload
|
|
561
|
-
});
|
|
562
|
-
var update = function update() {
|
|
563
|
-
var _devWorker;
|
|
564
|
-
return (_devWorker = devWorker) == null ? void 0 : _devWorker.update();
|
|
565
|
-
};
|
|
566
|
-
server.watcher.on('change', update);
|
|
567
|
-
server.watcher.on('add', update);
|
|
568
|
-
server.watcher.on('unlink', update);
|
|
569
|
-
(_server$httpServer = server.httpServer) == null || _server$httpServer.once('close', function () {
|
|
570
|
-
var _devWorker2;
|
|
571
|
-
(_devWorker2 = devWorker) == null || _devWorker2.exit();
|
|
572
|
-
server.watcher.off('change', update);
|
|
573
|
-
server.watcher.off('add', update);
|
|
574
|
-
server.watcher.off('unlink', update);
|
|
575
|
-
});
|
|
576
|
-
};
|
|
577
|
-
var remoteTypeUrls;
|
|
578
|
-
var _temp = function () {
|
|
579
|
-
if (host) {
|
|
580
|
-
return Promise.resolve(new Promise(function (resolve) {
|
|
581
|
-
dtsPlugin.consumeTypesAPI({
|
|
582
|
-
host: host,
|
|
583
|
-
extraOptions: extraOptions,
|
|
584
|
-
displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
|
|
585
|
-
}, resolve);
|
|
586
|
-
})).then(function (_Promise) {
|
|
587
|
-
remoteTypeUrls = _Promise;
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
}();
|
|
591
|
-
return Promise.resolve(_temp && _temp.then ? _temp.then(_temp2) : _temp2(_temp));
|
|
592
|
-
} catch (e) {
|
|
593
|
-
return Promise.reject(e);
|
|
594
|
-
}
|
|
595
|
-
};
|
|
596
|
-
startDevWorker()["catch"](function (error) {
|
|
597
|
-
logDtsError(error, normalizedDtsOptions);
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
};
|
|
601
|
-
var buildPlugin = {
|
|
602
|
-
name: 'module-federation-dts-build',
|
|
603
|
-
apply: 'build',
|
|
604
|
-
configResolved: function configResolved(config) {
|
|
605
|
-
resolvedConfig = config;
|
|
606
|
-
},
|
|
607
|
-
generateBundle: function generateBundle() {
|
|
608
|
-
try {
|
|
609
|
-
var _temp6 = function _temp6() {
|
|
610
|
-
var generateOptions;
|
|
611
|
-
try {
|
|
612
|
-
generateOptions = dtsPlugin.normalizeGenerateTypesOptions({
|
|
613
|
-
context: context,
|
|
614
|
-
outputDir: outputDir,
|
|
615
|
-
dtsOptions: normalizedDtsOptions,
|
|
616
|
-
pluginOptions: dtsModuleFederationConfig
|
|
617
|
-
});
|
|
618
|
-
} catch (error) {
|
|
619
|
-
logDtsError(error, normalizedDtsOptions);
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
if (!generateOptions) {
|
|
623
|
-
return;
|
|
624
|
-
}
|
|
625
|
-
var _temp4 = _catch(function () {
|
|
626
|
-
return Promise.resolve(dtsPlugin.generateTypesAPI({
|
|
627
|
-
dtsManagerOptions: generateOptions
|
|
628
|
-
})).then(function () {});
|
|
629
|
-
}, function (error) {
|
|
630
|
-
logDtsError(error, normalizedDtsOptions);
|
|
631
|
-
});
|
|
632
|
-
if (_temp4 && _temp4.then) return _temp4.then(function () {});
|
|
633
|
-
};
|
|
634
|
-
if (hasGeneratedBundle) {
|
|
635
|
-
return Promise.resolve();
|
|
636
|
-
}
|
|
637
|
-
hasGeneratedBundle = true;
|
|
638
|
-
if (!resolvedConfig) {
|
|
639
|
-
return Promise.resolve();
|
|
640
|
-
}
|
|
641
|
-
var normalizedDtsOptions;
|
|
642
|
-
try {
|
|
643
|
-
normalizedDtsOptions = dtsPlugin.normalizeDtsOptions(dtsModuleFederationConfig, resolvedConfig.root);
|
|
644
|
-
} catch (error) {
|
|
645
|
-
logDtsError(error, options.dts);
|
|
646
|
-
return Promise.resolve();
|
|
647
|
-
}
|
|
648
|
-
if (typeof normalizedDtsOptions !== 'object') {
|
|
649
|
-
return Promise.resolve();
|
|
650
|
-
}
|
|
651
|
-
var context = resolvedConfig.root;
|
|
652
|
-
var outputDir = resolveOutputDir(resolvedConfig);
|
|
653
|
-
var consumeOptions;
|
|
654
|
-
try {
|
|
655
|
-
consumeOptions = dtsPlugin.normalizeConsumeTypesOptions({
|
|
656
|
-
context: context,
|
|
657
|
-
dtsOptions: normalizedDtsOptions,
|
|
658
|
-
pluginOptions: dtsModuleFederationConfig
|
|
659
|
-
});
|
|
660
|
-
} catch (error) {
|
|
661
|
-
logDtsError(error, normalizedDtsOptions);
|
|
662
|
-
return Promise.resolve();
|
|
663
|
-
}
|
|
664
|
-
var _temp5 = function (_consumeOptions) {
|
|
665
|
-
if ((_consumeOptions = consumeOptions) != null && (_consumeOptions = _consumeOptions.host) != null && _consumeOptions.typesOnBuild) {
|
|
666
|
-
var _temp3 = _catch(function () {
|
|
667
|
-
return Promise.resolve(dtsPlugin.consumeTypesAPI(consumeOptions)).then(function () {});
|
|
668
|
-
}, function (error) {
|
|
669
|
-
logDtsError(error, normalizedDtsOptions);
|
|
670
|
-
});
|
|
671
|
-
if (_temp3 && _temp3.then) return _temp3.then(function () {});
|
|
672
|
-
}
|
|
673
|
-
}();
|
|
674
|
-
return Promise.resolve(_temp5 && _temp5.then ? _temp5.then(_temp6) : _temp6(_temp5));
|
|
675
|
-
} catch (e) {
|
|
676
|
-
return Promise.reject(e);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
};
|
|
680
|
-
return [devPlugin, buildPlugin];
|
|
681
|
-
}
|
|
682
|
-
|
|
683
|
-
function normalizeExposesItem(key, item) {
|
|
684
|
-
var importPath = '';
|
|
685
|
-
if (typeof item === 'string') {
|
|
686
|
-
importPath = item;
|
|
687
|
-
}
|
|
688
|
-
if (typeof item === 'object') {
|
|
689
|
-
importPath = item["import"];
|
|
690
|
-
}
|
|
691
|
-
return {
|
|
692
|
-
"import": importPath
|
|
693
|
-
};
|
|
694
|
-
}
|
|
695
|
-
function normalizeExposes(exposes) {
|
|
696
|
-
if (!exposes) return {};
|
|
697
|
-
var res = {};
|
|
698
|
-
Object.keys(exposes).forEach(function (key) {
|
|
699
|
-
res[key] = normalizeExposesItem(key, exposes[key]);
|
|
700
|
-
});
|
|
701
|
-
return res;
|
|
702
|
-
}
|
|
703
|
-
function normalizeRemotes(remotes) {
|
|
704
|
-
if (!remotes) return {};
|
|
705
|
-
var result = {};
|
|
706
|
-
if (typeof remotes === 'object') {
|
|
707
|
-
Object.keys(remotes).forEach(function (key) {
|
|
708
|
-
result[key] = normalizeRemoteItem(key, remotes[key]);
|
|
709
|
-
});
|
|
710
|
-
}
|
|
711
|
-
return result;
|
|
712
|
-
}
|
|
713
|
-
function normalizeRemoteItem(key, remote) {
|
|
714
|
-
if (typeof remote === 'string') {
|
|
715
|
-
var _remote$split = remote.split('@'),
|
|
716
|
-
entryGlobalName = _remote$split[0];
|
|
717
|
-
var entry = remote.replace(entryGlobalName + '@', '');
|
|
718
|
-
return {
|
|
719
|
-
type: 'var',
|
|
720
|
-
name: key,
|
|
721
|
-
entry: entry,
|
|
722
|
-
entryGlobalName: entryGlobalName,
|
|
723
|
-
shareScope: 'default'
|
|
724
|
-
};
|
|
725
|
-
}
|
|
726
|
-
return Object.assign({
|
|
727
|
-
type: 'var',
|
|
728
|
-
name: key,
|
|
729
|
-
shareScope: 'default',
|
|
730
|
-
entryGlobalName: key
|
|
731
|
-
}, remote);
|
|
732
|
-
}
|
|
733
|
-
function removePathFromNpmPackage(packageString) {
|
|
734
|
-
// 匹配npm包名的正则表达式,忽略路径部分
|
|
735
|
-
var regex = /^(?:@[^/]+\/)?[^/]+/;
|
|
736
|
-
// 使用正则表达式匹配并提取包名
|
|
737
|
-
var match = packageString.match(regex);
|
|
738
|
-
// 返回匹配到的包名,如果没有匹配到则返回原字符串
|
|
739
|
-
return match ? match[0] : packageString;
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
742
|
-
* Tries to find the package.json's version of a shared package
|
|
743
|
-
* if `package.json` is not declared in `exports`
|
|
744
|
-
* @param {string} sharedName
|
|
745
|
-
* @returns {string | undefined}
|
|
746
|
-
*/
|
|
747
|
-
function searchPackageVersion(sharedName) {
|
|
748
|
-
try {
|
|
749
|
-
var sharedPath = require.resolve(sharedName);
|
|
750
|
-
var potentialPackageJsonDir = path__namespace.dirname(sharedPath);
|
|
751
|
-
var rootDir = path__namespace.parse(potentialPackageJsonDir).root;
|
|
752
|
-
while (path__namespace.parse(potentialPackageJsonDir).base !== 'node_modules' && potentialPackageJsonDir !== rootDir) {
|
|
753
|
-
var potentialPackageJsonPath = path__namespace.join(potentialPackageJsonDir, 'package.json');
|
|
754
|
-
if (fs__namespace.existsSync(potentialPackageJsonPath)) {
|
|
755
|
-
var potentialPackageJson = require(potentialPackageJsonPath);
|
|
756
|
-
if (typeof potentialPackageJson == 'object' && potentialPackageJson !== null && typeof potentialPackageJson.version === 'string' && potentialPackageJson.name === sharedName) {
|
|
757
|
-
return potentialPackageJson.version;
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
potentialPackageJsonDir = path__namespace.dirname(potentialPackageJsonDir);
|
|
761
|
-
}
|
|
762
|
-
} catch (_) {}
|
|
763
|
-
return undefined;
|
|
764
|
-
}
|
|
765
|
-
function normalizeShareItem(key, shareItem) {
|
|
766
|
-
var version;
|
|
767
|
-
try {
|
|
768
|
-
try {
|
|
769
|
-
version = require(path__namespace.join(removePathFromNpmPackage(key), 'package.json')).version;
|
|
770
|
-
} catch (e1) {
|
|
771
|
-
try {
|
|
772
|
-
var localPath = path__namespace.join(process.cwd(), 'node_modules', removePathFromNpmPackage(key), 'package.json');
|
|
773
|
-
version = require(localPath).version;
|
|
774
|
-
} catch (e2) {
|
|
775
|
-
version = searchPackageVersion(key);
|
|
776
|
-
if (!version) console.error(e1);
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
} catch (e) {
|
|
780
|
-
console.error("Unexpected error resolving version for " + key + ":", e);
|
|
781
|
-
}
|
|
782
|
-
if (typeof shareItem === 'string') {
|
|
783
|
-
return {
|
|
784
|
-
name: shareItem,
|
|
785
|
-
version: version,
|
|
786
|
-
scope: 'default',
|
|
787
|
-
from: '',
|
|
788
|
-
shareConfig: {
|
|
789
|
-
"import": undefined,
|
|
790
|
-
singleton: false,
|
|
791
|
-
requiredVersion: version ? "^" + version : '*'
|
|
792
|
-
}
|
|
793
|
-
};
|
|
794
|
-
}
|
|
795
|
-
return {
|
|
796
|
-
name: key,
|
|
797
|
-
from: '',
|
|
798
|
-
version: shareItem.version || version,
|
|
799
|
-
scope: shareItem.shareScope || 'default',
|
|
800
|
-
shareConfig: {
|
|
801
|
-
"import": typeof shareItem === 'object' ? shareItem["import"] : undefined,
|
|
802
|
-
singleton: shareItem.singleton || false,
|
|
803
|
-
requiredVersion: shareItem.requiredVersion || (version ? "^" + version : '*'),
|
|
804
|
-
strictVersion: !!shareItem.strictVersion
|
|
805
|
-
}
|
|
806
|
-
};
|
|
807
|
-
}
|
|
808
|
-
function normalizeShared(shared) {
|
|
809
|
-
if (!shared) return {};
|
|
810
|
-
var result = {};
|
|
811
|
-
if (Array.isArray(shared)) {
|
|
812
|
-
shared.forEach(function (key) {
|
|
813
|
-
result[key] = normalizeShareItem(key, key);
|
|
814
|
-
});
|
|
815
|
-
return result;
|
|
816
|
-
}
|
|
817
|
-
if (typeof shared === 'object') {
|
|
818
|
-
Object.keys(shared).forEach(function (key) {
|
|
819
|
-
result[key] = normalizeShareItem(key, shared[key]);
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
return result;
|
|
823
|
-
}
|
|
824
|
-
function normalizeLibrary(library) {
|
|
825
|
-
if (!library) return undefined;
|
|
826
|
-
return library;
|
|
827
|
-
}
|
|
828
|
-
function normalizeManifest(manifest) {
|
|
829
|
-
if (manifest === void 0) {
|
|
830
|
-
manifest = false;
|
|
831
|
-
}
|
|
832
|
-
if (typeof manifest === 'boolean') {
|
|
833
|
-
return manifest;
|
|
834
|
-
}
|
|
835
|
-
return Object.assign({
|
|
836
|
-
filePath: '',
|
|
837
|
-
disableAssetsAnalyze: false,
|
|
838
|
-
fileName: 'mf-manifest.json'
|
|
839
|
-
}, manifest);
|
|
840
|
-
}
|
|
841
|
-
var config;
|
|
842
|
-
function getNormalizeModuleFederationOptions() {
|
|
843
|
-
return config;
|
|
844
|
-
}
|
|
845
|
-
function getNormalizeShareItem(key) {
|
|
846
|
-
var options = getNormalizeModuleFederationOptions();
|
|
847
|
-
var shareItem = options.shared[key] || options.shared[removePathFromNpmPackage(key)] || options.shared[removePathFromNpmPackage(key) + '/'];
|
|
848
|
-
return shareItem;
|
|
849
|
-
}
|
|
850
|
-
function normalizeModuleFederationOptions(options) {
|
|
851
|
-
if (options.virtualModuleDir && options.virtualModuleDir.includes('/')) {
|
|
852
|
-
throw new Error("Invalid virtualModuleDir: \"" + options.virtualModuleDir + "\". " + "The virtualModuleDir option cannot contain slashes (/). " + "Please use a single directory name like '__mf__virtual__your_app_name'.");
|
|
853
|
-
}
|
|
854
|
-
return config = {
|
|
855
|
-
exposes: normalizeExposes(options.exposes),
|
|
856
|
-
filename: options.filename || 'remoteEntry-[hash]',
|
|
857
|
-
library: normalizeLibrary(options.library),
|
|
858
|
-
name: options.name,
|
|
859
|
-
// remoteType: options.remoteType,
|
|
860
|
-
remotes: normalizeRemotes(options.remotes),
|
|
861
|
-
runtime: options.runtime,
|
|
862
|
-
shareScope: options.shareScope || 'default',
|
|
863
|
-
shared: normalizeShared(options.shared),
|
|
864
|
-
runtimePlugins: options.runtimePlugins || [],
|
|
865
|
-
implementation: options.implementation || require.resolve('@module-federation/runtime'),
|
|
866
|
-
manifest: normalizeManifest(options.manifest),
|
|
867
|
-
dev: options.dev,
|
|
868
|
-
dts: options.dts,
|
|
869
|
-
getPublicPath: options.getPublicPath,
|
|
870
|
-
publicPath: options.publicPath,
|
|
871
|
-
shareStrategy: options.shareStrategy || 'version-first',
|
|
872
|
-
ignoreOrigin: options.ignoreOrigin || false,
|
|
873
|
-
virtualModuleDir: options.virtualModuleDir || '__mf__virtual',
|
|
874
|
-
hostInitInjectLocation: options.hostInitInjectLocation || 'html',
|
|
875
|
-
bundleAllCSS: options.bundleAllCSS || false,
|
|
876
|
-
moduleParseTimeout: options.moduleParseTimeout || 10
|
|
877
|
-
};
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
/**
|
|
881
|
-
* Escaping rules:
|
|
882
|
-
* Convert using the format __${mapping}__, where _ and $ are not allowed in npm package names but can be used in variable names.
|
|
883
|
-
* @ => 1
|
|
884
|
-
* / => 2
|
|
885
|
-
* - => 3
|
|
886
|
-
* . => 4
|
|
887
|
-
*/
|
|
888
|
-
/**
|
|
889
|
-
* Encodes a package name into a valid file name.
|
|
890
|
-
* @param {string} name - The package name, e.g., "@scope/xx-xx.xx".
|
|
891
|
-
* @returns {string} - The encoded file name.
|
|
892
|
-
*/
|
|
893
|
-
function packageNameEncode(name) {
|
|
894
|
-
if (typeof name !== 'string') throw new Error('A string package name is required');
|
|
895
|
-
return name.replace(/@/g, '_mf_0_').replace(/\//g, '_mf_1_').replace(/-/g, '_mf_2_').replace(/\./g, '_mf_3_');
|
|
896
|
-
}
|
|
897
|
-
/**
|
|
898
|
-
* Decodes an encoded file name back to the original package name.
|
|
899
|
-
* @param {string} encoded - The encoded file name, e.g., "_mf_0_scope_mf_1_xx_mf_2_xx_mf_3_xx".
|
|
900
|
-
* @returns {string} - The decoded package name.
|
|
901
|
-
*/
|
|
902
|
-
function packageNameDecode(encoded) {
|
|
903
|
-
if (typeof encoded !== 'string') throw new Error('A string encoded file name is required');
|
|
904
|
-
return encoded.replace(/_mf_0_/g, '@').replace(/_mf_1_/g, '/').replace(/_mf_2_/g, '-').replace(/_mf_3_/g, '.');
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
/**
|
|
908
|
-
* https://github.com/module-federation/vite/issues/68
|
|
909
|
-
*/
|
|
910
|
-
function getLocalSharedImportMapPath_temp() {
|
|
911
|
-
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
912
|
-
name = _getNormalizeModuleFe.name;
|
|
913
|
-
return path__default["default"].resolve('.__mf__temp', packageNameEncode(name), 'localSharedImportMap');
|
|
914
|
-
}
|
|
915
|
-
function writeLocalSharedImportMap_temp(content) {
|
|
916
|
-
var localSharedImportMapId = getLocalSharedImportMapPath_temp();
|
|
917
|
-
createFile(localSharedImportMapId + '.js', '\n// Windows temporarily needs this file, https://github.com/module-federation/vite/issues/68\n' + content);
|
|
918
|
-
}
|
|
919
|
-
function createFile(filePath, content) {
|
|
920
|
-
var dir = path__default["default"].dirname(filePath);
|
|
921
|
-
fs.mkdirSync(dir, {
|
|
922
|
-
recursive: true
|
|
923
|
-
});
|
|
924
|
-
fs.writeFileSync(filePath, content);
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
/**
|
|
928
|
-
* Serializes a JavaScript object into a string of source code that can be evaluated.
|
|
929
|
-
* This function is used to create runtime plugin options without relying solely on JSON.stringify,
|
|
930
|
-
* allowing support for non-JSON types like RegExp, Date, Map, Set, and Functions.
|
|
931
|
-
* It also safely handles circular references.
|
|
932
|
-
*
|
|
933
|
-
* @param {Record<string, unknown>} options - The options object to serialize.
|
|
934
|
-
* @returns {string} The resulting JavaScript source code string.
|
|
935
|
-
*/
|
|
936
|
-
function serializeRuntimeOptions(options) {
|
|
937
|
-
// Use a WeakSet to track objects already encountered, which helps in detecting circular references.
|
|
938
|
-
var seenObjects = new WeakSet();
|
|
939
|
-
/**
|
|
940
|
-
* Recursive inner function to serialize any value into a source code string.
|
|
941
|
-
*/
|
|
942
|
-
function valueToCode(val) {
|
|
943
|
-
// 1. Handle primitive values
|
|
944
|
-
if (val === null) return 'null';
|
|
945
|
-
var type = typeof val;
|
|
946
|
-
if (type === 'string') return JSON.stringify(val);
|
|
947
|
-
if (type === 'number' || type === 'boolean') return String(val);
|
|
948
|
-
if (type === 'undefined') return 'undefined';
|
|
949
|
-
// Handle Symbol
|
|
950
|
-
if (type === 'symbol') {
|
|
951
|
-
var _val$description;
|
|
952
|
-
var desc = (_val$description = val.description) != null ? _val$description : '';
|
|
953
|
-
return "Symbol(" + JSON.stringify(desc) + ")";
|
|
954
|
-
}
|
|
955
|
-
// Handle Function (returns the function's source code)
|
|
956
|
-
if (type === 'function') return val.toString();
|
|
957
|
-
// 2. Handle special built-in objects
|
|
958
|
-
if (val instanceof Date) return "new Date(" + JSON.stringify(val.toISOString()) + ")";
|
|
959
|
-
if (val instanceof RegExp) {
|
|
960
|
-
return "new RegExp(" + JSON.stringify(val.source) + ", " + JSON.stringify(val.flags) + ")";
|
|
961
|
-
}
|
|
962
|
-
// 3. Check for circular references and mark object as seen
|
|
963
|
-
// This applies to objects, arrays, maps, and sets.
|
|
964
|
-
if (type === 'object') {
|
|
965
|
-
if (seenObjects.has(val)) {
|
|
966
|
-
// This object has been seen previously in the recursion path
|
|
967
|
-
return "\"__circular__\"";
|
|
968
|
-
}
|
|
969
|
-
seenObjects.add(val);
|
|
970
|
-
}
|
|
971
|
-
// 4. Handle Array, Map, Set
|
|
972
|
-
if (Array.isArray(val)) {
|
|
973
|
-
// Recursively serialize each element
|
|
974
|
-
return "[" + val.map(valueToCode).join(', ') + "]";
|
|
975
|
-
}
|
|
976
|
-
if (val instanceof Map) {
|
|
977
|
-
// Serialize Map entries into an array of [key, value] pairs
|
|
978
|
-
var entries = Array.from(val.entries()).map(function (_ref) {
|
|
979
|
-
var k = _ref[0],
|
|
980
|
-
v = _ref[1];
|
|
981
|
-
return "[" + valueToCode(k) + ", " + valueToCode(v) + "]";
|
|
982
|
-
});
|
|
983
|
-
return "new Map([" + entries.join(', ') + "])";
|
|
984
|
-
}
|
|
985
|
-
if (val instanceof Set) {
|
|
986
|
-
// Serialize Set values into an array
|
|
987
|
-
var items = Array.from(val.values()).map(valueToCode);
|
|
988
|
-
return "new Set([" + items.join(', ') + "])";
|
|
989
|
-
}
|
|
990
|
-
// 5. Handle plain objects (the default object type)
|
|
991
|
-
if (type === 'object') {
|
|
992
|
-
var properties = [];
|
|
993
|
-
// Iterate over the object's own enumerable properties
|
|
994
|
-
for (var key in val) {
|
|
995
|
-
if (Object.prototype.hasOwnProperty.call(val, key)) {
|
|
996
|
-
// Wrap the key in JSON.stringify to handle non-identifier keys
|
|
997
|
-
properties.push(JSON.stringify(key) + ": " + valueToCode(val[key]));
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
return "{" + properties.join(', ') + "}";
|
|
1001
|
-
}
|
|
1002
|
-
// 6. Fallback case (e.g., BigInt, other object types)
|
|
1003
|
-
// Coerce to string and then JSON.stringify that string for safety
|
|
1004
|
-
return JSON.stringify(String(val));
|
|
1005
|
-
}
|
|
1006
|
-
// Start serialization for the top-level object
|
|
1007
|
-
var topLevelProps = [];
|
|
1008
|
-
// Iterate over the properties of the root 'options' object
|
|
1009
|
-
for (var key in options) {
|
|
1010
|
-
if (Object.prototype.hasOwnProperty.call(options, key)) {
|
|
1011
|
-
topLevelProps.push(JSON.stringify(key) + ": " + valueToCode(options[key]));
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
return "{" + topLevelProps.join(', ') + "}";
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
// Cache root path
|
|
1018
|
-
var rootDir;
|
|
1019
|
-
function findNodeModulesDir(root) {
|
|
1020
|
-
if (root === void 0) {
|
|
1021
|
-
root = process.cwd();
|
|
1022
|
-
}
|
|
1023
|
-
var currentDir = root;
|
|
1024
|
-
while (currentDir !== path.parse(currentDir).root) {
|
|
1025
|
-
var nodeModulesPath = path.join(currentDir, 'node_modules');
|
|
1026
|
-
if (fs.existsSync(nodeModulesPath)) {
|
|
1027
|
-
return nodeModulesPath;
|
|
1028
|
-
}
|
|
1029
|
-
currentDir = path.dirname(currentDir);
|
|
1030
|
-
}
|
|
1031
|
-
return '';
|
|
1032
|
-
}
|
|
1033
|
-
// Cache nodeModulesDir result to avoid repeated calculations
|
|
1034
|
-
var cachedNodeModulesDir;
|
|
1035
|
-
function getNodeModulesDir() {
|
|
1036
|
-
if (!cachedNodeModulesDir) {
|
|
1037
|
-
cachedNodeModulesDir = findNodeModulesDir(rootDir);
|
|
1038
|
-
}
|
|
1039
|
-
return cachedNodeModulesDir;
|
|
1040
|
-
}
|
|
1041
|
-
function getSuffix(name) {
|
|
1042
|
-
var base = path.basename(name);
|
|
1043
|
-
var dotIndex = base.lastIndexOf('.');
|
|
1044
|
-
if (dotIndex > 0 && dotIndex < base.length - 1) {
|
|
1045
|
-
return base.slice(dotIndex);
|
|
1046
|
-
}
|
|
1047
|
-
return '.js';
|
|
1048
|
-
}
|
|
1049
|
-
var patternMap = {};
|
|
1050
|
-
var cacheMap = {};
|
|
1051
|
-
/**
|
|
1052
|
-
* Physically generate files as virtual modules under node_modules/__mf__virtual/*
|
|
1053
|
-
*/
|
|
1054
|
-
function assertModuleFound(tag, str) {
|
|
1055
|
-
if (str === void 0) {
|
|
1056
|
-
str = '';
|
|
1057
|
-
}
|
|
1058
|
-
var module = VirtualModule.findModule(tag, str);
|
|
1059
|
-
if (!module) {
|
|
1060
|
-
throw new Error("Module Federation shared module '" + str + "' not found. Please ensure it's installed as a dependency in your package.json.");
|
|
1061
|
-
}
|
|
1062
|
-
return module;
|
|
1063
|
-
}
|
|
1064
|
-
var VirtualModule = /*#__PURE__*/function () {
|
|
1065
|
-
function VirtualModule(name, tag, suffix) {
|
|
1066
|
-
if (tag === void 0) {
|
|
1067
|
-
tag = '__mf_v__';
|
|
1068
|
-
}
|
|
1069
|
-
if (suffix === void 0) {
|
|
1070
|
-
suffix = '';
|
|
1071
|
-
}
|
|
1072
|
-
this.name = void 0;
|
|
1073
|
-
this.tag = void 0;
|
|
1074
|
-
this.suffix = void 0;
|
|
1075
|
-
this.inited = false;
|
|
1076
|
-
this.name = name;
|
|
1077
|
-
this.tag = tag;
|
|
1078
|
-
this.suffix = suffix || getSuffix(name);
|
|
1079
|
-
if (!cacheMap[this.tag]) cacheMap[this.tag] = {};
|
|
1080
|
-
cacheMap[this.tag][this.name] = this;
|
|
1081
|
-
}
|
|
1082
|
-
/**
|
|
1083
|
-
* Set the root path for finding node_modules
|
|
1084
|
-
* @param root - Root path
|
|
1085
|
-
*/
|
|
1086
|
-
VirtualModule.setRoot = function setRoot(root) {
|
|
1087
|
-
rootDir = root;
|
|
1088
|
-
// Reset cache to ensure using the new root path
|
|
1089
|
-
cachedNodeModulesDir = undefined;
|
|
1090
|
-
}
|
|
1091
|
-
/**
|
|
1092
|
-
* Ensure virtual package directory exists
|
|
1093
|
-
*/;
|
|
1094
|
-
VirtualModule.ensureVirtualPackageExists = function ensureVirtualPackageExists() {
|
|
1095
|
-
var nodeModulesDir = getNodeModulesDir();
|
|
1096
|
-
var _getNormalizeModuleFe = getNormalizeModuleFederationOptions(),
|
|
1097
|
-
virtualModuleDir = _getNormalizeModuleFe.virtualModuleDir;
|
|
1098
|
-
var virtualPackagePath = path.resolve(nodeModulesDir, virtualModuleDir);
|
|
1099
|
-
if (!fs.existsSync(virtualPackagePath)) {
|
|
1100
|
-
fs.mkdirSync(virtualPackagePath);
|
|
1101
|
-
fs.writeFileSync(path.resolve(virtualPackagePath, 'empty.js'), '');
|
|
1102
|
-
fs.writeFileSync(path.resolve(virtualPackagePath, 'package.json'), JSON.stringify({
|
|
1103
|
-
name: virtualModuleDir,
|
|
1104
|
-
main: 'empty.js'
|
|
1105
|
-
}));
|
|
1106
|
-
}
|
|
1107
|
-
};
|
|
1108
|
-
VirtualModule.findModule = function findModule(tag, str) {
|
|
1109
|
-
if (str === void 0) {
|
|
1110
|
-
str = '';
|
|
1111
|
-
}
|
|
1112
|
-
if (!patternMap[tag]) patternMap[tag] = new RegExp("(.*" + packageNameEncode(tag) + "(.+?)" + packageNameEncode(tag) + ".*)");
|
|
1113
|
-
var moduleName = (str.match(patternMap[tag]) || [])[2];
|
|
1114
|
-
if (moduleName) return cacheMap[tag][packageNameDecode(moduleName)];
|
|
1115
|
-
return undefined;
|
|
1116
|
-
};
|
|
1117
|
-
var _proto = VirtualModule.prototype;
|
|
1118
|
-
_proto.getPath = function getPath() {
|
|
1119
|
-
return path.resolve(getNodeModulesDir(), this.getImportId());
|
|
1120
|
-
};
|
|
1121
|
-
_proto.getImportId = function getImportId() {
|
|
1122
|
-
var _getNormalizeModuleFe2 = getNormalizeModuleFederationOptions(),
|
|
1123
|
-
mfName = _getNormalizeModuleFe2.name,
|
|
1124
|
-
virtualModuleDir = _getNormalizeModuleFe2.virtualModuleDir;
|
|
1125
|
-
return virtualModuleDir + "/" + packageNameEncode("" + mfName + this.tag + this.name + this.tag) + this.suffix;
|
|
1126
|
-
};
|
|
1127
|
-
_proto.writeSync = function writeSync(code, force) {
|
|
1128
|
-
if (!force && this.inited) return;
|
|
1129
|
-
if (!this.inited) {
|
|
1130
|
-
this.inited = true;
|
|
1131
|
-
}
|
|
1132
|
-
fs.writeFileSync(this.getPath(), code);
|
|
1133
|
-
};
|
|
1134
|
-
_proto.write = function write(code) {
|
|
1135
|
-
fs.writeFile(this.getPath(), code, function () {});
|
|
1136
|
-
};
|
|
1137
|
-
return VirtualModule;
|
|
1138
|
-
}();
|
|
1139
|
-
|
|
1140
|
-
var VIRTUAL_EXPOSES = 'virtual:mf-exposes';
|
|
1141
|
-
function generateExposes() {
|
|
1142
|
-
var options = getNormalizeModuleFederationOptions();
|
|
1143
|
-
return "\n export default {\n " + Object.keys(options.exposes).map(function (key) {
|
|
1144
|
-
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 ";
|
|
1145
|
-
}).join(',') + "\n }\n ";
|
|
1146
|
-
}
|
|
1147
|
-
|
|
1148
|
-
var virtualRuntimeInitStatus = new VirtualModule('runtimeInit');
|
|
1149
|
-
function writeRuntimeInitStatus() {
|
|
1150
|
-
// Use globalThis singleton to ensure only one initPromise exists
|
|
1151
|
-
var globalKey = "__mf_init__" + virtualRuntimeInitStatus.getImportId() + "__";
|
|
1152
|
-
virtualRuntimeInitStatus.writeSync("\n const globalKey = " + JSON.stringify(globalKey) + "\n if (!globalThis[globalKey]) {\n let initResolve, initReject\n const initPromise = new Promise((re, rj) => {\n initResolve = re\n initReject = rj\n })\n globalThis[globalKey] = {\n initPromise,\n initResolve,\n initReject\n }\n }\n module.exports = globalThis[globalKey]\n ");
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
var cacheRemoteMap = {};
|
|
1156
|
-
var LOAD_REMOTE_TAG = '__loadRemote__';
|
|
1157
|
-
function getRemoteVirtualModule(remote, command) {
|
|
1158
|
-
if (!cacheRemoteMap[remote]) {
|
|
1159
|
-
cacheRemoteMap[remote] = new VirtualModule(remote, LOAD_REMOTE_TAG, '.js');
|
|
1160
|
-
cacheRemoteMap[remote].writeSync(generateRemotes(remote, command));
|
|
1161
|
-
}
|
|
1162
|
-
var virtual = cacheRemoteMap[remote];
|
|
1163
|
-
return virtual;
|
|
1164
|
-
}
|
|
1165
|
-
var usedRemotesMap = {
|
|
1166
|
-
// remote1: {remote1/App, remote1, remote1/Button}
|
|
1167
|
-
};
|
|
1168
|
-
function addUsedRemote(remoteKey, remoteModule) {
|
|
1169
|
-
if (!usedRemotesMap[remoteKey]) usedRemotesMap[remoteKey] = new Set();
|
|
1170
|
-
usedRemotesMap[remoteKey].add(remoteModule);
|
|
1171
|
-
}
|
|
1172
|
-
function getUsedRemotesMap() {
|
|
1173
|
-
return usedRemotesMap;
|
|
1174
|
-
}
|
|
1175
|
-
function generateRemotes(id, command) {
|
|
1176
|
-
return "\n const {initPromise} = require(\"" + virtualRuntimeInitStatus.getImportId() + "\")\n const res = initPromise.then(runtime => runtime.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 ";
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
/**
|
|
1180
|
-
* Even the resolveId hook cannot interfere with vite pre-build,
|
|
1181
|
-
* and adding query parameter virtual modules will also fail.
|
|
1182
|
-
* You can only proxy to the real file through alias
|
|
1183
|
-
*/
|
|
1184
|
-
// *** __prebuild__
|
|
1185
|
-
var preBuildCacheMap = {};
|
|
1186
|
-
var PREBUILD_TAG = '__prebuild__';
|
|
1187
|
-
function writePreBuildLibPath(pkg) {
|
|
1188
|
-
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(pkg, PREBUILD_TAG);
|
|
1189
|
-
preBuildCacheMap[pkg].writeSync('');
|
|
1190
|
-
}
|
|
1191
|
-
function getPreBuildLibImportId(pkg) {
|
|
1192
|
-
if (!preBuildCacheMap[pkg]) preBuildCacheMap[pkg] = new VirtualModule(pkg, PREBUILD_TAG);
|
|
1193
|
-
var importId = preBuildCacheMap[pkg].getImportId();
|
|
1194
|
-
return importId;
|
|
1195
|
-
}
|
|
1196
|
-
// *** __loadShare__
|
|
1197
|
-
var LOAD_SHARE_TAG = '__loadShare__';
|
|
1198
|
-
var loadShareCacheMap = {};
|
|
1199
|
-
function getLoadShareModulePath(pkg) {
|
|
1200
|
-
if (!loadShareCacheMap[pkg]) loadShareCacheMap[pkg] = new VirtualModule(pkg, LOAD_SHARE_TAG, '.js');
|
|
1201
|
-
var filepath = loadShareCacheMap[pkg].getPath();
|
|
1202
|
-
return filepath;
|
|
1203
|
-
}
|
|
1204
|
-
function writeLoadShareModule(pkg, shareItem, command) {
|
|
1205
|
-
loadShareCacheMap[pkg].writeSync("\n ;() => import(" + JSON.stringify(getPreBuildLibImportId(pkg)) + ").catch(() => {});\n // dev uses dynamic import to separate chunks\n " + (command !== 'build' ? ";() => import(" + JSON.stringify(pkg) + ").catch(() => {});" : '') + "\n const {initPromise} = require(\"" + virtualRuntimeInitStatus.getImportId() + "\")\n const res = initPromise.then(runtime => runtime.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 }))\n const exportModule = " + (command !== 'build' ? '/*mf top-level-await placeholder replacement mf*/' : 'await ') + "res.then(factory => factory())\n module.exports = exportModule\n ");
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
var usedShares = new Set();
|
|
1209
|
-
function getUsedShares() {
|
|
1210
|
-
return usedShares;
|
|
1211
|
-
}
|
|
1212
|
-
function addUsedShares(pkg) {
|
|
1213
|
-
usedShares.add(pkg);
|
|
1214
|
-
}
|
|
1215
|
-
// *** Expose locally provided shared modules here
|
|
1216
|
-
new VirtualModule('localSharedImportMap');
|
|
1217
|
-
function getLocalSharedImportMapPath() {
|
|
1218
|
-
return getLocalSharedImportMapPath_temp();
|
|
1219
|
-
// return localSharedImportMapModule.getPath()
|
|
1220
|
-
}
|
|
1221
|
-
var prevSharedCount;
|
|
1222
|
-
function writeLocalSharedImportMap() {
|
|
1223
|
-
var sharedCount = getUsedShares().size;
|
|
1224
|
-
if (prevSharedCount !== sharedCount) {
|
|
1225
|
-
prevSharedCount = sharedCount;
|
|
1226
|
-
writeLocalSharedImportMap_temp(generateLocalSharedImportMap());
|
|
1227
|
-
// localSharedImportMapModule.writeSync(generateLocalSharedImportMap(), true)
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
function generateLocalSharedImportMap() {
|
|
1231
|
-
var options = getNormalizeModuleFederationOptions();
|
|
1232
|
-
return "\n import {loadShare} from \"@module-federation/runtime\";\n const importMap = {\n " + Array.from(getUsedShares()).sort().map(function (pkg) {
|
|
1233
|
-
var shareItem = getNormalizeShareItem(pkg);
|
|
1234
|
-
return "\n " + JSON.stringify(pkg) + ": async () => {\n " + ((shareItem == null ? void 0 : shareItem.shareConfig["import"]) === false ? "throw new Error(`Shared module '${" + JSON.stringify(pkg) + "}' must be provided by host`);" : "let pkg = await import(\"" + getPreBuildLibImportId(pkg) + "\");\n return pkg;") + "\n }\n ";
|
|
1235
|
-
}).join(',') + "\n }\n const usedShared = {\n " + Array.from(getUsedShares()).sort().map(function (key) {
|
|
1236
|
-
var shareItem = getNormalizeShareItem(key);
|
|
1237
|
-
if (!shareItem) return null;
|
|
1238
|
-
return "\n " + JSON.stringify(key) + ": {\n name: " + JSON.stringify(key) + ",\n version: " + JSON.stringify(shareItem.version) + ",\n scope: [" + JSON.stringify(shareItem.scope) + "],\n loaded: false,\n from: " + JSON.stringify(options.name) + ",\n async get () {\n if (" + (shareItem.shareConfig["import"] === false) + ") {\n throw new Error(`Shared module '${" + JSON.stringify(key) + "}' must be provided by host`);\n }\n usedShared[" + JSON.stringify(key) + "].loaded = true\n const {" + JSON.stringify(key) + ": pkgDynamicImport} = importMap\n const res = await pkgDynamicImport()\n const exportModule = {...res}\n // All npm packages pre-built by vite will be converted to esm\n Object.defineProperty(exportModule, \"__esModule\", {\n value: true,\n enumerable: false\n })\n return function () {\n return exportModule\n }\n },\n shareConfig: {\n singleton: " + shareItem.shareConfig.singleton + ",\n requiredVersion: " + JSON.stringify(shareItem.shareConfig.requiredVersion) + ",\n " + (shareItem.shareConfig["import"] === false ? 'import: false,' : '') + "\n }\n }\n ";
|
|
1239
|
-
}).filter(function (x) {
|
|
1240
|
-
return x !== null;
|
|
1241
|
-
}).join(',') + "\n }\n const usedRemotes = [" + Object.keys(getUsedRemotesMap()).map(function (key) {
|
|
1242
|
-
var _JSON$stringify;
|
|
1243
|
-
var remote = options.remotes[key];
|
|
1244
|
-
if (!remote) return null;
|
|
1245
|
-
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 shareScope: " + ((_JSON$stringify = JSON.stringify(remote.shareScope)) != null ? _JSON$stringify : 'default') + ",\n }\n ";
|
|
1246
|
-
}).filter(function (x) {
|
|
1247
|
-
return x !== null;
|
|
1248
|
-
}).join(',') + "\n ]\n export {\n usedShared,\n usedRemotes\n }\n ";
|
|
1249
|
-
}
|
|
1250
|
-
var REMOTE_ENTRY_ID = 'virtual:mf-REMOTE_ENTRY_ID';
|
|
1251
|
-
function generateRemoteEntry(options) {
|
|
1252
|
-
var pluginImportNames = options.runtimePlugins.map(function (p, i) {
|
|
1253
|
-
if (typeof p === 'string') {
|
|
1254
|
-
return ["$runtimePlugin_" + i, "import $runtimePlugin_" + i + " from \"" + p + "\";", "undefined"];
|
|
1255
|
-
} else {
|
|
1256
|
-
return ["$runtimePlugin_" + i, "import $runtimePlugin_" + i + " from \"" + p[0] + "\";", serializeRuntimeOptions(p[1])];
|
|
1257
|
-
}
|
|
1258
|
-
});
|
|
1259
|
-
return "\n import {init as runtimeInit, loadRemote} from \"@module-federation/runtime\";\n " + pluginImportNames.map(function (item) {
|
|
1260
|
-
return item[1];
|
|
1261
|
-
}).join('\n') + "\n import exposesMap from \"" + VIRTUAL_EXPOSES + "\"\n import {usedShared, usedRemotes} from \"" + getLocalSharedImportMapPath() + "\"\n import {\n initResolve\n } from \"" + virtualRuntimeInitStatus.getImportId() + "\"\n const initTokens = {}\n const shareScopeName = " + JSON.stringify(options.shareScope) + "\n const mfName = " + JSON.stringify(options.name) + "\n async function init(shared = {}, initScope = []) {\n const initRes = runtimeInit({\n name: mfName,\n remotes: usedRemotes,\n shared: usedShared,\n plugins: [" + pluginImportNames.map(function (item) {
|
|
1262
|
-
return item[0] + "(" + item[2] + ")";
|
|
1263
|
-
}).join(', ') + "],\n " + (options.shareStrategy ? "shareStrategy: '" + options.shareStrategy + "'" : '') + "\n });\n // handling circular init calls\n var initToken = initTokens[shareScopeName];\n if (!initToken)\n initToken = initTokens[shareScopeName] = { from: mfName };\n if (initScope.indexOf(initToken) >= 0) return;\n initScope.push(initToken);\n initRes.initShareScopeMap('" + options.shareScope + "', shared);\n try {\n await Promise.all(await initRes.initializeSharing('" + options.shareScope + "', {\n strategy: '" + options.shareStrategy + "',\n from: \"build\",\n initScope\n }));\n } catch (e) {\n console.error(e)\n }\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 ";
|
|
1264
|
-
}
|
|
1265
|
-
/**
|
|
1266
|
-
* Inject entry file, automatically init when used as host,
|
|
1267
|
-
* and will not inject remoteEntry
|
|
1268
|
-
*/
|
|
1269
|
-
var HOST_AUTO_INIT_TAG = '__H_A_I__';
|
|
1270
|
-
var hostAutoInitModule = new VirtualModule('hostAutoInit', HOST_AUTO_INIT_TAG);
|
|
1271
|
-
function writeHostAutoInit() {
|
|
1272
|
-
hostAutoInitModule.writeSync("\n const remoteEntryPromise = import(\"" + REMOTE_ENTRY_ID + "\")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ");
|
|
1273
|
-
}
|
|
1274
|
-
function getHostAutoInitImportId() {
|
|
1275
|
-
return hostAutoInitModule.getImportId();
|
|
1276
|
-
}
|
|
1277
|
-
function getHostAutoInitPath() {
|
|
1278
|
-
return hostAutoInitModule.getPath();
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
function initVirtualModules() {
|
|
1282
|
-
writeLocalSharedImportMap();
|
|
1283
|
-
writeHostAutoInit();
|
|
1284
|
-
writeRuntimeInitStatus();
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
|
-
var ASSET_TYPES = ['js', 'css'];
|
|
1288
|
-
var LOAD_TIMINGS = ['sync', 'async'];
|
|
1289
|
-
var JS_EXTENSIONS = ['.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
|
|
1290
|
-
/**
|
|
1291
|
-
* Creates an empty asset map structure for tracking JS and CSS assets
|
|
1292
|
-
* @returns Initialized asset map with sync/async arrays for JS and CSS
|
|
1293
|
-
*/
|
|
1294
|
-
var createEmptyAssetMap = function createEmptyAssetMap() {
|
|
1295
|
-
return {
|
|
1296
|
-
js: {
|
|
1297
|
-
sync: [],
|
|
1298
|
-
async: []
|
|
1299
|
-
},
|
|
1300
|
-
css: {
|
|
1301
|
-
sync: [],
|
|
1302
|
-
async: []
|
|
1303
|
-
}
|
|
1304
|
-
};
|
|
1305
|
-
};
|
|
1306
|
-
/**
|
|
1307
|
-
* Tracks an asset in the preload map with deduplication
|
|
1308
|
-
* @param map - The preload map to update
|
|
1309
|
-
* @param key - The module key to track under
|
|
1310
|
-
* @param fileName - The asset filename to track
|
|
1311
|
-
* @param isAsync - Whether the asset is loaded async
|
|
1312
|
-
* @param type - The asset type ('js' or 'css')
|
|
1313
|
-
*/
|
|
1314
|
-
var trackAsset = function trackAsset(map, key, fileName, isAsync, type) {
|
|
1315
|
-
if (!map[key]) {
|
|
1316
|
-
map[key] = createEmptyAssetMap();
|
|
1317
|
-
}
|
|
1318
|
-
var target = isAsync ? map[key][type].async : map[key][type].sync;
|
|
1319
|
-
if (!target.includes(fileName)) {
|
|
1320
|
-
target.push(fileName);
|
|
1321
|
-
}
|
|
1322
|
-
};
|
|
1323
|
-
/**
|
|
1324
|
-
* Checks if a file is a CSS file by extension
|
|
1325
|
-
* @param fileName - The filename to check
|
|
1326
|
-
* @returns True if file has a CSS extension (.css, .scss, .less)
|
|
1327
|
-
*/
|
|
1328
|
-
var isCSSFile = function isCSSFile(fileName) {
|
|
1329
|
-
return fileName.endsWith('.css') || fileName.endsWith('.scss') || fileName.endsWith('.less');
|
|
1330
|
-
};
|
|
1331
|
-
/**
|
|
1332
|
-
* Collects all CSS assets from the bundle
|
|
1333
|
-
* @param bundle - The Rollup output bundle
|
|
1334
|
-
* @returns Set of CSS asset filenames
|
|
1335
|
-
*/
|
|
1336
|
-
var collectCssAssets = function collectCssAssets(bundle) {
|
|
1337
|
-
var cssAssets = new Set();
|
|
1338
|
-
for (var _i = 0, _Object$entries = Object.entries(bundle); _i < _Object$entries.length; _i++) {
|
|
1339
|
-
var _Object$entries$_i = _Object$entries[_i],
|
|
1340
|
-
fileName = _Object$entries$_i[0],
|
|
1341
|
-
fileData = _Object$entries$_i[1];
|
|
1342
|
-
if (fileData.type === 'asset' && isCSSFile(fileName)) {
|
|
1343
|
-
cssAssets.add(fileName);
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1346
|
-
return cssAssets;
|
|
1347
|
-
};
|
|
1348
|
-
/**
|
|
1349
|
-
* Processes module assets and tracks them in the files map
|
|
1350
|
-
* @param bundle - The Rollup output bundle
|
|
1351
|
-
* @param filesMap - The preload map to populate
|
|
1352
|
-
* @param moduleMatcher - Function that matches module paths to keys
|
|
1353
|
-
*/
|
|
1354
|
-
var processModuleAssets = function processModuleAssets(bundle, filesMap, moduleMatcher) {
|
|
1355
|
-
for (var _i2 = 0, _Object$entries2 = Object.entries(bundle); _i2 < _Object$entries2.length; _i2++) {
|
|
1356
|
-
var _Object$entries2$_i = _Object$entries2[_i2],
|
|
1357
|
-
fileName = _Object$entries2$_i[0],
|
|
1358
|
-
fileData = _Object$entries2$_i[1];
|
|
1359
|
-
if (fileData.type !== 'chunk') continue;
|
|
1360
|
-
if (!fileData.modules) continue;
|
|
1361
|
-
for (var _i3 = 0, _Object$keys = Object.keys(fileData.modules); _i3 < _Object$keys.length; _i3++) {
|
|
1362
|
-
var modulePath = _Object$keys[_i3];
|
|
1363
|
-
var matchKey = moduleMatcher(modulePath);
|
|
1364
|
-
if (!matchKey) continue;
|
|
1365
|
-
// Track main JS chunk
|
|
1366
|
-
trackAsset(filesMap, matchKey, fileName, false, 'js');
|
|
1367
|
-
// Handle dynamic imports
|
|
1368
|
-
if (fileData.dynamicImports) {
|
|
1369
|
-
for (var _iterator = _createForOfIteratorHelperLoose(fileData.dynamicImports), _step; !(_step = _iterator()).done;) {
|
|
1370
|
-
var dynamicImport = _step.value;
|
|
1371
|
-
var importData = bundle[dynamicImport];
|
|
1372
|
-
if (!importData) continue;
|
|
1373
|
-
var isCss = isCSSFile(dynamicImport);
|
|
1374
|
-
trackAsset(filesMap, matchKey, dynamicImport, true, isCss ? 'css' : 'js');
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
};
|
|
1380
|
-
/**
|
|
1381
|
-
* Deduplicates assets in the files map
|
|
1382
|
-
* @param filesMap - The preload map to deduplicate
|
|
1383
|
-
* @returns New deduplicated preload map
|
|
1384
|
-
*/
|
|
1385
|
-
var deduplicateAssets = function deduplicateAssets(filesMap) {
|
|
1386
|
-
var result = {};
|
|
1387
|
-
for (var _i4 = 0, _Object$entries3 = Object.entries(filesMap); _i4 < _Object$entries3.length; _i4++) {
|
|
1388
|
-
var _Object$entries3$_i = _Object$entries3[_i4],
|
|
1389
|
-
key = _Object$entries3$_i[0],
|
|
1390
|
-
assetMaps = _Object$entries3$_i[1];
|
|
1391
|
-
result[key] = createEmptyAssetMap();
|
|
1392
|
-
for (var _i5 = 0, _ASSET_TYPES = ASSET_TYPES; _i5 < _ASSET_TYPES.length; _i5++) {
|
|
1393
|
-
var type = _ASSET_TYPES[_i5];
|
|
1394
|
-
for (var _i6 = 0, _LOAD_TIMINGS = LOAD_TIMINGS; _i6 < _LOAD_TIMINGS.length; _i6++) {
|
|
1395
|
-
var timing = _LOAD_TIMINGS[_i6];
|
|
1396
|
-
result[key][type][timing] = Array.from(new Set(assetMaps[type][timing]));
|
|
1397
|
-
}
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
|
-
return result;
|
|
1401
|
-
};
|
|
1402
|
-
/**
|
|
1403
|
-
* Builds a mapping between module files and their share keys
|
|
1404
|
-
* @param shareKeys - Set of share keys to map
|
|
1405
|
-
* @param resolveFn - Function to resolve module paths
|
|
1406
|
-
* @returns Map of file paths to their corresponding share keys
|
|
1407
|
-
*/
|
|
1408
|
-
var buildFileToShareKeyMap = function buildFileToShareKeyMap(shareKeys, resolveFn) {
|
|
1409
|
-
try {
|
|
1410
|
-
var fileToShareKey = new Map();
|
|
1411
|
-
return Promise.resolve(Promise.all(Array.from(shareKeys).map(function (shareKey) {
|
|
1412
|
-
return resolveFn(getPreBuildLibImportId(shareKey)).then(function (resolution) {
|
|
1413
|
-
var _resolution$id;
|
|
1414
|
-
return {
|
|
1415
|
-
shareKey: shareKey,
|
|
1416
|
-
file: resolution == null || (_resolution$id = resolution.id) == null ? void 0 : _resolution$id.split('?')[0]
|
|
1417
|
-
};
|
|
1418
|
-
})["catch"](function () {
|
|
1419
|
-
return null;
|
|
1420
|
-
});
|
|
1421
|
-
}))).then(function (resolutions) {
|
|
1422
|
-
for (var _iterator2 = _createForOfIteratorHelperLoose(resolutions), _step2; !(_step2 = _iterator2()).done;) {
|
|
1423
|
-
var resolution = _step2.value;
|
|
1424
|
-
if (resolution != null && resolution.file) {
|
|
1425
|
-
fileToShareKey.set(resolution.file, resolution.shareKey);
|
|
1426
|
-
}
|
|
1427
|
-
}
|
|
1428
|
-
return fileToShareKey;
|
|
1429
|
-
});
|
|
1430
|
-
} catch (e) {
|
|
1431
|
-
return Promise.reject(e);
|
|
1432
|
-
}
|
|
1433
|
-
};
|
|
1434
|
-
|
|
1435
|
-
/**
|
|
1436
|
-
* Resolves the public path for remote entries
|
|
1437
|
-
* @param options - Module Federation options
|
|
1438
|
-
* @param viteBase - Vite's base config value
|
|
1439
|
-
* @param originalBase - Original base config before any transformations
|
|
1440
|
-
* @returns The resolved public path
|
|
1441
|
-
*/
|
|
1442
|
-
function resolvePublicPath(options, viteBase, originalBase) {
|
|
1443
|
-
// Use explicitly set publicPath if provided
|
|
1444
|
-
if (options.publicPath) {
|
|
1445
|
-
return options.publicPath;
|
|
1446
|
-
}
|
|
1447
|
-
// Handle empty original base case
|
|
1448
|
-
if (originalBase === '') {
|
|
1449
|
-
return 'auto';
|
|
1450
|
-
}
|
|
1451
|
-
// Use viteBase if available, ensuring it ends with a slash
|
|
1452
|
-
if (viteBase) {
|
|
1453
|
-
return viteBase.replace(/\/?$/, '/');
|
|
1454
|
-
}
|
|
1455
|
-
// Fallback to auto if no base is specified
|
|
1456
|
-
return 'auto';
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
|
-
var Manifest = function Manifest() {
|
|
1460
|
-
var mfOptions = getNormalizeModuleFederationOptions();
|
|
1461
|
-
var name = mfOptions.name,
|
|
1462
|
-
filename = mfOptions.filename,
|
|
1463
|
-
getPublicPath = mfOptions.getPublicPath,
|
|
1464
|
-
manifestOptions = mfOptions.manifest;
|
|
1465
|
-
var mfManifestName = '';
|
|
1466
|
-
if (manifestOptions === true) {
|
|
1467
|
-
mfManifestName = 'mf-manifest.json';
|
|
1468
|
-
}
|
|
1469
|
-
if (typeof manifestOptions !== 'boolean') {
|
|
1470
|
-
mfManifestName = path__namespace.join((manifestOptions == null ? void 0 : manifestOptions.filePath) || '', (manifestOptions == null ? void 0 : manifestOptions.fileName) || '');
|
|
1471
|
-
}
|
|
1472
|
-
var root;
|
|
1473
|
-
var remoteEntryFile;
|
|
1474
|
-
var publicPath;
|
|
1475
|
-
var _command;
|
|
1476
|
-
var _originalConfigBase;
|
|
1477
|
-
var viteConfig;
|
|
1478
|
-
/**
|
|
1479
|
-
* Adds global CSS assets to all module exports
|
|
1480
|
-
* @param filesMap - The preload map to update
|
|
1481
|
-
* @param cssAssets - Set of CSS asset filenames to add
|
|
1482
|
-
*/
|
|
1483
|
-
var addCssAssetsToAllExports = function addCssAssetsToAllExports(filesMap, cssAssets) {
|
|
1484
|
-
Object.keys(filesMap).forEach(function (key) {
|
|
1485
|
-
cssAssets.forEach(function (cssAsset) {
|
|
1486
|
-
trackAsset(filesMap, key, cssAsset, false, 'css');
|
|
1487
|
-
});
|
|
1488
|
-
});
|
|
1489
|
-
};
|
|
1490
|
-
return [{
|
|
1491
|
-
name: 'module-federation-manifest',
|
|
1492
|
-
apply: 'serve',
|
|
1493
|
-
/**
|
|
1494
|
-
* Stores resolved Vite config for later use
|
|
1495
|
-
*/
|
|
1496
|
-
/**
|
|
1497
|
-
* Finalizes configuration after all plugins are resolved
|
|
1498
|
-
* @param config - Fully resolved Vite config
|
|
1499
|
-
*/
|
|
1500
|
-
configResolved: function configResolved(config) {
|
|
1501
|
-
viteConfig = config;
|
|
1502
|
-
},
|
|
1503
|
-
/**
|
|
1504
|
-
* Configures dev server middleware to handle manifest requests
|
|
1505
|
-
* @param server - Vite dev server instance
|
|
1506
|
-
*/
|
|
1507
|
-
configureServer: function configureServer(server) {
|
|
1508
|
-
server.middlewares.use(function (req, res, next) {
|
|
1509
|
-
var _req$url;
|
|
1510
|
-
if (!mfManifestName) {
|
|
1511
|
-
next();
|
|
1512
|
-
return;
|
|
1513
|
-
}
|
|
1514
|
-
if (((_req$url = req.url) == null ? void 0 : _req$url.replace(/\?.*/, '')) === (viteConfig.base + mfManifestName).replace(/^\/?/, '/')) {
|
|
1515
|
-
res.setHeader('Content-Type', 'application/json');
|
|
1516
|
-
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
1517
|
-
res.end(JSON.stringify(_extends({}, generateMFManifest({}), {
|
|
1518
|
-
id: name,
|
|
1519
|
-
name: name,
|
|
1520
|
-
metaData: {
|
|
1521
|
-
name: name,
|
|
1522
|
-
type: 'app',
|
|
1523
|
-
buildInfo: {
|
|
1524
|
-
buildVersion: '1.0.0',
|
|
1525
|
-
buildName: name
|
|
1526
|
-
},
|
|
1527
|
-
remoteEntry: {
|
|
1528
|
-
name: filename,
|
|
1529
|
-
path: '',
|
|
1530
|
-
type: 'module'
|
|
1531
|
-
},
|
|
1532
|
-
ssrRemoteEntry: {
|
|
1533
|
-
name: filename,
|
|
1534
|
-
path: '',
|
|
1535
|
-
type: 'module'
|
|
1536
|
-
},
|
|
1537
|
-
types: {
|
|
1538
|
-
path: '',
|
|
1539
|
-
name: ''
|
|
1540
|
-
},
|
|
1541
|
-
globalName: name,
|
|
1542
|
-
pluginVersion: '0.2.5',
|
|
1543
|
-
publicPath: publicPath
|
|
1544
|
-
}
|
|
1545
|
-
})));
|
|
1546
|
-
} else {
|
|
1547
|
-
next();
|
|
1548
|
-
}
|
|
1549
|
-
});
|
|
1550
|
-
}
|
|
1551
|
-
}, {
|
|
1552
|
-
name: 'module-federation-manifest',
|
|
1553
|
-
enforce: 'post',
|
|
1554
|
-
/**
|
|
1555
|
-
* Initial plugin configuration
|
|
1556
|
-
* @param config - Vite config object
|
|
1557
|
-
* @param command - Current Vite command (serve/build)
|
|
1558
|
-
*/
|
|
1559
|
-
config: function config(_config, _ref) {
|
|
1560
|
-
var command = _ref.command;
|
|
1561
|
-
if (!_config.build) _config.build = {};
|
|
1562
|
-
if (!_config.build.manifest) {
|
|
1563
|
-
_config.build.manifest = _config.build.manifest || !!manifestOptions;
|
|
1564
|
-
}
|
|
1565
|
-
_command = command;
|
|
1566
|
-
_originalConfigBase = _config.base;
|
|
1567
|
-
},
|
|
1568
|
-
configResolved: function configResolved(config) {
|
|
1569
|
-
root = config.root;
|
|
1570
|
-
var base = config.base;
|
|
1571
|
-
if (_command === 'serve') {
|
|
1572
|
-
base = (config.server.origin || '') + config.base;
|
|
1573
|
-
}
|
|
1574
|
-
publicPath = resolvePublicPath(mfOptions, base, _originalConfigBase);
|
|
1575
|
-
},
|
|
1576
|
-
/**
|
|
1577
|
-
* Generates the module federation manifest file
|
|
1578
|
-
* @param options - Rollup output options
|
|
1579
|
-
* @param bundle - Generated bundle assets
|
|
1580
|
-
*/
|
|
1581
|
-
generateBundle: function generateBundle(options, bundle) {
|
|
1582
|
-
try {
|
|
1583
|
-
var _this = this;
|
|
1584
|
-
if (!mfManifestName) return Promise.resolve();
|
|
1585
|
-
var filesMap = {};
|
|
1586
|
-
// First pass: Find remoteEntry file
|
|
1587
|
-
for (var _i = 0, _Object$entries = Object.entries(bundle); _i < _Object$entries.length; _i++) {
|
|
1588
|
-
var _Object$entries$_i = _Object$entries[_i],
|
|
1589
|
-
_ = _Object$entries$_i[0],
|
|
1590
|
-
fileData = _Object$entries$_i[1];
|
|
1591
|
-
if (mfOptions.filename.replace(/[\[\]]/g, '_').replace(/\.[^/.]+$/, '') === fileData.name || fileData.name === 'remoteEntry') {
|
|
1592
|
-
remoteEntryFile = fileData.fileName;
|
|
1593
|
-
break; // We can break early since we only need to find remoteEntry once
|
|
1594
|
-
}
|
|
1595
|
-
}
|
|
1596
|
-
// Second pass: Collect all CSS assets
|
|
1597
|
-
var allCssAssets = mfOptions.bundleAllCSS ? collectCssAssets(bundle) : new Set();
|
|
1598
|
-
var exposesModules = Object.keys(mfOptions.exposes).map(function (item) {
|
|
1599
|
-
return mfOptions.exposes[item]["import"];
|
|
1600
|
-
});
|
|
1601
|
-
// Process exposed modules
|
|
1602
|
-
processModuleAssets(bundle, filesMap, function (modulePath) {
|
|
1603
|
-
var absoluteModulePath = path__namespace.resolve(root, modulePath);
|
|
1604
|
-
return exposesModules.find(function (exposeModule) {
|
|
1605
|
-
var exposePath = path__namespace.resolve(root, exposeModule);
|
|
1606
|
-
// First try exact path match
|
|
1607
|
-
if (absoluteModulePath === exposePath) {
|
|
1608
|
-
return true;
|
|
1609
|
-
}
|
|
1610
|
-
// Then try path match without known extensions
|
|
1611
|
-
var getPathWithoutKnownExt = function getPathWithoutKnownExt(filePath) {
|
|
1612
|
-
var ext = path__namespace.extname(filePath);
|
|
1613
|
-
return JS_EXTENSIONS.includes(ext) ? path__namespace.join(path__namespace.dirname(filePath), path__namespace.basename(filePath, ext)) : filePath;
|
|
1614
|
-
};
|
|
1615
|
-
var modulePathNoExt = getPathWithoutKnownExt(absoluteModulePath);
|
|
1616
|
-
var exposePathNoExt = getPathWithoutKnownExt(exposePath);
|
|
1617
|
-
return modulePathNoExt === exposePathNoExt;
|
|
1618
|
-
});
|
|
1619
|
-
});
|
|
1620
|
-
// Process shared modules
|
|
1621
|
-
return Promise.resolve(buildFileToShareKeyMap(getUsedShares(), _this.resolve.bind(_this))).then(function (fileToShareKey) {
|
|
1622
|
-
processModuleAssets(bundle, filesMap, function (modulePath) {
|
|
1623
|
-
return fileToShareKey.get(modulePath);
|
|
1624
|
-
});
|
|
1625
|
-
// Add all CSS assets to every export if bundleAllCSS is enabled
|
|
1626
|
-
if (mfOptions.bundleAllCSS) {
|
|
1627
|
-
addCssAssetsToAllExports(filesMap, allCssAssets);
|
|
1628
|
-
}
|
|
1629
|
-
// Final deduplication of all assets
|
|
1630
|
-
filesMap = deduplicateAssets(filesMap);
|
|
1631
|
-
_this.emitFile({
|
|
1632
|
-
type: 'asset',
|
|
1633
|
-
fileName: mfManifestName,
|
|
1634
|
-
source: JSON.stringify(generateMFManifest(filesMap))
|
|
1635
|
-
});
|
|
1636
|
-
});
|
|
1637
|
-
} catch (e) {
|
|
1638
|
-
return Promise.reject(e);
|
|
1639
|
-
}
|
|
1640
|
-
}
|
|
1641
|
-
}];
|
|
1642
|
-
/**
|
|
1643
|
-
* Generates the final manifest JSON structure
|
|
1644
|
-
* @param preloadMap - Map of module assets to include
|
|
1645
|
-
* @returns Complete manifest object
|
|
1646
|
-
*/
|
|
1647
|
-
function generateMFManifest(preloadMap) {
|
|
1648
|
-
var options = getNormalizeModuleFederationOptions();
|
|
1649
|
-
var name = options.name;
|
|
1650
|
-
var remoteEntry = {
|
|
1651
|
-
name: remoteEntryFile,
|
|
1652
|
-
path: '',
|
|
1653
|
-
type: 'module'
|
|
1654
|
-
};
|
|
1655
|
-
// Process remotes
|
|
1656
|
-
var remotes = Array.from(Object.entries(getUsedRemotesMap())).flatMap(function (_ref2) {
|
|
1657
|
-
var remoteKey = _ref2[0],
|
|
1658
|
-
modules = _ref2[1];
|
|
1659
|
-
return Array.from(modules).map(function (moduleKey) {
|
|
1660
|
-
return {
|
|
1661
|
-
federationContainerName: options.remotes[remoteKey].entry,
|
|
1662
|
-
moduleName: moduleKey.replace(remoteKey, '').replace('/', ''),
|
|
1663
|
-
alias: remoteKey,
|
|
1664
|
-
entry: '*'
|
|
1665
|
-
};
|
|
1666
|
-
});
|
|
1667
|
-
});
|
|
1668
|
-
// Process shared dependencies
|
|
1669
|
-
var shared = Array.from(getUsedShares()).map(function (shareKey) {
|
|
1670
|
-
var shareItem = getNormalizeShareItem(shareKey);
|
|
1671
|
-
var assets = preloadMap[shareKey] || createEmptyAssetMap();
|
|
1672
|
-
return {
|
|
1673
|
-
id: name + ":" + shareKey,
|
|
1674
|
-
name: shareKey,
|
|
1675
|
-
version: shareItem.version,
|
|
1676
|
-
requiredVersion: shareItem.shareConfig.requiredVersion,
|
|
1677
|
-
assets: {
|
|
1678
|
-
js: {
|
|
1679
|
-
async: assets.js.async,
|
|
1680
|
-
sync: assets.js.sync
|
|
1681
|
-
},
|
|
1682
|
-
css: {
|
|
1683
|
-
async: assets.css.async,
|
|
1684
|
-
sync: assets.css.sync
|
|
1685
|
-
}
|
|
1686
|
-
}
|
|
1687
|
-
};
|
|
1688
|
-
}).filter(Boolean);
|
|
1689
|
-
// Process exposed modules
|
|
1690
|
-
var exposes = Object.entries(options.exposes).map(function (_ref3) {
|
|
1691
|
-
var key = _ref3[0],
|
|
1692
|
-
value = _ref3[1];
|
|
1693
|
-
var formatKey = key.replace('./', '');
|
|
1694
|
-
var sourceFile = value["import"];
|
|
1695
|
-
var assets = preloadMap[sourceFile] || createEmptyAssetMap();
|
|
1696
|
-
return {
|
|
1697
|
-
id: name + ":" + formatKey,
|
|
1698
|
-
name: formatKey,
|
|
1699
|
-
assets: {
|
|
1700
|
-
js: {
|
|
1701
|
-
async: assets.js.async,
|
|
1702
|
-
sync: assets.js.sync
|
|
1703
|
-
},
|
|
1704
|
-
css: {
|
|
1705
|
-
async: assets.css.async,
|
|
1706
|
-
sync: assets.css.sync
|
|
1707
|
-
}
|
|
1708
|
-
},
|
|
1709
|
-
path: key
|
|
1710
|
-
};
|
|
1711
|
-
}).filter(Boolean);
|
|
1712
|
-
return {
|
|
1713
|
-
id: name,
|
|
1714
|
-
name: name,
|
|
1715
|
-
metaData: _extends({
|
|
1716
|
-
name: name,
|
|
1717
|
-
type: 'app',
|
|
1718
|
-
buildInfo: {
|
|
1719
|
-
buildVersion: '1.0.0',
|
|
1720
|
-
buildName: name
|
|
1721
|
-
},
|
|
1722
|
-
remoteEntry: remoteEntry,
|
|
1723
|
-
ssrRemoteEntry: remoteEntry,
|
|
1724
|
-
types: {
|
|
1725
|
-
path: '',
|
|
1726
|
-
name: ''
|
|
1727
|
-
},
|
|
1728
|
-
globalName: name,
|
|
1729
|
-
pluginVersion: '0.2.5'
|
|
1730
|
-
}, !!getPublicPath ? {
|
|
1731
|
-
getPublicPath: getPublicPath
|
|
1732
|
-
} : {
|
|
1733
|
-
publicPath: publicPath
|
|
1734
|
-
}),
|
|
1735
|
-
shared: shared,
|
|
1736
|
-
remotes: remotes,
|
|
1737
|
-
exposes: exposes
|
|
1738
|
-
};
|
|
1739
|
-
}
|
|
1740
|
-
};
|
|
1741
|
-
|
|
1742
|
-
var _resolve, _parseTimeout;
|
|
1743
|
-
var promise = new Promise(function (resolve, reject) {
|
|
1744
|
-
_resolve = function _resolve(v) {
|
|
1745
|
-
clearTimeout(_parseTimeout);
|
|
1746
|
-
_parseTimeout = null;
|
|
1747
|
-
resolve(v);
|
|
1748
|
-
};
|
|
1749
|
-
});
|
|
1750
|
-
function setParseTimeout(timeout) {
|
|
1751
|
-
if (!_parseTimeout) {
|
|
1752
|
-
_parseTimeout = setTimeout(function () {
|
|
1753
|
-
console.warn("Parse timeout (" + timeout + "s) - forcing resolve");
|
|
1754
|
-
_resolve(1);
|
|
1755
|
-
}, timeout * 1000);
|
|
1756
|
-
}
|
|
1757
|
-
}
|
|
1758
|
-
var parsePromise = promise;
|
|
1759
|
-
var exposesParseEnd = false;
|
|
1760
|
-
var parseStartSet = new Set();
|
|
1761
|
-
var parseEndSet = new Set();
|
|
1762
|
-
function pluginModuleParseEnd (excludeFn, options) {
|
|
1763
|
-
setParseTimeout(options.moduleParseTimeout);
|
|
1764
|
-
return [{
|
|
1765
|
-
name: '_',
|
|
1766
|
-
apply: 'serve',
|
|
1767
|
-
config: function config() {
|
|
1768
|
-
// No waiting in development mode
|
|
1769
|
-
_resolve(1);
|
|
1770
|
-
}
|
|
1771
|
-
}, {
|
|
1772
|
-
enforce: 'pre',
|
|
1773
|
-
name: 'parseStart',
|
|
1774
|
-
apply: 'build',
|
|
1775
|
-
load: function load(id) {
|
|
1776
|
-
if (excludeFn(id)) {
|
|
1777
|
-
return;
|
|
1778
|
-
}
|
|
1779
|
-
parseStartSet.add(id);
|
|
1780
|
-
}
|
|
1781
|
-
}, {
|
|
1782
|
-
enforce: 'post',
|
|
1783
|
-
name: 'parseEnd',
|
|
1784
|
-
apply: 'build',
|
|
1785
|
-
moduleParsed: function moduleParsed(module) {
|
|
1786
|
-
var id = module.id;
|
|
1787
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1788
|
-
// When the entry JS file is empty and only contains exposes export code, it’s necessary to wait for the exposes modules to be resolved in order to collect the dependencies being used.
|
|
1789
|
-
exposesParseEnd = true;
|
|
1790
|
-
}
|
|
1791
|
-
if (excludeFn(id)) {
|
|
1792
|
-
return;
|
|
1793
|
-
}
|
|
1794
|
-
parseEndSet.add(id);
|
|
1795
|
-
if (exposesParseEnd && parseStartSet.size === parseEndSet.size) {
|
|
1796
|
-
_resolve(1);
|
|
1797
|
-
}
|
|
1798
|
-
}
|
|
1799
|
-
}];
|
|
1800
|
-
}
|
|
1801
|
-
|
|
1802
|
-
var filter = pluginutils.createFilter();
|
|
1803
|
-
function pluginProxyRemoteEntry () {
|
|
1804
|
-
var viteConfig, _command;
|
|
1805
|
-
return {
|
|
1806
|
-
name: 'proxyRemoteEntry',
|
|
1807
|
-
enforce: 'post',
|
|
1808
|
-
configResolved: function configResolved(config) {
|
|
1809
|
-
viteConfig = config;
|
|
1810
|
-
},
|
|
1811
|
-
config: function config(_config, _ref) {
|
|
1812
|
-
var command = _ref.command;
|
|
1813
|
-
_command = command;
|
|
1814
|
-
},
|
|
1815
|
-
resolveId: function resolveId(id) {
|
|
1816
|
-
if (id === REMOTE_ENTRY_ID) {
|
|
1817
|
-
return REMOTE_ENTRY_ID;
|
|
1818
|
-
}
|
|
1819
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1820
|
-
return VIRTUAL_EXPOSES;
|
|
1821
|
-
}
|
|
1822
|
-
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
|
|
1823
|
-
return id;
|
|
1824
|
-
}
|
|
1825
|
-
},
|
|
1826
|
-
load: function load(id) {
|
|
1827
|
-
if (id === REMOTE_ENTRY_ID) {
|
|
1828
|
-
return parsePromise.then(function (_) {
|
|
1829
|
-
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1830
|
-
});
|
|
1831
|
-
}
|
|
1832
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1833
|
-
return generateExposes();
|
|
1834
|
-
}
|
|
1835
|
-
if (_command === 'serve' && id.includes(getHostAutoInitPath())) {
|
|
1836
|
-
return id;
|
|
1837
|
-
}
|
|
1838
|
-
},
|
|
1839
|
-
transform: function transform(code, id) {
|
|
1840
|
-
var transformedCode = function () {
|
|
1841
|
-
if (!filter(id)) return;
|
|
1842
|
-
if (id.includes(REMOTE_ENTRY_ID)) {
|
|
1843
|
-
return parsePromise.then(function (_) {
|
|
1844
|
-
return generateRemoteEntry(getNormalizeModuleFederationOptions());
|
|
1845
|
-
});
|
|
1846
|
-
}
|
|
1847
|
-
if (id === VIRTUAL_EXPOSES) {
|
|
1848
|
-
return generateExposes();
|
|
1849
|
-
}
|
|
1850
|
-
if (id.includes(getHostAutoInitPath())) {
|
|
1851
|
-
var options = getNormalizeModuleFederationOptions();
|
|
1852
|
-
if (_command === 'serve') {
|
|
1853
|
-
var _viteConfig$server, _viteConfig$server2;
|
|
1854
|
-
var host = typeof ((_viteConfig$server = viteConfig.server) == null ? void 0 : _viteConfig$server.host) === 'string' && viteConfig.server.host !== '0.0.0.0' ? viteConfig.server.host : 'localhost';
|
|
1855
|
-
var publicPath = JSON.stringify(resolvePublicPath(options, viteConfig.base) + options.filename);
|
|
1856
|
-
return "\n const origin = (window && " + !options.ignoreOrigin + ") ? window.origin : \"//" + host + ":" + ((_viteConfig$server2 = viteConfig.server) == null ? void 0 : _viteConfig$server2.port) + "\"\n const remoteEntryPromise = await import(origin + " + publicPath + ")\n // __tla only serves as a hack for vite-plugin-top-level-await.\n Promise.resolve(remoteEntryPromise)\n .then(remoteEntry => {\n return Promise.resolve(remoteEntry.__tla)\n .then(remoteEntry.init).catch(remoteEntry.init)\n })\n ";
|
|
1857
|
-
}
|
|
1858
|
-
return code;
|
|
1859
|
-
}
|
|
1860
|
-
}();
|
|
1861
|
-
return mapCodeToCodeWithSourcemap(transformedCode);
|
|
1862
|
-
}
|
|
1863
|
-
};
|
|
1864
|
-
}
|
|
1865
|
-
|
|
1866
|
-
pluginutils.createFilter();
|
|
1867
|
-
function pluginProxyRemotes (options) {
|
|
1868
|
-
var remotes = options.remotes;
|
|
1869
|
-
return {
|
|
1870
|
-
name: 'proxyRemotes',
|
|
1871
|
-
config: function config(_config, _ref) {
|
|
1872
|
-
var _command = _ref.command;
|
|
1873
|
-
Object.keys(remotes).forEach(function (key) {
|
|
1874
|
-
var remote = remotes[key];
|
|
1875
|
-
_config.resolve.alias.push({
|
|
1876
|
-
find: new RegExp("^(" + remote.name + "(/.*|$))"),
|
|
1877
|
-
replacement: '$1',
|
|
1878
|
-
customResolver: function customResolver(source) {
|
|
1879
|
-
var remoteModule = getRemoteVirtualModule(source, _command);
|
|
1880
|
-
addUsedRemote(remote.name, source);
|
|
1881
|
-
return remoteModule.getPath();
|
|
1882
|
-
}
|
|
1883
|
-
});
|
|
1884
|
-
});
|
|
1885
|
-
}
|
|
1886
|
-
};
|
|
1887
|
-
}
|
|
1888
|
-
|
|
1889
|
-
/**
|
|
1890
|
-
* example:
|
|
1891
|
-
* const store = new PromiseStore<number>();
|
|
1892
|
-
* store.get("example").then((result) => {
|
|
1893
|
-
* console.log("Result from example:", result); // 42
|
|
1894
|
-
* });
|
|
1895
|
-
* setTimeout(() => {
|
|
1896
|
-
* store.set("example", Promise.resolve(42));
|
|
1897
|
-
* }, 2000);
|
|
1898
|
-
*/
|
|
1899
|
-
var PromiseStore = /*#__PURE__*/function () {
|
|
1900
|
-
function PromiseStore() {
|
|
1901
|
-
this.promiseMap = new Map();
|
|
1902
|
-
this.resolveMap = new Map();
|
|
1903
|
-
}
|
|
1904
|
-
var _proto = PromiseStore.prototype;
|
|
1905
|
-
_proto.set = function set(id, promise) {
|
|
1906
|
-
if (this.resolveMap.has(id)) {
|
|
1907
|
-
promise.then(this.resolveMap.get(id));
|
|
1908
|
-
this.resolveMap["delete"](id);
|
|
1909
|
-
}
|
|
1910
|
-
this.promiseMap.set(id, promise);
|
|
1911
|
-
};
|
|
1912
|
-
_proto.get = function get(id) {
|
|
1913
|
-
var _this = this;
|
|
1914
|
-
if (this.promiseMap.has(id)) {
|
|
1915
|
-
return this.promiseMap.get(id);
|
|
1916
|
-
}
|
|
1917
|
-
var pendingPromise = new Promise(function (resolve) {
|
|
1918
|
-
_this.resolveMap.set(id, resolve);
|
|
1919
|
-
});
|
|
1920
|
-
this.promiseMap.set(id, pendingPromise);
|
|
1921
|
-
return pendingPromise;
|
|
1922
|
-
};
|
|
1923
|
-
return PromiseStore;
|
|
1924
|
-
}();
|
|
1925
|
-
|
|
1926
|
-
function proxySharedModule(options) {
|
|
1927
|
-
var _options$shared = options.shared,
|
|
1928
|
-
shared = _options$shared === void 0 ? {} : _options$shared;
|
|
1929
|
-
var _config;
|
|
1930
|
-
return [{
|
|
1931
|
-
name: 'generateLocalSharedImportMap',
|
|
1932
|
-
enforce: 'post',
|
|
1933
|
-
load: function load(id) {
|
|
1934
|
-
if (id.includes(getLocalSharedImportMapPath())) {
|
|
1935
|
-
return parsePromise.then(function (_) {
|
|
1936
|
-
return generateLocalSharedImportMap();
|
|
1937
|
-
});
|
|
1938
|
-
}
|
|
1939
|
-
},
|
|
1940
|
-
transform: function transform(_, id) {
|
|
1941
|
-
if (id.includes(getLocalSharedImportMapPath())) {
|
|
1942
|
-
return mapCodeToCodeWithSourcemap(parsePromise.then(function (_) {
|
|
1943
|
-
return generateLocalSharedImportMap();
|
|
1944
|
-
}));
|
|
1945
|
-
}
|
|
1946
|
-
}
|
|
1947
|
-
}, {
|
|
1948
|
-
name: 'proxyPreBuildShared',
|
|
1949
|
-
enforce: 'post',
|
|
1950
|
-
configResolved: function configResolved(config) {
|
|
1951
|
-
_config = config;
|
|
1952
|
-
},
|
|
1953
|
-
config: function config(_config2, _ref) {
|
|
1954
|
-
var _config2$resolve$alia, _config2$resolve$alia2;
|
|
1955
|
-
var command = _ref.command;
|
|
1956
|
-
(_config2$resolve$alia = _config2.resolve.alias).push.apply(_config2$resolve$alia, Object.keys(shared).map(function (key) {
|
|
1957
|
-
var pattern = key.endsWith('/') ? "(^" + key.replace(/\/$/, '') + "(/.+)?$)" : "(^" + key + "$)";
|
|
1958
|
-
return {
|
|
1959
|
-
// Intercept all shared requests and proxy them to loadShare
|
|
1960
|
-
find: new RegExp(pattern),
|
|
1961
|
-
replacement: '$1',
|
|
1962
|
-
customResolver: function customResolver(source, importer) {
|
|
1963
|
-
if (/\.css$/.test(source)) return;
|
|
1964
|
-
var loadSharePath = getLoadShareModulePath(source);
|
|
1965
|
-
writeLoadShareModule(source, shared[key], command);
|
|
1966
|
-
writePreBuildLibPath(source);
|
|
1967
|
-
addUsedShares(source);
|
|
1968
|
-
writeLocalSharedImportMap();
|
|
1969
|
-
return this.resolve(loadSharePath, importer);
|
|
1970
|
-
}
|
|
1971
|
-
};
|
|
1972
|
-
}));
|
|
1973
|
-
var savePrebuild = new PromiseStore();
|
|
1974
|
-
(_config2$resolve$alia2 = _config2.resolve.alias).push.apply(_config2$resolve$alia2, Object.keys(shared).map(function (key) {
|
|
1975
|
-
return command === 'build' ? {
|
|
1976
|
-
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
1977
|
-
replacement: function replacement($1) {
|
|
1978
|
-
var module = assertModuleFound(PREBUILD_TAG, $1);
|
|
1979
|
-
var pkgName = module.name;
|
|
1980
|
-
return pkgName;
|
|
1981
|
-
}
|
|
1982
|
-
} : {
|
|
1983
|
-
find: new RegExp("(.*" + PREBUILD_TAG + ".*)"),
|
|
1984
|
-
replacement: '$1',
|
|
1985
|
-
customResolver: function customResolver(source, importer) {
|
|
1986
|
-
try {
|
|
1987
|
-
var _this = this;
|
|
1988
|
-
var module = assertModuleFound(PREBUILD_TAG, source);
|
|
1989
|
-
var pkgName = module.name;
|
|
1990
|
-
return Promise.resolve(_this.resolve(pkgName, importer).then(function (item) {
|
|
1991
|
-
return item.id;
|
|
1992
|
-
})).then(function (result) {
|
|
1993
|
-
if (!result.includes(_config.cacheDir)) {
|
|
1994
|
-
// save pre-bunding module id
|
|
1995
|
-
savePrebuild.set(pkgName, Promise.resolve(result));
|
|
1996
|
-
}
|
|
1997
|
-
// Fix localSharedImportMap import id
|
|
1998
|
-
var _resolve = _this.resolve;
|
|
1999
|
-
return Promise.resolve(savePrebuild.get(pkgName)).then(function (_savePrebuild$get) {
|
|
2000
|
-
return Promise.resolve(_resolve.call(_this, _savePrebuild$get, importer));
|
|
2001
|
-
});
|
|
2002
|
-
});
|
|
2003
|
-
} catch (e) {
|
|
2004
|
-
return Promise.reject(e);
|
|
2005
|
-
}
|
|
2006
|
-
}
|
|
2007
|
-
};
|
|
2008
|
-
}));
|
|
2009
|
-
}
|
|
2010
|
-
}];
|
|
2011
|
-
}
|
|
2012
|
-
|
|
2013
|
-
var aliasToArrayPlugin = {
|
|
2014
|
-
name: 'alias-transform-plugin',
|
|
2015
|
-
config: function config(_config, _ref) {
|
|
2016
|
-
if (!_config.resolve) _config.resolve = {};
|
|
2017
|
-
if (!_config.resolve.alias) _config.resolve.alias = [];
|
|
2018
|
-
var alias = _config.resolve.alias;
|
|
2019
|
-
if (typeof alias === 'object' && !Array.isArray(alias)) {
|
|
2020
|
-
_config.resolve.alias = Object.entries(alias).map(function (_ref2) {
|
|
2021
|
-
var find = _ref2[0],
|
|
2022
|
-
replacement = _ref2[1];
|
|
2023
|
-
return {
|
|
2024
|
-
find: find,
|
|
2025
|
-
replacement: replacement
|
|
2026
|
-
};
|
|
2027
|
-
});
|
|
2028
|
-
}
|
|
2029
|
-
}
|
|
2030
|
-
};
|
|
2031
|
-
|
|
2032
|
-
var normalizeOptimizeDepsPlugin = {
|
|
2033
|
-
name: 'normalizeOptimizeDeps',
|
|
2034
|
-
config: function config(_config, _ref) {
|
|
2035
|
-
var optimizeDeps = _config.optimizeDeps;
|
|
2036
|
-
if (!optimizeDeps) {
|
|
2037
|
-
_config.optimizeDeps = {};
|
|
2038
|
-
optimizeDeps = _config.optimizeDeps;
|
|
2039
|
-
}
|
|
2040
|
-
// todo: fix this workaround
|
|
2041
|
-
optimizeDeps.force = true;
|
|
2042
|
-
if (!optimizeDeps.include) optimizeDeps.include = [];
|
|
2043
|
-
if (!optimizeDeps.needsInterop) optimizeDeps.needsInterop = [];
|
|
2044
|
-
}
|
|
2045
|
-
};
|
|
2046
|
-
|
|
2047
|
-
function federation(mfUserOptions) {
|
|
2048
|
-
var options = normalizeModuleFederationOptions(mfUserOptions);
|
|
2049
|
-
var name = options.name,
|
|
2050
|
-
shared = options.shared,
|
|
2051
|
-
filename = options.filename,
|
|
2052
|
-
hostInitInjectLocation = options.hostInitInjectLocation;
|
|
2053
|
-
if (!name) throw new Error('name is required');
|
|
2054
|
-
return [{
|
|
2055
|
-
name: 'vite:module-federation-config',
|
|
2056
|
-
enforce: 'pre',
|
|
2057
|
-
configResolved: function configResolved(config) {
|
|
2058
|
-
// Set root path
|
|
2059
|
-
VirtualModule.setRoot(config.root);
|
|
2060
|
-
// Ensure virtual package directory exists
|
|
2061
|
-
VirtualModule.ensureVirtualPackageExists();
|
|
2062
|
-
initVirtualModules();
|
|
2063
|
-
}
|
|
2064
|
-
}, aliasToArrayPlugin, checkAliasConflicts({
|
|
2065
|
-
shared: shared
|
|
2066
|
-
}), normalizeOptimizeDepsPlugin].concat(pluginDts(options), addEntry({
|
|
2067
|
-
entryName: 'remoteEntry',
|
|
2068
|
-
entryPath: REMOTE_ENTRY_ID,
|
|
2069
|
-
fileName: filename
|
|
2070
|
-
}), addEntry({
|
|
2071
|
-
entryName: 'hostInit',
|
|
2072
|
-
entryPath: getHostAutoInitPath(),
|
|
2073
|
-
inject: hostInitInjectLocation
|
|
2074
|
-
}), addEntry({
|
|
2075
|
-
entryName: 'virtualExposes',
|
|
2076
|
-
entryPath: VIRTUAL_EXPOSES
|
|
2077
|
-
}), [pluginProxyRemoteEntry(), pluginProxyRemotes(options)], pluginModuleParseEnd(function (id) {
|
|
2078
|
-
return id.includes(getHostAutoInitImportId()) || id.includes(REMOTE_ENTRY_ID) || id.includes(VIRTUAL_EXPOSES) || id.includes(getLocalSharedImportMapPath());
|
|
2079
|
-
}, {
|
|
2080
|
-
moduleParseTimeout: options.moduleParseTimeout
|
|
2081
|
-
}), proxySharedModule({
|
|
2082
|
-
shared: shared
|
|
2083
|
-
}), [PluginDevProxyModuleTopLevelAwait(), {
|
|
2084
|
-
name: 'module-federation-vite',
|
|
2085
|
-
enforce: 'post',
|
|
2086
|
-
// @ts-expect-error
|
|
2087
|
-
// used to expose plugin options: https://github.com/rolldown/rolldown/discussions/2577#discussioncomment-11137593
|
|
2088
|
-
_options: options,
|
|
2089
|
-
config: function config(_config, _ref) {
|
|
2090
|
-
var _config$optimizeDeps, _config$optimizeDeps2, _config$optimizeDeps3, _config$optimizeDeps4;
|
|
2091
|
-
// TODO: singleton
|
|
2092
|
-
_config.resolve.alias.push({
|
|
2093
|
-
find: '@module-federation/runtime',
|
|
2094
|
-
replacement: options.implementation
|
|
2095
|
-
});
|
|
2096
|
-
_config.build = defu__default["default"](_config.build || {}, {
|
|
2097
|
-
commonjsOptions: {
|
|
2098
|
-
strictRequires: 'auto'
|
|
2099
|
-
}
|
|
2100
|
-
});
|
|
2101
|
-
var virtualDir = options.virtualModuleDir || '__mf__virtual';
|
|
2102
|
-
(_config$optimizeDeps = _config.optimizeDeps) == null || (_config$optimizeDeps = _config$optimizeDeps.include) == null || _config$optimizeDeps.push('@module-federation/runtime');
|
|
2103
|
-
(_config$optimizeDeps2 = _config.optimizeDeps) == null || (_config$optimizeDeps2 = _config$optimizeDeps2.include) == null || _config$optimizeDeps2.push(virtualDir);
|
|
2104
|
-
(_config$optimizeDeps3 = _config.optimizeDeps) == null || (_config$optimizeDeps3 = _config$optimizeDeps3.needsInterop) == null || _config$optimizeDeps3.push(virtualDir);
|
|
2105
|
-
(_config$optimizeDeps4 = _config.optimizeDeps) == null || (_config$optimizeDeps4 = _config$optimizeDeps4.needsInterop) == null || _config$optimizeDeps4.push(getLocalSharedImportMapPath());
|
|
2106
|
-
}
|
|
2107
|
-
}], Manifest());
|
|
2108
|
-
}
|
|
2109
|
-
|
|
2110
|
-
exports.federation = federation;
|
|
2111
|
-
|
|
2112
|
-
}));
|