@module-federation/vite 1.16.6 → 1.16.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +179 -51
- package/lib/pluginDts-Cpmdbbr0.js +632 -0
- package/package.json +1 -1
- package/lib/packageUtils-CYnJFfPP.js +0 -307
- package/lib/pluginDts-BaVhdR6i.js +0 -309
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import fs, { existsSync, readFileSync, readdirSync } from "fs";
|
|
3
|
+
import { createRequire as createRequire$1 } from "module";
|
|
4
|
+
import * as path$1 from "node:path";
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
6
|
+
import { normalizeOptions } from "@module-federation/sdk";
|
|
7
|
+
import { consumeTypesAPI, generateTypesAPI, isTSProject, normalizeConsumeTypesOptions, normalizeDtsOptions, normalizeGenerateTypesOptions } from "@module-federation/dts-plugin";
|
|
8
|
+
import { rpc } from "@module-federation/dts-plugin/core";
|
|
9
|
+
//#region \0rolldown/runtime.js
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __exportAll = (all, no_symbols) => {
|
|
12
|
+
let target = {};
|
|
13
|
+
for (var name in all) __defProp(target, name, {
|
|
14
|
+
get: all[name],
|
|
15
|
+
enumerable: true
|
|
16
|
+
});
|
|
17
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/utils/buildPaths.ts
|
|
22
|
+
/**
|
|
23
|
+
* Rebase an import path for a bootstrap file that moved from root into `dir`.
|
|
24
|
+
*
|
|
25
|
+
* When entryFileNames places entries in a subdirectory (e.g. `static/js/`),
|
|
26
|
+
* the bootstrap file moves there too. Paths that resolved from the HTML root
|
|
27
|
+
* must resolve from the new directory instead.
|
|
28
|
+
*
|
|
29
|
+
* Cases: `/static/js/hostInit.js` → `./hostInit.js` (strip dir prefix)
|
|
30
|
+
* `./src/main.tsx` → `../../src/main.tsx` (climb back up for each dir level)
|
|
31
|
+
* `https://cdn.example.com` → unchanged (absolute URL)
|
|
32
|
+
*/
|
|
33
|
+
function rebaseImport(importSrc, dir) {
|
|
34
|
+
if (!dir) return importSrc;
|
|
35
|
+
if (isAbsoluteUrl(importSrc)) return importSrc;
|
|
36
|
+
const normalizedDir = dir.replace(/^\/+|\/+$/g, "");
|
|
37
|
+
if (!normalizedDir) return importSrc;
|
|
38
|
+
const stripDirPrefix = (src, prefix) => {
|
|
39
|
+
if (src === prefix) return "";
|
|
40
|
+
if (src.startsWith(prefix + "/")) return src.slice(prefix.length);
|
|
41
|
+
};
|
|
42
|
+
const absoluteRemainder = stripDirPrefix(importSrc, "/" + normalizedDir);
|
|
43
|
+
if (absoluteRemainder !== void 0) {
|
|
44
|
+
const remainder = absoluteRemainder.replace(/^\/+/, "");
|
|
45
|
+
return remainder ? "./" + remainder : "./";
|
|
46
|
+
}
|
|
47
|
+
const relativeRemainder = stripDirPrefix(importSrc, normalizedDir);
|
|
48
|
+
if (relativeRemainder !== void 0) {
|
|
49
|
+
const remainder = relativeRemainder.replace(/^\/+/, "");
|
|
50
|
+
return remainder ? "./" + remainder : "./";
|
|
51
|
+
}
|
|
52
|
+
const upLevels = normalizedDir.split("/").filter(Boolean).length;
|
|
53
|
+
const prefix = upLevels > 0 ? "../".repeat(upLevels) : "./";
|
|
54
|
+
if (importSrc.startsWith("./")) return prefix + importSrc.slice(2);
|
|
55
|
+
if (importSrc.startsWith("/")) return prefix + importSrc.slice(1);
|
|
56
|
+
return prefix + importSrc;
|
|
57
|
+
}
|
|
58
|
+
function normalizePathForImport(path) {
|
|
59
|
+
return path.replace(/\\/g, "/");
|
|
60
|
+
}
|
|
61
|
+
const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i;
|
|
62
|
+
function isAbsoluteUrl(src) {
|
|
63
|
+
if (/^[a-z]:[\\/]/i.test(src)) return false;
|
|
64
|
+
return EXTERNAL_URL_RE.test(src);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/utils/logger.ts
|
|
68
|
+
const MODULE_FEDERATION_LOG_PREFIX = "[Module Federation]";
|
|
69
|
+
function formatModuleFederationMessage(message) {
|
|
70
|
+
return `${MODULE_FEDERATION_LOG_PREFIX} ${message}`;
|
|
71
|
+
}
|
|
72
|
+
function createModuleFederationError(message) {
|
|
73
|
+
return new Error(formatModuleFederationMessage(message));
|
|
74
|
+
}
|
|
75
|
+
function toConsoleArgs(message, rest = []) {
|
|
76
|
+
if (typeof message === "string") return [formatModuleFederationMessage(message), ...rest];
|
|
77
|
+
if (message === void 0) return [MODULE_FEDERATION_LOG_PREFIX, ...rest];
|
|
78
|
+
return [
|
|
79
|
+
MODULE_FEDERATION_LOG_PREFIX,
|
|
80
|
+
message,
|
|
81
|
+
...rest
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
const moduleFederationConsole = {
|
|
85
|
+
log(message, ...rest) {
|
|
86
|
+
console.log(...toConsoleArgs(message, rest));
|
|
87
|
+
},
|
|
88
|
+
warn(message, ...rest) {
|
|
89
|
+
console.warn(...toConsoleArgs(message, rest));
|
|
90
|
+
},
|
|
91
|
+
error(message, ...rest) {
|
|
92
|
+
console.error(...toConsoleArgs(message, rest));
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
moduleFederationConsole.log;
|
|
96
|
+
const mfWarn = moduleFederationConsole.warn;
|
|
97
|
+
const mfError = moduleFederationConsole.error;
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/utils/packageUtils.ts
|
|
100
|
+
const dependencyPresenceCache = /* @__PURE__ */ new Map();
|
|
101
|
+
let packageDetectionCwd;
|
|
102
|
+
function getDependencyCacheKey(cwd, dependencyName) {
|
|
103
|
+
return `${cwd}:${dependencyName}`;
|
|
104
|
+
}
|
|
105
|
+
function setPackageDetectionCwd(cwd) {
|
|
106
|
+
packageDetectionCwd = cwd;
|
|
107
|
+
}
|
|
108
|
+
function getPackageDetectionCwd() {
|
|
109
|
+
return packageDetectionCwd || process.cwd();
|
|
110
|
+
}
|
|
111
|
+
function resolveImportPath(specifier) {
|
|
112
|
+
const resolved = import.meta.resolve(specifier);
|
|
113
|
+
if (!resolved.startsWith("file:")) return resolved;
|
|
114
|
+
const filePath = fileURLToPath(resolved);
|
|
115
|
+
if (!existsSync(filePath)) {
|
|
116
|
+
const error = /* @__PURE__ */ new Error(`Cannot find module '${specifier}'`);
|
|
117
|
+
error.code = "MODULE_NOT_FOUND";
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
return filePath;
|
|
121
|
+
}
|
|
122
|
+
const DEFAULT_EXPORT_CONDITIONS = [
|
|
123
|
+
"browser",
|
|
124
|
+
"import",
|
|
125
|
+
"module",
|
|
126
|
+
"default",
|
|
127
|
+
"require"
|
|
128
|
+
];
|
|
129
|
+
function resolveExportsEntry(exportsField, conditions = DEFAULT_EXPORT_CONDITIONS) {
|
|
130
|
+
if (typeof exportsField === "string") return exportsField;
|
|
131
|
+
if (!exportsField || typeof exportsField !== "object") return void 0;
|
|
132
|
+
const record = exportsField;
|
|
133
|
+
const rootExport = record["."];
|
|
134
|
+
if (rootExport) return resolveExportsEntry(rootExport);
|
|
135
|
+
for (const condition of conditions) {
|
|
136
|
+
const target = resolveExportsEntry(record[condition], conditions);
|
|
137
|
+
if (target) return target;
|
|
138
|
+
}
|
|
139
|
+
for (const target of Object.values(record)) {
|
|
140
|
+
const resolved = resolveExportsEntry(target, conditions);
|
|
141
|
+
if (resolved) return resolved;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function getPackageExportsTarget(pkg, packageName, exportsField) {
|
|
145
|
+
if (typeof exportsField === "string") return pkg === packageName ? exportsField : void 0;
|
|
146
|
+
if (!exportsField || typeof exportsField !== "object") return void 0;
|
|
147
|
+
const record = exportsField;
|
|
148
|
+
const subpath = pkg === packageName ? "." : `.${pkg.slice(packageName.length)}`;
|
|
149
|
+
if (subpath !== ".") return record[subpath];
|
|
150
|
+
return record["."] ?? (!Object.keys(record).some((key) => key.startsWith(".")) ? record : void 0);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Escaping rules:
|
|
154
|
+
* Convert using the format __${mapping}__, where _ and $ are not allowed in npm package names but can be used in variable names.
|
|
155
|
+
* @ => 1
|
|
156
|
+
* / => 2
|
|
157
|
+
* - => 3
|
|
158
|
+
* . => 4
|
|
159
|
+
*/
|
|
160
|
+
/**
|
|
161
|
+
* Encodes a package name into a valid file name.
|
|
162
|
+
* @param {string} name - The package name, e.g., "@scope/xx-xx.xx".
|
|
163
|
+
* @returns {string} - The encoded file name.
|
|
164
|
+
*/
|
|
165
|
+
function packageNameEncode(name) {
|
|
166
|
+
if (typeof name !== "string") throw createModuleFederationError("A string package name is required");
|
|
167
|
+
return name.replace(/@/g, "_mf_0_").replace(/\//g, "_mf_1_").replace(/-/g, "_mf_2_").replace(/\./g, "_mf_3_");
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Decodes an encoded file name back to the original package name.
|
|
171
|
+
* @param {string} encoded - The encoded file name, e.g., "_mf_0_scope_mf_1_xx_mf_2_xx_mf_3_xx".
|
|
172
|
+
* @returns {string} - The decoded package name.
|
|
173
|
+
*/
|
|
174
|
+
function packageNameDecode(encoded) {
|
|
175
|
+
if (typeof encoded !== "string") throw createModuleFederationError("A string encoded file name is required");
|
|
176
|
+
return encoded.replace(/_mf_0_/g, "@").replace(/_mf_1_/g, "/").replace(/_mf_2_/g, "-").replace(/_mf_3_/g, ".");
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Removes any subpath from an npm package specifier and returns the package name only.
|
|
180
|
+
* @param {string} packageString - The package specifier, e.g., "@scope/pkg/runtime" or "react/jsx-runtime".
|
|
181
|
+
* @returns {string} - The base npm package name.
|
|
182
|
+
*/
|
|
183
|
+
function getPackageName(packageString) {
|
|
184
|
+
const match = packageString.match(/^(?:@[^/]+\/)?[^/]+/);
|
|
185
|
+
return match ? match[0] : packageString;
|
|
186
|
+
}
|
|
187
|
+
function getPackageNameFromNodeModulePath(source) {
|
|
188
|
+
const normalized = source.replace(/\\/g, "/");
|
|
189
|
+
const nodeModulesIndex = normalized.lastIndexOf("/node_modules/");
|
|
190
|
+
if (nodeModulesIndex < 0) return;
|
|
191
|
+
const parts = normalized.slice(nodeModulesIndex + 14).split("/");
|
|
192
|
+
if (!parts[0]) return;
|
|
193
|
+
if (parts[0].startsWith("@")) return parts[1] ? `${parts[0]}/${parts[1]}` : void 0;
|
|
194
|
+
return parts[0];
|
|
195
|
+
}
|
|
196
|
+
function getSharedCacheKey(pkg, shareItem) {
|
|
197
|
+
return shareItem.shareConfig.singleton || !shareItem.version ? pkg : `${pkg}@${shareItem.version}`;
|
|
198
|
+
}
|
|
199
|
+
function getInstalledPackageJson(pkg, opts) {
|
|
200
|
+
const cwd = opts?.cwd || getPackageDetectionCwd();
|
|
201
|
+
const packageName = opts?.packageName || getPackageName(pkg);
|
|
202
|
+
const tryReadPackageJson = (packageJsonPath) => {
|
|
203
|
+
if (!existsSync(packageJsonPath)) return void 0;
|
|
204
|
+
try {
|
|
205
|
+
return {
|
|
206
|
+
path: packageJsonPath,
|
|
207
|
+
dir: path$1.dirname(packageJsonPath),
|
|
208
|
+
packageJson: JSON.parse(readFileSync(packageJsonPath, "utf-8"))
|
|
209
|
+
};
|
|
210
|
+
} catch {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
const findPackageInPnpmStore = (startDir) => {
|
|
215
|
+
let currentDir = startDir;
|
|
216
|
+
const rootDir = path$1.parse(currentDir).root;
|
|
217
|
+
while (true) {
|
|
218
|
+
const pnpmStoreDir = path$1.join(currentDir, "node_modules", ".pnpm");
|
|
219
|
+
if (existsSync(pnpmStoreDir)) try {
|
|
220
|
+
for (const entry of readdirSync(pnpmStoreDir, { withFileTypes: true })) {
|
|
221
|
+
if (!entry.isDirectory()) continue;
|
|
222
|
+
const candidate = tryReadPackageJson(path$1.join(pnpmStoreDir, entry.name, "node_modules", packageName, "package.json"));
|
|
223
|
+
if (candidate?.packageJson.name === packageName) return candidate;
|
|
224
|
+
}
|
|
225
|
+
} catch {}
|
|
226
|
+
if (currentDir === rootDir) break;
|
|
227
|
+
currentDir = path$1.dirname(currentDir);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
try {
|
|
231
|
+
const projectRequire = createRequire$1(pathToFileURL(path$1.join(cwd, "package.json")));
|
|
232
|
+
let resolvedPath;
|
|
233
|
+
if (opts?.fromResolvedEntry) resolvedPath = opts.fromResolvedEntry;
|
|
234
|
+
else try {
|
|
235
|
+
resolvedPath = projectRequire.resolve(pkg);
|
|
236
|
+
} catch {
|
|
237
|
+
resolvedPath = projectRequire.resolve(packageName);
|
|
238
|
+
}
|
|
239
|
+
let currentDir = path$1.dirname(resolvedPath);
|
|
240
|
+
const rootDir = path$1.parse(currentDir).root;
|
|
241
|
+
while (true) {
|
|
242
|
+
const packageJsonPath = path$1.join(currentDir, "package.json");
|
|
243
|
+
if (existsSync(packageJsonPath)) {
|
|
244
|
+
const packageJsonContent = readFileSync(packageJsonPath, "utf-8");
|
|
245
|
+
try {
|
|
246
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
247
|
+
if (packageJson.name === packageName) return {
|
|
248
|
+
path: packageJsonPath,
|
|
249
|
+
dir: currentDir,
|
|
250
|
+
packageJson
|
|
251
|
+
};
|
|
252
|
+
} catch (error) {
|
|
253
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (currentDir === rootDir) break;
|
|
257
|
+
currentDir = path$1.dirname(currentDir);
|
|
258
|
+
}
|
|
259
|
+
} catch {
|
|
260
|
+
let currentDir = cwd;
|
|
261
|
+
const rootDir = path$1.parse(currentDir).root;
|
|
262
|
+
while (true) {
|
|
263
|
+
const directCandidate = tryReadPackageJson(path$1.join(currentDir, "node_modules", packageName, "package.json"));
|
|
264
|
+
if (directCandidate?.packageJson.name === packageName) return directCandidate;
|
|
265
|
+
if (currentDir === rootDir) break;
|
|
266
|
+
currentDir = path$1.dirname(currentDir);
|
|
267
|
+
}
|
|
268
|
+
return findPackageInPnpmStore(cwd);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
function getInstalledPackageEntry(pkg, opts) {
|
|
272
|
+
const installed = getInstalledPackageJson(pkg, opts);
|
|
273
|
+
if (!installed) return void 0;
|
|
274
|
+
const cwd = opts?.cwd || getPackageDetectionCwd();
|
|
275
|
+
const packageName = opts?.packageName || getPackageName(pkg);
|
|
276
|
+
if (pkg !== packageName && opts?.resolveSubpathWithRequire !== false) try {
|
|
277
|
+
return createRequire$1(pathToFileURL(path$1.join(cwd, "package.json"))).resolve(pkg);
|
|
278
|
+
} catch {}
|
|
279
|
+
const packageJson = installed.packageJson;
|
|
280
|
+
const explicitEntry = resolveExportsEntry(getPackageExportsTarget(pkg, packageName, packageJson.exports), opts?.conditions) || (typeof packageJson.module === "string" ? packageJson.module : void 0) || (typeof packageJson.main === "string" ? packageJson.main : void 0) || "index.js";
|
|
281
|
+
return path$1.join(installed.dir, explicitEntry);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Detect whether the current runtime is Vite 8+ by checking for a Vite version flag
|
|
285
|
+
* on the plugin hook context, with Rolldown metadata kept as a compatibility fallback.
|
|
286
|
+
*/
|
|
287
|
+
function getIsRolldown(ctx) {
|
|
288
|
+
const viteVersion = ctx?.meta?.viteVersion;
|
|
289
|
+
const viteMajor = Number(String(viteVersion ?? "").split(".")[0]);
|
|
290
|
+
return Number.isFinite(viteMajor) && viteMajor >= 8 || !!ctx?.meta?.rolldownVersion;
|
|
291
|
+
}
|
|
292
|
+
/** Walk up from Vite `config.root` (Nuxt may point at `.nuxt` cache dirs). */
|
|
293
|
+
function isNuxtProjectRoot(root) {
|
|
294
|
+
let dir = root;
|
|
295
|
+
for (let i = 0; i < 8; i++) {
|
|
296
|
+
if (hasPackageDependency("nuxt", dir) || hasPackageDependency("nuxt-nightly", dir)) return true;
|
|
297
|
+
const parent = path$1.dirname(dir);
|
|
298
|
+
if (parent === dir) break;
|
|
299
|
+
dir = parent;
|
|
300
|
+
}
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
function hasPackageDependency(dependencyName, cwd = packageDetectionCwd || process.cwd()) {
|
|
304
|
+
const cacheKey = getDependencyCacheKey(cwd, dependencyName);
|
|
305
|
+
const cached = dependencyPresenceCache.get(cacheKey);
|
|
306
|
+
if (cached !== void 0) return cached;
|
|
307
|
+
try {
|
|
308
|
+
const packageJson = JSON.parse(readFileSync(path$1.join(cwd, "package.json"), "utf8"));
|
|
309
|
+
const hasDependency = [
|
|
310
|
+
packageJson.dependencies,
|
|
311
|
+
packageJson.devDependencies,
|
|
312
|
+
packageJson.peerDependencies,
|
|
313
|
+
packageJson.optionalDependencies
|
|
314
|
+
].some((deps) => !!deps?.[dependencyName]);
|
|
315
|
+
dependencyPresenceCache.set(cacheKey, hasDependency);
|
|
316
|
+
return hasDependency;
|
|
317
|
+
} catch {
|
|
318
|
+
dependencyPresenceCache.set(cacheKey, false);
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
//#endregion
|
|
323
|
+
//#region src/plugins/pluginDts.ts
|
|
324
|
+
var pluginDts_exports = /* @__PURE__ */ __exportAll({
|
|
325
|
+
DEFAULT_PUBLIC_TYPES_FOLDER: () => DEFAULT_PUBLIC_TYPES_FOLDER,
|
|
326
|
+
createDevDtsAssetMiddleware: () => createDevDtsAssetMiddleware,
|
|
327
|
+
default: () => pluginDts,
|
|
328
|
+
getDevDtsAssetPaths: () => getDevDtsAssetPaths,
|
|
329
|
+
resolveDtsPluginOptions: () => resolveDtsPluginOptions
|
|
330
|
+
});
|
|
331
|
+
const DEFAULT_DEV_OPTIONS = {
|
|
332
|
+
disableLiveReload: true,
|
|
333
|
+
disableHotTypesReload: false,
|
|
334
|
+
disableDynamicRemoteTypeHints: false
|
|
335
|
+
};
|
|
336
|
+
const DYNAMIC_HINTS_PLUGIN = "@module-federation/dts-plugin/dynamic-remote-type-hints-plugin";
|
|
337
|
+
const getIPv4 = () => process.env["FEDERATION_IPV4"] || "127.0.0.1";
|
|
338
|
+
const DEV_TYPES_FOLDER = ".dev-server";
|
|
339
|
+
const DEFAULT_PUBLIC_TYPES_FOLDER = "@mf-types";
|
|
340
|
+
const forkDevWorkerPath = resolveImportPath("@module-federation/dts-plugin/dist/fork-dev-worker.js");
|
|
341
|
+
var DevWorker = class {
|
|
342
|
+
worker = rpc.createRpcWorker(forkDevWorkerPath, {}, void 0, false);
|
|
343
|
+
constructor(options) {
|
|
344
|
+
this.worker.connect(options);
|
|
345
|
+
}
|
|
346
|
+
update() {
|
|
347
|
+
this.worker.process?.send?.({
|
|
348
|
+
type: rpc.RpcGMCallTypes.CALL,
|
|
349
|
+
id: this.worker.id,
|
|
350
|
+
args: [void 0, "update"]
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
exit() {
|
|
354
|
+
this.worker.terminate();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
const normalizeDevOptions = (dev) => {
|
|
358
|
+
if (dev === false) return false;
|
|
359
|
+
if (dev === true || typeof dev === "undefined") return { ...DEFAULT_DEV_OPTIONS };
|
|
360
|
+
return {
|
|
361
|
+
...DEFAULT_DEV_OPTIONS,
|
|
362
|
+
...dev
|
|
363
|
+
};
|
|
364
|
+
};
|
|
365
|
+
const buildDtsModuleFederationConfig = (options) => {
|
|
366
|
+
const exposes = {};
|
|
367
|
+
Object.entries(options.exposes).forEach(([key, value]) => {
|
|
368
|
+
if (value.import) exposes[key] = value.import;
|
|
369
|
+
});
|
|
370
|
+
const remotes = {};
|
|
371
|
+
Object.entries(options.remotes).forEach(([key, remote]) => {
|
|
372
|
+
if (!remote.entry) return;
|
|
373
|
+
remotes[key] = `${remote.entryGlobalName?.startsWith("http") || remote.entryGlobalName?.includes(".json") ? remote.name || key : remote.entryGlobalName || remote.name || key}@${remote.entry}`;
|
|
374
|
+
});
|
|
375
|
+
return {
|
|
376
|
+
...options,
|
|
377
|
+
exposes,
|
|
378
|
+
remotes
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
const resolveOutputDir = (config) => {
|
|
382
|
+
const { outDir } = config.build;
|
|
383
|
+
if (path$1.isAbsolute(outDir)) return normalizePathForImport(path$1.relative(config.root, outDir));
|
|
384
|
+
return outDir;
|
|
385
|
+
};
|
|
386
|
+
const ensureRuntimePlugin = (options, pluginId) => {
|
|
387
|
+
if (!options.runtimePlugins.some((plugin) => {
|
|
388
|
+
if (typeof plugin === "string") return plugin === pluginId;
|
|
389
|
+
return plugin[0] === pluginId;
|
|
390
|
+
})) options.runtimePlugins.push(pluginId);
|
|
391
|
+
};
|
|
392
|
+
const getExposeImportPaths = (options) => {
|
|
393
|
+
return Object.values(options.exposes).map((value) => {
|
|
394
|
+
return value.import;
|
|
395
|
+
}).filter((value) => Boolean(value));
|
|
396
|
+
};
|
|
397
|
+
const usesVueSfcExposes = (options) => {
|
|
398
|
+
return getExposeImportPaths(options).some((value) => value.endsWith(".vue"));
|
|
399
|
+
};
|
|
400
|
+
const resolveDtsPluginOptions = (dts, options, context) => {
|
|
401
|
+
if (dts === false) return false;
|
|
402
|
+
const inferredGenerateTypesDefaults = { generateAPITypes: true };
|
|
403
|
+
if (usesVueSfcExposes(options) && hasPackageDependency("vue-tsc", context)) inferredGenerateTypesDefaults.compilerInstance = "vue-tsc";
|
|
404
|
+
if (dts === true || typeof dts === "undefined") return { generateTypes: inferredGenerateTypesDefaults };
|
|
405
|
+
const generateTypes = dts.generateTypes;
|
|
406
|
+
return {
|
|
407
|
+
...dts,
|
|
408
|
+
generateTypes: generateTypes === false ? false : {
|
|
409
|
+
...inferredGenerateTypesDefaults,
|
|
410
|
+
...generateTypes === true || typeof generateTypes === "undefined" ? {} : generateTypes
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
const getBasePath = (base) => {
|
|
415
|
+
if (base.startsWith("http://") || base.startsWith("https://")) return new URL(base).pathname.replace(/\/$/, "") || "/";
|
|
416
|
+
return base.replace(/\/$/, "") || "/";
|
|
417
|
+
};
|
|
418
|
+
const joinBaseAndAsset = (base, assetFileName) => {
|
|
419
|
+
const basePath = getBasePath(base);
|
|
420
|
+
return `${basePath === "/" ? "" : basePath}/${assetFileName}`.replace(/\/{2,}/g, "/");
|
|
421
|
+
};
|
|
422
|
+
const getDevDtsAssetPaths = (options) => {
|
|
423
|
+
const { outputDir, publicTypesFolder, root, base } = options;
|
|
424
|
+
return {
|
|
425
|
+
apiFilePath: path$1.resolve(root, outputDir, `${DEV_TYPES_FOLDER}.d.ts`),
|
|
426
|
+
apiRequestPath: joinBaseAndAsset(base, `${publicTypesFolder}.d.ts`),
|
|
427
|
+
zipFilePath: path$1.resolve(root, outputDir, `${DEV_TYPES_FOLDER}.zip`),
|
|
428
|
+
zipRequestPath: joinBaseAndAsset(base, `${publicTypesFolder}.zip`)
|
|
429
|
+
};
|
|
430
|
+
};
|
|
431
|
+
const createDevDtsAssetMiddleware = (assetPaths) => {
|
|
432
|
+
return (req, res, next) => {
|
|
433
|
+
const requestPath = req.url?.split("?")[0];
|
|
434
|
+
const isZipRequest = requestPath === assetPaths.zipRequestPath;
|
|
435
|
+
const isApiRequest = requestPath === assetPaths.apiRequestPath;
|
|
436
|
+
if (!isZipRequest && !isApiRequest) {
|
|
437
|
+
next();
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
const filePath = isZipRequest ? assetPaths.zipFilePath : assetPaths.apiFilePath;
|
|
441
|
+
if (!fs.existsSync(filePath)) {
|
|
442
|
+
res.statusCode = 404;
|
|
443
|
+
res.end();
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
res.statusCode = 200;
|
|
447
|
+
res.setHeader("Content-Type", isZipRequest ? "application/x-gzip" : "application/typescript");
|
|
448
|
+
if (req.method === "HEAD") {
|
|
449
|
+
res.end();
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
const stream = fs.createReadStream(filePath);
|
|
453
|
+
stream.on("error", () => {
|
|
454
|
+
if (!res.headersSent) res.statusCode = 500;
|
|
455
|
+
res.end();
|
|
456
|
+
});
|
|
457
|
+
res.on("close", () => {
|
|
458
|
+
stream.destroy();
|
|
459
|
+
});
|
|
460
|
+
stream.pipe(res);
|
|
461
|
+
};
|
|
462
|
+
};
|
|
463
|
+
const normalizeDevDtsOptions = (dts, context) => {
|
|
464
|
+
return normalizeOptions(isTSProject(dts, context), {
|
|
465
|
+
generateTypes: { compileInChildProcess: true },
|
|
466
|
+
consumeTypes: { consumeAPITypes: true },
|
|
467
|
+
extraOptions: {},
|
|
468
|
+
displayErrorInTerminal: typeof dts === "object" && dts ? dts.displayErrorInTerminal : void 0
|
|
469
|
+
}, "mfOptions.dts")(dts);
|
|
470
|
+
};
|
|
471
|
+
const logDtsError = (error, dtsOptions) => {
|
|
472
|
+
if (dtsOptions === false) return;
|
|
473
|
+
if (typeof dtsOptions === "object" && dtsOptions && dtsOptions.displayErrorInTerminal === false) return;
|
|
474
|
+
mfError(error);
|
|
475
|
+
};
|
|
476
|
+
function pluginDts(options) {
|
|
477
|
+
if (options.dts === false) return [];
|
|
478
|
+
const baseDtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
|
|
479
|
+
const getDtsModuleFederationConfig = (context) => ({
|
|
480
|
+
...baseDtsModuleFederationConfig,
|
|
481
|
+
dts: resolveDtsPluginOptions(options.dts, options, context)
|
|
482
|
+
});
|
|
483
|
+
let resolvedConfig;
|
|
484
|
+
let devWorker;
|
|
485
|
+
let normalizedDevOptions;
|
|
486
|
+
let hasGeneratedBundle = false;
|
|
487
|
+
return [{
|
|
488
|
+
name: "module-federation-dts-dev",
|
|
489
|
+
apply: "serve",
|
|
490
|
+
config(config) {
|
|
491
|
+
normalizedDevOptions = normalizeDevOptions(options.dev);
|
|
492
|
+
if (!normalizedDevOptions) return;
|
|
493
|
+
if (normalizedDevOptions.disableDynamicRemoteTypeHints) return;
|
|
494
|
+
ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
|
|
495
|
+
const define = config.define ? { ...config.define } : {};
|
|
496
|
+
if (!("FEDERATION_IPV4" in define)) define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
|
|
497
|
+
config.define = define;
|
|
498
|
+
},
|
|
499
|
+
configResolved(config) {
|
|
500
|
+
resolvedConfig = config;
|
|
501
|
+
},
|
|
502
|
+
configureServer(server) {
|
|
503
|
+
if (!normalizedDevOptions || !resolvedConfig) return;
|
|
504
|
+
const devOptions = normalizedDevOptions;
|
|
505
|
+
if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) return;
|
|
506
|
+
if (!options.name) throw createModuleFederationError("name is required if you want to enable dev server!");
|
|
507
|
+
const outputDir = resolveOutputDir(resolvedConfig);
|
|
508
|
+
const dtsModuleFederationConfig = getDtsModuleFederationConfig(resolvedConfig.root);
|
|
509
|
+
const normalizedDtsOptions = normalizeDevDtsOptions(dtsModuleFederationConfig.dts, resolvedConfig.root);
|
|
510
|
+
if (typeof normalizedDtsOptions !== "object") return;
|
|
511
|
+
const normalizedGenerateTypes = normalizeOptions(Boolean(normalizedDtsOptions), { compileInChildProcess: true }, "mfOptions.dts.generateTypes")(normalizedDtsOptions.generateTypes);
|
|
512
|
+
const remote = normalizedGenerateTypes === false ? void 0 : {
|
|
513
|
+
implementation: normalizedDtsOptions.implementation,
|
|
514
|
+
context: resolvedConfig.root,
|
|
515
|
+
outputDir,
|
|
516
|
+
moduleFederationConfig: { ...dtsModuleFederationConfig },
|
|
517
|
+
hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || "@mf-types",
|
|
518
|
+
...normalizedGenerateTypes,
|
|
519
|
+
typesFolder: DEV_TYPES_FOLDER
|
|
520
|
+
};
|
|
521
|
+
if (remote) server.middlewares.use(createDevDtsAssetMiddleware(getDevDtsAssetPaths({
|
|
522
|
+
outputDir,
|
|
523
|
+
publicTypesFolder: remote.hostRemoteTypesFolder || "@mf-types",
|
|
524
|
+
root: resolvedConfig.root,
|
|
525
|
+
base: resolvedConfig.base
|
|
526
|
+
})));
|
|
527
|
+
if (remote && !remote.tsConfigPath && normalizedDtsOptions.tsConfigPath) remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
528
|
+
const normalizedConsumeTypes = normalizeOptions(Boolean(normalizedDtsOptions), { consumeAPITypes: true }, "mfOptions.dts.consumeTypes")(normalizedDtsOptions.consumeTypes);
|
|
529
|
+
const host = normalizedConsumeTypes === false ? void 0 : {
|
|
530
|
+
implementation: normalizedDtsOptions.implementation,
|
|
531
|
+
context: resolvedConfig.root,
|
|
532
|
+
moduleFederationConfig: dtsModuleFederationConfig,
|
|
533
|
+
typesFolder: normalizedConsumeTypes.typesFolder || "@mf-types",
|
|
534
|
+
abortOnError: false,
|
|
535
|
+
...normalizedConsumeTypes
|
|
536
|
+
};
|
|
537
|
+
const extraOptions = normalizedDtsOptions.extraOptions || {};
|
|
538
|
+
if (!remote && !host && devOptions.disableLiveReload) return;
|
|
539
|
+
const startDevWorker = async () => {
|
|
540
|
+
let remoteTypeUrls;
|
|
541
|
+
if (host) remoteTypeUrls = await new Promise((resolve) => {
|
|
542
|
+
consumeTypesAPI({
|
|
543
|
+
host,
|
|
544
|
+
extraOptions,
|
|
545
|
+
displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
|
|
546
|
+
}, resolve);
|
|
547
|
+
});
|
|
548
|
+
devWorker = new DevWorker({
|
|
549
|
+
name: options.name,
|
|
550
|
+
remote,
|
|
551
|
+
host: host ? {
|
|
552
|
+
...host,
|
|
553
|
+
remoteTypeUrls
|
|
554
|
+
} : void 0,
|
|
555
|
+
extraOptions,
|
|
556
|
+
disableLiveReload: devOptions.disableLiveReload,
|
|
557
|
+
disableHotTypesReload: devOptions.disableHotTypesReload
|
|
558
|
+
});
|
|
559
|
+
const update = () => devWorker?.update();
|
|
560
|
+
server.watcher.on("change", update);
|
|
561
|
+
server.watcher.on("add", update);
|
|
562
|
+
server.watcher.on("unlink", update);
|
|
563
|
+
server.httpServer?.once("close", () => {
|
|
564
|
+
devWorker?.exit();
|
|
565
|
+
server.watcher.off("change", update);
|
|
566
|
+
server.watcher.off("add", update);
|
|
567
|
+
server.watcher.off("unlink", update);
|
|
568
|
+
});
|
|
569
|
+
};
|
|
570
|
+
startDevWorker().catch((error) => {
|
|
571
|
+
logDtsError(error, normalizedDtsOptions);
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
}, {
|
|
575
|
+
name: "module-federation-dts-build",
|
|
576
|
+
apply: "build",
|
|
577
|
+
configResolved(config) {
|
|
578
|
+
resolvedConfig = config;
|
|
579
|
+
},
|
|
580
|
+
async generateBundle() {
|
|
581
|
+
if (hasGeneratedBundle) return;
|
|
582
|
+
hasGeneratedBundle = true;
|
|
583
|
+
if (!resolvedConfig) return;
|
|
584
|
+
let normalizedDtsOptions;
|
|
585
|
+
try {
|
|
586
|
+
normalizedDtsOptions = normalizeDtsOptions(getDtsModuleFederationConfig(resolvedConfig.root), resolvedConfig.root);
|
|
587
|
+
} catch (error) {
|
|
588
|
+
logDtsError(error, options.dts);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
if (typeof normalizedDtsOptions !== "object") return;
|
|
592
|
+
const context = resolvedConfig.root;
|
|
593
|
+
const outputDir = resolveOutputDir(resolvedConfig);
|
|
594
|
+
let consumeOptions;
|
|
595
|
+
try {
|
|
596
|
+
consumeOptions = normalizeConsumeTypesOptions({
|
|
597
|
+
context,
|
|
598
|
+
dtsOptions: normalizedDtsOptions,
|
|
599
|
+
pluginOptions: getDtsModuleFederationConfig(resolvedConfig.root)
|
|
600
|
+
});
|
|
601
|
+
} catch (error) {
|
|
602
|
+
logDtsError(error, normalizedDtsOptions);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (consumeOptions?.host?.typesOnBuild) try {
|
|
606
|
+
await consumeTypesAPI(consumeOptions);
|
|
607
|
+
} catch (error) {
|
|
608
|
+
logDtsError(error, normalizedDtsOptions);
|
|
609
|
+
}
|
|
610
|
+
let generateOptions;
|
|
611
|
+
try {
|
|
612
|
+
generateOptions = normalizeGenerateTypesOptions({
|
|
613
|
+
context,
|
|
614
|
+
outputDir,
|
|
615
|
+
dtsOptions: normalizedDtsOptions,
|
|
616
|
+
pluginOptions: getDtsModuleFederationConfig(resolvedConfig.root)
|
|
617
|
+
});
|
|
618
|
+
} catch (error) {
|
|
619
|
+
logDtsError(error, normalizedDtsOptions);
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
if (!generateOptions) return;
|
|
623
|
+
try {
|
|
624
|
+
await generateTypesAPI({ dtsManagerOptions: generateOptions });
|
|
625
|
+
} catch (error) {
|
|
626
|
+
logDtsError(error, normalizedDtsOptions);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}];
|
|
630
|
+
}
|
|
631
|
+
//#endregion
|
|
632
|
+
export { mfWarn as _, getIsRolldown as a, getPackageNameFromNodeModulePath as c, isNuxtProjectRoot as d, packageNameDecode as f, createModuleFederationError as g, setPackageDetectionCwd as h, getInstalledPackageJson as i, getSharedCacheKey as l, resolveImportPath as m, pluginDts_exports as n, getPackageDetectionCwd as o, packageNameEncode as p, getInstalledPackageEntry as r, getPackageName as s, DEFAULT_PUBLIC_TYPES_FOLDER as t, hasPackageDependency as u, normalizePathForImport as v, rebaseImport as y };
|