@vercel/microfrontends 0.14.0 → 0.15.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.
Files changed (121) hide show
  1. package/dist/bin/cli.cjs +377 -1626
  2. package/dist/config.cjs +460 -1004
  3. package/dist/config.cjs.map +1 -1
  4. package/dist/config.d.ts +4 -25
  5. package/dist/config.js +456 -983
  6. package/dist/config.js.map +1 -1
  7. package/dist/{v2/microfrontends → microfrontends}/server.cjs +63 -143
  8. package/dist/microfrontends/server.cjs.map +1 -0
  9. package/dist/{v2/microfrontends → microfrontends}/server.d.ts +4 -4
  10. package/dist/{v2/microfrontends → microfrontends}/server.js +63 -143
  11. package/dist/microfrontends/server.js.map +1 -0
  12. package/dist/{v2/microfrontends.cjs → microfrontends.cjs} +21 -21
  13. package/dist/microfrontends.cjs.map +1 -0
  14. package/dist/{v2/microfrontends.d.ts → microfrontends.d.ts} +4 -4
  15. package/dist/{v2/microfrontends.js → microfrontends.js} +20 -20
  16. package/dist/microfrontends.js.map +1 -0
  17. package/dist/next/client.cjs +1 -1
  18. package/dist/next/client.cjs.map +1 -1
  19. package/dist/next/client.js +1 -1
  20. package/dist/next/client.js.map +1 -1
  21. package/dist/next/config.cjs +1323 -1024
  22. package/dist/next/config.cjs.map +1 -1
  23. package/dist/next/config.d.ts +1 -1
  24. package/dist/next/config.js +1327 -1028
  25. package/dist/next/config.js.map +1 -1
  26. package/dist/next/endpoints.cjs +77 -18
  27. package/dist/next/endpoints.cjs.map +1 -1
  28. package/dist/next/endpoints.d.ts +13 -2
  29. package/dist/next/endpoints.js +77 -18
  30. package/dist/next/endpoints.js.map +1 -1
  31. package/dist/next/middleware.cjs +745 -396
  32. package/dist/next/middleware.cjs.map +1 -1
  33. package/dist/next/middleware.d.ts +10 -5
  34. package/dist/next/middleware.js +745 -396
  35. package/dist/next/middleware.js.map +1 -1
  36. package/dist/next/testing.cjs +595 -1032
  37. package/dist/next/testing.cjs.map +1 -1
  38. package/dist/next/testing.d.ts +14 -12
  39. package/dist/next/testing.js +589 -1016
  40. package/dist/next/testing.js.map +1 -1
  41. package/dist/overrides.cjs +40 -108
  42. package/dist/overrides.cjs.map +1 -1
  43. package/dist/overrides.d.ts +24 -2
  44. package/dist/overrides.js +36 -106
  45. package/dist/overrides.js.map +1 -1
  46. package/dist/{v2/routing.cjs → routing.cjs} +3 -3
  47. package/dist/routing.cjs.map +1 -0
  48. package/dist/{v2/schema.cjs → schema.cjs} +1 -1
  49. package/dist/schema.cjs.map +1 -0
  50. package/dist/schema.d.ts +1 -0
  51. package/dist/utils/mfe-port.cjs +211 -1333
  52. package/dist/utils/mfe-port.cjs.map +1 -1
  53. package/dist/utils/mfe-port.js +204 -1326
  54. package/dist/utils/mfe-port.js.map +1 -1
  55. package/dist/validation.cjs +31 -361
  56. package/dist/validation.cjs.map +1 -1
  57. package/dist/validation.d.ts +3 -146
  58. package/dist/validation.js +30 -359
  59. package/dist/validation.js.map +1 -1
  60. package/package.json +29 -92
  61. package/schema/schema.json +174 -244
  62. package/dist/config/client.cjs +0 -54
  63. package/dist/config/client.cjs.map +0 -1
  64. package/dist/config/client.d.ts +0 -23
  65. package/dist/config/client.js +0 -28
  66. package/dist/config/client.js.map +0 -1
  67. package/dist/config/edge.cjs +0 -508
  68. package/dist/config/edge.cjs.map +0 -1
  69. package/dist/config/edge.d.ts +0 -20
  70. package/dist/config/edge.js +0 -481
  71. package/dist/config/edge.js.map +0 -1
  72. package/dist/microfrontend-config-983a5139.d.ts +0 -154
  73. package/dist/schema-2922d49e.d.ts +0 -182
  74. package/dist/v2/config.cjs +0 -709
  75. package/dist/v2/config.cjs.map +0 -1
  76. package/dist/v2/config.d.ts +0 -4
  77. package/dist/v2/config.js +0 -684
  78. package/dist/v2/config.js.map +0 -1
  79. package/dist/v2/microfrontends/server.cjs.map +0 -1
  80. package/dist/v2/microfrontends/server.js.map +0 -1
  81. package/dist/v2/microfrontends.cjs.map +0 -1
  82. package/dist/v2/microfrontends.js.map +0 -1
  83. package/dist/v2/next/client.cjs +0 -3
  84. package/dist/v2/next/client.cjs.map +0 -1
  85. package/dist/v2/next/client.d.ts +0 -45
  86. package/dist/v2/next/client.js +0 -3
  87. package/dist/v2/next/client.js.map +0 -1
  88. package/dist/v2/next/config.cjs +0 -2178
  89. package/dist/v2/next/config.cjs.map +0 -1
  90. package/dist/v2/next/config.d.ts +0 -22
  91. package/dist/v2/next/config.js +0 -2143
  92. package/dist/v2/next/config.js.map +0 -1
  93. package/dist/v2/next/endpoints.cjs +0 -141
  94. package/dist/v2/next/endpoints.cjs.map +0 -1
  95. package/dist/v2/next/endpoints.d.ts +0 -26
  96. package/dist/v2/next/endpoints.js +0 -116
  97. package/dist/v2/next/endpoints.js.map +0 -1
  98. package/dist/v2/next/middleware.cjs +0 -1099
  99. package/dist/v2/next/middleware.cjs.map +0 -1
  100. package/dist/v2/next/middleware.d.ts +0 -34
  101. package/dist/v2/next/middleware.js +0 -1071
  102. package/dist/v2/next/middleware.js.map +0 -1
  103. package/dist/v2/next/testing.cjs +0 -992
  104. package/dist/v2/next/testing.cjs.map +0 -1
  105. package/dist/v2/next/testing.d.ts +0 -55
  106. package/dist/v2/next/testing.js +0 -961
  107. package/dist/v2/next/testing.js.map +0 -1
  108. package/dist/v2/overrides.cjs +0 -75
  109. package/dist/v2/overrides.cjs.map +0 -1
  110. package/dist/v2/overrides.d.ts +0 -24
  111. package/dist/v2/overrides.js +0 -45
  112. package/dist/v2/overrides.js.map +0 -1
  113. package/dist/v2/routing.cjs.map +0 -1
  114. package/dist/v2/schema.cjs.map +0 -1
  115. package/dist/v2/schema.d.ts +0 -1
  116. package/schema/schema-v2.json +0 -266
  117. /package/dist/{v2/routing.d.ts → routing.d.ts} +0 -0
  118. /package/dist/{v2/routing.js → routing.js} +0 -0
  119. /package/dist/{v2/routing.js.map → routing.js.map} +0 -0
  120. /package/dist/{v2/schema.js → schema.js} +0 -0
  121. /package/dist/{v2/schema.js.map → schema.js.map} +0 -0
