@module-federation/vite 1.16.5 → 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 +594 -416
- package/lib/pluginDts-Cpmdbbr0.js +632 -0
- package/package.json +3 -5
- package/lib/packageUtils-CxYRnFwy.js +0 -261
- package/lib/pluginDts-Bgdw5ODE.js +0 -309
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync, readdirSync } from "fs";
|
|
2
|
-
import { createRequire } from "module";
|
|
3
|
-
import path from "pathe";
|
|
4
|
-
import { fileURLToPath } from "url";
|
|
5
|
-
//#region src/utils/logger.ts
|
|
6
|
-
const MODULE_FEDERATION_LOG_PREFIX = "[Module Federation]";
|
|
7
|
-
function formatModuleFederationMessage(message) {
|
|
8
|
-
return `${MODULE_FEDERATION_LOG_PREFIX} ${message}`;
|
|
9
|
-
}
|
|
10
|
-
function createModuleFederationError(message) {
|
|
11
|
-
return new Error(formatModuleFederationMessage(message));
|
|
12
|
-
}
|
|
13
|
-
function toConsoleArgs(message, rest = []) {
|
|
14
|
-
if (typeof message === "string") return [formatModuleFederationMessage(message), ...rest];
|
|
15
|
-
if (message === void 0) return [MODULE_FEDERATION_LOG_PREFIX, ...rest];
|
|
16
|
-
return [
|
|
17
|
-
MODULE_FEDERATION_LOG_PREFIX,
|
|
18
|
-
message,
|
|
19
|
-
...rest
|
|
20
|
-
];
|
|
21
|
-
}
|
|
22
|
-
const moduleFederationConsole = {
|
|
23
|
-
log(message, ...rest) {
|
|
24
|
-
console.log(...toConsoleArgs(message, rest));
|
|
25
|
-
},
|
|
26
|
-
warn(message, ...rest) {
|
|
27
|
-
console.warn(...toConsoleArgs(message, rest));
|
|
28
|
-
},
|
|
29
|
-
error(message, ...rest) {
|
|
30
|
-
console.error(...toConsoleArgs(message, rest));
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
moduleFederationConsole.log;
|
|
34
|
-
const mfWarn = moduleFederationConsole.warn;
|
|
35
|
-
const mfError = moduleFederationConsole.error;
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/utils/packageUtils.ts
|
|
38
|
-
const dependencyPresenceCache = /* @__PURE__ */ new Map();
|
|
39
|
-
let packageDetectionCwd;
|
|
40
|
-
function getDependencyCacheKey(cwd, dependencyName) {
|
|
41
|
-
return `${cwd}:${dependencyName}`;
|
|
42
|
-
}
|
|
43
|
-
function setPackageDetectionCwd(cwd) {
|
|
44
|
-
packageDetectionCwd = cwd;
|
|
45
|
-
}
|
|
46
|
-
function getPackageDetectionCwd() {
|
|
47
|
-
return packageDetectionCwd || process.cwd();
|
|
48
|
-
}
|
|
49
|
-
function resolveImportPath(specifier) {
|
|
50
|
-
const resolved = import.meta.resolve(specifier);
|
|
51
|
-
if (!resolved.startsWith("file:")) return resolved;
|
|
52
|
-
const filePath = fileURLToPath(resolved);
|
|
53
|
-
if (!existsSync(filePath)) {
|
|
54
|
-
const error = /* @__PURE__ */ new Error(`Cannot find module '${specifier}'`);
|
|
55
|
-
error.code = "MODULE_NOT_FOUND";
|
|
56
|
-
throw error;
|
|
57
|
-
}
|
|
58
|
-
return filePath;
|
|
59
|
-
}
|
|
60
|
-
const DEFAULT_EXPORT_CONDITIONS = [
|
|
61
|
-
"browser",
|
|
62
|
-
"import",
|
|
63
|
-
"module",
|
|
64
|
-
"default",
|
|
65
|
-
"require"
|
|
66
|
-
];
|
|
67
|
-
function resolveExportsEntry(exportsField, conditions = DEFAULT_EXPORT_CONDITIONS) {
|
|
68
|
-
if (typeof exportsField === "string") return exportsField;
|
|
69
|
-
if (!exportsField || typeof exportsField !== "object") return void 0;
|
|
70
|
-
const record = exportsField;
|
|
71
|
-
const rootExport = record["."];
|
|
72
|
-
if (rootExport) return resolveExportsEntry(rootExport);
|
|
73
|
-
for (const condition of conditions) {
|
|
74
|
-
const target = resolveExportsEntry(record[condition], conditions);
|
|
75
|
-
if (target) return target;
|
|
76
|
-
}
|
|
77
|
-
for (const target of Object.values(record)) {
|
|
78
|
-
const resolved = resolveExportsEntry(target, conditions);
|
|
79
|
-
if (resolved) return resolved;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function getPackageExportsTarget(pkg, packageName, exportsField) {
|
|
83
|
-
if (typeof exportsField === "string") return pkg === packageName ? exportsField : void 0;
|
|
84
|
-
if (!exportsField || typeof exportsField !== "object") return void 0;
|
|
85
|
-
const record = exportsField;
|
|
86
|
-
const subpath = pkg === packageName ? "." : `.${pkg.slice(packageName.length)}`;
|
|
87
|
-
if (subpath !== ".") return record[subpath];
|
|
88
|
-
return record["."] ?? (!Object.keys(record).some((key) => key.startsWith(".")) ? record : void 0);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Escaping rules:
|
|
92
|
-
* Convert using the format __${mapping}__, where _ and $ are not allowed in npm package names but can be used in variable names.
|
|
93
|
-
* @ => 1
|
|
94
|
-
* / => 2
|
|
95
|
-
* - => 3
|
|
96
|
-
* . => 4
|
|
97
|
-
*/
|
|
98
|
-
/**
|
|
99
|
-
* Encodes a package name into a valid file name.
|
|
100
|
-
* @param {string} name - The package name, e.g., "@scope/xx-xx.xx".
|
|
101
|
-
* @returns {string} - The encoded file name.
|
|
102
|
-
*/
|
|
103
|
-
function packageNameEncode(name) {
|
|
104
|
-
if (typeof name !== "string") throw createModuleFederationError("A string package name is required");
|
|
105
|
-
return name.replace(/@/g, "_mf_0_").replace(/\//g, "_mf_1_").replace(/-/g, "_mf_2_").replace(/\./g, "_mf_3_");
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Decodes an encoded file name back to the original package name.
|
|
109
|
-
* @param {string} encoded - The encoded file name, e.g., "_mf_0_scope_mf_1_xx_mf_2_xx_mf_3_xx".
|
|
110
|
-
* @returns {string} - The decoded package name.
|
|
111
|
-
*/
|
|
112
|
-
function packageNameDecode(encoded) {
|
|
113
|
-
if (typeof encoded !== "string") throw createModuleFederationError("A string encoded file name is required");
|
|
114
|
-
return encoded.replace(/_mf_0_/g, "@").replace(/_mf_1_/g, "/").replace(/_mf_2_/g, "-").replace(/_mf_3_/g, ".");
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Removes any subpath from an npm package specifier and returns the package name only.
|
|
118
|
-
* @param {string} packageString - The package specifier, e.g., "@scope/pkg/runtime" or "react/jsx-runtime".
|
|
119
|
-
* @returns {string} - The base npm package name.
|
|
120
|
-
*/
|
|
121
|
-
function getPackageName(packageString) {
|
|
122
|
-
const match = packageString.match(/^(?:@[^/]+\/)?[^/]+/);
|
|
123
|
-
return match ? match[0] : packageString;
|
|
124
|
-
}
|
|
125
|
-
function getPackageNameFromNodeModulePath(source) {
|
|
126
|
-
const normalized = source.replace(/\\/g, "/");
|
|
127
|
-
const nodeModulesIndex = normalized.lastIndexOf("/node_modules/");
|
|
128
|
-
if (nodeModulesIndex < 0) return;
|
|
129
|
-
const parts = normalized.slice(nodeModulesIndex + 14).split("/");
|
|
130
|
-
if (!parts[0]) return;
|
|
131
|
-
if (parts[0].startsWith("@")) return parts[1] ? `${parts[0]}/${parts[1]}` : void 0;
|
|
132
|
-
return parts[0];
|
|
133
|
-
}
|
|
134
|
-
function getSharedCacheKey(pkg, shareItem) {
|
|
135
|
-
return shareItem.shareConfig.singleton || !shareItem.version ? pkg : `${pkg}@${shareItem.version}`;
|
|
136
|
-
}
|
|
137
|
-
function getInstalledPackageJson(pkg, opts) {
|
|
138
|
-
const cwd = opts?.cwd || getPackageDetectionCwd();
|
|
139
|
-
const packageName = opts?.packageName || getPackageName(pkg);
|
|
140
|
-
const tryReadPackageJson = (packageJsonPath) => {
|
|
141
|
-
if (!existsSync(packageJsonPath)) return void 0;
|
|
142
|
-
try {
|
|
143
|
-
return {
|
|
144
|
-
path: packageJsonPath,
|
|
145
|
-
dir: path.dirname(packageJsonPath),
|
|
146
|
-
packageJson: JSON.parse(readFileSync(packageJsonPath, "utf-8"))
|
|
147
|
-
};
|
|
148
|
-
} catch {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
const findPackageInPnpmStore = (startDir) => {
|
|
153
|
-
let currentDir = startDir;
|
|
154
|
-
const rootDir = path.parse(currentDir).root;
|
|
155
|
-
while (true) {
|
|
156
|
-
const pnpmStoreDir = path.join(currentDir, "node_modules", ".pnpm");
|
|
157
|
-
if (existsSync(pnpmStoreDir)) try {
|
|
158
|
-
for (const entry of readdirSync(pnpmStoreDir, { withFileTypes: true })) {
|
|
159
|
-
if (!entry.isDirectory()) continue;
|
|
160
|
-
const candidate = tryReadPackageJson(path.join(pnpmStoreDir, entry.name, "node_modules", packageName, "package.json"));
|
|
161
|
-
if (candidate?.packageJson.name === packageName) return candidate;
|
|
162
|
-
}
|
|
163
|
-
} catch {}
|
|
164
|
-
if (currentDir === rootDir) break;
|
|
165
|
-
currentDir = path.dirname(currentDir);
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
try {
|
|
169
|
-
const projectRequire = createRequire(new URL(`file://${path.join(cwd, "package.json")}`));
|
|
170
|
-
let resolvedPath;
|
|
171
|
-
if (opts?.fromResolvedEntry) resolvedPath = opts.fromResolvedEntry;
|
|
172
|
-
else try {
|
|
173
|
-
resolvedPath = projectRequire.resolve(pkg);
|
|
174
|
-
} catch {
|
|
175
|
-
resolvedPath = projectRequire.resolve(packageName);
|
|
176
|
-
}
|
|
177
|
-
let currentDir = path.dirname(resolvedPath);
|
|
178
|
-
const rootDir = path.parse(currentDir).root;
|
|
179
|
-
while (true) {
|
|
180
|
-
const packageJsonPath = path.join(currentDir, "package.json");
|
|
181
|
-
if (existsSync(packageJsonPath)) {
|
|
182
|
-
const packageJsonContent = readFileSync(packageJsonPath, "utf-8");
|
|
183
|
-
try {
|
|
184
|
-
const packageJson = JSON.parse(packageJsonContent);
|
|
185
|
-
if (packageJson.name === packageName) return {
|
|
186
|
-
path: packageJsonPath,
|
|
187
|
-
dir: currentDir,
|
|
188
|
-
packageJson
|
|
189
|
-
};
|
|
190
|
-
} catch (error) {
|
|
191
|
-
if (!(error instanceof SyntaxError)) throw error;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
if (currentDir === rootDir) break;
|
|
195
|
-
currentDir = path.dirname(currentDir);
|
|
196
|
-
}
|
|
197
|
-
} catch {
|
|
198
|
-
let currentDir = cwd;
|
|
199
|
-
const rootDir = path.parse(currentDir).root;
|
|
200
|
-
while (true) {
|
|
201
|
-
const directCandidate = tryReadPackageJson(path.join(currentDir, "node_modules", packageName, "package.json"));
|
|
202
|
-
if (directCandidate?.packageJson.name === packageName) return directCandidate;
|
|
203
|
-
if (currentDir === rootDir) break;
|
|
204
|
-
currentDir = path.dirname(currentDir);
|
|
205
|
-
}
|
|
206
|
-
return findPackageInPnpmStore(cwd);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
function getInstalledPackageEntry(pkg, opts) {
|
|
210
|
-
const installed = getInstalledPackageJson(pkg, opts);
|
|
211
|
-
if (!installed) return void 0;
|
|
212
|
-
const cwd = opts?.cwd || getPackageDetectionCwd();
|
|
213
|
-
const packageName = opts?.packageName || getPackageName(pkg);
|
|
214
|
-
if (pkg !== packageName && opts?.resolveSubpathWithRequire !== false) try {
|
|
215
|
-
return createRequire(new URL(`file://${path.join(cwd, "package.json")}`)).resolve(pkg);
|
|
216
|
-
} catch {}
|
|
217
|
-
const packageJson = installed.packageJson;
|
|
218
|
-
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";
|
|
219
|
-
return path.join(installed.dir, explicitEntry);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Detect whether the current runtime is Vite 8+ by checking for a Vite version flag
|
|
223
|
-
* on the plugin hook context, with Rolldown metadata kept as a compatibility fallback.
|
|
224
|
-
*/
|
|
225
|
-
function getIsRolldown(ctx) {
|
|
226
|
-
const viteVersion = ctx?.meta?.viteVersion;
|
|
227
|
-
const viteMajor = Number(String(viteVersion ?? "").split(".")[0]);
|
|
228
|
-
return Number.isFinite(viteMajor) && viteMajor >= 8 || !!ctx?.meta?.rolldownVersion;
|
|
229
|
-
}
|
|
230
|
-
/** Walk up from Vite `config.root` (Nuxt may point at `.nuxt` cache dirs). */
|
|
231
|
-
function isNuxtProjectRoot(root) {
|
|
232
|
-
let dir = root;
|
|
233
|
-
for (let i = 0; i < 8; i++) {
|
|
234
|
-
if (hasPackageDependency("nuxt", dir) || hasPackageDependency("nuxt-nightly", dir)) return true;
|
|
235
|
-
const parent = path.dirname(dir);
|
|
236
|
-
if (parent === dir) break;
|
|
237
|
-
dir = parent;
|
|
238
|
-
}
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
function hasPackageDependency(dependencyName, cwd = packageDetectionCwd || process.cwd()) {
|
|
242
|
-
const cacheKey = getDependencyCacheKey(cwd, dependencyName);
|
|
243
|
-
const cached = dependencyPresenceCache.get(cacheKey);
|
|
244
|
-
if (cached !== void 0) return cached;
|
|
245
|
-
try {
|
|
246
|
-
const packageJson = JSON.parse(readFileSync(path.join(cwd, "package.json"), "utf8"));
|
|
247
|
-
const hasDependency = [
|
|
248
|
-
packageJson.dependencies,
|
|
249
|
-
packageJson.devDependencies,
|
|
250
|
-
packageJson.peerDependencies,
|
|
251
|
-
packageJson.optionalDependencies
|
|
252
|
-
].some((deps) => !!deps?.[dependencyName]);
|
|
253
|
-
dependencyPresenceCache.set(cacheKey, hasDependency);
|
|
254
|
-
return hasDependency;
|
|
255
|
-
} catch {
|
|
256
|
-
dependencyPresenceCache.set(cacheKey, false);
|
|
257
|
-
return false;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
//#endregion
|
|
261
|
-
export { getPackageName as a, hasPackageDependency as c, packageNameEncode as d, resolveImportPath as f, mfWarn as g, mfError as h, getPackageDetectionCwd as i, isNuxtProjectRoot as l, createModuleFederationError as m, getInstalledPackageJson as n, getPackageNameFromNodeModulePath as o, setPackageDetectionCwd as p, getIsRolldown as r, getSharedCacheKey as s, getInstalledPackageEntry as t, packageNameDecode as u };
|
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
import { c as hasPackageDependency, f as resolveImportPath, h as mfError, m as createModuleFederationError } from "./packageUtils-CxYRnFwy.js";
|
|
2
|
-
import fs from "fs";
|
|
3
|
-
import * as path$1 from "pathe";
|
|
4
|
-
import { normalizeOptions } from "@module-federation/sdk";
|
|
5
|
-
import { consumeTypesAPI, generateTypesAPI, isTSProject, normalizeConsumeTypesOptions, normalizeDtsOptions, normalizeGenerateTypesOptions } from "@module-federation/dts-plugin";
|
|
6
|
-
import { rpc } from "@module-federation/dts-plugin/core";
|
|
7
|
-
//#region src/plugins/pluginDts.ts
|
|
8
|
-
const DEFAULT_DEV_OPTIONS = {
|
|
9
|
-
disableLiveReload: true,
|
|
10
|
-
disableHotTypesReload: false,
|
|
11
|
-
disableDynamicRemoteTypeHints: false
|
|
12
|
-
};
|
|
13
|
-
const DYNAMIC_HINTS_PLUGIN = "@module-federation/dts-plugin/dynamic-remote-type-hints-plugin";
|
|
14
|
-
const getIPv4 = () => process.env["FEDERATION_IPV4"] || "127.0.0.1";
|
|
15
|
-
const DEV_TYPES_FOLDER = ".dev-server";
|
|
16
|
-
const DEFAULT_PUBLIC_TYPES_FOLDER = "@mf-types";
|
|
17
|
-
const forkDevWorkerPath = resolveImportPath("@module-federation/dts-plugin/dist/fork-dev-worker.js");
|
|
18
|
-
var DevWorker = class {
|
|
19
|
-
worker = rpc.createRpcWorker(forkDevWorkerPath, {}, void 0, false);
|
|
20
|
-
constructor(options) {
|
|
21
|
-
this.worker.connect(options);
|
|
22
|
-
}
|
|
23
|
-
update() {
|
|
24
|
-
this.worker.process?.send?.({
|
|
25
|
-
type: rpc.RpcGMCallTypes.CALL,
|
|
26
|
-
id: this.worker.id,
|
|
27
|
-
args: [void 0, "update"]
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
exit() {
|
|
31
|
-
this.worker.terminate();
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
const normalizeDevOptions = (dev) => {
|
|
35
|
-
if (dev === false) return false;
|
|
36
|
-
if (dev === true || typeof dev === "undefined") return { ...DEFAULT_DEV_OPTIONS };
|
|
37
|
-
return {
|
|
38
|
-
...DEFAULT_DEV_OPTIONS,
|
|
39
|
-
...dev
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
const buildDtsModuleFederationConfig = (options) => {
|
|
43
|
-
const exposes = {};
|
|
44
|
-
Object.entries(options.exposes).forEach(([key, value]) => {
|
|
45
|
-
if (value.import) exposes[key] = value.import;
|
|
46
|
-
});
|
|
47
|
-
const remotes = {};
|
|
48
|
-
Object.entries(options.remotes).forEach(([key, remote]) => {
|
|
49
|
-
if (!remote.entry) return;
|
|
50
|
-
remotes[key] = `${remote.entryGlobalName?.startsWith("http") || remote.entryGlobalName?.includes(".json") ? remote.name || key : remote.entryGlobalName || remote.name || key}@${remote.entry}`;
|
|
51
|
-
});
|
|
52
|
-
return {
|
|
53
|
-
...options,
|
|
54
|
-
exposes,
|
|
55
|
-
remotes
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
const resolveOutputDir = (config) => {
|
|
59
|
-
const { outDir } = config.build;
|
|
60
|
-
if (path$1.isAbsolute(outDir)) return path$1.relative(config.root, outDir);
|
|
61
|
-
return outDir;
|
|
62
|
-
};
|
|
63
|
-
const ensureRuntimePlugin = (options, pluginId) => {
|
|
64
|
-
if (!options.runtimePlugins.some((plugin) => {
|
|
65
|
-
if (typeof plugin === "string") return plugin === pluginId;
|
|
66
|
-
return plugin[0] === pluginId;
|
|
67
|
-
})) options.runtimePlugins.push(pluginId);
|
|
68
|
-
};
|
|
69
|
-
const getExposeImportPaths = (options) => {
|
|
70
|
-
return Object.values(options.exposes).map((value) => {
|
|
71
|
-
return value.import;
|
|
72
|
-
}).filter((value) => Boolean(value));
|
|
73
|
-
};
|
|
74
|
-
const usesVueSfcExposes = (options) => {
|
|
75
|
-
return getExposeImportPaths(options).some((value) => value.endsWith(".vue"));
|
|
76
|
-
};
|
|
77
|
-
const resolveDtsPluginOptions = (dts, options, context) => {
|
|
78
|
-
if (dts === false) return false;
|
|
79
|
-
const inferredGenerateTypesDefaults = { generateAPITypes: true };
|
|
80
|
-
if (usesVueSfcExposes(options) && hasPackageDependency("vue-tsc", context)) inferredGenerateTypesDefaults.compilerInstance = "vue-tsc";
|
|
81
|
-
if (dts === true || typeof dts === "undefined") return { generateTypes: inferredGenerateTypesDefaults };
|
|
82
|
-
const generateTypes = dts.generateTypes;
|
|
83
|
-
return {
|
|
84
|
-
...dts,
|
|
85
|
-
generateTypes: generateTypes === false ? false : {
|
|
86
|
-
...inferredGenerateTypesDefaults,
|
|
87
|
-
...generateTypes === true || typeof generateTypes === "undefined" ? {} : generateTypes
|
|
88
|
-
}
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
const getBasePath = (base) => {
|
|
92
|
-
if (base.startsWith("http://") || base.startsWith("https://")) return new URL(base).pathname.replace(/\/$/, "") || "/";
|
|
93
|
-
return base.replace(/\/$/, "") || "/";
|
|
94
|
-
};
|
|
95
|
-
const joinBaseAndAsset = (base, assetFileName) => {
|
|
96
|
-
const basePath = getBasePath(base);
|
|
97
|
-
return `${basePath === "/" ? "" : basePath}/${assetFileName}`.replace(/\/{2,}/g, "/");
|
|
98
|
-
};
|
|
99
|
-
const getDevDtsAssetPaths = (options) => {
|
|
100
|
-
const { outputDir, publicTypesFolder, root, base } = options;
|
|
101
|
-
return {
|
|
102
|
-
apiFilePath: path$1.resolve(root, outputDir, `${DEV_TYPES_FOLDER}.d.ts`),
|
|
103
|
-
apiRequestPath: joinBaseAndAsset(base, `${publicTypesFolder}.d.ts`),
|
|
104
|
-
zipFilePath: path$1.resolve(root, outputDir, `${DEV_TYPES_FOLDER}.zip`),
|
|
105
|
-
zipRequestPath: joinBaseAndAsset(base, `${publicTypesFolder}.zip`)
|
|
106
|
-
};
|
|
107
|
-
};
|
|
108
|
-
const createDevDtsAssetMiddleware = (assetPaths) => {
|
|
109
|
-
return (req, res, next) => {
|
|
110
|
-
const requestPath = req.url?.split("?")[0];
|
|
111
|
-
const isZipRequest = requestPath === assetPaths.zipRequestPath;
|
|
112
|
-
const isApiRequest = requestPath === assetPaths.apiRequestPath;
|
|
113
|
-
if (!isZipRequest && !isApiRequest) {
|
|
114
|
-
next();
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
const filePath = isZipRequest ? assetPaths.zipFilePath : assetPaths.apiFilePath;
|
|
118
|
-
if (!fs.existsSync(filePath)) {
|
|
119
|
-
res.statusCode = 404;
|
|
120
|
-
res.end();
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
res.statusCode = 200;
|
|
124
|
-
res.setHeader("Content-Type", isZipRequest ? "application/x-gzip" : "application/typescript");
|
|
125
|
-
if (req.method === "HEAD") {
|
|
126
|
-
res.end();
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
const stream = fs.createReadStream(filePath);
|
|
130
|
-
stream.on("error", () => {
|
|
131
|
-
if (!res.headersSent) res.statusCode = 500;
|
|
132
|
-
res.end();
|
|
133
|
-
});
|
|
134
|
-
res.on("close", () => {
|
|
135
|
-
stream.destroy();
|
|
136
|
-
});
|
|
137
|
-
stream.pipe(res);
|
|
138
|
-
};
|
|
139
|
-
};
|
|
140
|
-
const normalizeDevDtsOptions = (dts, context) => {
|
|
141
|
-
return normalizeOptions(isTSProject(dts, context), {
|
|
142
|
-
generateTypes: { compileInChildProcess: true },
|
|
143
|
-
consumeTypes: { consumeAPITypes: true },
|
|
144
|
-
extraOptions: {},
|
|
145
|
-
displayErrorInTerminal: typeof dts === "object" && dts ? dts.displayErrorInTerminal : void 0
|
|
146
|
-
}, "mfOptions.dts")(dts);
|
|
147
|
-
};
|
|
148
|
-
const logDtsError = (error, dtsOptions) => {
|
|
149
|
-
if (dtsOptions === false) return;
|
|
150
|
-
if (typeof dtsOptions === "object" && dtsOptions && dtsOptions.displayErrorInTerminal === false) return;
|
|
151
|
-
mfError(error);
|
|
152
|
-
};
|
|
153
|
-
function pluginDts(options) {
|
|
154
|
-
if (options.dts === false) return [];
|
|
155
|
-
const baseDtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
|
|
156
|
-
const getDtsModuleFederationConfig = (context) => ({
|
|
157
|
-
...baseDtsModuleFederationConfig,
|
|
158
|
-
dts: resolveDtsPluginOptions(options.dts, options, context)
|
|
159
|
-
});
|
|
160
|
-
let resolvedConfig;
|
|
161
|
-
let devWorker;
|
|
162
|
-
let normalizedDevOptions;
|
|
163
|
-
let hasGeneratedBundle = false;
|
|
164
|
-
return [{
|
|
165
|
-
name: "module-federation-dts-dev",
|
|
166
|
-
apply: "serve",
|
|
167
|
-
config(config) {
|
|
168
|
-
normalizedDevOptions = normalizeDevOptions(options.dev);
|
|
169
|
-
if (!normalizedDevOptions) return;
|
|
170
|
-
if (normalizedDevOptions.disableDynamicRemoteTypeHints) return;
|
|
171
|
-
ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
|
|
172
|
-
const define = config.define ? { ...config.define } : {};
|
|
173
|
-
if (!("FEDERATION_IPV4" in define)) define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
|
|
174
|
-
config.define = define;
|
|
175
|
-
},
|
|
176
|
-
configResolved(config) {
|
|
177
|
-
resolvedConfig = config;
|
|
178
|
-
},
|
|
179
|
-
configureServer(server) {
|
|
180
|
-
if (!normalizedDevOptions || !resolvedConfig) return;
|
|
181
|
-
const devOptions = normalizedDevOptions;
|
|
182
|
-
if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) return;
|
|
183
|
-
if (!options.name) throw createModuleFederationError("name is required if you want to enable dev server!");
|
|
184
|
-
const outputDir = resolveOutputDir(resolvedConfig);
|
|
185
|
-
const dtsModuleFederationConfig = getDtsModuleFederationConfig(resolvedConfig.root);
|
|
186
|
-
const normalizedDtsOptions = normalizeDevDtsOptions(dtsModuleFederationConfig.dts, resolvedConfig.root);
|
|
187
|
-
if (typeof normalizedDtsOptions !== "object") return;
|
|
188
|
-
const normalizedGenerateTypes = normalizeOptions(Boolean(normalizedDtsOptions), { compileInChildProcess: true }, "mfOptions.dts.generateTypes")(normalizedDtsOptions.generateTypes);
|
|
189
|
-
const remote = normalizedGenerateTypes === false ? void 0 : {
|
|
190
|
-
implementation: normalizedDtsOptions.implementation,
|
|
191
|
-
context: resolvedConfig.root,
|
|
192
|
-
outputDir,
|
|
193
|
-
moduleFederationConfig: { ...dtsModuleFederationConfig },
|
|
194
|
-
hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || DEFAULT_PUBLIC_TYPES_FOLDER,
|
|
195
|
-
...normalizedGenerateTypes,
|
|
196
|
-
typesFolder: DEV_TYPES_FOLDER
|
|
197
|
-
};
|
|
198
|
-
if (remote) server.middlewares.use(createDevDtsAssetMiddleware(getDevDtsAssetPaths({
|
|
199
|
-
outputDir,
|
|
200
|
-
publicTypesFolder: remote.hostRemoteTypesFolder || DEFAULT_PUBLIC_TYPES_FOLDER,
|
|
201
|
-
root: resolvedConfig.root,
|
|
202
|
-
base: resolvedConfig.base
|
|
203
|
-
})));
|
|
204
|
-
if (remote && !remote.tsConfigPath && normalizedDtsOptions.tsConfigPath) remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
205
|
-
const normalizedConsumeTypes = normalizeOptions(Boolean(normalizedDtsOptions), { consumeAPITypes: true }, "mfOptions.dts.consumeTypes")(normalizedDtsOptions.consumeTypes);
|
|
206
|
-
const host = normalizedConsumeTypes === false ? void 0 : {
|
|
207
|
-
implementation: normalizedDtsOptions.implementation,
|
|
208
|
-
context: resolvedConfig.root,
|
|
209
|
-
moduleFederationConfig: dtsModuleFederationConfig,
|
|
210
|
-
typesFolder: normalizedConsumeTypes.typesFolder || "@mf-types",
|
|
211
|
-
abortOnError: false,
|
|
212
|
-
...normalizedConsumeTypes
|
|
213
|
-
};
|
|
214
|
-
const extraOptions = normalizedDtsOptions.extraOptions || {};
|
|
215
|
-
if (!remote && !host && devOptions.disableLiveReload) return;
|
|
216
|
-
const startDevWorker = async () => {
|
|
217
|
-
let remoteTypeUrls;
|
|
218
|
-
if (host) remoteTypeUrls = await new Promise((resolve) => {
|
|
219
|
-
consumeTypesAPI({
|
|
220
|
-
host,
|
|
221
|
-
extraOptions,
|
|
222
|
-
displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
|
|
223
|
-
}, resolve);
|
|
224
|
-
});
|
|
225
|
-
devWorker = new DevWorker({
|
|
226
|
-
name: options.name,
|
|
227
|
-
remote,
|
|
228
|
-
host: host ? {
|
|
229
|
-
...host,
|
|
230
|
-
remoteTypeUrls
|
|
231
|
-
} : void 0,
|
|
232
|
-
extraOptions,
|
|
233
|
-
disableLiveReload: devOptions.disableLiveReload,
|
|
234
|
-
disableHotTypesReload: devOptions.disableHotTypesReload
|
|
235
|
-
});
|
|
236
|
-
const update = () => devWorker?.update();
|
|
237
|
-
server.watcher.on("change", update);
|
|
238
|
-
server.watcher.on("add", update);
|
|
239
|
-
server.watcher.on("unlink", update);
|
|
240
|
-
server.httpServer?.once("close", () => {
|
|
241
|
-
devWorker?.exit();
|
|
242
|
-
server.watcher.off("change", update);
|
|
243
|
-
server.watcher.off("add", update);
|
|
244
|
-
server.watcher.off("unlink", update);
|
|
245
|
-
});
|
|
246
|
-
};
|
|
247
|
-
startDevWorker().catch((error) => {
|
|
248
|
-
logDtsError(error, normalizedDtsOptions);
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
}, {
|
|
252
|
-
name: "module-federation-dts-build",
|
|
253
|
-
apply: "build",
|
|
254
|
-
configResolved(config) {
|
|
255
|
-
resolvedConfig = config;
|
|
256
|
-
},
|
|
257
|
-
async generateBundle() {
|
|
258
|
-
if (hasGeneratedBundle) return;
|
|
259
|
-
hasGeneratedBundle = true;
|
|
260
|
-
if (!resolvedConfig) return;
|
|
261
|
-
let normalizedDtsOptions;
|
|
262
|
-
try {
|
|
263
|
-
normalizedDtsOptions = normalizeDtsOptions(getDtsModuleFederationConfig(resolvedConfig.root), resolvedConfig.root);
|
|
264
|
-
} catch (error) {
|
|
265
|
-
logDtsError(error, options.dts);
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
if (typeof normalizedDtsOptions !== "object") return;
|
|
269
|
-
const context = resolvedConfig.root;
|
|
270
|
-
const outputDir = resolveOutputDir(resolvedConfig);
|
|
271
|
-
let consumeOptions;
|
|
272
|
-
try {
|
|
273
|
-
consumeOptions = normalizeConsumeTypesOptions({
|
|
274
|
-
context,
|
|
275
|
-
dtsOptions: normalizedDtsOptions,
|
|
276
|
-
pluginOptions: getDtsModuleFederationConfig(resolvedConfig.root)
|
|
277
|
-
});
|
|
278
|
-
} catch (error) {
|
|
279
|
-
logDtsError(error, normalizedDtsOptions);
|
|
280
|
-
return;
|
|
281
|
-
}
|
|
282
|
-
if (consumeOptions?.host?.typesOnBuild) try {
|
|
283
|
-
await consumeTypesAPI(consumeOptions);
|
|
284
|
-
} catch (error) {
|
|
285
|
-
logDtsError(error, normalizedDtsOptions);
|
|
286
|
-
}
|
|
287
|
-
let generateOptions;
|
|
288
|
-
try {
|
|
289
|
-
generateOptions = normalizeGenerateTypesOptions({
|
|
290
|
-
context,
|
|
291
|
-
outputDir,
|
|
292
|
-
dtsOptions: normalizedDtsOptions,
|
|
293
|
-
pluginOptions: getDtsModuleFederationConfig(resolvedConfig.root)
|
|
294
|
-
});
|
|
295
|
-
} catch (error) {
|
|
296
|
-
logDtsError(error, normalizedDtsOptions);
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
if (!generateOptions) return;
|
|
300
|
-
try {
|
|
301
|
-
await generateTypesAPI({ dtsManagerOptions: generateOptions });
|
|
302
|
-
} catch (error) {
|
|
303
|
-
logDtsError(error, normalizedDtsOptions);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}];
|
|
307
|
-
}
|
|
308
|
-
//#endregion
|
|
309
|
-
export { pluginDts as default };
|