@module-federation/vite 1.23.0 → 1.23.2
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 +36 -0
- package/lib/{dtsConstants-DEMpRfBz.js → dtsConstants-r5OfegUr.js} +2 -32
- package/lib/index.d.ts +17 -0
- package/lib/index.js +605 -414
- package/lib/logger-DOwfTlw5.js +366 -0
- package/lib/{pluginDts-VSLhAx6d.js → pluginDts-DnEX3cp_.js} +3 -3
- package/lib/{ssrEntryLoader-BvAkpCyi.js → ssrEntryLoader-DEH3eV1e.js} +238 -54
- package/lib/{ssrVmStrategy-CDbcwMnf.js → ssrVmStrategy-COMHOCN2.js} +69 -5
- package/lib/utils/ssrEntryLoader.js +1 -1
- package/package.json +4 -4
- package/lib/buildPaths-BoaQTkxt.js +0 -66
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
//#region src/utils/buildPaths.ts
|
|
2
|
+
/**
|
|
3
|
+
* Rebase an import path for a bootstrap file that moved from root into `dir`.
|
|
4
|
+
*
|
|
5
|
+
* When entryFileNames places entries in a subdirectory (e.g. `static/js/`),
|
|
6
|
+
* the bootstrap file moves there too. Paths that resolved from the HTML root
|
|
7
|
+
* must resolve from the new directory instead.
|
|
8
|
+
*
|
|
9
|
+
* Cases: `/static/js/hostInit.js` → `./hostInit.js` (strip dir prefix)
|
|
10
|
+
* `./src/main.tsx` → `../../src/main.tsx` (climb back up for each dir level)
|
|
11
|
+
* `https://cdn.example.com` → unchanged (absolute URL)
|
|
12
|
+
*/
|
|
13
|
+
function rebaseImport(importSrc, dir) {
|
|
14
|
+
if (!dir) return importSrc;
|
|
15
|
+
if (isAbsoluteUrl(importSrc)) return importSrc;
|
|
16
|
+
const normalizedDir = dir.replace(/^\/+|\/+$/g, "");
|
|
17
|
+
if (!normalizedDir) return importSrc;
|
|
18
|
+
const stripDirPrefix = (src, prefix) => {
|
|
19
|
+
if (src === prefix) return "";
|
|
20
|
+
if (src.startsWith(prefix + "/")) return src.slice(prefix.length);
|
|
21
|
+
};
|
|
22
|
+
const absoluteRemainder = stripDirPrefix(importSrc, "/" + normalizedDir);
|
|
23
|
+
if (absoluteRemainder !== void 0) {
|
|
24
|
+
const remainder = absoluteRemainder.replace(/^\/+/, "");
|
|
25
|
+
return remainder ? "./" + remainder : "./";
|
|
26
|
+
}
|
|
27
|
+
const relativeRemainder = stripDirPrefix(importSrc, normalizedDir);
|
|
28
|
+
if (relativeRemainder !== void 0) {
|
|
29
|
+
const remainder = relativeRemainder.replace(/^\/+/, "");
|
|
30
|
+
return remainder ? "./" + remainder : "./";
|
|
31
|
+
}
|
|
32
|
+
const upLevels = normalizedDir.split("/").filter(Boolean).length;
|
|
33
|
+
const prefix = upLevels > 0 ? "../".repeat(upLevels) : "./";
|
|
34
|
+
if (importSrc.startsWith("./")) return prefix + importSrc.slice(2);
|
|
35
|
+
if (importSrc.startsWith("/")) return prefix + importSrc.slice(1);
|
|
36
|
+
return prefix + importSrc;
|
|
37
|
+
}
|
|
38
|
+
function normalizePathForImport(path) {
|
|
39
|
+
return path.replace(/\\/g, "/");
|
|
40
|
+
}
|
|
41
|
+
const EXTERNAL_URL_RE = /^(?:[a-z]+:|\/\/)/i;
|
|
42
|
+
function isAbsoluteUrl(src) {
|
|
43
|
+
if (/^[a-z]:[\\/]/i.test(src)) return false;
|
|
44
|
+
return EXTERNAL_URL_RE.test(src);
|
|
45
|
+
}
|
|
46
|
+
const HASH_PLACEHOLDER_RE = /(?:[._-]?\[hash(?::\d+)?\])/g;
|
|
47
|
+
function hasFileExtension(fileName) {
|
|
48
|
+
return fileName.slice(Math.max(fileName.lastIndexOf("/"), fileName.lastIndexOf("\\")) + 1).lastIndexOf(".") > 0;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Resolve a bundler `filename` template that still contains `[hash]` placeholders
|
|
52
|
+
* into the concrete, stable file name Module Federation serves.
|
|
53
|
+
*
|
|
54
|
+
* The federation entries are emitted by us rather than hashed by the bundler, so
|
|
55
|
+
* the placeholder is dropped instead of being substituted. When stripping it also
|
|
56
|
+
* removes the extension (`mf-[hash:8]` → `mf`), `.js` is appended so the result
|
|
57
|
+
* stays a loadable module. The extension check deliberately looks at the basename
|
|
58
|
+
* only — a dotted directory (`assets/v1.2/entry`) must not be mistaken for one.
|
|
59
|
+
*/
|
|
60
|
+
function resolveHashPlaceholderFileName(fileName) {
|
|
61
|
+
if (!fileName.includes("[hash")) return fileName;
|
|
62
|
+
const normalized = fileName.replace(HASH_PLACEHOLDER_RE, "");
|
|
63
|
+
return hasFileExtension(normalized) ? normalized : `${normalized}.js`;
|
|
64
|
+
}
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/utils/codeRewriter.ts
|
|
67
|
+
const BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
68
|
+
var CodeRewriter = class {
|
|
69
|
+
original;
|
|
70
|
+
replacements = [];
|
|
71
|
+
constructor(original) {
|
|
72
|
+
this.original = original;
|
|
73
|
+
}
|
|
74
|
+
overwrite(start, end, content) {
|
|
75
|
+
if (start < 0 || end < start || end > this.original.length) throw new Error(`Invalid overwrite range: ${start}-${end}`);
|
|
76
|
+
this.replacements.push({
|
|
77
|
+
start,
|
|
78
|
+
end,
|
|
79
|
+
content
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
toString() {
|
|
83
|
+
return applyReplacements(this.original, this.getSortedReplacements()).code;
|
|
84
|
+
}
|
|
85
|
+
generateMap(source = "") {
|
|
86
|
+
const { code, replacements } = applyReplacements(this.original, this.getSortedReplacements());
|
|
87
|
+
return {
|
|
88
|
+
version: 3,
|
|
89
|
+
sources: [source],
|
|
90
|
+
sourcesContent: [this.original],
|
|
91
|
+
names: [],
|
|
92
|
+
mappings: generateLineMappings(code, this.original, replacements)
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
getSortedReplacements() {
|
|
96
|
+
return [...this.replacements].sort((a, b) => a.start - b.start || a.end - b.end);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
function createSourceMap(code, source = "") {
|
|
100
|
+
return {
|
|
101
|
+
version: 3,
|
|
102
|
+
sources: [source],
|
|
103
|
+
sourcesContent: [code],
|
|
104
|
+
names: [],
|
|
105
|
+
mappings: generateLineMappings(code, code, [])
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function applyReplacements(original, replacements) {
|
|
109
|
+
let code = "";
|
|
110
|
+
let cursor = 0;
|
|
111
|
+
let delta = 0;
|
|
112
|
+
const applied = [];
|
|
113
|
+
for (const replacement of replacements) {
|
|
114
|
+
if (replacement.start < cursor) throw new Error("Overlapping overwrite ranges are not supported");
|
|
115
|
+
code += original.slice(cursor, replacement.start);
|
|
116
|
+
const generatedStart = replacement.start + delta;
|
|
117
|
+
code += replacement.content;
|
|
118
|
+
const generatedEnd = generatedStart + replacement.content.length;
|
|
119
|
+
applied.push({
|
|
120
|
+
...replacement,
|
|
121
|
+
generatedStart,
|
|
122
|
+
generatedEnd
|
|
123
|
+
});
|
|
124
|
+
cursor = replacement.end;
|
|
125
|
+
delta += replacement.content.length - (replacement.end - replacement.start);
|
|
126
|
+
}
|
|
127
|
+
code += original.slice(cursor);
|
|
128
|
+
return {
|
|
129
|
+
code,
|
|
130
|
+
replacements: applied
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function generateLineMappings(generated, original, replacements) {
|
|
134
|
+
const generatedLineStarts = getLineStarts(generated);
|
|
135
|
+
const originalLineStarts = getLineStarts(original);
|
|
136
|
+
let previousOriginalLine = 0;
|
|
137
|
+
let previousOriginalColumn = 0;
|
|
138
|
+
let mappings = "";
|
|
139
|
+
generatedLineStarts.forEach((generatedOffset, lineIndex) => {
|
|
140
|
+
if (lineIndex > 0) mappings += ";";
|
|
141
|
+
const originalOffset = generatedOffsetToOriginalOffset(generatedOffset, replacements);
|
|
142
|
+
const originalLine = findLine(originalLineStarts, originalOffset);
|
|
143
|
+
const originalColumn = originalOffset - originalLineStarts[originalLine];
|
|
144
|
+
mappings += encodeSegment([
|
|
145
|
+
0,
|
|
146
|
+
0,
|
|
147
|
+
originalLine - previousOriginalLine,
|
|
148
|
+
originalColumn - previousOriginalColumn
|
|
149
|
+
]);
|
|
150
|
+
previousOriginalLine = originalLine;
|
|
151
|
+
previousOriginalColumn = originalColumn;
|
|
152
|
+
});
|
|
153
|
+
return mappings;
|
|
154
|
+
}
|
|
155
|
+
function generatedOffsetToOriginalOffset(offset, replacements) {
|
|
156
|
+
let delta = 0;
|
|
157
|
+
for (const replacement of replacements) {
|
|
158
|
+
if (offset < replacement.generatedStart) break;
|
|
159
|
+
if (offset < replacement.generatedEnd) return replacement.start;
|
|
160
|
+
delta += replacement.content.length - (replacement.end - replacement.start);
|
|
161
|
+
}
|
|
162
|
+
return offset - delta;
|
|
163
|
+
}
|
|
164
|
+
function getLineStarts(code) {
|
|
165
|
+
const starts = [0];
|
|
166
|
+
for (let i = 0; i < code.length; i++) if (code.charCodeAt(i) === 10) starts.push(i + 1);
|
|
167
|
+
return starts;
|
|
168
|
+
}
|
|
169
|
+
function findLine(lineStarts, offset) {
|
|
170
|
+
let low = 0;
|
|
171
|
+
let high = lineStarts.length - 1;
|
|
172
|
+
while (low <= high) {
|
|
173
|
+
const mid = low + high >> 1;
|
|
174
|
+
if (lineStarts[mid] <= offset) low = mid + 1;
|
|
175
|
+
else high = mid - 1;
|
|
176
|
+
}
|
|
177
|
+
return Math.max(0, high);
|
|
178
|
+
}
|
|
179
|
+
function encodeSegment(values) {
|
|
180
|
+
return values.map(encodeVlq).join("");
|
|
181
|
+
}
|
|
182
|
+
function encodeVlq(value) {
|
|
183
|
+
let vlq = value < 0 ? (-value << 1) + 1 : value << 1;
|
|
184
|
+
let encoded = "";
|
|
185
|
+
do {
|
|
186
|
+
let digit = vlq & 31;
|
|
187
|
+
vlq >>>= 5;
|
|
188
|
+
if (vlq > 0) digit |= 32;
|
|
189
|
+
encoded += BASE64_CHARS[digit];
|
|
190
|
+
} while (vlq > 0);
|
|
191
|
+
return encoded;
|
|
192
|
+
}
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/utils/codePositionMap.ts
|
|
195
|
+
const REGEX_PREFIX_KEYWORDS = /* @__PURE__ */ new Set([
|
|
196
|
+
"await",
|
|
197
|
+
"case",
|
|
198
|
+
"delete",
|
|
199
|
+
"in",
|
|
200
|
+
"instanceof",
|
|
201
|
+
"new",
|
|
202
|
+
"return",
|
|
203
|
+
"throw",
|
|
204
|
+
"typeof",
|
|
205
|
+
"void",
|
|
206
|
+
"yield"
|
|
207
|
+
]);
|
|
208
|
+
function isJsxClosingTagSlash(code, slashIndex) {
|
|
209
|
+
if (code[slashIndex - 1] !== "<") return false;
|
|
210
|
+
let cursor = slashIndex + 1;
|
|
211
|
+
while (/\s/.test(code[cursor] || "")) cursor++;
|
|
212
|
+
if (code[cursor] === ">") return true;
|
|
213
|
+
const tagStart = cursor;
|
|
214
|
+
while (/[-:.$_\u200C\u200D\p{ID_Continue}]/u.test(code[cursor] || "")) cursor++;
|
|
215
|
+
if (cursor === tagStart) return false;
|
|
216
|
+
while (/\s/.test(code[cursor] || "")) cursor++;
|
|
217
|
+
return code[cursor] === ">";
|
|
218
|
+
}
|
|
219
|
+
/** Mark comments, string/template literals, and regular expressions as non-code. */
|
|
220
|
+
function createCodePositionMap(code, onComment) {
|
|
221
|
+
const positions = Array(code.length).fill(true);
|
|
222
|
+
const mask = (start, end) => {
|
|
223
|
+
for (let index = start; index < end; index++) positions[index] = false;
|
|
224
|
+
};
|
|
225
|
+
let canStartRegex = true;
|
|
226
|
+
for (let index = 0; index < code.length;) {
|
|
227
|
+
const char = code[index];
|
|
228
|
+
const next = code[index + 1];
|
|
229
|
+
if (/\s/.test(char)) {
|
|
230
|
+
index++;
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (char === "/" && next === "/") {
|
|
234
|
+
const start = index;
|
|
235
|
+
index += 2;
|
|
236
|
+
while (index < code.length && code[index] !== "\n" && code[index] !== "\r") index++;
|
|
237
|
+
mask(start, index);
|
|
238
|
+
onComment?.(start, index);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (char === "/" && next === "*") {
|
|
242
|
+
const start = index;
|
|
243
|
+
index += 2;
|
|
244
|
+
while (index < code.length && !(code[index] === "*" && code[index + 1] === "/")) index++;
|
|
245
|
+
index = Math.min(code.length, index + 2);
|
|
246
|
+
mask(start, index);
|
|
247
|
+
onComment?.(start, index);
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (char === "\"" || char === "'" || char === "`") {
|
|
251
|
+
const quote = char;
|
|
252
|
+
const start = index++;
|
|
253
|
+
while (index < code.length) {
|
|
254
|
+
if (code[index] === "\\") {
|
|
255
|
+
index += 2;
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (code[index] === quote) {
|
|
259
|
+
index++;
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
index++;
|
|
263
|
+
}
|
|
264
|
+
mask(start, index);
|
|
265
|
+
canStartRegex = false;
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const closesJsxTag = isJsxClosingTagSlash(code, index);
|
|
269
|
+
if (char === "/" && canStartRegex && !closesJsxTag) {
|
|
270
|
+
const start = index;
|
|
271
|
+
let cursor = index + 1;
|
|
272
|
+
let escaped = false;
|
|
273
|
+
let inCharacterClass = false;
|
|
274
|
+
let closed = false;
|
|
275
|
+
for (; cursor < code.length; cursor++) {
|
|
276
|
+
const regexChar = code[cursor];
|
|
277
|
+
if (regexChar === "\n" || regexChar === "\r") break;
|
|
278
|
+
if (escaped) {
|
|
279
|
+
escaped = false;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (regexChar === "\\") {
|
|
283
|
+
escaped = true;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (regexChar === "[") {
|
|
287
|
+
inCharacterClass = true;
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (regexChar === "]" && inCharacterClass) {
|
|
291
|
+
inCharacterClass = false;
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
if (regexChar === "/" && !inCharacterClass) {
|
|
295
|
+
cursor++;
|
|
296
|
+
while (/[$_\p{ID_Continue}]/u.test(code[cursor] || "")) cursor++;
|
|
297
|
+
closed = true;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (closed) {
|
|
302
|
+
mask(start, cursor);
|
|
303
|
+
index = cursor;
|
|
304
|
+
canStartRegex = false;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (/[$_\p{ID_Start}]/u.test(char)) {
|
|
309
|
+
const start = index++;
|
|
310
|
+
while (/[$_\u200C\u200D\p{ID_Continue}]/u.test(code[index] || "")) index++;
|
|
311
|
+
canStartRegex = REGEX_PREFIX_KEYWORDS.has(code.slice(start, index));
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (/\d/.test(char)) {
|
|
315
|
+
index++;
|
|
316
|
+
while (/[\w.]/.test(code[index] || "")) index++;
|
|
317
|
+
canStartRegex = false;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if ((char === "+" || char === "-") && next === char) {
|
|
321
|
+
index += 2;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (char === "!" && next !== "=") {
|
|
325
|
+
index++;
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
if (char === ")" || char === "]" || char === "}") canStartRegex = false;
|
|
329
|
+
else if (char !== ".") canStartRegex = true;
|
|
330
|
+
index++;
|
|
331
|
+
}
|
|
332
|
+
return positions;
|
|
333
|
+
}
|
|
334
|
+
//#endregion
|
|
335
|
+
//#region src/utils/logger.ts
|
|
336
|
+
const MODULE_FEDERATION_LOG_PREFIX = "[Module Federation]";
|
|
337
|
+
function formatModuleFederationMessage(message) {
|
|
338
|
+
return `${MODULE_FEDERATION_LOG_PREFIX} ${message}`;
|
|
339
|
+
}
|
|
340
|
+
function createModuleFederationError(message) {
|
|
341
|
+
return new Error(formatModuleFederationMessage(message));
|
|
342
|
+
}
|
|
343
|
+
function toConsoleArgs(message, rest = []) {
|
|
344
|
+
if (typeof message === "string") return [formatModuleFederationMessage(message), ...rest];
|
|
345
|
+
if (message === void 0) return [MODULE_FEDERATION_LOG_PREFIX, ...rest];
|
|
346
|
+
return [
|
|
347
|
+
MODULE_FEDERATION_LOG_PREFIX,
|
|
348
|
+
message,
|
|
349
|
+
...rest
|
|
350
|
+
];
|
|
351
|
+
}
|
|
352
|
+
const moduleFederationConsole = {
|
|
353
|
+
log(message, ...rest) {
|
|
354
|
+
console.log(...toConsoleArgs(message, rest));
|
|
355
|
+
},
|
|
356
|
+
warn(message, ...rest) {
|
|
357
|
+
console.warn(...toConsoleArgs(message, rest));
|
|
358
|
+
},
|
|
359
|
+
error(message, ...rest) {
|
|
360
|
+
console.error(...toConsoleArgs(message, rest));
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
const mfWarn = moduleFederationConsole.warn;
|
|
364
|
+
const mfError = moduleFederationConsole.error;
|
|
365
|
+
//#endregion
|
|
366
|
+
export { CodeRewriter as a, isAbsoluteUrl as c, resolveHashPlaceholderFileName as d, createCodePositionMap as i, normalizePathForImport as l, mfError as n, createSourceMap as o, mfWarn as r, EXTERNAL_URL_RE as s, createModuleFederationError as t, rebaseImport as u };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { l as normalizePathForImport, n as mfError, t as createModuleFederationError } from "./logger-DOwfTlw5.js";
|
|
2
|
+
import { g as resolveImportPath, u as hasPackageDependency } from "./dtsConstants-r5OfegUr.js";
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
import * as path$1 from "node:path";
|
|
5
|
-
import os from "os";
|
|
6
5
|
import { normalizeOptions } from "@module-federation/sdk";
|
|
6
|
+
import os from "os";
|
|
7
7
|
import { consumeTypesAPI, generateTypesAPI, isTSProject, normalizeConsumeTypesOptions, normalizeDtsOptions, normalizeGenerateTypesOptions } from "@module-federation/dts-plugin";
|
|
8
8
|
import { rpc } from "@module-federation/dts-plugin/core";
|
|
9
9
|
//#region src/plugins/pluginDts.ts
|