package/dist/config.js CHANGED
@@ -1,92 +1,5 @@
1
- // src/config/client/client-config.ts
2
- function getConfigForClient(config) {
3
- return {
4
- applications: Object.fromEntries(
5
- Object.entries(config.applications).map(([name, application]) => [
6
- name,
7
- {
8
- default: application.default,
9
- routing: application.routing
10
- }
11
- ])
12
- )
13
- };
14
- }
15
- function getClientConfigFromEnv() {
16
- const clientConfigJson = process.env.NEXT_PUBLIC_MFE_CLIENT_CONFIG;
17
- if (!clientConfigJson) {
18
- throw new Error(
19
- "Could not find client micro-frontends config in the environment"
20
- );
21
- }
22
- return JSON.parse(clientConfigJson);
23
- }
24
-
25
- // src/config/well-known/endpoints.ts
26
- async function getWellKnownClientData(flagValues = {}) {
27
- var _a;
28
- const config = getClientConfigFromEnv();
29
- for (const [applicationName, application] of Object.entries(
30
- config.applications
31
- )) {
32
- if (!application.routing) {
33
- continue;
34
- }
35
- const newRoutingMatches = [];
36
- for (const pathGroup of application.routing.matches) {
37
- if ((_a = pathGroup.options) == null ? void 0 : _a.flag) {
38
- const flagName = pathGroup.options.flag;
39
- const flagFn = flagValues[flagName];
40
- if (!flagFn) {
41
- throw new Error(
42
- `Flag "${flagName}" was specified to control routing for path group "${pathGroup.group}" in application ${applicationName} but not found in provided flag values.`
43
- );
44
- }
45
- const flagEnabled = await flagFn();
46
- if (flagEnabled) {
47
- newRoutingMatches.push(pathGroup);
48
- }
49
- } else {
50
- newRoutingMatches.push(pathGroup);
51
- }
52
- }
53
- application.routing.matches = newRoutingMatches;
54
- }
55
- return {
56
- config
57
- };
58
- }
59
-
60
- // src/config/types.ts
61
- var isCommonApplicationConfig = (app) => !app.default && typeof app.routing === "object";
62
- var isDefaultApplicationConfig = (app) => app.default && typeof app.routing === "undefined";
63
-
64
- // src/config/microfrontend-config.ts
65
- import fs2 from "node:fs";
66
-
67
- // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
68
- import path from "node:path";
69
-
70
- // src/config-v2/microfrontends/server/constants.ts
71
- var MFE_CONFIG_DEFAULT_FILE_PATH = "microfrontends";
72
- var MFE_CONFIG_DEFAULT_FILE_NAME = "microfrontends.json";
73
-
74
- // src/utils/is-vercel.ts
75
- function isVercel() {
76
- return process.env.VERCEL === "1";
77
- }
78
-
79
- // src/config-v2/microfrontends/server/utils/get-output-file-path.ts
80
- function getOutputFilePath() {
81
- if (isVercel()) {
82
- return path.join(
83
- ".vercel",
84
- MFE_CONFIG_DEFAULT_FILE_PATH,
85
- MFE_CONFIG_DEFAULT_FILE_NAME
86
- );
87
- }
88
- return path.join(MFE_CONFIG_DEFAULT_FILE_PATH, MFE_CONFIG_DEFAULT_FILE_NAME);
89
- }
1
+ // src/config/microfrontends-config/isomorphic/index.ts
2
+ import { parse } from "jsonc-parser";
90
3
 
91
4
  // src/config/errors.ts
92
5
  var MicrofrontendError = class extends Error {
@@ -180,131 +93,299 @@ var MicrofrontendError = class extends Error {
180
93
  }
181
94
  };
182
95
 
