@vercel/microfrontends 0.12.1 → 0.14.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 +3 -3
- package/dist/bin/cli.cjs +241 -176
- package/dist/config/client.d.ts +1 -1
- package/dist/config.cjs.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.js.map +1 -1
- package/dist/{index-83133f2d.d.ts → index-bf67a461.d.ts} +3 -10
- package/dist/next/client.cjs +1 -1
- package/dist/next/client.cjs.map +1 -1
- package/dist/next/client.js +1 -1
- package/dist/next/client.js.map +1 -1
- package/dist/next/endpoints.cjs.map +1 -1
- package/dist/next/endpoints.d.ts +1 -1
- package/dist/next/endpoints.js.map +1 -1
- package/dist/next/middleware.cjs +0 -29
- package/dist/next/middleware.cjs.map +1 -1
- package/dist/next/middleware.js +0 -29
- package/dist/next/middleware.js.map +1 -1
- package/dist/{types-a995174e.d.ts → types-a29d224a.d.ts} +1 -7
- package/dist/{types-15b7f215.d.ts → types-cfe3308b.d.ts} +1 -1
- package/dist/types-fc30696d.d.ts +11 -0
- package/dist/utils/mfe-port.cjs +2704 -0
- package/dist/utils/mfe-port.cjs.map +1 -0
- package/dist/utils/mfe-port.d.ts +8 -0
- package/dist/utils/mfe-port.js +2669 -0
- package/dist/utils/mfe-port.js.map +1 -0
- package/dist/v2/config.cjs +0 -14
- package/dist/v2/config.cjs.map +1 -1
- package/dist/v2/config.d.ts +4 -3
- package/dist/v2/config.js +0 -14
- package/dist/v2/config.js.map +1 -1
- package/dist/v2/microfrontends/server.cjs +68 -54
- package/dist/v2/microfrontends/server.cjs.map +1 -1
- package/dist/v2/microfrontends/server.d.ts +6 -5
- package/dist/v2/microfrontends/server.js +65 -51
- package/dist/v2/microfrontends/server.js.map +1 -1
- package/dist/v2/microfrontends.cjs +0 -14
- package/dist/v2/microfrontends.cjs.map +1 -1
- package/dist/v2/microfrontends.d.ts +4 -3
- package/dist/v2/microfrontends.js +0 -14
- package/dist/v2/microfrontends.js.map +1 -1
- package/dist/v2/next/client.cjs +1 -1
- package/dist/v2/next/client.cjs.map +1 -1
- package/dist/v2/next/client.js +1 -1
- package/dist/v2/next/client.js.map +1 -1
- package/dist/v2/next/config.cjs +116 -65
- package/dist/v2/next/config.cjs.map +1 -1
- package/dist/v2/next/config.js +113 -62
- package/dist/v2/next/config.js.map +1 -1
- package/dist/v2/next/endpoints.cjs.map +1 -1
- package/dist/v2/next/endpoints.d.ts +13 -2
- package/dist/v2/next/endpoints.js.map +1 -1
- package/dist/v2/next/middleware.cjs +20 -59
- package/dist/v2/next/middleware.cjs.map +1 -1
- package/dist/v2/next/middleware.d.ts +7 -2
- package/dist/v2/next/middleware.js +20 -59
- package/dist/v2/next/middleware.js.map +1 -1
- package/dist/v2/next/testing.cjs +992 -0
- package/dist/v2/next/testing.cjs.map +1 -0
- package/dist/v2/next/testing.d.ts +55 -0
- package/dist/v2/next/testing.js +961 -0
- package/dist/v2/next/testing.js.map +1 -0
- package/dist/v2/overrides.d.ts +3 -3
- package/dist/v2/routing.cjs +19 -0
- package/dist/v2/routing.cjs.map +1 -0
- package/dist/v2/routing.d.ts +26 -0
- package/dist/v2/routing.js +1 -0
- package/dist/v2/routing.js.map +1 -0
- package/dist/v2/schema.cjs.map +1 -1
- package/dist/v2/schema.d.ts +1 -1
- package/dist/validation.cjs +0 -4
- package/dist/validation.cjs.map +1 -1
- package/dist/validation.d.ts +0 -6
- package/dist/validation.js +0 -4
- package/dist/validation.js.map +1 -1
- package/package.json +25 -3
- package/schema/schema-v2.json +0 -4
|
@@ -0,0 +1,2669 @@
|
|
|
1
|
+
// src/utils/mfe-port.ts
|
|
2
|
+
import path6 from "node:path";
|
|
3
|
+
import fs8 from "node:fs";
|
|
4
|
+
|
|
5
|
+
// src/config/types.ts
|
|
6
|
+
var isDefaultApplicationConfig = (app) => app.default && typeof app.routing === "undefined";
|
|
7
|
+
|
|
8
|
+
// src/config/microfrontend-config.ts
|
|
9
|
+
import fs2 from "node:fs";
|
|
10
|
+
|
|
11
|
+
// src/config-v2/microfrontends/server/utils/get-output-file-path.ts
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
|
|
14
|
+
// src/config-v2/microfrontends/server/constants.ts
|
|
15
|
+
var MFE_CONFIG_DEFAULT_FILE_PATH = "microfrontends";
|
|
16
|
+
var MFE_CONFIG_DEFAULT_FILE_NAME = "microfrontends.json";
|
|
17
|
+
|
|
18
|
+
// src/utils/is-vercel.ts
|
|
19
|
+
function isVercel() {
|
|
20
|
+
return process.env.VERCEL === "1";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/config-v2/microfrontends/server/utils/get-output-file-path.ts
|
|
24
|
+
function getOutputFilePath() {
|
|
25
|
+
if (isVercel()) {
|
|
26
|
+
return path.join(
|
|
27
|
+
".vercel",
|
|
28
|
+
MFE_CONFIG_DEFAULT_FILE_PATH,
|
|
29
|
+
MFE_CONFIG_DEFAULT_FILE_NAME
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
return path.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/config/errors.ts
|
|
36
|
+
var MicrofrontendError = class extends Error {
|
|
37
|
+
constructor(message, opts) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "MicrofrontendsError";
|
|
40
|
+
this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/microfrontends";
|
|
41
|
+
this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
|
|
42
|
+
this.subtype = opts == null ? void 0 : opts.subtype;
|
|
43
|
+
Error.captureStackTrace(this, MicrofrontendError);
|
|
44
|
+
}
|
|
45
|
+
isKnown() {
|
|
46
|
+
return this.type !== "unknown";
|
|
47
|
+
}
|
|
48
|
+
isUnknown() {
|
|
49
|
+
return !this.isKnown();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Converts an error to a MicrofrontendsError.
|
|
53
|
+
* @param original - The original error to convert.
|
|
54
|
+
* @returns The converted MicrofrontendsError.
|
|
55
|
+
*/
|
|
56
|
+
static convert(original, opts) {
|
|
57
|
+
if (opts == null ? void 0 : opts.fileName) {
|
|
58
|
+
const err = MicrofrontendError.convertFSError(original, opts.fileName);
|
|
59
|
+
if (err) {
|
|
60
|
+
return err;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (original.message.includes(
|
|
64
|
+
"Code generation from strings disallowed for this context"
|
|
65
|
+
)) {
|
|
66
|
+
return new MicrofrontendError(original.message, {
|
|
67
|
+
type: "config",
|
|
68
|
+
subtype: "unsupported_validation_env",
|
|
69
|
+
source: "ajv"
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return new MicrofrontendError(original.message);
|
|
73
|
+
}
|
|
74
|
+
static convertFSError(original, fileName) {
|
|
75
|
+
if (original instanceof Error && "code" in original) {
|
|
76
|
+
if (original.code === "ENOENT") {
|
|
77
|
+
return new MicrofrontendError(`Could not find "${fileName}"`, {
|
|
78
|
+
type: "config",
|
|
79
|
+
subtype: "unable_to_read_file",
|
|
80
|
+
source: "fs"
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (original.code === "EACCES") {
|
|
84
|
+
return new MicrofrontendError(
|
|
85
|
+
`Permission denied while accessing "${fileName}"`,
|
|
86
|
+
{
|
|
87
|
+
type: "config",
|
|
88
|
+
subtype: "invalid_permissions",
|
|
89
|
+
source: "fs"
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (original instanceof SyntaxError) {
|
|
95
|
+
return new MicrofrontendError(
|
|
96
|
+
`Failed to parse "${fileName}": Invalid JSON format.`,
|
|
97
|
+
{
|
|
98
|
+
type: "config",
|
|
99
|
+
subtype: "invalid_syntax",
|
|
100
|
+
source: "fs"
|
|
101
|
+
}
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Handles an unknown error and returns a MicrofrontendsError instance.
|
|
108
|
+
* @param err - The error to handle.
|
|
109
|
+
* @returns A MicrofrontendsError instance.
|
|
110
|
+
*/
|
|
111
|
+
static handle(err, opts) {
|
|
112
|
+
if (err instanceof MicrofrontendError) {
|
|
113
|
+
return err;
|
|
114
|
+
}
|
|
115
|
+
if (err instanceof Error) {
|
|
116
|
+
return MicrofrontendError.convert(err, opts);
|
|
117
|
+
}
|
|
118
|
+
if (typeof err === "object" && err !== null) {
|
|
119
|
+
if ("message" in err && typeof err.message === "string") {
|
|
120
|
+
return MicrofrontendError.convert(new Error(err.message), opts);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return new MicrofrontendError("An unknown error occurred");
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/routing/url.ts
|
|
128
|
+
function buildUrlSafeString(givenOpts = {}) {
|
|
129
|
+
const options = {
|
|
130
|
+
joinString: "-",
|
|
131
|
+
lowercaseOnly: true,
|
|
132
|
+
maxLen: 100,
|
|
133
|
+
regexRemovePattern: /(?:(?!(?:[a-z0-9])).)/gi,
|
|
134
|
+
trimWhitespace: true,
|
|
135
|
+
...givenOpts
|
|
136
|
+
};
|
|
137
|
+
return {
|
|
138
|
+
generate: (...args) => {
|
|
139
|
+
const reJoinString = new RegExp(`${options.joinString}+`, "g");
|
|
140
|
+
let tag;
|
|
141
|
+
if (args.length === 0) {
|
|
142
|
+
throw new Error("generate method must be passed at least one argument");
|
|
143
|
+
}
|
|
144
|
+
for (let i = 0; i < args.length; i++) {
|
|
145
|
+
const arg = args[i];
|
|
146
|
+
if (typeof arg !== "string")
|
|
147
|
+
throw new Error("all supplied arguments must be Strings");
|
|
148
|
+
if (options.trimWhitespace) {
|
|
149
|
+
args[i] = arg.trim();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
tag = args.join(options.joinString);
|
|
153
|
+
tag = tag.replace(/\s/g, options.joinString);
|
|
154
|
+
if (options.lowercaseOnly)
|
|
155
|
+
tag = tag.toLowerCase();
|
|
156
|
+
tag = tag.replace(options.regexRemovePattern, (match) => {
|
|
157
|
+
if (match === options.joinString)
|
|
158
|
+
return match;
|
|
159
|
+
return "";
|
|
160
|
+
});
|
|
161
|
+
if (tag.length > options.maxLen)
|
|
162
|
+
tag = tag.substring(0, options.maxLen);
|
|
163
|
+
tag = tag.replace(reJoinString, options.joinString);
|
|
164
|
+
return tag;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
var urlSafeString = buildUrlSafeString().generate;
|
|
169
|
+
function makeUrlSafe(name) {
|
|
170
|
+
return urlSafeString(name.replace(/\//g, "-")).replace(/^-*/g, "").replace(/-*$/g, "");
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/config/overrides/config.ts
|
|
174
|
+
var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
|
|
175
|
+
var _Overrides = class {
|
|
176
|
+
constructor(config) {
|
|
177
|
+
this.config = config;
|
|
178
|
+
}
|
|
179
|
+
static getAppEnvOverrideCookieName(zone) {
|
|
180
|
+
return `${_Overrides.overrideEnvCookiePrefix}${zone}`;
|
|
181
|
+
}
|
|
182
|
+
static isOverrideCookie(cookie) {
|
|
183
|
+
var _a;
|
|
184
|
+
return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
|
|
185
|
+
}
|
|
186
|
+
static getOverrideFromCookie(cookie) {
|
|
187
|
+
if (!_Overrides.isOverrideCookie(cookie) || !cookie.value)
|
|
188
|
+
return;
|
|
189
|
+
return {
|
|
190
|
+
zone: cookie.name.replace(_Overrides.overrideEnvCookiePrefix, ""),
|
|
191
|
+
host: cookie.value
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
static parseOverrides(cookies) {
|
|
195
|
+
const overridesConfig = { applications: {} };
|
|
196
|
+
cookies.forEach((cookie) => {
|
|
197
|
+
const override = _Overrides.getOverrideFromCookie(cookie);
|
|
198
|
+
if (!override)
|
|
199
|
+
return;
|
|
200
|
+
overridesConfig.applications[override.zone] = {
|
|
201
|
+
environment: { host: override.host }
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
return overridesConfig;
|
|
205
|
+
}
|
|
206
|
+
static validOverrideDomainsForZone(microfrontendConfig, zone) {
|
|
207
|
+
var _a, _b, _c, _d, _e;
|
|
208
|
+
const projectName = (_a = microfrontendConfig.getZone(zone).vercel) == null ? void 0 : _a.projectName;
|
|
209
|
+
if (!projectName) {
|
|
210
|
+
return [microfrontendConfig.getZone(zone).production.host];
|
|
211
|
+
}
|
|
212
|
+
const parsedProjectName = makeUrlSafe(projectName);
|
|
213
|
+
const previewDeploymentSuffix = (_c = (_b = microfrontendConfig.options) == null ? void 0 : _b.vercel) == null ? void 0 : _c.previewDeploymentSuffix;
|
|
214
|
+
const teamSlug = (_e = (_d = microfrontendConfig.options) == null ? void 0 : _d.vercel) == null ? void 0 : _e.teamSlug;
|
|
215
|
+
if (!teamSlug && !previewDeploymentSuffix) {
|
|
216
|
+
return [microfrontendConfig.getZone(zone).production.host];
|
|
217
|
+
}
|
|
218
|
+
const suffix = previewDeploymentSuffix ? `.${previewDeploymentSuffix}` : `-${teamSlug}.vercel.app`;
|
|
219
|
+
return [
|
|
220
|
+
`${parsedProjectName}-git-([a-zA-Z0-9-]+)${suffix}`,
|
|
221
|
+
microfrontendConfig.getZone(zone).production.host
|
|
222
|
+
];
|
|
223
|
+
}
|
|
224
|
+
static validateOverrideDomain(microfrontendConfig, zone, domain) {
|
|
225
|
+
return new RegExp(
|
|
226
|
+
`^${_Overrides.validOverrideDomainsForZone(microfrontendConfig, zone).join(
|
|
227
|
+
"|"
|
|
228
|
+
)}$`
|
|
229
|
+
).test(domain);
|
|
230
|
+
}
|
|
231
|
+
serialize() {
|
|
232
|
+
return this.config;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var Overrides = _Overrides;
|
|
236
|
+
Overrides.overrideEnvCookiePrefix = `${OVERRIDES_COOKIE_PREFIX}:env:`;
|
|
237
|
+
|
|
238
|
+
// src/config/common/host.ts
|
|
239
|
+
var Host = class {
|
|
240
|
+
constructor({ protocol, host, port }) {
|
|
241
|
+
this.protocol = protocol || "https";
|
|
242
|
+
this.host = host;
|
|
243
|
+
this.port = Host.getPort({ port, protocol: this.protocol });
|
|
244
|
+
this.serialized = {
|
|
245
|
+
protocol,
|
|
246
|
+
host,
|
|
247
|
+
...port ? { port } : void 0
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
isLocal() {
|
|
251
|
+
return this.host === "localhost" || this.host === "127.0.0.1";
|
|
252
|
+
}
|
|
253
|
+
static getPort({
|
|
254
|
+
protocol,
|
|
255
|
+
port
|
|
256
|
+
}) {
|
|
257
|
+
if (!port) {
|
|
258
|
+
if (protocol === "http") {
|
|
259
|
+
return 80;
|
|
260
|
+
}
|
|
261
|
+
return 443;
|
|
262
|
+
}
|
|
263
|
+
return port;
|
|
264
|
+
}
|
|
265
|
+
isDefaultPort() {
|
|
266
|
+
return this.port === Host.getPort({ protocol: this.protocol });
|
|
267
|
+
}
|
|
268
|
+
toString(opts = {}) {
|
|
269
|
+
const url = this.toUrl(opts);
|
|
270
|
+
return url.toString().replace(/\/$/, "");
|
|
271
|
+
}
|
|
272
|
+
toUrl(opts = {}) {
|
|
273
|
+
const { includeDefaultPort } = opts;
|
|
274
|
+
const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
|
|
275
|
+
return new URL(url);
|
|
276
|
+
}
|
|
277
|
+
serialize() {
|
|
278
|
+
return this.serialized;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
// src/config/common/application.ts
|
|
283
|
+
var Application = class {
|
|
284
|
+
constructor(name, {
|
|
285
|
+
app,
|
|
286
|
+
overrides
|
|
287
|
+
}) {
|
|
288
|
+
Application.validate(name, app);
|
|
289
|
+
this.name = name;
|
|
290
|
+
this.default = app.default;
|
|
291
|
+
this.routing = app.routing;
|
|
292
|
+
this.development = {
|
|
293
|
+
local: new Host(app.development.local),
|
|
294
|
+
fallback: app.development.fallback ? new Host(app.development.fallback) : void 0
|
|
295
|
+
};
|
|
296
|
+
this.production = new Host(app.production);
|
|
297
|
+
this.vercel = app.vercel;
|
|
298
|
+
this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
|
|
299
|
+
environment: new Host(overrides.environment)
|
|
300
|
+
} : void 0;
|
|
301
|
+
}
|
|
302
|
+
isDefault() {
|
|
303
|
+
return this.default;
|
|
304
|
+
}
|
|
305
|
+
static validate(name, app) {
|
|
306
|
+
var _a, _b, _c, _d, _e;
|
|
307
|
+
if (((_b = (_a = app.routing) == null ? void 0 : _a.assetPrefix) == null ? void 0 : _b.startsWith("/")) || ((_d = (_c = app.routing) == null ? void 0 : _c.assetPrefix) == null ? void 0 : _d.endsWith("/"))) {
|
|
308
|
+
throw new MicrofrontendError(
|
|
309
|
+
`Invalid assetPrefix for application "${name}". Must not start or end with a slash.`,
|
|
310
|
+
{ type: "zone", subtype: "invalid_asset_prefix" }
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
for (const group of ((_e = app.routing) == null ? void 0 : _e.matches) ?? []) {
|
|
314
|
+
for (const p of group.paths) {
|
|
315
|
+
if (p === "/") {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (p.endsWith("/")) {
|
|
319
|
+
throw new MicrofrontendError(
|
|
320
|
+
`Invalid path for application "${name}". ${p} must not end with a slash.`,
|
|
321
|
+
{ type: "zone", subtype: "invalid_path" }
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
if (!p.startsWith("/")) {
|
|
325
|
+
throw new MicrofrontendError(
|
|
326
|
+
`Invalid path for application "${name}". ${p} must start with a slash.`,
|
|
327
|
+
{ type: "zone", subtype: "invalid_path" }
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
serialize() {
|
|
334
|
+
var _a, _b;
|
|
335
|
+
if (this.routing === void 0 || this.default) {
|
|
336
|
+
return {
|
|
337
|
+
default: true,
|
|
338
|
+
development: {
|
|
339
|
+
local: this.development.local.serialize(),
|
|
340
|
+
fallback: (_a = this.development.fallback) == null ? void 0 : _a.serialize()
|
|
341
|
+
},
|
|
342
|
+
production: this.production.serialize(),
|
|
343
|
+
vercel: this.vercel
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
default: false,
|
|
348
|
+
routing: this.routing,
|
|
349
|
+
development: {
|
|
350
|
+
local: this.development.local.serialize(),
|
|
351
|
+
fallback: (_b = this.development.fallback) == null ? void 0 : _b.serialize()
|
|
352
|
+
},
|
|
353
|
+
production: this.production.serialize(),
|
|
354
|
+
vercel: this.vercel
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// src/config/common/microfrontend-config.ts
|
|
360
|
+
var SUPPORTED_VERSIONS = ["1"];
|
|
361
|
+
var DEFAULT_LOCAL_PROXY_PORT = 3024;
|
|
362
|
+
var MicrofrontendConfigCommon = class {
|
|
363
|
+
constructor({
|
|
364
|
+
config,
|
|
365
|
+
overrides
|
|
366
|
+
}) {
|
|
367
|
+
this.zones = {};
|
|
368
|
+
var _a, _b, _c;
|
|
369
|
+
if (!SUPPORTED_VERSIONS.includes(config.version)) {
|
|
370
|
+
throw new MicrofrontendError(
|
|
371
|
+
`Unsupported version: ${config.version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
|
|
372
|
+
", "
|
|
373
|
+
)}`,
|
|
374
|
+
{ type: "config", subtype: "unsupported_version" }
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
378
|
+
this.overrides = overrides && !disableOverrides ? new Overrides(overrides) : void 0;
|
|
379
|
+
for (const [zoneName, zoneConfig] of Object.entries(config.applications)) {
|
|
380
|
+
this.zones[zoneName] = new Application(zoneName, {
|
|
381
|
+
app: zoneConfig,
|
|
382
|
+
overrides: !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.config.applications[zoneName] : void 0
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
this.config = config;
|
|
386
|
+
this.name = config.name;
|
|
387
|
+
this.version = config.version;
|
|
388
|
+
this.options = config.options;
|
|
389
|
+
this.$schema = config.$schema;
|
|
390
|
+
}
|
|
391
|
+
isOverridesDisabled() {
|
|
392
|
+
var _a, _b;
|
|
393
|
+
return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
394
|
+
}
|
|
395
|
+
static getConfigFromEnv() {
|
|
396
|
+
const config = process.env.MFE_CONFIG;
|
|
397
|
+
if (!config) {
|
|
398
|
+
throw new MicrofrontendError(`Missing "MFE_CONFIG" in environment.`, {
|
|
399
|
+
type: "config",
|
|
400
|
+
subtype: "not_found_in_env"
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
return config;
|
|
404
|
+
}
|
|
405
|
+
static fromEnv(_) {
|
|
406
|
+
throw new Error("Not implemented");
|
|
407
|
+
}
|
|
408
|
+
getConfig() {
|
|
409
|
+
return this.config;
|
|
410
|
+
}
|
|
411
|
+
getAllApplications() {
|
|
412
|
+
return Object.values(this.zones);
|
|
413
|
+
}
|
|
414
|
+
getZone(name) {
|
|
415
|
+
const zone = this.zones[name];
|
|
416
|
+
if (!zone) {
|
|
417
|
+
throw new MicrofrontendError(
|
|
418
|
+
`Could not find microfrontends configuration for application "${name}"`,
|
|
419
|
+
{
|
|
420
|
+
type: "zone",
|
|
421
|
+
subtype: "not_found"
|
|
422
|
+
}
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
return zone;
|
|
426
|
+
}
|
|
427
|
+
getApplicationByProjectId(projectId) {
|
|
428
|
+
return Object.values(this.zones).find(
|
|
429
|
+
(zone) => {
|
|
430
|
+
var _a;
|
|
431
|
+
return ((_a = zone.vercel) == null ? void 0 : _a.projectId) === projectId;
|
|
432
|
+
}
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
getDefaultZone() {
|
|
436
|
+
const zone = Object.values(this.zones).find((z) => z.default);
|
|
437
|
+
if (!zone) {
|
|
438
|
+
throw new MicrofrontendError(
|
|
439
|
+
`Could not find default zone in microfrontends configuration`,
|
|
440
|
+
{
|
|
441
|
+
type: "zone",
|
|
442
|
+
subtype: "not_found"
|
|
443
|
+
}
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
return zone;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Returns the configured port for the local proxy
|
|
450
|
+
*/
|
|
451
|
+
getLocalProxyPort() {
|
|
452
|
+
var _a, _b;
|
|
453
|
+
return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Serializes the class back to the Schema type.
|
|
457
|
+
*
|
|
458
|
+
* NOTE: This is used when writing the config to disk and must always match the input Schema
|
|
459
|
+
*/
|
|
460
|
+
toSchemaJson() {
|
|
461
|
+
const applications = {};
|
|
462
|
+
for (const [name, zone] of Object.entries(this.zones)) {
|
|
463
|
+
applications[name] = zone.serialize();
|
|
464
|
+
}
|
|
465
|
+
return {
|
|
466
|
+
$schema: this.$schema,
|
|
467
|
+
name: this.name,
|
|
468
|
+
version: this.version,
|
|
469
|
+
options: this.options,
|
|
470
|
+
applications
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
serialize() {
|
|
474
|
+
var _a;
|
|
475
|
+
const applications = {};
|
|
476
|
+
for (const [name, zone] of Object.entries(this.zones)) {
|
|
477
|
+
applications[name] = zone.serialize();
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
config: {
|
|
481
|
+
name: this.name,
|
|
482
|
+
version: this.version,
|
|
483
|
+
applications,
|
|
484
|
+
options: this.options,
|
|
485
|
+
$schema: this.$schema
|
|
486
|
+
},
|
|
487
|
+
overrides: (_a = this.overrides) == null ? void 0 : _a.serialize()
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
write(_) {
|
|
491
|
+
throw new MicrofrontendError(
|
|
492
|
+
`Writing to file to disk requires using an instance of "MicrofrontendConfig".`,
|
|
493
|
+
{ type: "config", subtype: "unsupported_operation" }
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
// src/config/utils/get-output-file-path.ts
|
|
499
|
+
import path2 from "node:path";
|
|
500
|
+
|
|
501
|
+
// src/config/constants.ts
|
|
502
|
+
var MFE_CONFIG_DEFAULT_FILE_PATH2 = "micro-frontends";
|
|
503
|
+
var MFE_CONFIG_DEFAULT_FILE_NAME2 = "micro-frontends.config.json";
|
|
504
|
+
|
|
505
|
+
// src/config/utils/get-output-file-path.ts
|
|
506
|
+
function getOutputFilePath2() {
|
|
507
|
+
if (isVercel()) {
|
|
508
|
+
return path2.join(
|
|
509
|
+
".vercel",
|
|
510
|
+
MFE_CONFIG_DEFAULT_FILE_PATH2,
|
|
511
|
+
MFE_CONFIG_DEFAULT_FILE_NAME2
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
return path2.join(MFE_CONFIG_DEFAULT_FILE_PATH2, MFE_CONFIG_DEFAULT_FILE_NAME2);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// src/config/validation.ts
|
|
518
|
+
import { parse } from "jsonc-parser";
|
|
519
|
+
import { pathToRegexp, parse as parsePathRegexp } from "path-to-regexp";
|
|
520
|
+
import { Ajv } from "ajv";
|
|
521
|
+
|
|
522
|
+
// schema/schema.json
|
|
523
|
+
var schema_default = {
|
|
524
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
525
|
+
$ref: "#/definitions/Config",
|
|
526
|
+
definitions: {
|
|
527
|
+
Config: {
|
|
528
|
+
type: "object",
|
|
529
|
+
properties: {
|
|
530
|
+
version: {
|
|
531
|
+
type: "string"
|
|
532
|
+
},
|
|
533
|
+
$schema: {
|
|
534
|
+
type: "string"
|
|
535
|
+
},
|
|
536
|
+
name: {
|
|
537
|
+
type: "string",
|
|
538
|
+
description: 'Name for the micro-frontend site (eg. "vercel.com", "vercel-site" etc.).'
|
|
539
|
+
},
|
|
540
|
+
applications: {
|
|
541
|
+
$ref: "#/definitions/ApplicationConfigsById"
|
|
542
|
+
},
|
|
543
|
+
options: {
|
|
544
|
+
$ref: "#/definitions/Options",
|
|
545
|
+
description: "Optional configuration for the entire micro-frontends setup."
|
|
546
|
+
}
|
|
547
|
+
},
|
|
548
|
+
required: ["version", "applications"],
|
|
549
|
+
description: "Configuration for micro-frontend applications\n\nTODO: Add proxy configuration"
|
|
550
|
+
},
|
|
551
|
+
ApplicationConfigsById: {
|
|
552
|
+
type: "object",
|
|
553
|
+
additionalProperties: {
|
|
554
|
+
$ref: "#/definitions/ApplicationConfig"
|
|
555
|
+
}
|
|
556
|
+
},
|
|
557
|
+
ApplicationConfig: {
|
|
558
|
+
anyOf: [
|
|
559
|
+
{
|
|
560
|
+
$ref: "#/definitions/DefaultApplicationConfig"
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
$ref: "#/definitions/CommonApplicationConfig"
|
|
564
|
+
}
|
|
565
|
+
],
|
|
566
|
+
description: "A Micro-Frontend Deployment Target"
|
|
567
|
+
},
|
|
568
|
+
DefaultApplicationConfig: {
|
|
569
|
+
type: "object",
|
|
570
|
+
properties: {
|
|
571
|
+
default: {
|
|
572
|
+
type: "boolean",
|
|
573
|
+
const: true,
|
|
574
|
+
description: "The default application is used no other application is matched via the routing config"
|
|
575
|
+
},
|
|
576
|
+
routing: {
|
|
577
|
+
$ref: "#/definitions/Routing"
|
|
578
|
+
},
|
|
579
|
+
development: {
|
|
580
|
+
type: "object",
|
|
581
|
+
properties: {
|
|
582
|
+
local: {
|
|
583
|
+
$ref: "#/definitions/HostConfig"
|
|
584
|
+
},
|
|
585
|
+
fallback: {
|
|
586
|
+
$ref: "#/definitions/HostConfig",
|
|
587
|
+
description: "Fallback for local development, could be a host config that points to any environment. If this is not provided, or the application is not running - requests to the application in local development will error."
|
|
588
|
+
},
|
|
589
|
+
task: {
|
|
590
|
+
type: "string",
|
|
591
|
+
description: "Optional task to run when starting the development server. Should reference a script in the package.json of the application."
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
required: ["local"]
|
|
595
|
+
},
|
|
596
|
+
production: {
|
|
597
|
+
$ref: "#/definitions/HostConfig"
|
|
598
|
+
},
|
|
599
|
+
metadata: {
|
|
600
|
+
type: "object",
|
|
601
|
+
additionalProperties: {
|
|
602
|
+
type: "string"
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
federation: {
|
|
606
|
+
type: "object",
|
|
607
|
+
properties: {
|
|
608
|
+
exposes: {
|
|
609
|
+
type: "array",
|
|
610
|
+
items: {
|
|
611
|
+
type: "object",
|
|
612
|
+
properties: {
|
|
613
|
+
name: {
|
|
614
|
+
type: "string",
|
|
615
|
+
description: "The name of the module - should be used when importing the module from another application"
|
|
616
|
+
},
|
|
617
|
+
path: {
|
|
618
|
+
type: "string",
|
|
619
|
+
description: "Relative path to the module within its `application`"
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
required: ["name", "path"]
|
|
623
|
+
},
|
|
624
|
+
description: "Modules that are exposed by this application"
|
|
625
|
+
},
|
|
626
|
+
uses: {
|
|
627
|
+
type: "array",
|
|
628
|
+
items: {
|
|
629
|
+
type: "string"
|
|
630
|
+
},
|
|
631
|
+
description: "Modules that are used by this application. Only the name of the module is required."
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
vercel: {
|
|
636
|
+
$ref: "#/definitions/Vercel"
|
|
637
|
+
}
|
|
638
|
+
},
|
|
639
|
+
required: ["default", "development", "production"]
|
|
640
|
+
},
|
|
641
|
+
Routing: {
|
|
642
|
+
type: "object",
|
|
643
|
+
properties: {
|
|
644
|
+
assetPrefix: {
|
|
645
|
+
type: "string",
|
|
646
|
+
description: "[assetPrefix] for the application"
|
|
647
|
+
},
|
|
648
|
+
matches: {
|
|
649
|
+
type: "array",
|
|
650
|
+
items: {
|
|
651
|
+
$ref: "#/definitions/PathGroup"
|
|
652
|
+
},
|
|
653
|
+
description: "Path expressions that are routed to this application."
|
|
654
|
+
}
|
|
655
|
+
},
|
|
656
|
+
required: ["matches"]
|
|
657
|
+
},
|
|
658
|
+
PathGroup: {
|
|
659
|
+
type: "object",
|
|
660
|
+
properties: {
|
|
661
|
+
group: {
|
|
662
|
+
type: "string",
|
|
663
|
+
description: "Optional group name for the paths"
|
|
664
|
+
},
|
|
665
|
+
options: {
|
|
666
|
+
type: "object",
|
|
667
|
+
properties: {
|
|
668
|
+
flag: {
|
|
669
|
+
type: "string",
|
|
670
|
+
description: "flag name that can be used to enable/disable all paths in the group"
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
},
|
|
674
|
+
paths: {
|
|
675
|
+
type: "array",
|
|
676
|
+
items: {
|
|
677
|
+
type: "string"
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
},
|
|
681
|
+
required: ["paths"]
|
|
682
|
+
},
|
|
683
|
+
HostConfig: {
|
|
684
|
+
type: "object",
|
|
685
|
+
properties: {
|
|
686
|
+
protocol: {
|
|
687
|
+
type: "string",
|
|
688
|
+
enum: ["http", "https"],
|
|
689
|
+
description: 'The protocol to be used for the connection.\n- `http`: Hypertext Transfer Protocol (HTTP).\n- `https`: Secure Hypertext Transfer Protocol (HTTPS).\n\n* @defaultValue "https"'
|
|
690
|
+
},
|
|
691
|
+
host: {
|
|
692
|
+
type: "string",
|
|
693
|
+
description: "The hostname or IP address of the server. This can be a domain name (e.g., `example.com`) or an IP address (e.g., `192.168.1.1`)."
|
|
694
|
+
},
|
|
695
|
+
port: {
|
|
696
|
+
type: "number",
|
|
697
|
+
description: "The port number to be used for the connection. Common values include `80` for HTTP and `443` for HTTPS.\n\nThis field is optional and can be omitted if the default ports for the specified protocol are used"
|
|
698
|
+
}
|
|
699
|
+
},
|
|
700
|
+
required: ["host"]
|
|
701
|
+
},
|
|
702
|
+
Vercel: {
|
|
703
|
+
type: "object",
|
|
704
|
+
properties: {
|
|
705
|
+
projectId: {
|
|
706
|
+
type: "string",
|
|
707
|
+
description: "Vercel project ID"
|
|
708
|
+
},
|
|
709
|
+
projectName: {
|
|
710
|
+
type: "string",
|
|
711
|
+
description: "Vercel project name (temporary until we can use project ID)"
|
|
712
|
+
},
|
|
713
|
+
defaultRoute: {
|
|
714
|
+
type: "string",
|
|
715
|
+
description: "The default route for the application. Used to render screenshots, favicons, and provide direct zone links"
|
|
716
|
+
},
|
|
717
|
+
routeSpeedInsightsToDefaultZone: {
|
|
718
|
+
type: "boolean",
|
|
719
|
+
description: "Whether to route Speed Insights to the default zone or each individual microfrontend."
|
|
720
|
+
}
|
|
721
|
+
},
|
|
722
|
+
required: ["projectId"]
|
|
723
|
+
},
|
|
724
|
+
CommonApplicationConfig: {
|
|
725
|
+
type: "object",
|
|
726
|
+
properties: {
|
|
727
|
+
default: {
|
|
728
|
+
type: "boolean",
|
|
729
|
+
const: false,
|
|
730
|
+
description: "The default application is used no other application is matched via the routing config"
|
|
731
|
+
},
|
|
732
|
+
routing: {
|
|
733
|
+
$ref: "#/definitions/Routing"
|
|
734
|
+
},
|
|
735
|
+
development: {
|
|
736
|
+
type: "object",
|
|
737
|
+
properties: {
|
|
738
|
+
local: {
|
|
739
|
+
$ref: "#/definitions/HostConfig"
|
|
740
|
+
},
|
|
741
|
+
fallback: {
|
|
742
|
+
$ref: "#/definitions/HostConfig",
|
|
743
|
+
description: "Fallback for local development, could be a host config that points to any environment. If this is not provided, or the application is not running - requests to the application in local development will error."
|
|
744
|
+
},
|
|
745
|
+
task: {
|
|
746
|
+
type: "string",
|
|
747
|
+
description: "Optional task to run when starting the development server. Should reference a script in the package.json of the application."
|
|
748
|
+
}
|
|
749
|
+
},
|
|
750
|
+
required: ["local"]
|
|
751
|
+
},
|
|
752
|
+
production: {
|
|
753
|
+
$ref: "#/definitions/HostConfig"
|
|
754
|
+
},
|
|
755
|
+
metadata: {
|
|
756
|
+
type: "object",
|
|
757
|
+
additionalProperties: {
|
|
758
|
+
type: "string"
|
|
759
|
+
}
|
|
760
|
+
},
|
|
761
|
+
federation: {
|
|
762
|
+
type: "object",
|
|
763
|
+
properties: {
|
|
764
|
+
exposes: {
|
|
765
|
+
type: "array",
|
|
766
|
+
items: {
|
|
767
|
+
type: "object",
|
|
768
|
+
properties: {
|
|
769
|
+
name: {
|
|
770
|
+
type: "string",
|
|
771
|
+
description: "The name of the module - should be used when importing the module from another application"
|
|
772
|
+
},
|
|
773
|
+
path: {
|
|
774
|
+
type: "string",
|
|
775
|
+
description: "Relative path to the module within its `application`"
|
|
776
|
+
}
|
|
777
|
+
},
|
|
778
|
+
required: ["name", "path"]
|
|
779
|
+
},
|
|
780
|
+
description: "Modules that are exposed by this application"
|
|
781
|
+
},
|
|
782
|
+
uses: {
|
|
783
|
+
type: "array",
|
|
784
|
+
items: {
|
|
785
|
+
type: "string"
|
|
786
|
+
},
|
|
787
|
+
description: "Modules that are used by this application. Only the name of the module is required."
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
},
|
|
791
|
+
vercel: {
|
|
792
|
+
$ref: "#/definitions/Vercel"
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
required: ["default", "development", "production", "routing"]
|
|
796
|
+
},
|
|
797
|
+
Options: {
|
|
798
|
+
type: "object",
|
|
799
|
+
properties: {
|
|
800
|
+
vercel: {
|
|
801
|
+
$ref: "#/definitions/VercelOptions",
|
|
802
|
+
description: "Micro-Frontends wide options for Vercel."
|
|
803
|
+
},
|
|
804
|
+
localProxy: {
|
|
805
|
+
$ref: "#/definitions/LocalProxyOptions",
|
|
806
|
+
description: "Options for local proxy."
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
},
|
|
810
|
+
VercelOptions: {
|
|
811
|
+
type: "object",
|
|
812
|
+
properties: {
|
|
813
|
+
previewDeploymentSuffix: {
|
|
814
|
+
type: "string",
|
|
815
|
+
description: "If your team uses a custom Preview Deployment Suffix, please specify it here. See https://vercel.com/docs/deployments/preview-deployment-suffix. The default is `vercel.app`."
|
|
816
|
+
},
|
|
817
|
+
teamSlug: {
|
|
818
|
+
type: "string",
|
|
819
|
+
description: "Team slug for the Vercel team"
|
|
820
|
+
},
|
|
821
|
+
disableOverrides: {
|
|
822
|
+
type: "boolean",
|
|
823
|
+
description: "If you want to disable the overrides for the site. For example, if you are managing rewrites between applications externally, you may wish to disable the overrides on the toolbar as they will have no effect."
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
},
|
|
827
|
+
LocalProxyOptions: {
|
|
828
|
+
type: "object",
|
|
829
|
+
properties: {
|
|
830
|
+
port: {
|
|
831
|
+
type: "number",
|
|
832
|
+
description: "The port number used by the local proxy server.\n\nThe default is `3024`."
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
// src/config/utils/load-schema.ts
|
|
840
|
+
var SCHEMA = schema_default;
|
|
841
|
+
|
|
842
|
+
// src/config/validation.ts
|
|
843
|
+
var validateSchema = (configString) => {
|
|
844
|
+
const parsedConfig = parse(configString);
|
|
845
|
+
const ajv = new Ajv();
|
|
846
|
+
const validate = ajv.compile(SCHEMA);
|
|
847
|
+
const isValid = validate(parsedConfig);
|
|
848
|
+
if (!isValid) {
|
|
849
|
+
throw new MicrofrontendError(
|
|
850
|
+
`Invalid config: ${ajv.errorsText(validate.errors)}`,
|
|
851
|
+
{ type: "config", subtype: "does_not_match_schema" }
|
|
852
|
+
);
|
|
853
|
+
}
|
|
854
|
+
return parsedConfig;
|
|
855
|
+
};
|
|
856
|
+
var SUPPORTED_VERSIONS2 = ["1"];
|
|
857
|
+
var validateVersion = (version) => {
|
|
858
|
+
if (!SUPPORTED_VERSIONS2.includes(version)) {
|
|
859
|
+
throw new MicrofrontendError(
|
|
860
|
+
`Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS2.join(
|
|
861
|
+
", "
|
|
862
|
+
)}`,
|
|
863
|
+
{ type: "config", subtype: "unsupported_version" }
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
function validateMainPath(applicationConfigsById) {
|
|
868
|
+
for (const [id, app] of Object.entries(applicationConfigsById)) {
|
|
869
|
+
const { defaultRoute } = app.vercel ?? {};
|
|
870
|
+
if (!defaultRoute) {
|
|
871
|
+
continue;
|
|
872
|
+
}
|
|
873
|
+
if (isDefaultApplicationConfig(app)) {
|
|
874
|
+
const pathsWithApp = [];
|
|
875
|
+
for (const [otherId, otherApp] of Object.entries(
|
|
876
|
+
applicationConfigsById
|
|
877
|
+
)) {
|
|
878
|
+
if (isDefaultApplicationConfig(otherApp)) {
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
pathsWithApp.push({
|
|
882
|
+
id: otherId,
|
|
883
|
+
paths: otherApp.routing.matches.flatMap((match) => match.paths)
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
for (const { id: otherId, paths } of pathsWithApp) {
|
|
887
|
+
const isValid = paths.every((path7) => {
|
|
888
|
+
const matcher = pathToRegexp(path7);
|
|
889
|
+
return !matcher.test(defaultRoute);
|
|
890
|
+
});
|
|
891
|
+
if (!isValid) {
|
|
892
|
+
throw new MicrofrontendError(
|
|
893
|
+
`default route "${defaultRoute}" cannot be used for "${id}" because it is matched by "${otherId}"`,
|
|
894
|
+
{ type: "config", subtype: "invalid_main_path" }
|
|
895
|
+
);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
} else {
|
|
899
|
+
const allPaths = app.routing.matches.flatMap((match) => match.paths);
|
|
900
|
+
const isValid = allPaths.some((path7) => {
|
|
901
|
+
const matcher = pathToRegexp(path7);
|
|
902
|
+
return matcher.test(defaultRoute);
|
|
903
|
+
});
|
|
904
|
+
if (!isValid) {
|
|
905
|
+
throw new MicrofrontendError(
|
|
906
|
+
`default route "${defaultRoute}" is not included by the routing config for application "${id}"`,
|
|
907
|
+
{ type: "config", subtype: "invalid_main_path" }
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
var validatePaths = (applicationConfigsById) => {
|
|
914
|
+
const pathsByApplicationId = /* @__PURE__ */ new Map();
|
|
915
|
+
const errors = [];
|
|
916
|
+
for (const [id, app] of Object.entries(applicationConfigsById)) {
|
|
917
|
+
if (isDefaultApplicationConfig(app)) {
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
for (const pathMatch of app.routing.matches) {
|
|
921
|
+
for (const path7 of pathMatch.paths) {
|
|
922
|
+
const maybeError = validatePathExpression(path7);
|
|
923
|
+
if (maybeError) {
|
|
924
|
+
errors.push(maybeError);
|
|
925
|
+
}
|
|
926
|
+
const existing = pathsByApplicationId.get(path7);
|
|
927
|
+
if (existing) {
|
|
928
|
+
existing.applications.push(id);
|
|
929
|
+
} else {
|
|
930
|
+
pathsByApplicationId.set(path7, {
|
|
931
|
+
applications: [id],
|
|
932
|
+
matcher: pathToRegexp(path7),
|
|
933
|
+
applicationId: id
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
const entries = Array.from(pathsByApplicationId.entries());
|
|
940
|
+
entries.forEach(([path7, { applications: ids, matcher, applicationId }]) => {
|
|
941
|
+
if (ids.length > 1) {
|
|
942
|
+
errors.push(
|
|
943
|
+
`Duplicate path "${path7}" for applications "${ids.join(", ")}"`
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
entries.forEach(
|
|
947
|
+
([
|
|
948
|
+
matchPath,
|
|
949
|
+
{ applications: matchIds, applicationId: matchApplicationId }
|
|
950
|
+
]) => {
|
|
951
|
+
if (path7 === matchPath) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
if (applicationId === matchApplicationId) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
if (matcher.test(matchPath)) {
|
|
958
|
+
const source = `"${path7}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
|
|
959
|
+
const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
|
|
960
|
+
errors.push(
|
|
961
|
+
`Overlapping path detected between ${source} and ${destination}`
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
);
|
|
966
|
+
});
|
|
967
|
+
if (errors.length) {
|
|
968
|
+
throw new MicrofrontendError(`Invalid paths: ${errors.join(", ")}`, {
|
|
969
|
+
type: "config",
|
|
970
|
+
subtype: "conflicting_paths"
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
};
|
|
974
|
+
var PATH_DEFAULT_PATTERN = "[^\\/#\\?]+?";
|
|
975
|
+
function validatePathExpression(path7) {
|
|
976
|
+
const tokens = parsePathRegexp(path7);
|
|
977
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
978
|
+
const token = tokens[i];
|
|
979
|
+
if (token === void 0) {
|
|
980
|
+
return `token ${i} in ${path7} is undefined, this shouldn't happen`;
|
|
981
|
+
}
|
|
982
|
+
if (typeof token !== "string") {
|
|
983
|
+
if (token.pattern !== PATH_DEFAULT_PATTERN) {
|
|
984
|
+
return `Path ${path7} cannot use a regular expression wildcard`;
|
|
985
|
+
}
|
|
986
|
+
if (token.prefix !== "/") {
|
|
987
|
+
return `Wildcard :${token.name} must be immediately after a / in ${path7}`;
|
|
988
|
+
}
|
|
989
|
+
if (token.suffix) {
|
|
990
|
+
return `Wildcard suffix on :${token.name} is not allowed. Suffixes are not supported`;
|
|
991
|
+
}
|
|
992
|
+
if (token.modifier && i !== tokens.length - 1) {
|
|
993
|
+
return `Modifier ${token.modifier} is not allowed on wildcard :${token.name} in ${path7}. Modifiers are only allowed in the last path component`;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
return void 0;
|
|
998
|
+
}
|
|
999
|
+
var validateDefaults = (applicationConfigsById) => {
|
|
1000
|
+
const defaultApplicationIds = Object.entries(applicationConfigsById).reduce((acc, [id, app]) => app.default ? [...acc, id] : acc, []);
|
|
1001
|
+
if (defaultApplicationIds.length === 0) {
|
|
1002
|
+
throw new MicrofrontendError(
|
|
1003
|
+
`No default application found. At least one application must be marked as default.`,
|
|
1004
|
+
{ type: "config", subtype: "no_default_application" }
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
if (defaultApplicationIds.length > 1) {
|
|
1008
|
+
throw new MicrofrontendError(
|
|
1009
|
+
`Only one default application is allowed. Found ${defaultApplicationIds.join(", ")}.`,
|
|
1010
|
+
{ type: "config", subtype: "multiple_default_applications" }
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
var validateOptions = (options) => {
|
|
1015
|
+
var _a;
|
|
1016
|
+
if ((_a = options == null ? void 0 : options.vercel) == null ? void 0 : _a.previewDeploymentSuffix) {
|
|
1017
|
+
if (!/^[a-zA-Z]{2,}\.[a-zA-Z]{2,}$/.test(
|
|
1018
|
+
options.vercel.previewDeploymentSuffix
|
|
1019
|
+
)) {
|
|
1020
|
+
throw new MicrofrontendError(
|
|
1021
|
+
`Invalid preview deployment suffix: ${options.vercel.previewDeploymentSuffix}. Should have be formatted like "vercel.app".`,
|
|
1022
|
+
{ type: "config", subtype: "invalid_preview_deployment_suffix" }
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
|
|
1028
|
+
// src/config/utils/convert.ts
|
|
1029
|
+
function convertV1RoutingToV2Routing(routing) {
|
|
1030
|
+
return routing.matches.map((group) => {
|
|
1031
|
+
var _a;
|
|
1032
|
+
return {
|
|
1033
|
+
group: group.group,
|
|
1034
|
+
flag: (_a = group.options) == null ? void 0 : _a.flag,
|
|
1035
|
+
paths: group.paths
|
|
1036
|
+
};
|
|
1037
|
+
});
|
|
1038
|
+
}
|
|
1039
|
+
function convertV1ApplicationToV2Application(application) {
|
|
1040
|
+
const common = {
|
|
1041
|
+
production: application.production,
|
|
1042
|
+
development: application.development,
|
|
1043
|
+
vercel: application.vercel
|
|
1044
|
+
};
|
|
1045
|
+
if (application.default) {
|
|
1046
|
+
return common;
|
|
1047
|
+
}
|
|
1048
|
+
return {
|
|
1049
|
+
...common,
|
|
1050
|
+
routing: convertV1RoutingToV2Routing(application.routing)
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
function convertV1ConfigToV2Config(config, fromApp) {
|
|
1054
|
+
if (!config.applications[fromApp]) {
|
|
1055
|
+
throw new Error(`Application "${fromApp}" not found in the config`);
|
|
1056
|
+
}
|
|
1057
|
+
const common = {
|
|
1058
|
+
version: "2",
|
|
1059
|
+
options: config.options
|
|
1060
|
+
};
|
|
1061
|
+
if (config.applications[fromApp].default) {
|
|
1062
|
+
return {
|
|
1063
|
+
...common,
|
|
1064
|
+
applications: Object.fromEntries(
|
|
1065
|
+
Object.entries(config.applications).map(([id, application]) => [
|
|
1066
|
+
id,
|
|
1067
|
+
convertV1ApplicationToV2Application(application)
|
|
1068
|
+
])
|
|
1069
|
+
)
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
const defaultApplication = Object.entries(config.applications).find(
|
|
1073
|
+
([, application]) => application.default
|
|
1074
|
+
);
|
|
1075
|
+
if (!defaultApplication) {
|
|
1076
|
+
throw new Error("No default application found in the config");
|
|
1077
|
+
}
|
|
1078
|
+
return {
|
|
1079
|
+
...common,
|
|
1080
|
+
partOf: defaultApplication[0]
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
// src/config/utils/write-file.ts
|
|
1085
|
+
import fs from "node:fs";
|
|
1086
|
+
import { dirname } from "node:path";
|
|
1087
|
+
function writeFile(outputPath, config, prettify) {
|
|
1088
|
+
fs.mkdirSync(dirname(outputPath), { recursive: true });
|
|
1089
|
+
fs.writeFileSync(
|
|
1090
|
+
outputPath,
|
|
1091
|
+
JSON.stringify(config, null, prettify ? 2 : void 0)
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
// src/config/microfrontend-config.ts
|
|
1096
|
+
var MicrofrontendConfig = class extends MicrofrontendConfigCommon {
|
|
1097
|
+
static validate(configString) {
|
|
1098
|
+
const config = validateSchema(configString);
|
|
1099
|
+
validateVersion(config.version);
|
|
1100
|
+
validatePaths(config.applications);
|
|
1101
|
+
validateMainPath(config.applications);
|
|
1102
|
+
validateDefaults(config.applications);
|
|
1103
|
+
validateOptions(config.options);
|
|
1104
|
+
return config;
|
|
1105
|
+
}
|
|
1106
|
+
static fromEnv({
|
|
1107
|
+
cookies
|
|
1108
|
+
}) {
|
|
1109
|
+
return new MicrofrontendConfigCommon({
|
|
1110
|
+
config: MicrofrontendConfig.validate(
|
|
1111
|
+
MicrofrontendConfigCommon.getConfigFromEnv()
|
|
1112
|
+
),
|
|
1113
|
+
overrides: Overrides.parseOverrides(cookies)
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
static fromFile({
|
|
1117
|
+
filePath
|
|
1118
|
+
}) {
|
|
1119
|
+
try {
|
|
1120
|
+
const config = fs2.readFileSync(filePath, "utf-8");
|
|
1121
|
+
return new MicrofrontendConfig({
|
|
1122
|
+
config: MicrofrontendConfig.validate(config)
|
|
1123
|
+
});
|
|
1124
|
+
} catch (e) {
|
|
1125
|
+
throw MicrofrontendError.handle(e, {
|
|
1126
|
+
fileName: filePath
|
|
1127
|
+
});
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* Writes the configuration to a file.
|
|
1132
|
+
*/
|
|
1133
|
+
write(fromApp, opts = {}) {
|
|
1134
|
+
const { pretty = true, versions = ["v1", "v2"] } = opts;
|
|
1135
|
+
const config = this.toSchemaJson();
|
|
1136
|
+
if (versions.includes("v1")) {
|
|
1137
|
+
const outputPath = getOutputFilePath2();
|
|
1138
|
+
writeFile(outputPath, config, pretty);
|
|
1139
|
+
}
|
|
1140
|
+
if (versions.includes("v2")) {
|
|
1141
|
+
const outputPath = getOutputFilePath();
|
|
1142
|
+
const v2Config = convertV1ConfigToV2Config(config, fromApp);
|
|
1143
|
+
writeFile(outputPath, v2Config, pretty);
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
|
|
1148
|
+
// src/config-v2/microfrontends/server/index.ts
|
|
1149
|
+
import fs7 from "node:fs";
|
|
1150
|
+
import { dirname as dirname4, join as join2 } from "node:path";
|
|
1151
|
+
|
|
1152
|
+
// src/config-v2/overrides/constants.ts
|
|
1153
|
+
var OVERRIDES_COOKIE_PREFIX2 = "vercel-micro-frontends-override";
|
|
1154
|
+
var OVERRIDES_ENV_COOKIE_PREFIX = `${OVERRIDES_COOKIE_PREFIX2}:env:`;
|
|
1155
|
+
|
|
1156
|
+
// src/config-v2/overrides/is-override-cookie.ts
|
|
1157
|
+
function isOverrideCookie(cookie) {
|
|
1158
|
+
var _a;
|
|
1159
|
+
return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX2));
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
// src/config-v2/overrides/get-override-from-cookie.ts
|
|
1163
|
+
function getOverrideFromCookie(cookie) {
|
|
1164
|
+
if (!isOverrideCookie(cookie) || !cookie.value)
|
|
1165
|
+
return;
|
|
1166
|
+
return {
|
|
1167
|
+
application: cookie.name.replace(OVERRIDES_ENV_COOKIE_PREFIX, ""),
|
|
1168
|
+
host: cookie.value
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// src/config-v2/overrides/parse-overrides.ts
|
|
1173
|
+
function parseOverrides(cookies) {
|
|
1174
|
+
const overridesConfig = { applications: {} };
|
|
1175
|
+
cookies.forEach((cookie) => {
|
|
1176
|
+
const override = getOverrideFromCookie(cookie);
|
|
1177
|
+
if (!override)
|
|
1178
|
+
return;
|
|
1179
|
+
overridesConfig.applications[override.application] = {
|
|
1180
|
+
environment: { host: override.host }
|
|
1181
|
+
};
|
|
1182
|
+
});
|
|
1183
|
+
return overridesConfig;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
// src/config-v2/errors.ts
|
|
1187
|
+
var MicrofrontendError2 = class extends Error {
|
|
1188
|
+
constructor(message, opts) {
|
|
1189
|
+
super(message);
|
|
1190
|
+
this.name = "MicrofrontendsError";
|
|
1191
|
+
this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/microfrontends";
|
|
1192
|
+
this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
|
|
1193
|
+
this.subtype = opts == null ? void 0 : opts.subtype;
|
|
1194
|
+
Error.captureStackTrace(this, MicrofrontendError2);
|
|
1195
|
+
}
|
|
1196
|
+
isKnown() {
|
|
1197
|
+
return this.type !== "unknown";
|
|
1198
|
+
}
|
|
1199
|
+
isUnknown() {
|
|
1200
|
+
return !this.isKnown();
|
|
1201
|
+
}
|
|
1202
|
+
/**
|
|
1203
|
+
* Converts an error to a MicrofrontendsError.
|
|
1204
|
+
* @param original - The original error to convert.
|
|
1205
|
+
* @returns The converted MicrofrontendsError.
|
|
1206
|
+
*/
|
|
1207
|
+
static convert(original, opts) {
|
|
1208
|
+
if (opts == null ? void 0 : opts.fileName) {
|
|
1209
|
+
const err = MicrofrontendError2.convertFSError(original, opts.fileName);
|
|
1210
|
+
if (err) {
|
|
1211
|
+
return err;
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
if (original.message.includes(
|
|
1215
|
+
"Code generation from strings disallowed for this context"
|
|
1216
|
+
)) {
|
|
1217
|
+
return new MicrofrontendError2(original.message, {
|
|
1218
|
+
type: "config",
|
|
1219
|
+
subtype: "unsupported_validation_env",
|
|
1220
|
+
source: "ajv"
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
return new MicrofrontendError2(original.message);
|
|
1224
|
+
}
|
|
1225
|
+
static convertFSError(original, fileName) {
|
|
1226
|
+
if (original instanceof Error && "code" in original) {
|
|
1227
|
+
if (original.code === "ENOENT") {
|
|
1228
|
+
return new MicrofrontendError2(`Could not find "${fileName}"`, {
|
|
1229
|
+
type: "config",
|
|
1230
|
+
subtype: "unable_to_read_file",
|
|
1231
|
+
source: "fs"
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
if (original.code === "EACCES") {
|
|
1235
|
+
return new MicrofrontendError2(
|
|
1236
|
+
`Permission denied while accessing "${fileName}"`,
|
|
1237
|
+
{
|
|
1238
|
+
type: "config",
|
|
1239
|
+
subtype: "invalid_permissions",
|
|
1240
|
+
source: "fs"
|
|
1241
|
+
}
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
if (original instanceof SyntaxError) {
|
|
1246
|
+
return new MicrofrontendError2(
|
|
1247
|
+
`Failed to parse "${fileName}": Invalid JSON format.`,
|
|
1248
|
+
{
|
|
1249
|
+
type: "config",
|
|
1250
|
+
subtype: "invalid_syntax",
|
|
1251
|
+
source: "fs"
|
|
1252
|
+
}
|
|
1253
|
+
);
|
|
1254
|
+
}
|
|
1255
|
+
return null;
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Handles an unknown error and returns a MicrofrontendsError instance.
|
|
1259
|
+
* @param err - The error to handle.
|
|
1260
|
+
* @returns A MicrofrontendsError instance.
|
|
1261
|
+
*/
|
|
1262
|
+
static handle(err, opts) {
|
|
1263
|
+
if (err instanceof MicrofrontendError2) {
|
|
1264
|
+
return err;
|
|
1265
|
+
}
|
|
1266
|
+
if (err instanceof Error) {
|
|
1267
|
+
return MicrofrontendError2.convert(err, opts);
|
|
1268
|
+
}
|
|
1269
|
+
if (typeof err === "object" && err !== null) {
|
|
1270
|
+
if ("message" in err && typeof err.message === "string") {
|
|
1271
|
+
return MicrofrontendError2.convert(new Error(err.message), opts);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
return new MicrofrontendError2("An unknown error occurred");
|
|
1275
|
+
}
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
// src/config-v2/microfrontends-config/utils/get-config-from-env.ts
|
|
1279
|
+
function getConfigStringFromEnv() {
|
|
1280
|
+
const config = process.env.MFE_CONFIG;
|
|
1281
|
+
if (!config) {
|
|
1282
|
+
throw new MicrofrontendError2(`Missing "MFE_CONFIG" in environment.`, {
|
|
1283
|
+
type: "config",
|
|
1284
|
+
subtype: "not_found_in_env"
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
return config;
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// src/config-v2/microfrontends-config/isomorphic/index.ts
|
|
1291
|
+
import { parse as parse2 } from "jsonc-parser";
|
|
1292
|
+
|
|
1293
|
+
// src/config-v2/schema/utils/is-main-config.ts
|
|
1294
|
+
function isMainConfig(c) {
|
|
1295
|
+
return !("partOf" in c);
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
// src/config-v2/schema/utils/is-default-app.ts
|
|
1299
|
+
function isDefaultApp(a) {
|
|
1300
|
+
return !("routing" in a);
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// src/config-v2/microfrontends-config/client/index.ts
|
|
1304
|
+
import { pathToRegexp as pathToRegexp2 } from "path-to-regexp";
|
|
1305
|
+
var MicrofrontendConfigClient = class {
|
|
1306
|
+
constructor(config, opts) {
|
|
1307
|
+
this.pathCache = {};
|
|
1308
|
+
this.serialized = config;
|
|
1309
|
+
if (opts == null ? void 0 : opts.removeFlaggedPaths) {
|
|
1310
|
+
for (const app of Object.values(config.applications)) {
|
|
1311
|
+
if (app.routing) {
|
|
1312
|
+
app.routing = app.routing.filter((match) => !match.flag);
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
this.applications = config.applications;
|
|
1317
|
+
}
|
|
1318
|
+
/**
|
|
1319
|
+
* Create a new `MicrofrontendConfigClient` from a JSON string.
|
|
1320
|
+
* Config must be passed in to remain framework agnostic
|
|
1321
|
+
*/
|
|
1322
|
+
static fromEnv(config, opts) {
|
|
1323
|
+
if (!config) {
|
|
1324
|
+
throw new Error("No microfrontends configuration found");
|
|
1325
|
+
}
|
|
1326
|
+
return new MicrofrontendConfigClient(
|
|
1327
|
+
JSON.parse(config),
|
|
1328
|
+
opts
|
|
1329
|
+
);
|
|
1330
|
+
}
|
|
1331
|
+
isEqual(other) {
|
|
1332
|
+
return JSON.stringify(this.applications) === JSON.stringify(other.applications);
|
|
1333
|
+
}
|
|
1334
|
+
getApplicationNameForPath(path7) {
|
|
1335
|
+
if (!path7.startsWith("/")) {
|
|
1336
|
+
throw new Error(`Path must start with a /`);
|
|
1337
|
+
}
|
|
1338
|
+
if (this.pathCache[path7]) {
|
|
1339
|
+
return this.pathCache[path7];
|
|
1340
|
+
}
|
|
1341
|
+
const pathname = new URL(path7, "https://example.com").pathname;
|
|
1342
|
+
for (const [name, application] of Object.entries(this.applications)) {
|
|
1343
|
+
if (application.routing) {
|
|
1344
|
+
for (const group of application.routing) {
|
|
1345
|
+
for (const childPath of group.paths) {
|
|
1346
|
+
const regexp = pathToRegexp2(childPath);
|
|
1347
|
+
if (regexp.test(pathname)) {
|
|
1348
|
+
this.pathCache[path7] = name;
|
|
1349
|
+
return name;
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
const defaultApplication = Object.entries(this.applications).find(
|
|
1356
|
+
([, application]) => application.default
|
|
1357
|
+
);
|
|
1358
|
+
if (!defaultApplication) {
|
|
1359
|
+
return null;
|
|
1360
|
+
}
|
|
1361
|
+
this.pathCache[path7] = defaultApplication[0];
|
|
1362
|
+
return defaultApplication[0];
|
|
1363
|
+
}
|
|
1364
|
+
serialize() {
|
|
1365
|
+
return this.serialized;
|
|
1366
|
+
}
|
|
1367
|
+
};
|
|
1368
|
+
|
|
1369
|
+
// src/config-v2/microfrontends-config/isomorphic/validation.ts
|
|
1370
|
+
import { pathToRegexp as pathToRegexp3, parse as parsePathRegexp2 } from "path-to-regexp";
|
|
1371
|
+
var SUPPORTED_VERSIONS3 = ["2"];
|
|
1372
|
+
var validateConfigVersion = (version) => {
|
|
1373
|
+
if (!SUPPORTED_VERSIONS3.includes(version)) {
|
|
1374
|
+
throw new MicrofrontendError2(
|
|
1375
|
+
`Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS3.join(
|
|
1376
|
+
", "
|
|
1377
|
+
)}`,
|
|
1378
|
+
{ type: "config", subtype: "unsupported_version" }
|
|
1379
|
+
);
|
|
1380
|
+
}
|
|
1381
|
+
};
|
|
1382
|
+
var validateConfigPaths = (applicationConfigsById) => {
|
|
1383
|
+
if (!applicationConfigsById) {
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
const pathsByApplicationId = /* @__PURE__ */ new Map();
|
|
1387
|
+
const errors = [];
|
|
1388
|
+
for (const [id, app] of Object.entries(applicationConfigsById)) {
|
|
1389
|
+
if (isDefaultApp(app)) {
|
|
1390
|
+
continue;
|
|
1391
|
+
}
|
|
1392
|
+
for (const pathMatch of app.routing) {
|
|
1393
|
+
for (const path7 of pathMatch.paths) {
|
|
1394
|
+
const tokens = parsePathRegexp2(path7);
|
|
1395
|
+
for (const token of tokens.slice(0, -1)) {
|
|
1396
|
+
if (typeof token !== "string") {
|
|
1397
|
+
errors.push(
|
|
1398
|
+
`Path ${path7} may only have a :wildcard in the last path component`
|
|
1399
|
+
);
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
const existing = pathsByApplicationId.get(path7);
|
|
1403
|
+
if (existing) {
|
|
1404
|
+
existing.applications.push(id);
|
|
1405
|
+
} else {
|
|
1406
|
+
pathsByApplicationId.set(path7, {
|
|
1407
|
+
applications: [id],
|
|
1408
|
+
matcher: pathToRegexp3(path7),
|
|
1409
|
+
applicationId: id
|
|
1410
|
+
});
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
const entries = Array.from(pathsByApplicationId.entries());
|
|
1416
|
+
entries.forEach(([path7, { applications: ids, matcher, applicationId }]) => {
|
|
1417
|
+
if (ids.length > 1) {
|
|
1418
|
+
errors.push(
|
|
1419
|
+
`Duplicate path "${path7}" for applications "${ids.join(", ")}"`
|
|
1420
|
+
);
|
|
1421
|
+
}
|
|
1422
|
+
entries.forEach(
|
|
1423
|
+
([
|
|
1424
|
+
matchPath,
|
|
1425
|
+
{ applications: matchIds, applicationId: matchApplicationId }
|
|
1426
|
+
]) => {
|
|
1427
|
+
if (path7 === matchPath) {
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1430
|
+
if (applicationId === matchApplicationId) {
|
|
1431
|
+
return;
|
|
1432
|
+
}
|
|
1433
|
+
if (matcher.test(matchPath)) {
|
|
1434
|
+
const source = `"${path7}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
|
|
1435
|
+
const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
|
|
1436
|
+
errors.push(
|
|
1437
|
+
`Overlapping path detected between ${source} and ${destination}`
|
|
1438
|
+
);
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
);
|
|
1442
|
+
});
|
|
1443
|
+
if (errors.length) {
|
|
1444
|
+
throw new MicrofrontendError2(`Invalid paths: ${errors.join(", ")}`, {
|
|
1445
|
+
type: "config",
|
|
1446
|
+
subtype: "conflicting_paths"
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
var validateAppPaths = (name, app) => {
|
|
1451
|
+
for (const group of app.routing) {
|
|
1452
|
+
for (const p of group.paths) {
|
|
1453
|
+
if (p === "/") {
|
|
1454
|
+
continue;
|
|
1455
|
+
}
|
|
1456
|
+
if (p.endsWith("/")) {
|
|
1457
|
+
throw new MicrofrontendError2(
|
|
1458
|
+
`Invalid path for application "${name}". ${p} must not end with a slash.`,
|
|
1459
|
+
{ type: "application", subtype: "invalid_path" }
|
|
1460
|
+
);
|
|
1461
|
+
}
|
|
1462
|
+
if (!p.startsWith("/")) {
|
|
1463
|
+
throw new MicrofrontendError2(
|
|
1464
|
+
`Invalid path for application "${name}". ${p} must start with a slash.`,
|
|
1465
|
+
{ type: "application", subtype: "invalid_path" }
|
|
1466
|
+
);
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
};
|
|
1471
|
+
var validateConfigDefaultApplication = (applicationConfigsById) => {
|
|
1472
|
+
if (!applicationConfigsById) {
|
|
1473
|
+
return;
|
|
1474
|
+
}
|
|
1475
|
+
const applicationsWithRouting = Object.entries(applicationConfigsById).filter(
|
|
1476
|
+
([, app]) => !isDefaultApp(app)
|
|
1477
|
+
);
|
|
1478
|
+
const applicationsWithRoutingNames = applicationsWithRouting.map(
|
|
1479
|
+
([key]) => key
|
|
1480
|
+
);
|
|
1481
|
+
const numApplications = Object.keys(applicationConfigsById).length;
|
|
1482
|
+
const numApplicationsWithRouting = applicationsWithRoutingNames.length;
|
|
1483
|
+
const numApplicationsWithoutRouting = numApplications - numApplicationsWithRouting;
|
|
1484
|
+
if (numApplicationsWithoutRouting === 0) {
|
|
1485
|
+
throw new MicrofrontendError2(
|
|
1486
|
+
`No default application found. At least one application needs to be the default by omitting routing.`,
|
|
1487
|
+
{ type: "config", subtype: "no_default_application" }
|
|
1488
|
+
);
|
|
1489
|
+
}
|
|
1490
|
+
if (numApplicationsWithoutRouting > 1) {
|
|
1491
|
+
throw new MicrofrontendError2(
|
|
1492
|
+
`Only one application can omit "routing". Found ${applicationsWithRoutingNames.length - Object.keys(applicationConfigsById).length > 1}.`,
|
|
1493
|
+
{ type: "config", subtype: "multiple_default_applications" }
|
|
1494
|
+
);
|
|
1495
|
+
}
|
|
1496
|
+
};
|
|
1497
|
+
|
|
1498
|
+
// src/config-v2/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
|
|
1499
|
+
var PREFIX = "vc-ap";
|
|
1500
|
+
function generateAssetPrefixFromName({
|
|
1501
|
+
name
|
|
1502
|
+
}) {
|
|
1503
|
+
if (!name) {
|
|
1504
|
+
throw new Error("Name is required to generate an asset prefix");
|
|
1505
|
+
}
|
|
1506
|
+
return `${PREFIX}-${name}`;
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
// src/config-v2/microfrontends-config/isomorphic/utils/generate-port.ts
|
|
1510
|
+
function generatePortFromName({
|
|
1511
|
+
name,
|
|
1512
|
+
minPort = 3e3,
|
|
1513
|
+
maxPort = 8e3
|
|
1514
|
+
}) {
|
|
1515
|
+
if (!name) {
|
|
1516
|
+
throw new Error("Name is required to generate a port");
|
|
1517
|
+
}
|
|
1518
|
+
let hash = 0;
|
|
1519
|
+
for (let i = 0; i < name.length; i++) {
|
|
1520
|
+
hash = (hash << 5) - hash + name.charCodeAt(i);
|
|
1521
|
+
hash |= 0;
|
|
1522
|
+
}
|
|
1523
|
+
hash = Math.abs(hash);
|
|
1524
|
+
const range = maxPort - minPort;
|
|
1525
|
+
const port = minPort + hash % range;
|
|
1526
|
+
return port;
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
// src/config-v2/microfrontends-config/isomorphic/host.ts
|
|
1530
|
+
var Host2 = class {
|
|
1531
|
+
constructor(hostConfig, options) {
|
|
1532
|
+
const { protocol = "https", host, port } = hostConfig;
|
|
1533
|
+
this.protocol = protocol;
|
|
1534
|
+
this.host = host;
|
|
1535
|
+
this.port = Host2.getPort({ port, protocol: this.protocol });
|
|
1536
|
+
this.local = options == null ? void 0 : options.isLocal;
|
|
1537
|
+
}
|
|
1538
|
+
isLocal() {
|
|
1539
|
+
return this.local || this.host === "localhost" || this.host === "127.0.0.1";
|
|
1540
|
+
}
|
|
1541
|
+
static getPort({
|
|
1542
|
+
protocol,
|
|
1543
|
+
port
|
|
1544
|
+
}) {
|
|
1545
|
+
if (!port) {
|
|
1546
|
+
if (protocol === "http") {
|
|
1547
|
+
return 80;
|
|
1548
|
+
}
|
|
1549
|
+
return 443;
|
|
1550
|
+
}
|
|
1551
|
+
return port;
|
|
1552
|
+
}
|
|
1553
|
+
isDefaultPort() {
|
|
1554
|
+
return this.port === Host2.getPort({ protocol: this.protocol });
|
|
1555
|
+
}
|
|
1556
|
+
toString(opts = {}) {
|
|
1557
|
+
const url = this.toUrl(opts);
|
|
1558
|
+
return url.toString().replace(/\/$/, "");
|
|
1559
|
+
}
|
|
1560
|
+
toUrl(opts = {}) {
|
|
1561
|
+
const { includeDefaultPort } = opts;
|
|
1562
|
+
const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
|
|
1563
|
+
return new URL(url);
|
|
1564
|
+
}
|
|
1565
|
+
};
|
|
1566
|
+
var LocalHost = class extends Host2 {
|
|
1567
|
+
constructor({
|
|
1568
|
+
appName,
|
|
1569
|
+
...hostConfig
|
|
1570
|
+
}) {
|
|
1571
|
+
const host = hostConfig.host ?? "localhost";
|
|
1572
|
+
const port = hostConfig.port ?? generatePortFromName({ name: appName });
|
|
1573
|
+
const protocol = hostConfig.protocol ?? "http";
|
|
1574
|
+
super({ protocol, host, port });
|
|
1575
|
+
}
|
|
1576
|
+
};
|
|
1577
|
+
|
|
1578
|
+
// src/config-v2/microfrontends-config/isomorphic/application.ts
|
|
1579
|
+
var Application2 = class {
|
|
1580
|
+
constructor(name, {
|
|
1581
|
+
app,
|
|
1582
|
+
overrides,
|
|
1583
|
+
isDefault
|
|
1584
|
+
}) {
|
|
1585
|
+
var _a, _b;
|
|
1586
|
+
this.name = name;
|
|
1587
|
+
this.development = {
|
|
1588
|
+
local: new LocalHost({
|
|
1589
|
+
appName: name,
|
|
1590
|
+
...(_a = app.development) == null ? void 0 : _a.local
|
|
1591
|
+
}),
|
|
1592
|
+
fallback: ((_b = app.development) == null ? void 0 : _b.fallback) ? new Host2(app.development.fallback) : void 0
|
|
1593
|
+
};
|
|
1594
|
+
this.production = app.production ? new Host2(app.production) : void 0;
|
|
1595
|
+
this.vercel = app.vercel;
|
|
1596
|
+
this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
|
|
1597
|
+
environment: new Host2(overrides.environment)
|
|
1598
|
+
} : void 0;
|
|
1599
|
+
this.default = isDefault ?? false;
|
|
1600
|
+
this.serialized = app;
|
|
1601
|
+
}
|
|
1602
|
+
isDefault() {
|
|
1603
|
+
return this.default;
|
|
1604
|
+
}
|
|
1605
|
+
getAssetPrefix() {
|
|
1606
|
+
return generateAssetPrefixFromName({ name: this.name });
|
|
1607
|
+
}
|
|
1608
|
+
serialize() {
|
|
1609
|
+
return this.serialized;
|
|
1610
|
+
}
|
|
1611
|
+
};
|
|
1612
|
+
var DefaultApplication = class extends Application2 {
|
|
1613
|
+
constructor(name, {
|
|
1614
|
+
app,
|
|
1615
|
+
overrides
|
|
1616
|
+
}) {
|
|
1617
|
+
super(name, {
|
|
1618
|
+
app,
|
|
1619
|
+
overrides,
|
|
1620
|
+
isDefault: true
|
|
1621
|
+
});
|
|
1622
|
+
this.default = true;
|
|
1623
|
+
this.production = new Host2(app.production);
|
|
1624
|
+
}
|
|
1625
|
+
getAssetPrefix() {
|
|
1626
|
+
return "";
|
|
1627
|
+
}
|
|
1628
|
+
};
|
|
1629
|
+
var ChildApplication = class extends Application2 {
|
|
1630
|
+
constructor(name, {
|
|
1631
|
+
app,
|
|
1632
|
+
overrides
|
|
1633
|
+
}) {
|
|
1634
|
+
ChildApplication.validate(name, app);
|
|
1635
|
+
super(name, {
|
|
1636
|
+
app,
|
|
1637
|
+
overrides,
|
|
1638
|
+
isDefault: false
|
|
1639
|
+
});
|
|
1640
|
+
this.default = false;
|
|
1641
|
+
this.routing = app.routing;
|
|
1642
|
+
}
|
|
1643
|
+
static validate(name, app) {
|
|
1644
|
+
validateAppPaths(name, app);
|
|
1645
|
+
}
|
|
1646
|
+
};
|
|
1647
|
+
|
|
1648
|
+
// src/config-v2/microfrontends-config/isomorphic/constants.ts
|
|
1649
|
+
var DEFAULT_LOCAL_PROXY_PORT2 = 3024;
|
|
1650
|
+
|
|
1651
|
+
// src/config-v2/microfrontends-config/isomorphic/index.ts
|
|
1652
|
+
var MicrofrontendConfigIsomorphic = class {
|
|
1653
|
+
constructor({
|
|
1654
|
+
config,
|
|
1655
|
+
overrides,
|
|
1656
|
+
meta
|
|
1657
|
+
}) {
|
|
1658
|
+
this.childApplications = {};
|
|
1659
|
+
var _a, _b, _c, _d;
|
|
1660
|
+
MicrofrontendConfigIsomorphic.validate(config);
|
|
1661
|
+
const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
1662
|
+
this.overrides = overrides && !disableOverrides ? overrides : void 0;
|
|
1663
|
+
this.isMainConfig = isMainConfig(config);
|
|
1664
|
+
if (isMainConfig(config)) {
|
|
1665
|
+
for (const [appId, appConfig] of Object.entries(config.applications)) {
|
|
1666
|
+
const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
|
|
1667
|
+
if (isDefaultApp(appConfig)) {
|
|
1668
|
+
this.defaultApplication = new DefaultApplication(appId, {
|
|
1669
|
+
app: appConfig,
|
|
1670
|
+
overrides: appOverrides
|
|
1671
|
+
});
|
|
1672
|
+
} else {
|
|
1673
|
+
this.childApplications[appId] = new ChildApplication(appId, {
|
|
1674
|
+
app: appConfig,
|
|
1675
|
+
overrides: appOverrides
|
|
1676
|
+
});
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
} else {
|
|
1680
|
+
this.partOf = config.partOf;
|
|
1681
|
+
const appOverrides = !disableOverrides ? (_d = this.overrides) == null ? void 0 : _d.applications[meta.fromApp] : void 0;
|
|
1682
|
+
this.childApplications[meta.fromApp] = new ChildApplication(
|
|
1683
|
+
meta.fromApp,
|
|
1684
|
+
{
|
|
1685
|
+
// we don't know routing because we're not in the main config
|
|
1686
|
+
app: { routing: [] },
|
|
1687
|
+
overrides: appOverrides
|
|
1688
|
+
}
|
|
1689
|
+
);
|
|
1690
|
+
}
|
|
1691
|
+
if (isMainConfig(config) && !this.defaultApplication) {
|
|
1692
|
+
throw new MicrofrontendError2(
|
|
1693
|
+
`Could not find default application in microfrontends configuration`,
|
|
1694
|
+
{
|
|
1695
|
+
type: "application",
|
|
1696
|
+
subtype: "not_found"
|
|
1697
|
+
}
|
|
1698
|
+
);
|
|
1699
|
+
}
|
|
1700
|
+
this.config = config;
|
|
1701
|
+
this.options = config.options;
|
|
1702
|
+
this.serialized = {
|
|
1703
|
+
config,
|
|
1704
|
+
overrides,
|
|
1705
|
+
meta
|
|
1706
|
+
};
|
|
1707
|
+
}
|
|
1708
|
+
static validate(config) {
|
|
1709
|
+
const c = typeof config === "string" ? parse2(config) : config;
|
|
1710
|
+
if (isMainConfig(c)) {
|
|
1711
|
+
validateConfigVersion(c.version);
|
|
1712
|
+
validateConfigPaths(c.applications);
|
|
1713
|
+
validateConfigDefaultApplication(c.applications);
|
|
1714
|
+
}
|
|
1715
|
+
return c;
|
|
1716
|
+
}
|
|
1717
|
+
static fromEnv({
|
|
1718
|
+
meta,
|
|
1719
|
+
cookies
|
|
1720
|
+
}) {
|
|
1721
|
+
return new MicrofrontendConfigIsomorphic({
|
|
1722
|
+
config: parse2(getConfigStringFromEnv()),
|
|
1723
|
+
overrides: parseOverrides(cookies ?? []),
|
|
1724
|
+
meta
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
isOverridesDisabled() {
|
|
1728
|
+
var _a, _b;
|
|
1729
|
+
return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
1730
|
+
}
|
|
1731
|
+
getConfig() {
|
|
1732
|
+
return this.config;
|
|
1733
|
+
}
|
|
1734
|
+
getApplicationsByType() {
|
|
1735
|
+
return {
|
|
1736
|
+
defaultApplication: this.defaultApplication,
|
|
1737
|
+
applications: Object.values(this.childApplications)
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
getChildApplications() {
|
|
1741
|
+
return Object.values(this.childApplications);
|
|
1742
|
+
}
|
|
1743
|
+
getAllApplications() {
|
|
1744
|
+
return [
|
|
1745
|
+
this.defaultApplication,
|
|
1746
|
+
...Object.values(this.childApplications)
|
|
1747
|
+
].filter(Boolean);
|
|
1748
|
+
}
|
|
1749
|
+
getApplication(name) {
|
|
1750
|
+
var _a;
|
|
1751
|
+
if (((_a = this.defaultApplication) == null ? void 0 : _a.name) === name) {
|
|
1752
|
+
return this.defaultApplication;
|
|
1753
|
+
}
|
|
1754
|
+
const app = this.childApplications[name];
|
|
1755
|
+
if (!app) {
|
|
1756
|
+
throw new MicrofrontendError2(
|
|
1757
|
+
`Could not find microfrontends configuration for application "${name}"`,
|
|
1758
|
+
{
|
|
1759
|
+
type: "application",
|
|
1760
|
+
subtype: "not_found"
|
|
1761
|
+
}
|
|
1762
|
+
);
|
|
1763
|
+
}
|
|
1764
|
+
return app;
|
|
1765
|
+
}
|
|
1766
|
+
getApplicationByProjectId(projectId) {
|
|
1767
|
+
var _a, _b;
|
|
1768
|
+
if (((_b = (_a = this.defaultApplication) == null ? void 0 : _a.vercel) == null ? void 0 : _b.projectId) === projectId) {
|
|
1769
|
+
return this.defaultApplication;
|
|
1770
|
+
}
|
|
1771
|
+
return Object.values(this.childApplications).find(
|
|
1772
|
+
(app) => {
|
|
1773
|
+
var _a2;
|
|
1774
|
+
return ((_a2 = app.vercel) == null ? void 0 : _a2.projectId) === projectId;
|
|
1775
|
+
}
|
|
1776
|
+
);
|
|
1777
|
+
}
|
|
1778
|
+
/**
|
|
1779
|
+
* Returns the default application. This can throw if the default application
|
|
1780
|
+
* is undefined ( )
|
|
1781
|
+
*/
|
|
1782
|
+
getDefaultApplication() {
|
|
1783
|
+
if (!this.defaultApplication) {
|
|
1784
|
+
throw new MicrofrontendError2(
|
|
1785
|
+
`Could not find default application in microfrontends configuration`,
|
|
1786
|
+
{
|
|
1787
|
+
type: "application",
|
|
1788
|
+
subtype: "not_found"
|
|
1789
|
+
}
|
|
1790
|
+
);
|
|
1791
|
+
}
|
|
1792
|
+
return this.defaultApplication;
|
|
1793
|
+
}
|
|
1794
|
+
/**
|
|
1795
|
+
* Returns the configured port for the local proxy
|
|
1796
|
+
*/
|
|
1797
|
+
getLocalProxyPort() {
|
|
1798
|
+
var _a, _b;
|
|
1799
|
+
return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT2;
|
|
1800
|
+
}
|
|
1801
|
+
/**
|
|
1802
|
+
* Serializes the class back to the Schema type.
|
|
1803
|
+
*
|
|
1804
|
+
* NOTE: This is used when writing the config to disk and must always match the input Schema
|
|
1805
|
+
*/
|
|
1806
|
+
toSchemaJson() {
|
|
1807
|
+
return this.serialized.config;
|
|
1808
|
+
}
|
|
1809
|
+
toClientConfig() {
|
|
1810
|
+
const applications = Object.fromEntries(
|
|
1811
|
+
Object.entries(this.childApplications).map(([name, application]) => [
|
|
1812
|
+
name,
|
|
1813
|
+
{
|
|
1814
|
+
default: false,
|
|
1815
|
+
routing: application.routing
|
|
1816
|
+
}
|
|
1817
|
+
])
|
|
1818
|
+
);
|
|
1819
|
+
if (this.defaultApplication) {
|
|
1820
|
+
applications[this.defaultApplication.name] = {
|
|
1821
|
+
default: true
|
|
1822
|
+
};
|
|
1823
|
+
}
|
|
1824
|
+
return new MicrofrontendConfigClient({
|
|
1825
|
+
applications
|
|
1826
|
+
});
|
|
1827
|
+
}
|
|
1828
|
+
serialize() {
|
|
1829
|
+
return this.serialized;
|
|
1830
|
+
}
|
|
1831
|
+
};
|
|
1832
|
+
|
|
1833
|
+
// src/config-v2/microfrontends-config/isomorphic/child.ts
|
|
1834
|
+
var MicrofrontendChildConfig = class extends MicrofrontendConfigIsomorphic {
|
|
1835
|
+
constructor({
|
|
1836
|
+
config,
|
|
1837
|
+
overrides,
|
|
1838
|
+
meta
|
|
1839
|
+
}) {
|
|
1840
|
+
super({ config, overrides, meta });
|
|
1841
|
+
this.isMainConfig = false;
|
|
1842
|
+
this.partOf = config.partOf;
|
|
1843
|
+
}
|
|
1844
|
+
};
|
|
1845
|
+
|
|
1846
|
+
// src/config-v2/microfrontends-config/isomorphic/main.ts
|
|
1847
|
+
var MicrofrontendMainConfig = class extends MicrofrontendConfigIsomorphic {
|
|
1848
|
+
constructor({
|
|
1849
|
+
config,
|
|
1850
|
+
overrides,
|
|
1851
|
+
meta
|
|
1852
|
+
}) {
|
|
1853
|
+
var _a, _b, _c;
|
|
1854
|
+
super({ config, overrides, meta });
|
|
1855
|
+
this.isMainConfig = true;
|
|
1856
|
+
const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
|
|
1857
|
+
let defaultApplication;
|
|
1858
|
+
for (const [appId, appConfig] of Object.entries(config.applications)) {
|
|
1859
|
+
const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
|
|
1860
|
+
if (isDefaultApp(appConfig)) {
|
|
1861
|
+
defaultApplication = new DefaultApplication(appId, {
|
|
1862
|
+
app: appConfig,
|
|
1863
|
+
overrides: appOverrides
|
|
1864
|
+
});
|
|
1865
|
+
} else {
|
|
1866
|
+
this.childApplications[appId] = new ChildApplication(appId, {
|
|
1867
|
+
app: appConfig,
|
|
1868
|
+
overrides: appOverrides
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1872
|
+
if (!defaultApplication) {
|
|
1873
|
+
throw new MicrofrontendError2(
|
|
1874
|
+
`Could not find default application in microfrontends configuration`,
|
|
1875
|
+
{
|
|
1876
|
+
type: "application",
|
|
1877
|
+
subtype: "not_found"
|
|
1878
|
+
}
|
|
1879
|
+
);
|
|
1880
|
+
}
|
|
1881
|
+
this.defaultApplication = defaultApplication;
|
|
1882
|
+
}
|
|
1883
|
+
};
|
|
1884
|
+
|
|
1885
|
+
// src/config-v2/microfrontends/isomorphic/index.ts
|
|
1886
|
+
var Microfrontends = class {
|
|
1887
|
+
constructor({
|
|
1888
|
+
config,
|
|
1889
|
+
overrides,
|
|
1890
|
+
meta
|
|
1891
|
+
}) {
|
|
1892
|
+
if (isMainConfig(config)) {
|
|
1893
|
+
this.config = new MicrofrontendMainConfig({ config, overrides, meta });
|
|
1894
|
+
} else {
|
|
1895
|
+
this.config = new MicrofrontendChildConfig({ config, overrides, meta });
|
|
1896
|
+
}
|
|
1897
|
+
}
|
|
1898
|
+
isChildConfig() {
|
|
1899
|
+
return this.config instanceof MicrofrontendChildConfig;
|
|
1900
|
+
}
|
|
1901
|
+
static fromEnv({
|
|
1902
|
+
cookies,
|
|
1903
|
+
meta
|
|
1904
|
+
}) {
|
|
1905
|
+
const config = MicrofrontendConfigIsomorphic.fromEnv({
|
|
1906
|
+
cookies,
|
|
1907
|
+
meta
|
|
1908
|
+
});
|
|
1909
|
+
return new Microfrontends(config.serialize());
|
|
1910
|
+
}
|
|
1911
|
+
};
|
|
1912
|
+
|
|
1913
|
+
// src/config-v2/microfrontends/utils/find-repository-root.ts
|
|
1914
|
+
import fs3 from "node:fs";
|
|
1915
|
+
import path3 from "node:path";
|
|
1916
|
+
var GIT_DIRECTORY = ".git";
|
|
1917
|
+
function findRepositoryRoot(startDir) {
|
|
1918
|
+
let currentDir = startDir || process.cwd();
|
|
1919
|
+
while (currentDir !== path3.parse(currentDir).root) {
|
|
1920
|
+
const gitPath = path3.join(currentDir, GIT_DIRECTORY);
|
|
1921
|
+
if (fs3.existsSync(gitPath) && fs3.statSync(gitPath).isDirectory()) {
|
|
1922
|
+
return currentDir;
|
|
1923
|
+
}
|
|
1924
|
+
currentDir = path3.dirname(currentDir);
|
|
1925
|
+
}
|
|
1926
|
+
throw new Error(
|
|
1927
|
+
"Repository root not found. Specify the root of the repository with the `repository.root` option."
|
|
1928
|
+
);
|
|
1929
|
+
}
|
|
1930
|
+
|
|
1931
|
+
// src/config-v2/microfrontends/utils/find-package-path.ts
|
|
1932
|
+
import { dirname as dirname2 } from "node:path";
|
|
1933
|
+
import { readFileSync } from "node:fs";
|
|
1934
|
+
import fg from "fast-glob";
|
|
1935
|
+
var configCache = {};
|
|
1936
|
+
function findPackagePathWithGlob({
|
|
1937
|
+
repositoryRoot,
|
|
1938
|
+
name
|
|
1939
|
+
}) {
|
|
1940
|
+
try {
|
|
1941
|
+
const packageJsonPaths = fg.globSync("**/package.json", {
|
|
1942
|
+
cwd: repositoryRoot,
|
|
1943
|
+
absolute: true,
|
|
1944
|
+
onlyFiles: true,
|
|
1945
|
+
followSymbolicLinks: false,
|
|
1946
|
+
ignore: ["**/node_modules/**", "**/.git/**"]
|
|
1947
|
+
});
|
|
1948
|
+
const matchingPaths = [];
|
|
1949
|
+
for (const packageJsonPath2 of packageJsonPaths) {
|
|
1950
|
+
const packageJsonContent = readFileSync(packageJsonPath2, "utf-8");
|
|
1951
|
+
const packageJson = JSON.parse(packageJsonContent);
|
|
1952
|
+
if (packageJson.name === name) {
|
|
1953
|
+
matchingPaths.push(packageJsonPath2);
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
if (matchingPaths.length > 1) {
|
|
1957
|
+
throw new Error(
|
|
1958
|
+
`Found multiple packages with the name "${name}" in the repository: ${matchingPaths.join(", ")}`
|
|
1959
|
+
);
|
|
1960
|
+
}
|
|
1961
|
+
if (matchingPaths.length === 0) {
|
|
1962
|
+
throw new Error(
|
|
1963
|
+
`Could not find package with the name "${name}" in the repository`
|
|
1964
|
+
);
|
|
1965
|
+
}
|
|
1966
|
+
const [packageJsonPath] = matchingPaths;
|
|
1967
|
+
return dirname2(packageJsonPath);
|
|
1968
|
+
} catch (error) {
|
|
1969
|
+
return null;
|
|
1970
|
+
}
|
|
1971
|
+
}
|
|
1972
|
+
function findPackagePath(opts) {
|
|
1973
|
+
const cacheKey = `${opts.repositoryRoot}-${opts.name}`;
|
|
1974
|
+
if (configCache[cacheKey]) {
|
|
1975
|
+
return configCache[cacheKey];
|
|
1976
|
+
}
|
|
1977
|
+
const result = findPackagePathWithGlob(opts);
|
|
1978
|
+
if (!result) {
|
|
1979
|
+
throw new Error(
|
|
1980
|
+
`Could not find package with the name "${opts.name}" in the repository`
|
|
1981
|
+
);
|
|
1982
|
+
}
|
|
1983
|
+
configCache[cacheKey] = result;
|
|
1984
|
+
return result;
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
// src/config-v2/microfrontends/utils/find-default-package.ts
|
|
1988
|
+
import { dirname as dirname3 } from "node:path";
|
|
1989
|
+
import { readFileSync as readFileSync2 } from "node:fs";
|
|
1990
|
+
import { parse as parse3 } from "jsonc-parser";
|
|
1991
|
+
import fg2 from "fast-glob";
|
|
1992
|
+
|
|
1993
|
+
// src/config-v2/constants.ts
|
|
1994
|
+
var CONFIGURATION_FILENAMES = [
|
|
1995
|
+
"microfrontends.jsonc",
|
|
1996
|
+
"microfrontends.json"
|
|
1997
|
+
];
|
|
1998
|
+
|
|
1999
|
+
// src/config-v2/microfrontends/utils/find-default-package.ts
|
|
2000
|
+
var configCache2 = {};
|
|
2001
|
+
function findDefaultMicrofrontendsPackages({
|
|
2002
|
+
repositoryRoot,
|
|
2003
|
+
applicationName
|
|
2004
|
+
}) {
|
|
2005
|
+
try {
|
|
2006
|
+
const microfrontendsJsonPaths = fg2.globSync(
|
|
2007
|
+
`**/{${CONFIGURATION_FILENAMES.join(",")}}`,
|
|
2008
|
+
{
|
|
2009
|
+
cwd: repositoryRoot,
|
|
2010
|
+
absolute: true,
|
|
2011
|
+
onlyFiles: true,
|
|
2012
|
+
followSymbolicLinks: false,
|
|
2013
|
+
ignore: ["**/node_modules/**", "**/.git/**"]
|
|
2014
|
+
}
|
|
2015
|
+
);
|
|
2016
|
+
const matchingPaths = [];
|
|
2017
|
+
for (const microfrontendsJsonPath of microfrontendsJsonPaths) {
|
|
2018
|
+
const microfrontendsJsonContent = readFileSync2(
|
|
2019
|
+
microfrontendsJsonPath,
|
|
2020
|
+
"utf-8"
|
|
2021
|
+
);
|
|
2022
|
+
const microfrontendsJson = parse3(microfrontendsJsonContent);
|
|
2023
|
+
if (isMainConfig(microfrontendsJson) && microfrontendsJson.applications[applicationName]) {
|
|
2024
|
+
matchingPaths.push(microfrontendsJsonPath);
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
if (matchingPaths.length > 1) {
|
|
2028
|
+
throw new Error(
|
|
2029
|
+
`Found multiple default applications referencing "${applicationName}" in the repository, this is not yet supported.
|
|
2030
|
+
${matchingPaths.join("\n \u2022 ")}`
|
|
2031
|
+
);
|
|
2032
|
+
}
|
|
2033
|
+
if (matchingPaths.length === 0) {
|
|
2034
|
+
throw new Error(
|
|
2035
|
+
`Could not find default application with "applications.${applicationName}"`
|
|
2036
|
+
);
|
|
2037
|
+
}
|
|
2038
|
+
const [packageJsonPath] = matchingPaths;
|
|
2039
|
+
return dirname3(packageJsonPath);
|
|
2040
|
+
} catch (error) {
|
|
2041
|
+
return null;
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
function findDefaultMicrofrontendsPackage(opts) {
|
|
2045
|
+
const cacheKey = `${opts.repositoryRoot}-${opts.applicationName}`;
|
|
2046
|
+
if (configCache2[cacheKey]) {
|
|
2047
|
+
return configCache2[cacheKey];
|
|
2048
|
+
}
|
|
2049
|
+
const result = findDefaultMicrofrontendsPackages(opts);
|
|
2050
|
+
if (!result) {
|
|
2051
|
+
throw new Error(
|
|
2052
|
+
`Error trying to resolve the main microfrontends configuration`
|
|
2053
|
+
);
|
|
2054
|
+
}
|
|
2055
|
+
configCache2[cacheKey] = result;
|
|
2056
|
+
return result;
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
// src/config-v2/microfrontends/utils/is-monorepo.ts
|
|
2060
|
+
import fs4 from "node:fs";
|
|
2061
|
+
import path4 from "node:path";
|
|
2062
|
+
function isMonorepo({
|
|
2063
|
+
repositoryRoot
|
|
2064
|
+
}) {
|
|
2065
|
+
try {
|
|
2066
|
+
if (fs4.existsSync(path4.join(repositoryRoot, "pnpm-workspace.yaml"))) {
|
|
2067
|
+
return true;
|
|
2068
|
+
}
|
|
2069
|
+
if (fs4.existsSync(path4.join(repositoryRoot, "vlt-workspaces.json"))) {
|
|
2070
|
+
return true;
|
|
2071
|
+
}
|
|
2072
|
+
const packageJsonPath = path4.join(repositoryRoot, "package.json");
|
|
2073
|
+
if (!fs4.existsSync(packageJsonPath)) {
|
|
2074
|
+
return false;
|
|
2075
|
+
}
|
|
2076
|
+
const packageJson = JSON.parse(
|
|
2077
|
+
fs4.readFileSync(packageJsonPath, "utf-8")
|
|
2078
|
+
);
|
|
2079
|
+
return packageJson.workspaces !== void 0;
|
|
2080
|
+
} catch (error) {
|
|
2081
|
+
console.error("Error determining if repository is a monorepo", error);
|
|
2082
|
+
return false;
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
|
|
2086
|
+
// src/config-v2/microfrontends/utils/find-package-root.ts
|
|
2087
|
+
import fs5 from "node:fs";
|
|
2088
|
+
import path5 from "node:path";
|
|
2089
|
+
var PACKAGE_JSON = "package.json";
|
|
2090
|
+
function findPackageRoot(startDir) {
|
|
2091
|
+
let currentDir = startDir || process.cwd();
|
|
2092
|
+
while (currentDir !== path5.parse(currentDir).root) {
|
|
2093
|
+
const pkgJsonPath = path5.join(currentDir, PACKAGE_JSON);
|
|
2094
|
+
if (fs5.existsSync(pkgJsonPath)) {
|
|
2095
|
+
return currentDir;
|
|
2096
|
+
}
|
|
2097
|
+
currentDir = path5.dirname(currentDir);
|
|
2098
|
+
}
|
|
2099
|
+
throw new Error(
|
|
2100
|
+
"Package root not found. Specify the root of the package with the `package.root` option."
|
|
2101
|
+
);
|
|
2102
|
+
}
|
|
2103
|
+
|
|
2104
|
+
// src/config-v2/microfrontends/utils/find-config.ts
|
|
2105
|
+
import fs6 from "node:fs";
|
|
2106
|
+
import { join } from "node:path";
|
|
2107
|
+
function findConfig({ dir }) {
|
|
2108
|
+
for (const filename of CONFIGURATION_FILENAMES) {
|
|
2109
|
+
const maybeConfig = join(dir, filename);
|
|
2110
|
+
if (fs6.existsSync(maybeConfig)) {
|
|
2111
|
+
return maybeConfig;
|
|
2112
|
+
}
|
|
2113
|
+
}
|
|
2114
|
+
return null;
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
// src/config-v2/microfrontends/server/validation.ts
|
|
2118
|
+
import { parse as parse4 } from "jsonc-parser";
|
|
2119
|
+
import { Ajv as Ajv2 } from "ajv";
|
|
2120
|
+
|
|
2121
|
+
// schema/schema-v2.json
|
|
2122
|
+
var schema_v2_default = {
|
|
2123
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
2124
|
+
$ref: "#/definitions/Config",
|
|
2125
|
+
definitions: {
|
|
2126
|
+
Config: {
|
|
2127
|
+
anyOf: [
|
|
2128
|
+
{
|
|
2129
|
+
$ref: "#/definitions/MainConfig"
|
|
2130
|
+
},
|
|
2131
|
+
{
|
|
2132
|
+
$ref: "#/definitions/ChildConfig"
|
|
2133
|
+
}
|
|
2134
|
+
]
|
|
2135
|
+
},
|
|
2136
|
+
MainConfig: {
|
|
2137
|
+
type: "object",
|
|
2138
|
+
properties: {
|
|
2139
|
+
$schema: {
|
|
2140
|
+
type: "string"
|
|
2141
|
+
},
|
|
2142
|
+
version: {
|
|
2143
|
+
type: "string",
|
|
2144
|
+
const: "2"
|
|
2145
|
+
},
|
|
2146
|
+
options: {
|
|
2147
|
+
$ref: "#/definitions/Options"
|
|
2148
|
+
},
|
|
2149
|
+
remotes: {
|
|
2150
|
+
type: "object",
|
|
2151
|
+
additionalProperties: {
|
|
2152
|
+
$ref: "#/definitions/Application"
|
|
2153
|
+
},
|
|
2154
|
+
description: "Applications that only serve a subset of the microfrontend routes only need to reference the name of the primary application that owns the full microfrontends configuration."
|
|
2155
|
+
},
|
|
2156
|
+
applications: {
|
|
2157
|
+
$ref: "#/definitions/ApplicationRouting",
|
|
2158
|
+
description: "Mapping of application names to the routes that they host. Only needs to be defined in the application that owns the primary microfrontend domain"
|
|
2159
|
+
}
|
|
2160
|
+
},
|
|
2161
|
+
required: ["applications", "version"]
|
|
2162
|
+
},
|
|
2163
|
+
Options: {
|
|
2164
|
+
type: "object",
|
|
2165
|
+
properties: {
|
|
2166
|
+
vercel: {
|
|
2167
|
+
$ref: "#/definitions/VercelOptions",
|
|
2168
|
+
description: "Micro-Frontends wide options for Vercel."
|
|
2169
|
+
},
|
|
2170
|
+
localProxy: {
|
|
2171
|
+
$ref: "#/definitions/LocalProxyOptions",
|
|
2172
|
+
description: "Options for local proxy."
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
},
|
|
2176
|
+
VercelOptions: {
|
|
2177
|
+
type: "object",
|
|
2178
|
+
properties: {
|
|
2179
|
+
teamSlug: {
|
|
2180
|
+
type: "string",
|
|
2181
|
+
description: "Team slug for the Vercel team"
|
|
2182
|
+
},
|
|
2183
|
+
disableOverrides: {
|
|
2184
|
+
type: "boolean",
|
|
2185
|
+
description: "If you want to disable the overrides for the site. For example, if you are managing rewrites between applications externally, you may wish to disable the overrides on the toolbar as they will have no effect."
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
},
|
|
2189
|
+
LocalProxyOptions: {
|
|
2190
|
+
type: "object",
|
|
2191
|
+
properties: {
|
|
2192
|
+
port: {
|
|
2193
|
+
type: "number",
|
|
2194
|
+
description: "The port number used by the local proxy server.\n\nThe default is `3024`."
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
},
|
|
2198
|
+
Application: {
|
|
2199
|
+
anyOf: [
|
|
2200
|
+
{
|
|
2201
|
+
$ref: "#/definitions/DefaultApplication"
|
|
2202
|
+
},
|
|
2203
|
+
{
|
|
2204
|
+
$ref: "#/definitions/ChildApplication"
|
|
2205
|
+
}
|
|
2206
|
+
]
|
|
2207
|
+
},
|
|
2208
|
+
DefaultApplication: {
|
|
2209
|
+
type: "object",
|
|
2210
|
+
properties: {
|
|
2211
|
+
vercel: {
|
|
2212
|
+
$ref: "#/definitions/Vercel"
|
|
2213
|
+
},
|
|
2214
|
+
development: {
|
|
2215
|
+
$ref: "#/definitions/Development"
|
|
2216
|
+
},
|
|
2217
|
+
production: {
|
|
2218
|
+
$ref: "#/definitions/HostConfig"
|
|
2219
|
+
}
|
|
2220
|
+
},
|
|
2221
|
+
required: ["production"]
|
|
2222
|
+
},
|
|
2223
|
+
Vercel: {
|
|
2224
|
+
type: "object",
|
|
2225
|
+
properties: {
|
|
2226
|
+
projectId: {
|
|
2227
|
+
type: "string",
|
|
2228
|
+
description: "Vercel project ID"
|
|
2229
|
+
}
|
|
2230
|
+
},
|
|
2231
|
+
required: ["projectId"]
|
|
2232
|
+
},
|
|
2233
|
+
Development: {
|
|
2234
|
+
type: "object",
|
|
2235
|
+
properties: {
|
|
2236
|
+
local: {
|
|
2237
|
+
$ref: "#/definitions/LocalHostConfig"
|
|
2238
|
+
},
|
|
2239
|
+
fallback: {
|
|
2240
|
+
$ref: "#/definitions/HostConfig",
|
|
2241
|
+
description: "Fallback for local development, could be a host config that points to any environment. If this is not provided, or the application is not running - requests to the application in local development will error."
|
|
2242
|
+
},
|
|
2243
|
+
task: {
|
|
2244
|
+
type: "string",
|
|
2245
|
+
description: "Optional task to run when starting the development server. Should reference a script in the package.json of the application."
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
},
|
|
2249
|
+
LocalHostConfig: {
|
|
2250
|
+
type: "object",
|
|
2251
|
+
properties: {
|
|
2252
|
+
host: {
|
|
2253
|
+
type: "string",
|
|
2254
|
+
description: "The hostname or IP address of the server. This can be a domain name (e.g., `example.com`) or an IP address (e.g., `192.168.1.1`)."
|
|
2255
|
+
},
|
|
2256
|
+
protocol: {
|
|
2257
|
+
type: "string",
|
|
2258
|
+
enum: ["http", "https"],
|
|
2259
|
+
description: 'The protocol to be used for the connection.\n- `http`: Hypertext Transfer Protocol (HTTP).\n- `https`: Secure Hypertext Transfer Protocol (HTTPS).\n\n* @defaultValue "http" for local development, "https" for otherwise'
|
|
2260
|
+
},
|
|
2261
|
+
port: {
|
|
2262
|
+
type: "number",
|
|
2263
|
+
description: "The port number to be used for the connection. Common values include `80` for HTTP and `443` for HTTPS."
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
},
|
|
2267
|
+
HostConfig: {
|
|
2268
|
+
type: "object",
|
|
2269
|
+
properties: {
|
|
2270
|
+
protocol: {
|
|
2271
|
+
type: "string",
|
|
2272
|
+
enum: ["http", "https"],
|
|
2273
|
+
description: 'The protocol to be used for the connection.\n- `http`: Hypertext Transfer Protocol (HTTP).\n- `https`: Secure Hypertext Transfer Protocol (HTTPS).\n\n* @defaultValue "http" for local development, "https" for otherwise'
|
|
2274
|
+
},
|
|
2275
|
+
host: {
|
|
2276
|
+
type: "string",
|
|
2277
|
+
description: "The hostname or IP address of the server. This can be a domain name (e.g., `example.com`) or an IP address (e.g., `192.168.1.1`)."
|
|
2278
|
+
},
|
|
2279
|
+
port: {
|
|
2280
|
+
type: "number",
|
|
2281
|
+
description: "The port number to be used for the connection. Common values include `80` for HTTP and `443` for HTTPS."
|
|
2282
|
+
}
|
|
2283
|
+
},
|
|
2284
|
+
required: ["host"]
|
|
2285
|
+
},
|
|
2286
|
+
ChildApplication: {
|
|
2287
|
+
type: "object",
|
|
2288
|
+
properties: {
|
|
2289
|
+
vercel: {
|
|
2290
|
+
$ref: "#/definitions/Vercel"
|
|
2291
|
+
},
|
|
2292
|
+
development: {
|
|
2293
|
+
$ref: "#/definitions/Development"
|
|
2294
|
+
},
|
|
2295
|
+
routing: {
|
|
2296
|
+
$ref: "#/definitions/Routing",
|
|
2297
|
+
description: "Groups of path expressions that are routed to this application."
|
|
2298
|
+
},
|
|
2299
|
+
production: {
|
|
2300
|
+
$ref: "#/definitions/HostConfig"
|
|
2301
|
+
}
|
|
2302
|
+
},
|
|
2303
|
+
required: ["routing"]
|
|
2304
|
+
},
|
|
2305
|
+
Routing: {
|
|
2306
|
+
type: "array",
|
|
2307
|
+
items: {
|
|
2308
|
+
$ref: "#/definitions/PathGroup"
|
|
2309
|
+
}
|
|
2310
|
+
},
|
|
2311
|
+
PathGroup: {
|
|
2312
|
+
type: "object",
|
|
2313
|
+
properties: {
|
|
2314
|
+
group: {
|
|
2315
|
+
type: "string",
|
|
2316
|
+
description: "Optional group name for the paths"
|
|
2317
|
+
},
|
|
2318
|
+
flag: {
|
|
2319
|
+
type: "string",
|
|
2320
|
+
description: "flag name that can be used to enable/disable all paths in the group"
|
|
2321
|
+
},
|
|
2322
|
+
paths: {
|
|
2323
|
+
type: "array",
|
|
2324
|
+
items: {
|
|
2325
|
+
type: "string"
|
|
2326
|
+
}
|
|
2327
|
+
}
|
|
2328
|
+
},
|
|
2329
|
+
required: ["paths"]
|
|
2330
|
+
},
|
|
2331
|
+
ApplicationRouting: {
|
|
2332
|
+
type: "object",
|
|
2333
|
+
additionalProperties: {
|
|
2334
|
+
$ref: "#/definitions/Application"
|
|
2335
|
+
}
|
|
2336
|
+
},
|
|
2337
|
+
ChildConfig: {
|
|
2338
|
+
type: "object",
|
|
2339
|
+
properties: {
|
|
2340
|
+
$schema: {
|
|
2341
|
+
type: "string"
|
|
2342
|
+
},
|
|
2343
|
+
version: {
|
|
2344
|
+
type: "string",
|
|
2345
|
+
const: "2"
|
|
2346
|
+
},
|
|
2347
|
+
options: {
|
|
2348
|
+
$ref: "#/definitions/Options"
|
|
2349
|
+
},
|
|
2350
|
+
remotes: {
|
|
2351
|
+
type: "object",
|
|
2352
|
+
additionalProperties: {
|
|
2353
|
+
$ref: "#/definitions/Application"
|
|
2354
|
+
},
|
|
2355
|
+
description: "Applications that only serve a subset of the microfrontend routes only need to reference the name of the primary application that owns the full microfrontends configuration."
|
|
2356
|
+
},
|
|
2357
|
+
partOf: {
|
|
2358
|
+
type: "string",
|
|
2359
|
+
description: "Applications that only serve a subset of the microfrontend routes only need to reference the name of the primary application that owns the full microfrontends configuration."
|
|
2360
|
+
}
|
|
2361
|
+
},
|
|
2362
|
+
required: ["partOf", "version"]
|
|
2363
|
+
}
|
|
2364
|
+
}
|
|
2365
|
+
};
|
|
2366
|
+
|
|
2367
|
+
// src/config-v2/schema/utils/load.ts
|
|
2368
|
+
var SCHEMA2 = schema_v2_default;
|
|
2369
|
+
|
|
2370
|
+
// src/config-v2/microfrontends/server/validation.ts
|
|
2371
|
+
function validateSchema2(configString) {
|
|
2372
|
+
const parsedConfig = parse4(configString);
|
|
2373
|
+
const ajv = new Ajv2();
|
|
2374
|
+
const validate = ajv.compile(SCHEMA2);
|
|
2375
|
+
const isValid = validate(parsedConfig);
|
|
2376
|
+
if (!isValid) {
|
|
2377
|
+
throw new MicrofrontendError(
|
|
2378
|
+
`Invalid config: ${ajv.errorsText(validate.errors)}`,
|
|
2379
|
+
{ type: "config", subtype: "does_not_match_schema" }
|
|
2380
|
+
);
|
|
2381
|
+
}
|
|
2382
|
+
return parsedConfig;
|
|
2383
|
+
}
|
|
2384
|
+
|
|
2385
|
+
// src/config-v2/microfrontends/server/index.ts
|
|
2386
|
+
var MicrofrontendsServer = class extends Microfrontends {
|
|
2387
|
+
/**
|
|
2388
|
+
* Writes the configuration to a file.
|
|
2389
|
+
*/
|
|
2390
|
+
writeConfig(opts = {
|
|
2391
|
+
pretty: true
|
|
2392
|
+
}) {
|
|
2393
|
+
const outputPath = getOutputFilePath();
|
|
2394
|
+
fs7.mkdirSync(dirname4(outputPath), { recursive: true });
|
|
2395
|
+
fs7.writeFileSync(
|
|
2396
|
+
outputPath,
|
|
2397
|
+
JSON.stringify(
|
|
2398
|
+
this.config.toSchemaJson(),
|
|
2399
|
+
null,
|
|
2400
|
+
opts.pretty ?? true ? 2 : void 0
|
|
2401
|
+
)
|
|
2402
|
+
);
|
|
2403
|
+
}
|
|
2404
|
+
// --------- Static Methods ---------
|
|
2405
|
+
/**
|
|
2406
|
+
* Generates a MicrofrontendsServer instance from an unknown object.
|
|
2407
|
+
*/
|
|
2408
|
+
static fromUnknown({
|
|
2409
|
+
config,
|
|
2410
|
+
cookies,
|
|
2411
|
+
meta
|
|
2412
|
+
}) {
|
|
2413
|
+
const overrides = cookies ? parseOverrides(cookies) : void 0;
|
|
2414
|
+
if (typeof config === "string") {
|
|
2415
|
+
return new MicrofrontendsServer({
|
|
2416
|
+
config: MicrofrontendsServer.validate(config),
|
|
2417
|
+
overrides,
|
|
2418
|
+
meta
|
|
2419
|
+
});
|
|
2420
|
+
}
|
|
2421
|
+
if (typeof config === "object") {
|
|
2422
|
+
return new MicrofrontendsServer({
|
|
2423
|
+
config,
|
|
2424
|
+
overrides,
|
|
2425
|
+
meta
|
|
2426
|
+
});
|
|
2427
|
+
}
|
|
2428
|
+
throw new MicrofrontendError2(
|
|
2429
|
+
"Invalid config: must be a string or an object",
|
|
2430
|
+
{ type: "config", subtype: "does_not_match_schema" }
|
|
2431
|
+
);
|
|
2432
|
+
}
|
|
2433
|
+
/**
|
|
2434
|
+
* Generates a MicrofrontendsServer instance from the environment.
|
|
2435
|
+
* Uses additional validation that is only available when in a node runtime
|
|
2436
|
+
*/
|
|
2437
|
+
static fromEnv({
|
|
2438
|
+
cookies,
|
|
2439
|
+
meta
|
|
2440
|
+
}) {
|
|
2441
|
+
return new MicrofrontendsServer({
|
|
2442
|
+
config: MicrofrontendsServer.validate(getConfigStringFromEnv()),
|
|
2443
|
+
overrides: parseOverrides(cookies),
|
|
2444
|
+
meta
|
|
2445
|
+
});
|
|
2446
|
+
}
|
|
2447
|
+
/**
|
|
2448
|
+
* Validates the configuration against the JSON schema
|
|
2449
|
+
*/
|
|
2450
|
+
static validate(config) {
|
|
2451
|
+
if (typeof config === "string") {
|
|
2452
|
+
const c = validateSchema2(config);
|
|
2453
|
+
return c;
|
|
2454
|
+
}
|
|
2455
|
+
return config;
|
|
2456
|
+
}
|
|
2457
|
+
/**
|
|
2458
|
+
* Looks up the configuration by inferring the package root and looking for a microfrontends config file. If a file is not found,
|
|
2459
|
+
* it will look for a package in the repository with a microfrontends file that contains the current application
|
|
2460
|
+
* and use that configuration.
|
|
2461
|
+
*
|
|
2462
|
+
* This can return either a Child or Main configuration.
|
|
2463
|
+
*/
|
|
2464
|
+
static infer({
|
|
2465
|
+
directory,
|
|
2466
|
+
filePath,
|
|
2467
|
+
meta,
|
|
2468
|
+
cookies,
|
|
2469
|
+
options
|
|
2470
|
+
} = {}) {
|
|
2471
|
+
if (filePath && meta) {
|
|
2472
|
+
return MicrofrontendsServer.fromFile({
|
|
2473
|
+
filePath,
|
|
2474
|
+
cookies,
|
|
2475
|
+
meta,
|
|
2476
|
+
options
|
|
2477
|
+
});
|
|
2478
|
+
}
|
|
2479
|
+
try {
|
|
2480
|
+
const packageRoot = findPackageRoot(directory);
|
|
2481
|
+
const packageJsonPath = join2(packageRoot, "package.json");
|
|
2482
|
+
const packageJson = JSON.parse(
|
|
2483
|
+
fs7.readFileSync(packageJsonPath, "utf-8")
|
|
2484
|
+
);
|
|
2485
|
+
if (!packageJson.name) {
|
|
2486
|
+
throw new Error(`No name found in package.json at ${packageJsonPath}`);
|
|
2487
|
+
}
|
|
2488
|
+
const configMeta = meta ?? { fromApp: packageJson.name };
|
|
2489
|
+
const maybeConfig = findConfig({ dir: packageRoot });
|
|
2490
|
+
if (maybeConfig) {
|
|
2491
|
+
return MicrofrontendsServer.fromFile({
|
|
2492
|
+
filePath: maybeConfig,
|
|
2493
|
+
cookies,
|
|
2494
|
+
meta: configMeta,
|
|
2495
|
+
options
|
|
2496
|
+
});
|
|
2497
|
+
}
|
|
2498
|
+
const repositoryRoot = findRepositoryRoot();
|
|
2499
|
+
const isMonorepo2 = isMonorepo({ repositoryRoot });
|
|
2500
|
+
if (isMonorepo2) {
|
|
2501
|
+
const defaultPackage = findDefaultMicrofrontendsPackage({
|
|
2502
|
+
repositoryRoot,
|
|
2503
|
+
applicationName: packageJson.name
|
|
2504
|
+
});
|
|
2505
|
+
const maybeConfigFromDefault = findConfig({ dir: defaultPackage });
|
|
2506
|
+
if (maybeConfigFromDefault) {
|
|
2507
|
+
return MicrofrontendsServer.fromFile({
|
|
2508
|
+
filePath: maybeConfigFromDefault,
|
|
2509
|
+
cookies,
|
|
2510
|
+
meta: configMeta,
|
|
2511
|
+
options
|
|
2512
|
+
});
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
throw new Error("Unable to infer");
|
|
2516
|
+
} catch (e) {
|
|
2517
|
+
throw new MicrofrontendError2(
|
|
2518
|
+
"Unable to infer microfrontends configuration",
|
|
2519
|
+
{ type: "config", subtype: "inference_failed" }
|
|
2520
|
+
);
|
|
2521
|
+
}
|
|
2522
|
+
}
|
|
2523
|
+
/*
|
|
2524
|
+
* Generates a MicrofrontendsServer instance from a file.
|
|
2525
|
+
*/
|
|
2526
|
+
static fromFile({
|
|
2527
|
+
filePath,
|
|
2528
|
+
cookies,
|
|
2529
|
+
meta,
|
|
2530
|
+
options
|
|
2531
|
+
}) {
|
|
2532
|
+
try {
|
|
2533
|
+
const configJson = fs7.readFileSync(filePath, "utf-8");
|
|
2534
|
+
const config = MicrofrontendsServer.validate(configJson);
|
|
2535
|
+
if (!isMainConfig(config) && (options == null ? void 0 : options.resolveMainConfig)) {
|
|
2536
|
+
const repositoryRoot = findRepositoryRoot();
|
|
2537
|
+
const isMonorepo2 = isMonorepo({ repositoryRoot });
|
|
2538
|
+
if (isMonorepo2) {
|
|
2539
|
+
const packagePath = findPackagePath({
|
|
2540
|
+
repositoryRoot,
|
|
2541
|
+
name: config.partOf
|
|
2542
|
+
});
|
|
2543
|
+
if (!packagePath) {
|
|
2544
|
+
throw new MicrofrontendError2(
|
|
2545
|
+
`Could not find default application "${config.partOf}" in the repository`,
|
|
2546
|
+
{ type: "config", subtype: "not_found" }
|
|
2547
|
+
);
|
|
2548
|
+
}
|
|
2549
|
+
const maybeConfig = findConfig({ dir: packagePath });
|
|
2550
|
+
if (!maybeConfig) {
|
|
2551
|
+
throw new MicrofrontendError2(
|
|
2552
|
+
`Could not find microfrontends configuration in ${packagePath}`,
|
|
2553
|
+
{ type: "config", subtype: "not_found" }
|
|
2554
|
+
);
|
|
2555
|
+
}
|
|
2556
|
+
return MicrofrontendsServer.fromMainConfigFile({
|
|
2557
|
+
filePath: maybeConfig,
|
|
2558
|
+
overrides: cookies ? parseOverrides(cookies) : void 0
|
|
2559
|
+
});
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
return new MicrofrontendsServer({
|
|
2563
|
+
config,
|
|
2564
|
+
overrides: cookies ? parseOverrides(cookies) : void 0,
|
|
2565
|
+
meta
|
|
2566
|
+
});
|
|
2567
|
+
} catch (e) {
|
|
2568
|
+
throw MicrofrontendError2.handle(e, {
|
|
2569
|
+
fileName: filePath
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
/*
|
|
2574
|
+
* Generates a MicrofrontendMainConfig instance from a file.
|
|
2575
|
+
*/
|
|
2576
|
+
static fromMainConfigFile({
|
|
2577
|
+
filePath,
|
|
2578
|
+
overrides
|
|
2579
|
+
}) {
|
|
2580
|
+
try {
|
|
2581
|
+
const config = fs7.readFileSync(filePath, "utf-8");
|
|
2582
|
+
const validatedConfig = MicrofrontendsServer.validate(config);
|
|
2583
|
+
if (!isMainConfig(validatedConfig)) {
|
|
2584
|
+
throw new MicrofrontendError2(
|
|
2585
|
+
`${filePath} is not a main microfrontend config`,
|
|
2586
|
+
{
|
|
2587
|
+
type: "config",
|
|
2588
|
+
subtype: "invalid_main_path"
|
|
2589
|
+
}
|
|
2590
|
+
);
|
|
2591
|
+
}
|
|
2592
|
+
const [defaultApplication] = Object.entries(validatedConfig.applications).filter(([, app]) => isDefaultApp(app)).map(([name]) => name);
|
|
2593
|
+
if (!defaultApplication) {
|
|
2594
|
+
throw new MicrofrontendError2(
|
|
2595
|
+
`No default application found. At least one application needs to be the default by omitting routing.`,
|
|
2596
|
+
{ type: "config", subtype: "no_default_application" }
|
|
2597
|
+
);
|
|
2598
|
+
}
|
|
2599
|
+
return new MicrofrontendsServer({
|
|
2600
|
+
config: validatedConfig,
|
|
2601
|
+
overrides,
|
|
2602
|
+
meta: { fromApp: defaultApplication }
|
|
2603
|
+
});
|
|
2604
|
+
} catch (e) {
|
|
2605
|
+
throw MicrofrontendError2.handle(e, {
|
|
2606
|
+
fileName: filePath
|
|
2607
|
+
});
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
};
|
|
2611
|
+
|
|
2612
|
+
// src/utils/mfe-port.ts
|
|
2613
|
+
function mfePort(packageDir) {
|
|
2614
|
+
const { name: appName, version } = getPackageJson(packageDir);
|
|
2615
|
+
const result = loadV2({ packageDir, appName }) || loadV1({ packageDir, appName });
|
|
2616
|
+
if (!result) {
|
|
2617
|
+
throw new MicrofrontendError2(
|
|
2618
|
+
`Unable to determine configured port for ${appName}`,
|
|
2619
|
+
{ type: "config", subtype: "not_found" }
|
|
2620
|
+
);
|
|
2621
|
+
}
|
|
2622
|
+
const { port } = result;
|
|
2623
|
+
return {
|
|
2624
|
+
name: appName,
|
|
2625
|
+
version,
|
|
2626
|
+
port
|
|
2627
|
+
};
|
|
2628
|
+
}
|
|
2629
|
+
function getPackageJson(packageDir) {
|
|
2630
|
+
const filePath = path6.join(packageDir, "package.json");
|
|
2631
|
+
return JSON.parse(fs8.readFileSync(filePath, "utf-8"));
|
|
2632
|
+
}
|
|
2633
|
+
function loadV2({
|
|
2634
|
+
packageDir,
|
|
2635
|
+
appName
|
|
2636
|
+
}) {
|
|
2637
|
+
let config;
|
|
2638
|
+
try {
|
|
2639
|
+
config = MicrofrontendsServer.infer({
|
|
2640
|
+
directory: packageDir,
|
|
2641
|
+
meta: { fromApp: appName },
|
|
2642
|
+
options: { resolveMainConfig: true }
|
|
2643
|
+
});
|
|
2644
|
+
} catch (e) {
|
|
2645
|
+
return void 0;
|
|
2646
|
+
}
|
|
2647
|
+
const app = config.config.getApplication(appName);
|
|
2648
|
+
const port = app.development.local.port;
|
|
2649
|
+
return { port };
|
|
2650
|
+
}
|
|
2651
|
+
function loadV1({
|
|
2652
|
+
packageDir,
|
|
2653
|
+
appName
|
|
2654
|
+
}) {
|
|
2655
|
+
const filePath = path6.join(packageDir, "micro-frontends.jsonc");
|
|
2656
|
+
let config;
|
|
2657
|
+
try {
|
|
2658
|
+
config = MicrofrontendConfig.fromFile({ filePath });
|
|
2659
|
+
} catch (e) {
|
|
2660
|
+
return void 0;
|
|
2661
|
+
}
|
|
2662
|
+
const zone = config.getZone(appName);
|
|
2663
|
+
const port = zone.development.local.port;
|
|
2664
|
+
return { port };
|
|
2665
|
+
}
|
|
2666
|
+
export {
|
|
2667
|
+
mfePort
|
|
2668
|
+
};
|
|
2669
|
+
//# sourceMappingURL=mfe-port.js.map
|