@module-federation/vite 1.15.4 → 1.16.0
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 +30 -1
- package/lib/index.cjs +1123 -870
- package/lib/index.d.cts +22 -6
- package/lib/index.d.mts +22 -6
- package/lib/index.mjs +1063 -793
- package/lib/packageUtils-CbbnJvKu.cjs +358 -0
- package/lib/packageUtils-DOekOsFz.mjs +250 -0
- package/lib/pluginDts-B5pcUmam.mjs +309 -0
- package/lib/pluginDts-BEOKyKQ-.cjs +311 -0
- package/lib/utils/ssrEntryLoader.cjs +301 -0
- package/lib/utils/ssrEntryLoader.d.cts +65 -0
- package/lib/utils/ssrEntryLoader.d.mts +66 -0
- package/lib/utils/ssrEntryLoader.mjs +301 -0
- package/package.json +13 -5
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { f as createModuleFederationError, h as __require, p as mfError, s as hasPackageDependency } from "./packageUtils-DOekOsFz.mjs";
|
|
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 = __require.resolve("@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 };
|
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
const require_packageUtils = require("./packageUtils-CbbnJvKu.cjs");
|
|
2
|
+
let fs = require("fs");
|
|
3
|
+
fs = require_packageUtils.__toESM(fs);
|
|
4
|
+
let pathe = require("pathe");
|
|
5
|
+
pathe = require_packageUtils.__toESM(pathe);
|
|
6
|
+
let _module_federation_sdk = require("@module-federation/sdk");
|
|
7
|
+
let _module_federation_dts_plugin = require("@module-federation/dts-plugin");
|
|
8
|
+
let _module_federation_dts_plugin_core = require("@module-federation/dts-plugin/core");
|
|
9
|
+
//#region src/plugins/pluginDts.ts
|
|
10
|
+
const DEFAULT_DEV_OPTIONS = {
|
|
11
|
+
disableLiveReload: true,
|
|
12
|
+
disableHotTypesReload: false,
|
|
13
|
+
disableDynamicRemoteTypeHints: false
|
|
14
|
+
};
|
|
15
|
+
const DYNAMIC_HINTS_PLUGIN = "@module-federation/dts-plugin/dynamic-remote-type-hints-plugin";
|
|
16
|
+
const getIPv4 = () => process.env["FEDERATION_IPV4"] || "127.0.0.1";
|
|
17
|
+
const DEV_TYPES_FOLDER = ".dev-server";
|
|
18
|
+
const DEFAULT_PUBLIC_TYPES_FOLDER = "@mf-types";
|
|
19
|
+
const forkDevWorkerPath = require.resolve("@module-federation/dts-plugin/dist/fork-dev-worker.js");
|
|
20
|
+
var DevWorker = class {
|
|
21
|
+
worker = _module_federation_dts_plugin_core.rpc.createRpcWorker(forkDevWorkerPath, {}, void 0, false);
|
|
22
|
+
constructor(options) {
|
|
23
|
+
this.worker.connect(options);
|
|
24
|
+
}
|
|
25
|
+
update() {
|
|
26
|
+
this.worker.process?.send?.({
|
|
27
|
+
type: _module_federation_dts_plugin_core.rpc.RpcGMCallTypes.CALL,
|
|
28
|
+
id: this.worker.id,
|
|
29
|
+
args: [void 0, "update"]
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exit() {
|
|
33
|
+
this.worker.terminate();
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const normalizeDevOptions = (dev) => {
|
|
37
|
+
if (dev === false) return false;
|
|
38
|
+
if (dev === true || typeof dev === "undefined") return { ...DEFAULT_DEV_OPTIONS };
|
|
39
|
+
return {
|
|
40
|
+
...DEFAULT_DEV_OPTIONS,
|
|
41
|
+
...dev
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const buildDtsModuleFederationConfig = (options) => {
|
|
45
|
+
const exposes = {};
|
|
46
|
+
Object.entries(options.exposes).forEach(([key, value]) => {
|
|
47
|
+
if (value.import) exposes[key] = value.import;
|
|
48
|
+
});
|
|
49
|
+
const remotes = {};
|
|
50
|
+
Object.entries(options.remotes).forEach(([key, remote]) => {
|
|
51
|
+
if (!remote.entry) return;
|
|
52
|
+
remotes[key] = `${remote.entryGlobalName?.startsWith("http") || remote.entryGlobalName?.includes(".json") ? remote.name || key : remote.entryGlobalName || remote.name || key}@${remote.entry}`;
|
|
53
|
+
});
|
|
54
|
+
return {
|
|
55
|
+
...options,
|
|
56
|
+
exposes,
|
|
57
|
+
remotes
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
const resolveOutputDir = (config) => {
|
|
61
|
+
const { outDir } = config.build;
|
|
62
|
+
if (pathe.isAbsolute(outDir)) return pathe.relative(config.root, outDir);
|
|
63
|
+
return outDir;
|
|
64
|
+
};
|
|
65
|
+
const ensureRuntimePlugin = (options, pluginId) => {
|
|
66
|
+
if (!options.runtimePlugins.some((plugin) => {
|
|
67
|
+
if (typeof plugin === "string") return plugin === pluginId;
|
|
68
|
+
return plugin[0] === pluginId;
|
|
69
|
+
})) options.runtimePlugins.push(pluginId);
|
|
70
|
+
};
|
|
71
|
+
const getExposeImportPaths = (options) => {
|
|
72
|
+
return Object.values(options.exposes).map((value) => {
|
|
73
|
+
return value.import;
|
|
74
|
+
}).filter((value) => Boolean(value));
|
|
75
|
+
};
|
|
76
|
+
const usesVueSfcExposes = (options) => {
|
|
77
|
+
return getExposeImportPaths(options).some((value) => value.endsWith(".vue"));
|
|
78
|
+
};
|
|
79
|
+
const resolveDtsPluginOptions = (dts, options, context) => {
|
|
80
|
+
if (dts === false) return false;
|
|
81
|
+
const inferredGenerateTypesDefaults = { generateAPITypes: true };
|
|
82
|
+
if (usesVueSfcExposes(options) && require_packageUtils.hasPackageDependency("vue-tsc", context)) inferredGenerateTypesDefaults.compilerInstance = "vue-tsc";
|
|
83
|
+
if (dts === true || typeof dts === "undefined") return { generateTypes: inferredGenerateTypesDefaults };
|
|
84
|
+
const generateTypes = dts.generateTypes;
|
|
85
|
+
return {
|
|
86
|
+
...dts,
|
|
87
|
+
generateTypes: generateTypes === false ? false : {
|
|
88
|
+
...inferredGenerateTypesDefaults,
|
|
89
|
+
...generateTypes === true || typeof generateTypes === "undefined" ? {} : generateTypes
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const getBasePath = (base) => {
|
|
94
|
+
if (base.startsWith("http://") || base.startsWith("https://")) return new URL(base).pathname.replace(/\/$/, "") || "/";
|
|
95
|
+
return base.replace(/\/$/, "") || "/";
|
|
96
|
+
};
|
|
97
|
+
const joinBaseAndAsset = (base, assetFileName) => {
|
|
98
|
+
const basePath = getBasePath(base);
|
|
99
|
+
return `${basePath === "/" ? "" : basePath}/${assetFileName}`.replace(/\/{2,}/g, "/");
|
|
100
|
+
};
|
|
101
|
+
const getDevDtsAssetPaths = (options) => {
|
|
102
|
+
const { outputDir, publicTypesFolder, root, base } = options;
|
|
103
|
+
return {
|
|
104
|
+
apiFilePath: pathe.resolve(root, outputDir, `${DEV_TYPES_FOLDER}.d.ts`),
|
|
105
|
+
apiRequestPath: joinBaseAndAsset(base, `${publicTypesFolder}.d.ts`),
|
|
106
|
+
zipFilePath: pathe.resolve(root, outputDir, `${DEV_TYPES_FOLDER}.zip`),
|
|
107
|
+
zipRequestPath: joinBaseAndAsset(base, `${publicTypesFolder}.zip`)
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
const createDevDtsAssetMiddleware = (assetPaths) => {
|
|
111
|
+
return (req, res, next) => {
|
|
112
|
+
const requestPath = req.url?.split("?")[0];
|
|
113
|
+
const isZipRequest = requestPath === assetPaths.zipRequestPath;
|
|
114
|
+
const isApiRequest = requestPath === assetPaths.apiRequestPath;
|
|
115
|
+
if (!isZipRequest && !isApiRequest) {
|
|
116
|
+
next();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const filePath = isZipRequest ? assetPaths.zipFilePath : assetPaths.apiFilePath;
|
|
120
|
+
if (!fs.default.existsSync(filePath)) {
|
|
121
|
+
res.statusCode = 404;
|
|
122
|
+
res.end();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
res.statusCode = 200;
|
|
126
|
+
res.setHeader("Content-Type", isZipRequest ? "application/x-gzip" : "application/typescript");
|
|
127
|
+
if (req.method === "HEAD") {
|
|
128
|
+
res.end();
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const stream = fs.default.createReadStream(filePath);
|
|
132
|
+
stream.on("error", () => {
|
|
133
|
+
if (!res.headersSent) res.statusCode = 500;
|
|
134
|
+
res.end();
|
|
135
|
+
});
|
|
136
|
+
res.on("close", () => {
|
|
137
|
+
stream.destroy();
|
|
138
|
+
});
|
|
139
|
+
stream.pipe(res);
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
const normalizeDevDtsOptions = (dts, context) => {
|
|
143
|
+
return (0, _module_federation_sdk.normalizeOptions)((0, _module_federation_dts_plugin.isTSProject)(dts, context), {
|
|
144
|
+
generateTypes: { compileInChildProcess: true },
|
|
145
|
+
consumeTypes: { consumeAPITypes: true },
|
|
146
|
+
extraOptions: {},
|
|
147
|
+
displayErrorInTerminal: typeof dts === "object" && dts ? dts.displayErrorInTerminal : void 0
|
|
148
|
+
}, "mfOptions.dts")(dts);
|
|
149
|
+
};
|
|
150
|
+
const logDtsError = (error, dtsOptions) => {
|
|
151
|
+
if (dtsOptions === false) return;
|
|
152
|
+
if (typeof dtsOptions === "object" && dtsOptions && dtsOptions.displayErrorInTerminal === false) return;
|
|
153
|
+
require_packageUtils.mfError(error);
|
|
154
|
+
};
|
|
155
|
+
function pluginDts(options) {
|
|
156
|
+
if (options.dts === false) return [];
|
|
157
|
+
const baseDtsModuleFederationConfig = buildDtsModuleFederationConfig(options);
|
|
158
|
+
const getDtsModuleFederationConfig = (context) => ({
|
|
159
|
+
...baseDtsModuleFederationConfig,
|
|
160
|
+
dts: resolveDtsPluginOptions(options.dts, options, context)
|
|
161
|
+
});
|
|
162
|
+
let resolvedConfig;
|
|
163
|
+
let devWorker;
|
|
164
|
+
let normalizedDevOptions;
|
|
165
|
+
let hasGeneratedBundle = false;
|
|
166
|
+
return [{
|
|
167
|
+
name: "module-federation-dts-dev",
|
|
168
|
+
apply: "serve",
|
|
169
|
+
config(config) {
|
|
170
|
+
normalizedDevOptions = normalizeDevOptions(options.dev);
|
|
171
|
+
if (!normalizedDevOptions) return;
|
|
172
|
+
if (normalizedDevOptions.disableDynamicRemoteTypeHints) return;
|
|
173
|
+
ensureRuntimePlugin(options, DYNAMIC_HINTS_PLUGIN);
|
|
174
|
+
const define = config.define ? { ...config.define } : {};
|
|
175
|
+
if (!("FEDERATION_IPV4" in define)) define.FEDERATION_IPV4 = JSON.stringify(getIPv4());
|
|
176
|
+
config.define = define;
|
|
177
|
+
},
|
|
178
|
+
configResolved(config) {
|
|
179
|
+
resolvedConfig = config;
|
|
180
|
+
},
|
|
181
|
+
configureServer(server) {
|
|
182
|
+
if (!normalizedDevOptions || !resolvedConfig) return;
|
|
183
|
+
const devOptions = normalizedDevOptions;
|
|
184
|
+
if (devOptions.disableDynamicRemoteTypeHints && devOptions.disableHotTypesReload && devOptions.disableLiveReload) return;
|
|
185
|
+
if (!options.name) throw require_packageUtils.createModuleFederationError("name is required if you want to enable dev server!");
|
|
186
|
+
const outputDir = resolveOutputDir(resolvedConfig);
|
|
187
|
+
const dtsModuleFederationConfig = getDtsModuleFederationConfig(resolvedConfig.root);
|
|
188
|
+
const normalizedDtsOptions = normalizeDevDtsOptions(dtsModuleFederationConfig.dts, resolvedConfig.root);
|
|
189
|
+
if (typeof normalizedDtsOptions !== "object") return;
|
|
190
|
+
const normalizedGenerateTypes = (0, _module_federation_sdk.normalizeOptions)(Boolean(normalizedDtsOptions), { compileInChildProcess: true }, "mfOptions.dts.generateTypes")(normalizedDtsOptions.generateTypes);
|
|
191
|
+
const remote = normalizedGenerateTypes === false ? void 0 : {
|
|
192
|
+
implementation: normalizedDtsOptions.implementation,
|
|
193
|
+
context: resolvedConfig.root,
|
|
194
|
+
outputDir,
|
|
195
|
+
moduleFederationConfig: { ...dtsModuleFederationConfig },
|
|
196
|
+
hostRemoteTypesFolder: normalizedGenerateTypes.typesFolder || DEFAULT_PUBLIC_TYPES_FOLDER,
|
|
197
|
+
...normalizedGenerateTypes,
|
|
198
|
+
typesFolder: DEV_TYPES_FOLDER
|
|
199
|
+
};
|
|
200
|
+
if (remote) server.middlewares.use(createDevDtsAssetMiddleware(getDevDtsAssetPaths({
|
|
201
|
+
outputDir,
|
|
202
|
+
publicTypesFolder: remote.hostRemoteTypesFolder || DEFAULT_PUBLIC_TYPES_FOLDER,
|
|
203
|
+
root: resolvedConfig.root,
|
|
204
|
+
base: resolvedConfig.base
|
|
205
|
+
})));
|
|
206
|
+
if (remote && !remote.tsConfigPath && normalizedDtsOptions.tsConfigPath) remote.tsConfigPath = normalizedDtsOptions.tsConfigPath;
|
|
207
|
+
const normalizedConsumeTypes = (0, _module_federation_sdk.normalizeOptions)(Boolean(normalizedDtsOptions), { consumeAPITypes: true }, "mfOptions.dts.consumeTypes")(normalizedDtsOptions.consumeTypes);
|
|
208
|
+
const host = normalizedConsumeTypes === false ? void 0 : {
|
|
209
|
+
implementation: normalizedDtsOptions.implementation,
|
|
210
|
+
context: resolvedConfig.root,
|
|
211
|
+
moduleFederationConfig: dtsModuleFederationConfig,
|
|
212
|
+
typesFolder: normalizedConsumeTypes.typesFolder || "@mf-types",
|
|
213
|
+
abortOnError: false,
|
|
214
|
+
...normalizedConsumeTypes
|
|
215
|
+
};
|
|
216
|
+
const extraOptions = normalizedDtsOptions.extraOptions || {};
|
|
217
|
+
if (!remote && !host && devOptions.disableLiveReload) return;
|
|
218
|
+
const startDevWorker = async () => {
|
|
219
|
+
let remoteTypeUrls;
|
|
220
|
+
if (host) remoteTypeUrls = await new Promise((resolve) => {
|
|
221
|
+
(0, _module_federation_dts_plugin.consumeTypesAPI)({
|
|
222
|
+
host,
|
|
223
|
+
extraOptions,
|
|
224
|
+
displayErrorInTerminal: normalizedDtsOptions.displayErrorInTerminal
|
|
225
|
+
}, resolve);
|
|
226
|
+
});
|
|
227
|
+
devWorker = new DevWorker({
|
|
228
|
+
name: options.name,
|
|
229
|
+
remote,
|
|
230
|
+
host: host ? {
|
|
231
|
+
...host,
|
|
232
|
+
remoteTypeUrls
|
|
233
|
+
} : void 0,
|
|
234
|
+
extraOptions,
|
|
235
|
+
disableLiveReload: devOptions.disableLiveReload,
|
|
236
|
+
disableHotTypesReload: devOptions.disableHotTypesReload
|
|
237
|
+
});
|
|
238
|
+
const update = () => devWorker?.update();
|
|
239
|
+
server.watcher.on("change", update);
|
|
240
|
+
server.watcher.on("add", update);
|
|
241
|
+
server.watcher.on("unlink", update);
|
|
242
|
+
server.httpServer?.once("close", () => {
|
|
243
|
+
devWorker?.exit();
|
|
244
|
+
server.watcher.off("change", update);
|
|
245
|
+
server.watcher.off("add", update);
|
|
246
|
+
server.watcher.off("unlink", update);
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
startDevWorker().catch((error) => {
|
|
250
|
+
logDtsError(error, normalizedDtsOptions);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
}, {
|
|
254
|
+
name: "module-federation-dts-build",
|
|
255
|
+
apply: "build",
|
|
256
|
+
configResolved(config) {
|
|
257
|
+
resolvedConfig = config;
|
|
258
|
+
},
|
|
259
|
+
async generateBundle() {
|
|
260
|
+
if (hasGeneratedBundle) return;
|
|
261
|
+
hasGeneratedBundle = true;
|
|
262
|
+
if (!resolvedConfig) return;
|
|
263
|
+
let normalizedDtsOptions;
|
|
264
|
+
try {
|
|
265
|
+
normalizedDtsOptions = (0, _module_federation_dts_plugin.normalizeDtsOptions)(getDtsModuleFederationConfig(resolvedConfig.root), resolvedConfig.root);
|
|
266
|
+
} catch (error) {
|
|
267
|
+
logDtsError(error, options.dts);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (typeof normalizedDtsOptions !== "object") return;
|
|
271
|
+
const context = resolvedConfig.root;
|
|
272
|
+
const outputDir = resolveOutputDir(resolvedConfig);
|
|
273
|
+
let consumeOptions;
|
|
274
|
+
try {
|
|
275
|
+
consumeOptions = (0, _module_federation_dts_plugin.normalizeConsumeTypesOptions)({
|
|
276
|
+
context,
|
|
277
|
+
dtsOptions: normalizedDtsOptions,
|
|
278
|
+
pluginOptions: getDtsModuleFederationConfig(resolvedConfig.root)
|
|
279
|
+
});
|
|
280
|
+
} catch (error) {
|
|
281
|
+
logDtsError(error, normalizedDtsOptions);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (consumeOptions?.host?.typesOnBuild) try {
|
|
285
|
+
await (0, _module_federation_dts_plugin.consumeTypesAPI)(consumeOptions);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
logDtsError(error, normalizedDtsOptions);
|
|
288
|
+
}
|
|
289
|
+
let generateOptions;
|
|
290
|
+
try {
|
|
291
|
+
generateOptions = (0, _module_federation_dts_plugin.normalizeGenerateTypesOptions)({
|
|
292
|
+
context,
|
|
293
|
+
outputDir,
|
|
294
|
+
dtsOptions: normalizedDtsOptions,
|
|
295
|
+
pluginOptions: getDtsModuleFederationConfig(resolvedConfig.root)
|
|
296
|
+
});
|
|
297
|
+
} catch (error) {
|
|
298
|
+
logDtsError(error, normalizedDtsOptions);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
if (!generateOptions) return;
|
|
302
|
+
try {
|
|
303
|
+
await (0, _module_federation_dts_plugin.generateTypesAPI)({ dtsManagerOptions: generateOptions });
|
|
304
|
+
} catch (error) {
|
|
305
|
+
logDtsError(error, normalizedDtsOptions);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}];
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
exports.default = pluginDts;
|