183
- // src/routing/url.ts
184
- function buildUrlSafeString(givenOpts = {}) {
185
- const options = {
186
- joinString: "-",
187
- lowercaseOnly: true,
188
- maxLen: 100,
189
- regexRemovePattern: /(?:(?!(?:[a-z0-9])).)/gi,
190
- trimWhitespace: true,
191
- ...givenOpts
192
- };
193
- return {
194
- generate: (...args) => {
195
- const reJoinString = new RegExp(`${options.joinString}+`, "g");
196
- let tag;
197
- if (args.length === 0) {
198
- throw new Error("generate method must be passed at least one argument");
96
+ // src/config/microfrontends-config/utils/get-config-from-env.ts
97
+ function getConfigStringFromEnv() {
98
+ const config = process.env.MFE_CONFIG;
99
+ if (!config) {
100
+ throw new MicrofrontendError(`Missing "MFE_CONFIG" in environment.`, {
101
+ type: "config",
102
+ subtype: "not_found_in_env"
103
+ });
104
+ }
105
+ return config;
106
+ }
107
+
108
+ // src/config/schema/utils/is-main-config.ts
109
+ function isMainConfig(c) {
110
+ return !("partOf" in c);
111
+ }
112
+
113
+ // src/config/schema/utils/is-default-app.ts
114
+ function isDefaultApp(a) {
115
+ return !("routing" in a);
116
+ }
117
+
118
+ // src/config/microfrontends-config/client/index.ts
119
+ import { pathToRegexp } from "path-to-regexp";
120
+ var MicrofrontendConfigClient = class {
121
+ constructor(config, opts) {
122
+ this.pathCache = {};
123
+ this.serialized = config;
124
+ if (opts == null ? void 0 : opts.removeFlaggedPaths) {
125
+ for (const app of Object.values(config.applications)) {
126
+ if (app.routing) {
127
+ app.routing = app.routing.filter((match) => !match.flag);
128
+ }
199
129
  }
200
- for (let i = 0; i < args.length; i++) {
201
- const arg = args[i];
202
- if (typeof arg !== "string")
203
- throw new Error("all supplied arguments must be Strings");
204
- if (options.trimWhitespace) {
205
- args[i] = arg.trim();
130
+ }
131
+ this.applications = config.applications;
132
+ }
133
+ /**
134
+ * Create a new `MicrofrontendConfigClient` from a JSON string.
135
+ * Config must be passed in to remain framework agnostic
136
+ */
137
+ static fromEnv(config, opts) {
138
+ if (!config) {
139
+ throw new Error("No microfrontends configuration found");
140
+ }
141
+ return new MicrofrontendConfigClient(
142
+ JSON.parse(config),
143
+ opts
144
+ );
145
+ }
146
+ isEqual(other) {
147
+ return JSON.stringify(this.applications) === JSON.stringify(other.applications);
148
+ }
149
+ getApplicationNameForPath(path) {
150
+ if (!path.startsWith("/")) {
151
+ throw new Error(`Path must start with a /`);
152
+ }
153
+ if (this.pathCache[path]) {
154
+ return this.pathCache[path];
155
+ }
156
+ const pathname = new URL(path, "https://example.com").pathname;
157
+ for (const [name, application] of Object.entries(this.applications)) {
158
+ if (application.routing) {
159
+ for (const group of application.routing) {
160
+ for (const childPath of group.paths) {
161
+ const regexp = pathToRegexp(childPath);
162
+ if (regexp.test(pathname)) {
163
+ this.pathCache[path] = name;
164
+ return name;
165
+ }
166
+ }
206
167
  }
207
168
  }
208
- tag = args.join(options.joinString);
209
- tag = tag.replace(/\s/g, options.joinString);
210
- if (options.lowercaseOnly)
211
- tag = tag.toLowerCase();
212
- tag = tag.replace(options.regexRemovePattern, (match) => {
213
- if (match === options.joinString)
214
- return match;
215
- return "";
216
- });
217
- if (tag.length > options.maxLen)
218
- tag = tag.substring(0, options.maxLen);
219
- tag = tag.replace(reJoinString, options.joinString);
220
- return tag;
221
169
  }
170
+ const defaultApplication = Object.entries(this.applications).find(
171
+ ([, application]) => application.default
172
+ );
173
+ if (!defaultApplication) {
174
+ return null;
175
+ }
176
+ this.pathCache[path] = defaultApplication[0];
177
+ return defaultApplication[0];
178
+ }
179
+ serialize() {
180
+ return this.serialized;
181
+ }
182
+ };
183
+
184
+ // src/config/overrides/constants.ts
185
+ var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
186
+ var OVERRIDES_ENV_COOKIE_PREFIX = `${OVERRIDES_COOKIE_PREFIX}:env:`;
187
+
188
+ // src/config/overrides/is-override-cookie.ts
189
+ function isOverrideCookie(cookie) {
190
+ var _a;
191
+ return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
192
+ }
193
+
194
+ // src/config/overrides/get-override-from-cookie.ts
195
+ function getOverrideFromCookie(cookie) {
196
+ if (!isOverrideCookie(cookie) || !cookie.value)
197
+ return;
198
+ return {
199
+ application: cookie.name.replace(OVERRIDES_ENV_COOKIE_PREFIX, ""),
200
+ host: cookie.value
222
201
  };
223
202
  }
224
- var urlSafeString = buildUrlSafeString().generate;
225
- function makeUrlSafe(name) {
226
- return urlSafeString(name.replace(/\//g, "-")).replace(/^-*/g, "").replace(/-*$/g, "");
203
+
204
+ // src/config/overrides/parse-overrides.ts
205
+ function parseOverrides(cookies) {
206
+ const overridesConfig = { applications: {} };
207
+ cookies.forEach((cookie) => {
208
+ const override = getOverrideFromCookie(cookie);
209
+ if (!override)
210
+ return;
211
+ overridesConfig.applications[override.application] = {
212
+ environment: { host: override.host }
213
+ };
214
+ });
215
+ return overridesConfig;
227
216
  }
228
217
 
229
- // src/config/overrides/config.ts
230
- var OVERRIDES_COOKIE_PREFIX = "vercel-micro-frontends-override";
231
- var _Overrides = class {
232
- constructor(config) {
233
- this.config = config;
234
- }
235
- static getAppEnvOverrideCookieName(zone) {
236
- return `${_Overrides.overrideEnvCookiePrefix}${zone}`;
218
+ // src/config/microfrontends-config/isomorphic/validation.ts
219
+ import { pathToRegexp as pathToRegexp2, parse as parsePathRegexp } from "path-to-regexp";
220
+ var SUPPORTED_VERSIONS = ["2"];
221
+ var validateConfigVersion = (version) => {
222
+ if (!SUPPORTED_VERSIONS.includes(version)) {
223
+ throw new MicrofrontendError(
224
+ `Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
225
+ ", "
226
+ )}`,
227
+ { type: "config", subtype: "unsupported_version" }
228
+ );
237
229
  }
238
- static isOverrideCookie(cookie) {
239
- var _a;
240
- return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
230
+ };
231
+ var validateConfigPaths = (applicationConfigsById) => {
232
+ if (!applicationConfigsById) {
233
+ return;
241
234
  }
242
- static getOverrideFromCookie(cookie) {
243
- if (!_Overrides.isOverrideCookie(cookie) || !cookie.value)
244
- return;
245
- return {
246
- zone: cookie.name.replace(_Overrides.overrideEnvCookiePrefix, ""),
247
- host: cookie.value
248
- };
235
+ const pathsByApplicationId = /* @__PURE__ */ new Map();
236
+ const errors = [];
237
+ for (const [id, app] of Object.entries(applicationConfigsById)) {
238
+ if (isDefaultApp(app)) {
239
+ continue;
240
+ }
241
+ for (const pathMatch of app.routing) {
242
+ for (const path of pathMatch.paths) {
243
+ const tokens = parsePathRegexp(path);
244
+ for (const token of tokens.slice(0, -1)) {
245
+ if (typeof token !== "string") {
246
+ errors.push(
247
+ `Path ${path} may only have a :wildcard in the last path component`
248
+ );
249
+ }
250
+ }
251
+ const existing = pathsByApplicationId.get(path);
252
+ if (existing) {
253
+ existing.applications.push(id);
254
+ } else {
255
+ pathsByApplicationId.set(path, {
256
+ applications: [id],
257
+ matcher: pathToRegexp2(path),
258
+ applicationId: id
259
+ });
260
+ }
261
+ }
262
+ }
249
263
  }
250
- static parseOverrides(cookies) {
251
- const overridesConfig = { applications: {} };
252
- cookies.forEach((cookie) => {
253
- const override = _Overrides.getOverrideFromCookie(cookie);
254
- if (!override)
255
- return;
256
- overridesConfig.applications[override.zone] = {
257
- environment: { host: override.host }
258
- };
264
+ const entries = Array.from(pathsByApplicationId.entries());
265
+ entries.forEach(([path, { applications: ids, matcher, applicationId }]) => {
266
+ if (ids.length > 1) {
267
+ errors.push(
268
+ `Duplicate path "${path}" for applications "${ids.join(", ")}"`
269
+ );
270
+ }
271
+ entries.forEach(
272
+ ([
273
+ matchPath,
274
+ { applications: matchIds, applicationId: matchApplicationId }
275
+ ]) => {
276
+ if (path === matchPath) {
277
+ return;
278
+ }
279
+ if (applicationId === matchApplicationId) {
280
+ return;
281
+ }
282
+ if (matcher.test(matchPath)) {
283
+ const source = `"${path}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
284
+ const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
285
+ errors.push(
286
+ `Overlapping path detected between ${source} and ${destination}`
287
+ );
288
+ }
289
+ }
290
+ );
291
+ });
292
+ if (errors.length) {
293
+ throw new MicrofrontendError(`Invalid paths: ${errors.join(", ")}`, {
294
+ type: "config",
295
+ subtype: "conflicting_paths"
259
296
  });
260
- return overridesConfig;
261
297
  }
262
- static validOverrideDomainsForZone(microfrontendConfig, zone) {
263
- var _a, _b, _c, _d, _e;
264
- const projectName = (_a = microfrontendConfig.getZone(zone).vercel) == null ? void 0 : _a.projectName;
265
- if (!projectName) {
266
- return [microfrontendConfig.getZone(zone).production.host];
267
- }
268
- const parsedProjectName = makeUrlSafe(projectName);
269
- const previewDeploymentSuffix = (_c = (_b = microfrontendConfig.options) == null ? void 0 : _b.vercel) == null ? void 0 : _c.previewDeploymentSuffix;
270
- const teamSlug = (_e = (_d = microfrontendConfig.options) == null ? void 0 : _d.vercel) == null ? void 0 : _e.teamSlug;
271
- if (!teamSlug && !previewDeploymentSuffix) {
272
- return [microfrontendConfig.getZone(zone).production.host];
298
+ };
299
+ var validateAppPaths = (name, app) => {
300
+ for (const group of app.routing) {
301
+ for (const p of group.paths) {
302
+ if (p === "/") {
303
+ continue;
304
+ }
305
+ if (p.endsWith("/")) {
306
+ throw new MicrofrontendError(
307
+ `Invalid path for application "${name}". ${p} must not end with a slash.`,
308
+ { type: "application", subtype: "invalid_path" }
309
+ );
310
+ }
311
+ if (!p.startsWith("/")) {
312
+ throw new MicrofrontendError(
313
+ `Invalid path for application "${name}". ${p} must start with a slash.`,
314
+ { type: "application", subtype: "invalid_path" }
315
+ );
316
+ }
273
317
  }
274
- const suffix = previewDeploymentSuffix ? `.${previewDeploymentSuffix}` : `-${teamSlug}.vercel.app`;
275
- return [
276
- `${parsedProjectName}-git-([a-zA-Z0-9-]+)${suffix}`,
277
- microfrontendConfig.getZone(zone).production.host
278
- ];
279
318
  }
280
- static validateOverrideDomain(microfrontendConfig, zone, domain) {
281
- return new RegExp(
282
- `^${_Overrides.validOverrideDomainsForZone(microfrontendConfig, zone).join(
283
- "|"
284
- )}$`
285
- ).test(domain);
319
+ };
320
+ var validateConfigDefaultApplication = (applicationConfigsById) => {
321
+ if (!applicationConfigsById) {
322
+ return;
286
323
  }
287
- serialize() {
288
- return this.config;
324
+ const applicationsWithRouting = Object.entries(applicationConfigsById).filter(
325
+ ([, app]) => !isDefaultApp(app)
326
+ );
327
+ const applicationsWithRoutingNames = applicationsWithRouting.map(
328
+ ([key]) => key
329
+ );
330
+ const numApplications = Object.keys(applicationConfigsById).length;
331
+ const numApplicationsWithRouting = applicationsWithRoutingNames.length;
332
+ const numApplicationsWithoutRouting = numApplications - numApplicationsWithRouting;
333
+ if (numApplicationsWithoutRouting === 0) {
334
+ throw new MicrofrontendError(
335
+ `No default application found. At least one application needs to be the default by omitting routing.`,
336
+ { type: "config", subtype: "no_default_application" }
337
+ );
338
+ }
339
+ if (numApplicationsWithoutRouting > 1) {
340
+ throw new MicrofrontendError(
341
+ `Only one application can omit "routing". Found ${applicationsWithRoutingNames.length - Object.keys(applicationConfigsById).length > 1}.`,
342
+ { type: "config", subtype: "multiple_default_applications" }
343
+ );
289
344
  }
290
345
  };
291
- var Overrides = _Overrides;
292
- Overrides.overrideEnvCookiePrefix = `${OVERRIDES_COOKIE_PREFIX}:env:`;
293
346
 
294
- // src/config/common/host.ts
347
+ // src/config/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
348
+ var PREFIX = "vc-ap";
349
+ function generateAssetPrefixFromName({
350
+ name
351
+ }) {
352
+ if (!name) {
353
+ throw new Error("Name is required to generate an asset prefix");
354
+ }
355
+ return `${PREFIX}-${name}`;
356
+ }
357
+
358
+ // src/config/microfrontends-config/isomorphic/utils/generate-port.ts
359
+ function generatePortFromName({
360
+ name,
361
+ minPort = 3e3,
362
+ maxPort = 8e3
363
+ }) {
364
+ if (!name) {
365
+ throw new Error("Name is required to generate a port");
366
+ }
367
+ let hash = 0;
368
+ for (let i = 0; i < name.length; i++) {
369
+ hash = (hash << 5) - hash + name.charCodeAt(i);
370
+ hash |= 0;
371
+ }
372
+ hash = Math.abs(hash);
373
+ const range = maxPort - minPort;
374
+ const port = minPort + hash % range;
375
+ return port;
376
+ }
377
+
378
+ // src/config/microfrontends-config/isomorphic/host.ts
295
379
  var Host = class {
296
- constructor({ protocol, host, port }) {
297
- this.protocol = protocol || "https";
380
+ constructor(hostConfig, options) {
381
+ const { protocol = "https", host, port } = hostConfig;
382
+ this.protocol = protocol;
298
383
  this.host = host;
299
384
  this.port = Host.getPort({ port, protocol: this.protocol });
300
- this.serialized = {
301
- protocol,
302
- host,
303
- ...port ? { port } : void 0
304
- };
385
+ this.local = options == null ? void 0 : options.isLocal;
305
386
  }
306
387
  isLocal() {
307
- return this.host === "localhost" || this.host === "127.0.0.1";
388
+ return this.local || this.host === "localhost" || this.host === "127.0.0.1";
308
389
  }
309
390
  static getPort({
310
391
  protocol,
@@ -330,176 +411,234 @@ var Host = class {
330
411
  const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
331
412
  return new URL(url);
332
413
  }
333
- serialize() {
334
- return this.serialized;
414
+ };
415
+ var LocalHost = class extends Host {
416
+ constructor({
417
+ appName,
418
+ ...hostConfig
419
+ }) {
420
+ const host = hostConfig.host ?? "localhost";
421
+ const port = hostConfig.port ?? generatePortFromName({ name: appName });
422
+ const protocol = hostConfig.protocol ?? "http";
423
+ super({ protocol, host, port });
335
424
  }
336
425
  };
337
426
 
338
- // src/config/common/application.ts
427
+ // src/config/microfrontends-config/isomorphic/application.ts
339
428
  var Application = class {
340
429
  constructor(name, {
341
430
  app,
342
- overrides
431
+ overrides,
432
+ isDefault
343
433
  }) {
344
- Application.validate(name, app);
434
+ var _a, _b;
345
435
  this.name = name;
346
- this.default = app.default;
347
- this.routing = app.routing;
348
436
  this.development = {
349
- local: new Host(app.development.local),
350
- fallback: app.development.fallback ? new Host(app.development.fallback) : void 0
437
+ local: new LocalHost({
438
+ appName: name,
439
+ ...(_a = app.development) == null ? void 0 : _a.local
440
+ }),
441
+ fallback: ((_b = app.development) == null ? void 0 : _b.fallback) ? new Host(app.development.fallback) : void 0
351
442
  };
352
- this.production = new Host(app.production);
443
+ this.production = app.production ? new Host(app.production) : void 0;
353
444
  this.vercel = app.vercel;
354
445
  this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
355
446
  environment: new Host(overrides.environment)
356
447
  } : void 0;
448
+ this.default = isDefault ?? false;
449
+ this.serialized = app;
357
450
  }
358
451
  isDefault() {
359
452
  return this.default;
360
453
  }
361
- static validate(name, app) {
362
- var _a, _b, _c, _d, _e;
363
- 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("/"))) {
364
- throw new MicrofrontendError(
365
- `Invalid assetPrefix for application "${name}". Must not start or end with a slash.`,
366
- { type: "zone", subtype: "invalid_asset_prefix" }
367
- );
368
- }
369
- for (const group of ((_e = app.routing) == null ? void 0 : _e.matches) ?? []) {
370
- for (const p of group.paths) {
371
- if (p === "/") {
372
- continue;
373
- }
374
- if (p.endsWith("/")) {
375
- throw new MicrofrontendError(
376
- `Invalid path for application "${name}". ${p} must not end with a slash.`,
377
- { type: "zone", subtype: "invalid_path" }
378
- );
379
- }
380
- if (!p.startsWith("/")) {
381
- throw new MicrofrontendError(
382
- `Invalid path for application "${name}". ${p} must start with a slash.`,
383
- { type: "zone", subtype: "invalid_path" }
384
- );
385
- }
386
- }
387
- }
454
+ getAssetPrefix() {
455
+ return generateAssetPrefixFromName({ name: this.name });
388
456
  }
389
457
  serialize() {
390
- var _a, _b;
391
- if (this.routing === void 0 || this.default) {
392
- return {
393
- default: true,
394
- development: {
395
- local: this.development.local.serialize(),
396
- fallback: (_a = this.development.fallback) == null ? void 0 : _a.serialize()
397
- },
398
- production: this.production.serialize(),
399
- vercel: this.vercel
400
- };
401
- }
402
- return {
403
- default: false,
404
- routing: this.routing,
405
- development: {
406
- local: this.development.local.serialize(),
407
- fallback: (_b = this.development.fallback) == null ? void 0 : _b.serialize()
408
- },
409
- production: this.production.serialize(),
410
- vercel: this.vercel
411
- };
458
+ return this.serialized;
459
+ }
460
+ };
461
+ var DefaultApplication = class extends Application {
462
+ constructor(name, {
463
+ app,
464
+ overrides
465
+ }) {
466
+ super(name, {
467
+ app,
468
+ overrides,
469
+ isDefault: true
470
+ });
471
+ this.default = true;
472
+ this.production = new Host(app.production);
473
+ }
474
+ getAssetPrefix() {
475
+ return "";
476
+ }
477
+ };
478
+ var ChildApplication = class extends Application {
479
+ constructor(name, {
480
+ app,
481
+ overrides
482
+ }) {
483
+ ChildApplication.validate(name, app);
484
+ super(name, {
485
+ app,
486
+ overrides,
487
+ isDefault: false
488
+ });
489
+ this.default = false;
490
+ this.routing = app.routing;
491
+ }
492
+ static validate(name, app) {
493
+ validateAppPaths(name, app);
412
494
  }
413
495
  };
414
496
 
415
- // src/config/common/microfrontend-config.ts
416
- var SUPPORTED_VERSIONS = ["1"];
497
+ // src/config/microfrontends-config/isomorphic/constants.ts
417
498
  var DEFAULT_LOCAL_PROXY_PORT = 3024;
418
- var MicrofrontendConfigCommon = class {
499
+
500
+ // src/config/microfrontends-config/isomorphic/index.ts
501
+ var MicrofrontendConfigIsomorphic = class {
419
502
  constructor({
420
503
  config,
421
- overrides
504
+ overrides,
505
+ meta
422
506
  }) {
423
- this.zones = {};
424
- var _a, _b, _c;
425
- if (!SUPPORTED_VERSIONS.includes(config.version)) {
426
- throw new MicrofrontendError(
427
- `Unsupported version: ${config.version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
428
- ", "
429
- )}`,
430
- { type: "config", subtype: "unsupported_version" }
507
+ this.childApplications = {};
508
+ var _a, _b, _c, _d;
509
+ MicrofrontendConfigIsomorphic.validate(config);
510
+ const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
511
+ this.overrides = overrides && !disableOverrides ? overrides : void 0;
512
+ this.isMainConfig = isMainConfig(config);
513
+ if (isMainConfig(config)) {
514
+ for (const [appId, appConfig] of Object.entries(config.applications)) {
515
+ const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
516
+ if (isDefaultApp(appConfig)) {
517
+ this.defaultApplication = new DefaultApplication(appId, {
518
+ app: appConfig,
519
+ overrides: appOverrides
520
+ });
521
+ } else {
522
+ this.childApplications[appId] = new ChildApplication(appId, {
523
+ app: appConfig,
524
+ overrides: appOverrides
525
+ });
526
+ }
527
+ }
528
+ } else {
529
+ this.partOf = config.partOf;
530
+ const appOverrides = !disableOverrides ? (_d = this.overrides) == null ? void 0 : _d.applications[meta.fromApp] : void 0;
531
+ this.childApplications[meta.fromApp] = new ChildApplication(
532
+ meta.fromApp,
533
+ {
534
+ // we don't know routing because we're not in the main config
535
+ app: { routing: [] },
536
+ overrides: appOverrides
537
+ }
431
538
  );
432
539
  }
433
- const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
434
- this.overrides = overrides && !disableOverrides ? new Overrides(overrides) : void 0;
435
- for (const [zoneName, zoneConfig] of Object.entries(config.applications)) {
436
- this.zones[zoneName] = new Application(zoneName, {
437
- app: zoneConfig,
438
- overrides: !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.config.applications[zoneName] : void 0
439
- });
540
+ if (isMainConfig(config) && !this.defaultApplication) {
541
+ throw new MicrofrontendError(
542
+ `Could not find default application in microfrontends configuration`,
543
+ {
544
+ type: "application",
545
+ subtype: "not_found"
546
+ }
547
+ );
440
548
  }
441
549
  this.config = config;
442
- this.name = config.name;
443
- this.version = config.version;
444
550
  this.options = config.options;
445
- this.$schema = config.$schema;
551
+ this.serialized = {
552
+ config,
553
+ overrides,
554
+ meta
555
+ };
556
+ }
557
+ static validate(config) {
558
+ const c = typeof config === "string" ? parse(config) : config;
559
+ if (isMainConfig(c)) {
560
+ validateConfigVersion(c.version);
561
+ validateConfigPaths(c.applications);
562
+ validateConfigDefaultApplication(c.applications);
563
+ }
564
+ return c;
565
+ }
566
+ static fromEnv({
567
+ meta,
568
+ cookies
569
+ }) {
570
+ return new MicrofrontendConfigIsomorphic({
571
+ config: parse(getConfigStringFromEnv()),
572
+ overrides: parseOverrides(cookies ?? []),
573
+ meta
574
+ });
446
575
  }
447
576
  isOverridesDisabled() {
448
577
  var _a, _b;
449
578
  return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
450
579
  }
451
- static getConfigFromEnv() {
452
- const config = process.env.MFE_CONFIG;
453
- if (!config) {
454
- throw new MicrofrontendError(`Missing "MFE_CONFIG" in environment.`, {
455
- type: "config",
456
- subtype: "not_found_in_env"
457
- });
458
- }
459
- return config;
460
- }
461
- static fromEnv(_) {
462
- throw new Error("Not implemented");
463
- }
464
580
  getConfig() {
465
581
  return this.config;
466
582
  }
583
+ getApplicationsByType() {
584
+ return {
585
+ defaultApplication: this.defaultApplication,
586
+ applications: Object.values(this.childApplications)
587
+ };
588
+ }
589
+ getChildApplications() {
590
+ return Object.values(this.childApplications);
591
+ }
467
592
  getAllApplications() {
468
- return Object.values(this.zones);
593
+ return [
594
+ this.defaultApplication,
595
+ ...Object.values(this.childApplications)
596
+ ].filter(Boolean);
469
597
  }
470
- getZone(name) {
471
- const zone = this.zones[name];
472
- if (!zone) {
598
+ getApplication(name) {
599
+ var _a;
600
+ if (((_a = this.defaultApplication) == null ? void 0 : _a.name) === name) {
601
+ return this.defaultApplication;
602
+ }
603
+ const app = this.childApplications[name];
604
+ if (!app) {
473
605
  throw new MicrofrontendError(
474
606
  `Could not find microfrontends configuration for application "${name}"`,
475
607
  {
476
- type: "zone",
608
+ type: "application",
477
609
  subtype: "not_found"
478
610
  }
479
611
  );
480
612
  }
481
- return zone;
613
+ return app;
482
614
  }
483
615
  getApplicationByProjectId(projectId) {
484
- return Object.values(this.zones).find(
485
- (zone) => {
486
- var _a;
487
- return ((_a = zone.vercel) == null ? void 0 : _a.projectId) === projectId;
616
+ var _a, _b;
617
+ if (((_b = (_a = this.defaultApplication) == null ? void 0 : _a.vercel) == null ? void 0 : _b.projectId) === projectId) {
618
+ return this.defaultApplication;
619
+ }
620
+ return Object.values(this.childApplications).find(
621
+ (app) => {
622
+ var _a2;
623
+ return ((_a2 = app.vercel) == null ? void 0 : _a2.projectId) === projectId;
488
624
  }
489
625
  );
490
626
  }
491
- getDefaultZone() {
492
- const zone = Object.values(this.zones).find((z) => z.default);
493
- if (!zone) {
627
+ /**
628
+ * Returns the default application. This can throw if the default application
629
+ * is undefined ( )
630
+ */
631
+ getDefaultApplication() {
632
+ if (!this.defaultApplication) {
494
633
  throw new MicrofrontendError(
495
- `Could not find default zone in microfrontends configuration`,
634
+ `Could not find default application in microfrontends configuration`,
496
635
  {
497
- type: "zone",
636
+ type: "application",
498
637
  subtype: "not_found"
499
638
  }
500
639
  );
501
640
  }
502
- return zone;
641
+ return this.defaultApplication;
503
642
  }
504
643
  /**
505
644
  * Returns the configured port for the local proxy
@@ -514,698 +653,32 @@ var MicrofrontendConfigCommon = class {
514
653
  * NOTE: This is used when writing the config to disk and must always match the input Schema
515
654
  */
516
655
  toSchemaJson() {
517
- const applications = {};
518
- for (const [name, zone] of Object.entries(this.zones)) {
519
- applications[name] = zone.serialize();
520
- }
521
- return {
522
- $schema: this.$schema,
523
- name: this.name,
524
- version: this.version,
525
- options: this.options,
526
- applications
527
- };
528
- }
529
- serialize() {
530
- var _a;
531
- const applications = {};
532
- for (const [name, zone] of Object.entries(this.zones)) {
533
- applications[name] = zone.serialize();
534
- }
535
- return {
536
- config: {
537
- name: this.name,
538
- version: this.version,
539
- applications,
540
- options: this.options,
541
- $schema: this.$schema
542
- },
543
- overrides: (_a = this.overrides) == null ? void 0 : _a.serialize()
544
- };
545
- }
546
- write(_) {
547
- throw new MicrofrontendError(
548
- `Writing to file to disk requires using an instance of "MicrofrontendConfig".`,
549
- { type: "config", subtype: "unsupported_operation" }
550
- );
551
- }
552
- };
553
-
554
- // src/config/utils/get-output-file-path.ts
555
- import path2 from "node:path";
556
-
557
- // src/config/constants.ts
558
- var MFE_CONFIG_DEFAULT_FILE_PATH2 = "micro-frontends";
559
- var MFE_CONFIG_DEFAULT_FILE_NAME2 = "micro-frontends.config.json";
560
-
561
- // src/config/utils/get-output-file-path.ts
562
- function getOutputFilePath2() {
563
- if (isVercel()) {
564
- return path2.join(
565
- ".vercel",
566
- MFE_CONFIG_DEFAULT_FILE_PATH2,
567
- MFE_CONFIG_DEFAULT_FILE_NAME2
568
- );
656
+ return this.serialized.config;
569
657
  }
570
- return path2.join(MFE_CONFIG_DEFAULT_FILE_PATH2, MFE_CONFIG_DEFAULT_FILE_NAME2);
571
- }
572
-
573
- // src/config/validation.ts
574
- import { parse } from "jsonc-parser";
575
- import { pathToRegexp, parse as parsePathRegexp } from "path-to-regexp";
576
- import { Ajv } from "ajv";
577
-
578
- // schema/schema.json
579
- var schema_default = {
580
- $schema: "http://json-schema.org/draft-07/schema#",
581
- $ref: "#/definitions/Config",
582
- definitions: {
583
- Config: {
584
- type: "object",
585
- properties: {
586
- version: {
587
- type: "string"
588
- },
589
- $schema: {
590
- type: "string"
591
- },
592
- name: {
593
- type: "string",
594
- description: 'Name for the micro-frontend site (eg. "vercel.com", "vercel-site" etc.).'
595
- },
596
- applications: {
597
- $ref: "#/definitions/ApplicationConfigsById"
598
- },
599
- options: {
600
- $ref: "#/definitions/Options",
601
- description: "Optional configuration for the entire micro-frontends setup."
602
- }
603
- },
604
- required: ["version", "applications"],
605
- description: "Configuration for micro-frontend applications\n\nTODO: Add proxy configuration"
606
- },
607
- ApplicationConfigsById: {
608
- type: "object",
609
- additionalProperties: {
610
- $ref: "#/definitions/ApplicationConfig"
611
- }
612
- },
613
- ApplicationConfig: {
614
- anyOf: [
615
- {
616
- $ref: "#/definitions/DefaultApplicationConfig"
617
- },
658
+ toClientConfig() {
659
+ const applications = Object.fromEntries(
660
+ Object.entries(this.childApplications).map(([name, application]) => [
661
+ name,
618
662
  {
619
- $ref: "#/definitions/CommonApplicationConfig"
620
- }
621
- ],
622
- description: "A Micro-Frontend Deployment Target"
623
- },
624
- DefaultApplicationConfig: {
625
- type: "object",
626
- properties: {
627
- default: {
628
- type: "boolean",
629
- const: true,
630
- description: "The default application is used no other application is matched via the routing config"
631
- },
632
- routing: {
633
- $ref: "#/definitions/Routing"
634
- },
635
- development: {
636
- type: "object",
637
- properties: {
638
- local: {
639
- $ref: "#/definitions/HostConfig"
640
- },
641
- fallback: {
642
- $ref: "#/definitions/HostConfig",
643
- 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."
644
- },
645
- task: {
646
- type: "string",
647
- description: "Optional task to run when starting the development server. Should reference a script in the package.json of the application."
648
- }
649
- },
650
- required: ["local"]
651
- },
652
- production: {
653
- $ref: "#/definitions/HostConfig"
654
- },
655
- metadata: {
656
- type: "object",
657
- additionalProperties: {
658
- type: "string"
659
- }
660
- },
661
- federation: {
662
- type: "object",
663
- properties: {
664
- exposes: {
665
- type: "array",
666
- items: {
667
- type: "object",
668
- properties: {
669
- name: {
670
- type: "string",
671
- description: "The name of the module - should be used when importing the module from another application"
672
- },
673
- path: {
674
- type: "string",
675
- description: "Relative path to the module within its `application`"
676
- }
677
- },
678
- required: ["name", "path"]
679
- },
680
- description: "Modules that are exposed by this application"
681
- },
682
- uses: {
683
- type: "array",
684
- items: {
685
- type: "string"
686
- },
687
- description: "Modules that are used by this application. Only the name of the module is required."
688
- }
689
- }
690
- },
691
- vercel: {
692
- $ref: "#/definitions/Vercel"
693
- }
694
- },
695
- required: ["default", "development", "production"]
696
- },
697
- Routing: {
698
- type: "object",
699
- properties: {
700
- assetPrefix: {
701
- type: "string",
702
- description: "[assetPrefix] for the application"
703
- },
704
- matches: {
705
- type: "array",
706
- items: {
707
- $ref: "#/definitions/PathGroup"
708
- },
709
- description: "Path expressions that are routed to this application."
710
- }
711
- },
712
- required: ["matches"]
713
- },
714
- PathGroup: {
715
- type: "object",
716
- properties: {
717
- group: {
718
- type: "string",
719
- description: "Optional group name for the paths"
720
- },
721
- options: {
722
- type: "object",
723
- properties: {
724
- flag: {
725
- type: "string",
726
- description: "flag name that can be used to enable/disable all paths in the group"
727
- }
728
- }
729
- },
730
- paths: {
731
- type: "array",
732
- items: {
733
- type: "string"
734
- }
735
- }
736
- },
737
- required: ["paths"]
738
- },
739
- HostConfig: {
740
- type: "object",
741
- properties: {
742
- protocol: {
743
- type: "string",
744
- enum: ["http", "https"],
745
- 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"'
746
- },
747
- host: {
748
- type: "string",
749
- 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`)."
750
- },
751
- port: {
752
- type: "number",
753
- 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"
754
- }
755
- },
756
- required: ["host"]
757
- },
758
- Vercel: {
759
- type: "object",
760
- properties: {
761
- projectId: {
762
- type: "string",
763
- description: "Vercel project ID"
764
- },
765
- projectName: {
766
- type: "string",
767
- description: "Vercel project name (temporary until we can use project ID)"
768
- },
769
- defaultRoute: {
770
- type: "string",
771
- description: "The default route for the application. Used to render screenshots, favicons, and provide direct zone links"
772
- },
773
- routeSpeedInsightsToDefaultZone: {
774
- type: "boolean",
775
- description: "Whether to route Speed Insights to the default zone or each individual microfrontend."
776
- }
777
- },
778
- required: ["projectId"]
779
- },
780
- CommonApplicationConfig: {
781
- type: "object",
782
- properties: {
783
- default: {
784
- type: "boolean",
785
- const: false,
786
- description: "The default application is used no other application is matched via the routing config"
787
- },
788
- routing: {
789
- $ref: "#/definitions/Routing"
790
- },
791
- development: {
792
- type: "object",
793
- properties: {
794
- local: {
795
- $ref: "#/definitions/HostConfig"
796
- },
797
- fallback: {
798
- $ref: "#/definitions/HostConfig",
799
- 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."
800
- },
801
- task: {
802
- type: "string",
803
- description: "Optional task to run when starting the development server. Should reference a script in the package.json of the application."
804
- }
805
- },
806
- required: ["local"]
807
- },
808
- production: {
809
- $ref: "#/definitions/HostConfig"
810
- },
811
- metadata: {
812
- type: "object",
813
- additionalProperties: {
814
- type: "string"
815
- }
816
- },
817
- federation: {
818
- type: "object",
819
- properties: {
820
- exposes: {
821
- type: "array",
822
- items: {
823
- type: "object",
824
- properties: {
825
- name: {
826
- type: "string",
827
- description: "The name of the module - should be used when importing the module from another application"
828
- },
829
- path: {
830
- type: "string",
831
- description: "Relative path to the module within its `application`"
832
- }
833
- },
834
- required: ["name", "path"]
835
- },
836
- description: "Modules that are exposed by this application"
837
- },
838
- uses: {
839
- type: "array",
840
- items: {
841
- type: "string"
842
- },
843
- description: "Modules that are used by this application. Only the name of the module is required."
844
- }
845
- }
846
- },
847
- vercel: {
848
- $ref: "#/definitions/Vercel"
849
- }
850
- },
851
- required: ["default", "development", "production", "routing"]
852
- },
853
- Options: {
854
- type: "object",
855
- properties: {
856
- vercel: {
857
- $ref: "#/definitions/VercelOptions",
858
- description: "Micro-Frontends wide options for Vercel."
859
- },
860
- localProxy: {
861
- $ref: "#/definitions/LocalProxyOptions",
862
- description: "Options for local proxy."
863
- }
864
- }
865
- },
866
- VercelOptions: {
867
- type: "object",
868
- properties: {
869
- previewDeploymentSuffix: {
870
- type: "string",
871
- 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`."
872
- },
873
- teamSlug: {
874
- type: "string",
875
- description: "Team slug for the Vercel team"
876
- },
877
- disableOverrides: {
878
- type: "boolean",
879
- 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."
880
- }
881
- }
882
- },
883
- LocalProxyOptions: {
884
- type: "object",
885
- properties: {
886
- port: {
887
- type: "number",
888
- description: "The port number used by the local proxy server.\n\nThe default is `3024`."
889
- }
890
- }
891
- }
892
- }
893
- };
894
-
895
- // src/config/utils/load-schema.ts
896
- var SCHEMA = schema_default;
897
-
898
- // src/config/validation.ts
899
- var validateSchema = (configString) => {
900
- const parsedConfig = parse(configString);
901
- const ajv = new Ajv();
902
- const validate = ajv.compile(SCHEMA);
903
- const isValid = validate(parsedConfig);
904
- if (!isValid) {
905
- throw new MicrofrontendError(
906
- `Invalid config: ${ajv.errorsText(validate.errors)}`,
907
- { type: "config", subtype: "does_not_match_schema" }
908
- );
909
- }
910
- return parsedConfig;
911
- };
912
- var SUPPORTED_VERSIONS2 = ["1"];
913
- var validateVersion = (version) => {
914
- if (!SUPPORTED_VERSIONS2.includes(version)) {
915
- throw new MicrofrontendError(
916
- `Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS2.join(
917
- ", "
918
- )}`,
919
- { type: "config", subtype: "unsupported_version" }
920
- );
921
- }
922
- };
923
- function validateMainPath(applicationConfigsById) {
924
- for (const [id, app] of Object.entries(applicationConfigsById)) {
925
- const { defaultRoute } = app.vercel ?? {};
926
- if (!defaultRoute) {
927
- continue;
928
- }
929
- if (isDefaultApplicationConfig(app)) {
930
- const pathsWithApp = [];
931
- for (const [otherId, otherApp] of Object.entries(
932
- applicationConfigsById
933
- )) {
934
- if (isDefaultApplicationConfig(otherApp)) {
935
- continue;
936
- }
937
- pathsWithApp.push({
938
- id: otherId,
939
- paths: otherApp.routing.matches.flatMap((match) => match.paths)
940
- });
941
- }
942
- for (const { id: otherId, paths } of pathsWithApp) {
943
- const isValid = paths.every((path3) => {
944
- const matcher = pathToRegexp(path3);
945
- return !matcher.test(defaultRoute);
946
- });
947
- if (!isValid) {
948
- throw new MicrofrontendError(
949
- `default route "${defaultRoute}" cannot be used for "${id}" because it is matched by "${otherId}"`,
950
- { type: "config", subtype: "invalid_main_path" }
951
- );
952
- }
953
- }
954
- } else {
955
- const allPaths = app.routing.matches.flatMap((match) => match.paths);
956
- const isValid = allPaths.some((path3) => {
957
- const matcher = pathToRegexp(path3);
958
- return matcher.test(defaultRoute);
959
- });
960
- if (!isValid) {
961
- throw new MicrofrontendError(
962
- `default route "${defaultRoute}" is not included by the routing config for application "${id}"`,
963
- { type: "config", subtype: "invalid_main_path" }
964
- );
965
- }
966
- }
967
- }
968
- }
969
- var validatePaths = (applicationConfigsById) => {
970
- const pathsByApplicationId = /* @__PURE__ */ new Map();
971
- const errors = [];
972
- for (const [id, app] of Object.entries(applicationConfigsById)) {
973
- if (isDefaultApplicationConfig(app)) {
974
- continue;
975
- }
976
- for (const pathMatch of app.routing.matches) {
977
- for (const path3 of pathMatch.paths) {
978
- const maybeError = validatePathExpression(path3);
979
- if (maybeError) {
980
- errors.push(maybeError);
981
- }
982
- const existing = pathsByApplicationId.get(path3);
983
- if (existing) {
984
- existing.applications.push(id);
985
- } else {
986
- pathsByApplicationId.set(path3, {
987
- applications: [id],
988
- matcher: pathToRegexp(path3),
989
- applicationId: id
990
- });
991
- }
992
- }
993
- }
994
- }
995
- const entries = Array.from(pathsByApplicationId.entries());
996
- entries.forEach(([path3, { applications: ids, matcher, applicationId }]) => {
997
- if (ids.length > 1) {
998
- errors.push(
999
- `Duplicate path "${path3}" for applications "${ids.join(", ")}"`
1000
- );
1001
- }
1002
- entries.forEach(
1003
- ([
1004
- matchPath,
1005
- { applications: matchIds, applicationId: matchApplicationId }
1006
- ]) => {
1007
- if (path3 === matchPath) {
1008
- return;
1009
- }
1010
- if (applicationId === matchApplicationId) {
1011
- return;
1012
- }
1013
- if (matcher.test(matchPath)) {
1014
- const source = `"${path3}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
1015
- const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
1016
- errors.push(
1017
- `Overlapping path detected between ${source} and ${destination}`
1018
- );
663
+ default: false,
664
+ routing: application.routing
1019
665
  }
1020
- }
1021
- );
1022
- });
1023
- if (errors.length) {
1024
- throw new MicrofrontendError(`Invalid paths: ${errors.join(", ")}`, {
1025
- type: "config",
1026
- subtype: "conflicting_paths"
1027
- });
1028
- }
1029
- };
1030
- var PATH_DEFAULT_PATTERN = "[^\\/#\\?]+?";
1031
- function validatePathExpression(path3) {
1032
- const tokens = parsePathRegexp(path3);
1033
- for (let i = 0; i < tokens.length; i++) {
1034
- const token = tokens[i];
1035
- if (token === void 0) {
1036
- return `token ${i} in ${path3} is undefined, this shouldn't happen`;
1037
- }
1038
- if (typeof token !== "string") {
1039
- if (token.pattern !== PATH_DEFAULT_PATTERN) {
1040
- return `Path ${path3} cannot use a regular expression wildcard`;
1041
- }
1042
- if (token.prefix !== "/") {
1043
- return `Wildcard :${token.name} must be immediately after a / in ${path3}`;
1044
- }
1045
- if (token.suffix) {
1046
- return `Wildcard suffix on :${token.name} is not allowed. Suffixes are not supported`;
1047
- }
1048
- if (token.modifier && i !== tokens.length - 1) {
1049
- return `Modifier ${token.modifier} is not allowed on wildcard :${token.name} in ${path3}. Modifiers are only allowed in the last path component`;
1050
- }
1051
- }
1052
- }
1053
- return void 0;
1054
- }
1055
- var validateDefaults = (applicationConfigsById) => {
1056
- const defaultApplicationIds = Object.entries(applicationConfigsById).reduce((acc, [id, app]) => app.default ? [...acc, id] : acc, []);
1057
- if (defaultApplicationIds.length === 0) {
1058
- throw new MicrofrontendError(
1059
- `No default application found. At least one application must be marked as default.`,
1060
- { type: "config", subtype: "no_default_application" }
1061
- );
1062
- }
1063
- if (defaultApplicationIds.length > 1) {
1064
- throw new MicrofrontendError(
1065
- `Only one default application is allowed. Found ${defaultApplicationIds.join(", ")}.`,
1066
- { type: "config", subtype: "multiple_default_applications" }
666
+ ])
1067
667
  );
1068
- }
1069
- };
1070
- var validateOptions = (options) => {
1071
- var _a;
1072
- if ((_a = options == null ? void 0 : options.vercel) == null ? void 0 : _a.previewDeploymentSuffix) {
1073
- if (!/^[a-zA-Z]{2,}\.[a-zA-Z]{2,}$/.test(
1074
- options.vercel.previewDeploymentSuffix
1075
- )) {
1076
- throw new MicrofrontendError(
1077
- `Invalid preview deployment suffix: ${options.vercel.previewDeploymentSuffix}. Should have be formatted like "vercel.app".`,
1078
- { type: "config", subtype: "invalid_preview_deployment_suffix" }
1079
- );
668
+ if (this.defaultApplication) {
669
+ applications[this.defaultApplication.name] = {
670
+ default: true
671
+ };
1080
672
  }
1081
- }
1082
- };
1083
-
1084
- // src/config/utils/convert.ts
1085
- function convertV1RoutingToV2Routing(routing) {
1086
- return routing.matches.map((group) => {
1087
- var _a;
1088
- return {
1089
- group: group.group,
1090
- flag: (_a = group.options) == null ? void 0 : _a.flag,
1091
- paths: group.paths
1092
- };
1093
- });
1094
- }
1095
- function convertV1ApplicationToV2Application(application) {
1096
- const common = {
1097
- production: application.production,
1098
- development: application.development,
1099
- vercel: application.vercel
1100
- };
1101
- if (application.default) {
1102
- return common;
1103
- }
1104
- return {
1105
- ...common,
1106
- routing: convertV1RoutingToV2Routing(application.routing)
1107
- };
1108
- }
1109
- function convertV1ConfigToV2Config(config, fromApp) {
1110
- if (!config.applications[fromApp]) {
1111
- throw new Error(`Application "${fromApp}" not found in the config`);
1112
- }
1113
- const common = {
1114
- version: "2",
1115
- options: config.options
1116
- };
1117
- if (config.applications[fromApp].default) {
1118
- return {
1119
- ...common,
1120
- applications: Object.fromEntries(
1121
- Object.entries(config.applications).map(([id, application]) => [
1122
- id,
1123
- convertV1ApplicationToV2Application(application)
1124
- ])
1125
- )
1126
- };
1127
- }
1128
- const defaultApplication = Object.entries(config.applications).find(
1129
- ([, application]) => application.default
1130
- );
1131
- if (!defaultApplication) {
1132
- throw new Error("No default application found in the config");
1133
- }
1134
- return {
1135
- ...common,
1136
- partOf: defaultApplication[0]
1137
- };
1138
- }
1139
-
1140
- // src/config/utils/write-file.ts
1141
- import fs from "node:fs";
1142
- import { dirname } from "node:path";
1143
- function writeFile(outputPath, config, prettify) {
1144
- fs.mkdirSync(dirname(outputPath), { recursive: true });
1145
- fs.writeFileSync(
1146
- outputPath,
1147
- JSON.stringify(config, null, prettify ? 2 : void 0)
1148
- );
1149
- }
1150
-
1151
- // src/config/microfrontend-config.ts
1152
- var MicrofrontendConfig = class extends MicrofrontendConfigCommon {
1153
- static validate(configString) {
1154
- const config = validateSchema(configString);
1155
- validateVersion(config.version);
1156
- validatePaths(config.applications);
1157
- validateMainPath(config.applications);
1158
- validateDefaults(config.applications);
1159
- validateOptions(config.options);
1160
- return config;
1161
- }
1162
- static fromEnv({
1163
- cookies
1164
- }) {
1165
- return new MicrofrontendConfigCommon({
1166
- config: MicrofrontendConfig.validate(
1167
- MicrofrontendConfigCommon.getConfigFromEnv()
1168
- ),
1169
- overrides: Overrides.parseOverrides(cookies)
673
+ return new MicrofrontendConfigClient({
674
+ applications
1170
675
  });
1171
676
  }
1172
- static fromFile({
1173
- filePath
1174
- }) {
1175
- try {
1176
- const config = fs2.readFileSync(filePath, "utf-8");
1177
- return new MicrofrontendConfig({
1178
- config: MicrofrontendConfig.validate(config)
1179
- });
1180
- } catch (e) {
1181
- throw MicrofrontendError.handle(e, {
1182
- fileName: filePath
1183
- });
1184
- }
1185
- }
1186
- /**
1187
- * Writes the configuration to a file.
1188
- */
1189
- write(fromApp, opts = {}) {
1190
- const { pretty = true, versions = ["v1", "v2"] } = opts;
1191
- const config = this.toSchemaJson();
1192
- if (versions.includes("v1")) {
1193
- const outputPath = getOutputFilePath2();
1194
- writeFile(outputPath, config, pretty);
1195
- }
1196
- if (versions.includes("v2")) {
1197
- const outputPath = getOutputFilePath();
1198
- const v2Config = convertV1ConfigToV2Config(config, fromApp);
1199
- writeFile(outputPath, v2Config, pretty);
1200
- }
677
+ serialize() {
678
+ return this.serialized;
1201
679
  }
1202
680
  };
1203
681
  export {
1204
- MicrofrontendConfig,
1205
- getClientConfigFromEnv,
1206
- getConfigForClient,
1207
- getWellKnownClientData,
1208
- isCommonApplicationConfig,
1209
- isDefaultApplicationConfig
682
+ MicrofrontendConfigIsomorphic
1210
683
  };
1211
684
  //# sourceMappingURL=config.js.map