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