@vercel/microfrontends 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/dist/bin/cli.cjs +392 -1620
  2. package/dist/config.cjs +478 -1001
  3. package/dist/config.cjs.map +1 -1
  4. package/dist/config.d.ts +4 -25
  5. package/dist/config.js +474 -980
  6. package/dist/config.js.map +1 -1
  7. package/dist/{v2/microfrontends → microfrontends}/server.cjs +91 -150
  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 +91 -150
  11. package/dist/microfrontends/server.js.map +1 -0
  12. package/dist/{v2/microfrontends.cjs → microfrontends.cjs} +49 -28
  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} +48 -27
  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 +1344 -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 +1343 -1023
  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 +765 -395
  32. package/dist/next/middleware.cjs.map +1 -1
  33. package/dist/next/middleware.d.ts +10 -5
  34. package/dist/next/middleware.js +765 -395
  35. package/dist/next/middleware.js.map +1 -1
  36. package/dist/next/testing.cjs +615 -1031
  37. package/dist/next/testing.cjs.map +1 -1
  38. package/dist/next/testing.d.ts +14 -12
  39. package/dist/next/testing.js +609 -1015
  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 +237 -1338
  52. package/dist/utils/mfe-port.cjs.map +1 -1
  53. package/dist/utils/mfe-port.js +230 -1331
  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,812 +93,164 @@ 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");
199
- }
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();
206
- }
207
- }
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
- }
222
- };
223
- }
224
- var urlSafeString = buildUrlSafeString().generate;
225
- function makeUrlSafe(name) {
226
- return urlSafeString(name.replace(/\//g, "-")).replace(/^-*/g, "").replace(/-*$/g, "");
227
- }
228
-
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}`;
237
- }
238
- static isOverrideCookie(cookie) {
239
- var _a;
240
- return Boolean((_a = cookie.name) == null ? void 0 : _a.startsWith(OVERRIDES_COOKIE_PREFIX));
241
- }
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
- };
249
- }
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
- };
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"
259
103
  });
260
- return overridesConfig;
261
104
  }
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];
273
- }
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
- }
280
- static validateOverrideDomain(microfrontendConfig, zone, domain) {
281
- return new RegExp(
282
- `^${_Overrides.validOverrideDomainsForZone(microfrontendConfig, zone).join(
283
- "|"
284
- )}$`
285
- ).test(domain);
286
- }
287
- serialize() {
288
- return this.config;
289
- }
290
- };
291
- var Overrides = _Overrides;
292
- Overrides.overrideEnvCookiePrefix = `${OVERRIDES_COOKIE_PREFIX}:env:`;
105
+ return config;
106
+ }
293
107
 
294
- // src/config/common/host.ts
295
- var Host = class {
296
- constructor({ protocol, host, port }) {
297
- this.protocol = protocol || "https";
298
- this.host = host;
299
- this.port = Host.getPort({ port, protocol: this.protocol });
300
- this.serialized = {
301
- protocol,
302
- host,
303
- ...port ? { port } : void 0
304
- };
305
- }
306
- isLocal() {
307
- return this.host === "localhost" || this.host === "127.0.0.1";
308
- }
309
- static getPort({
310
- protocol,
311
- port
312
- }) {
313
- if (!port) {
314
- if (protocol === "http") {
315
- return 80;
316
- }
317
- return 443;
318
- }
319
- return port;
320
- }
321
- isDefaultPort() {
322
- return this.port === Host.getPort({ protocol: this.protocol });
323
- }
324
- toString(opts = {}) {
325
- const url = this.toUrl(opts);
326
- return url.toString().replace(/\/$/, "");
327
- }
328
- toUrl(opts = {}) {
329
- const { includeDefaultPort } = opts;
330
- const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
331
- return new URL(url);
332
- }
333
- serialize() {
334
- return this.serialized;
335
- }
336
- };
108
+ // src/config/schema/utils/is-main-config.ts
109
+ function isMainConfig(c) {
110
+ return !("partOf" in c);
111
+ }
337
112
 
338
- // src/config/common/application.ts
339
- var Application = class {
340
- constructor(name, {
341
- app,
342
- overrides
343
- }) {
344
- Application.validate(name, app);
345
- this.name = name;
346
- this.default = app.default;
347
- this.routing = app.routing;
348
- this.development = {
349
- local: new Host(app.development.local),
350
- fallback: app.development.fallback ? new Host(app.development.fallback) : void 0
351
- };
352
- this.production = new Host(app.production);
353
- this.vercel = app.vercel;
354
- this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
355
- environment: new Host(overrides.environment)
356
- } : void 0;
357
- }
358
- isDefault() {
359
- return this.default;
360
- }
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
- );
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);
385
128
  }
386
129
  }
387
130
  }
131
+ this.applications = config.applications;
388
132
  }
389
- 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
- };
412
- }
413
- };
414
-
415
- // src/config/common/microfrontend-config.ts
416
- var SUPPORTED_VERSIONS = ["1"];
417
- var DEFAULT_LOCAL_PROXY_PORT = 3024;
418
- var MicrofrontendConfigCommon = class {
419
- constructor({
420
- config,
421
- overrides
422
- }) {
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" }
431
- );
432
- }
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
- });
440
- }
441
- this.config = config;
442
- this.name = config.name;
443
- this.version = config.version;
444
- this.options = config.options;
445
- this.$schema = config.$schema;
446
- }
447
- isOverridesDisabled() {
448
- var _a, _b;
449
- return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
450
- }
451
- static getConfigFromEnv() {
452
- const config = process.env.MFE_CONFIG;
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) {
453
138
  if (!config) {
454
- throw new MicrofrontendError(`Missing "MFE_CONFIG" in environment.`, {
455
- type: "config",
456
- subtype: "not_found_in_env"
457
- });
139
+ throw new Error("No microfrontends configuration found");
458
140
  }
459
- return config;
460
- }
461
- static fromEnv(_) {
462
- throw new Error("Not implemented");
463
- }
464
- getConfig() {
465
- return this.config;
466
- }
467
- getAllApplications() {
468
- return Object.values(this.zones);
469
- }
470
- getZone(name) {
471
- const zone = this.zones[name];
472
- if (!zone) {
473
- throw new MicrofrontendError(
474
- `Could not find microfrontends configuration for application "${name}"`,
475
- {
476
- type: "zone",
477
- subtype: "not_found"
478
- }
479
- );
480
- }
481
- return zone;
482
- }
483
- 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;
488
- }
141
+ return new MicrofrontendConfigClient(
142
+ JSON.parse(config),
143
+ opts
489
144
  );
490
145
  }
491
- getDefaultZone() {
492
- const zone = Object.values(this.zones).find((z) => z.default);
493
- if (!zone) {
494
- throw new MicrofrontendError(
495
- `Could not find default zone in microfrontends configuration`,
496
- {
497
- type: "zone",
498
- subtype: "not_found"
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
+ }
499
167
  }
500
- );
168
+ }
501
169
  }
502
- return zone;
503
- }
504
- /**
505
- * Returns the configured port for the local proxy
506
- */
507
- getLocalProxyPort() {
508
- var _a, _b;
509
- return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
510
- }
511
- /**
512
- * Serializes the class back to the Schema type.
513
- *
514
- * NOTE: This is used when writing the config to disk and must always match the input Schema
515
- */
516
- toSchemaJson() {
517
- const applications = {};
518
- for (const [name, zone] of Object.entries(this.zones)) {
519
- applications[name] = zone.serialize();
170
+ const defaultApplication = Object.entries(this.applications).find(
171
+ ([, application]) => application.default
172
+ );
173
+ if (!defaultApplication) {
174
+ return null;
520
175
  }
521
- return {
522
- $schema: this.$schema,
523
- name: this.name,
524
- version: this.version,
525
- options: this.options,
526
- applications
527
- };
176
+ this.pathCache[path] = defaultApplication[0];
177
+ return defaultApplication[0];
528
178
  }
529
179
  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
- );
180
+ return this.serialized;
551
181
  }
552
182
  };
553
183
 
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";
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:`;
560
187
 
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
- );
569
- }
570
- return path2.join(MFE_CONFIG_DEFAULT_FILE_PATH2, MFE_CONFIG_DEFAULT_FILE_NAME2);
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));
571
192
  }
572
193
 
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
- },
618
- {
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
- };
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
201
+ };
202
+ }
894
203
 
895
- // src/config/utils/load-schema.ts
896
- var SCHEMA = schema_default;
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;
216
+ }
897
217
 
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) {
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)) {
905
223
  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(
224
+ `Unsupported version: ${version}. Supported versions are: ${SUPPORTED_VERSIONS.join(
917
225
  ", "
918
226
  )}`,
919
227
  { type: "config", subtype: "unsupported_version" }
920
228
  );
921
229
  }
922
230
  };
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
- }
231
+ var validateConfigPaths = (applicationConfigsById) => {
232
+ if (!applicationConfigsById) {
233
+ return;
967
234
  }
968
- }
969
- var validatePaths = (applicationConfigsById) => {
970
235
  const pathsByApplicationId = /* @__PURE__ */ new Map();
971
236
  const errors = [];
972
237
  for (const [id, app] of Object.entries(applicationConfigsById)) {
973
- if (isDefaultApplicationConfig(app)) {
238
+ if (isDefaultApp(app)) {
974
239
  continue;
975
240
  }
976
- for (const pathMatch of app.routing.matches) {
977
- for (const path3 of pathMatch.paths) {
978
- const maybeError = validatePathExpression(path3);
241
+ for (const pathMatch of app.routing) {
242
+ for (const path of pathMatch.paths) {
243
+ const maybeError = validatePathExpression(path);
979
244
  if (maybeError) {
980
245
  errors.push(maybeError);
981
246
  }
982
- const existing = pathsByApplicationId.get(path3);
247
+ const existing = pathsByApplicationId.get(path);
983
248
  if (existing) {
984
249
  existing.applications.push(id);
985
250
  } else {
986
- pathsByApplicationId.set(path3, {
251
+ pathsByApplicationId.set(path, {
987
252
  applications: [id],
988
- matcher: pathToRegexp(path3),
253
+ matcher: pathToRegexp2(path),
989
254
  applicationId: id
990
255
  });
991
256
  }
@@ -993,10 +258,10 @@ var validatePaths = (applicationConfigsById) => {
993
258
  }
994
259
  }
995
260
  const entries = Array.from(pathsByApplicationId.entries());
996
- entries.forEach(([path3, { applications: ids, matcher, applicationId }]) => {
261
+ entries.forEach(([path, { applications: ids, matcher, applicationId }]) => {
997
262
  if (ids.length > 1) {
998
263
  errors.push(
999
- `Duplicate path "${path3}" for applications "${ids.join(", ")}"`
264
+ `Duplicate path "${path}" for applications "${ids.join(", ")}"`
1000
265
  );
1001
266
  }
1002
267
  entries.forEach(
@@ -1004,14 +269,14 @@ var validatePaths = (applicationConfigsById) => {
1004
269
  matchPath,
1005
270
  { applications: matchIds, applicationId: matchApplicationId }
1006
271
  ]) => {
1007
- if (path3 === matchPath) {
272
+ if (path === matchPath) {
1008
273
  return;
1009
274
  }
1010
275
  if (applicationId === matchApplicationId) {
1011
276
  return;
1012
277
  }
1013
278
  if (matcher.test(matchPath)) {
1014
- const source = `"${path3}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
279
+ const source = `"${path}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
1015
280
  const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
1016
281
  errors.push(
1017
282
  `Overlapping path detected between ${source} and ${destination}`
@@ -1028,184 +293,413 @@ var validatePaths = (applicationConfigsById) => {
1028
293
  }
1029
294
  };
1030
295
  var PATH_DEFAULT_PATTERN = "[^\\/#\\?]+?";
1031
- function validatePathExpression(path3) {
1032
- const tokens = parsePathRegexp(path3);
296
+ function validatePathExpression(path) {
297
+ const tokens = parsePathRegexp(path);
1033
298
  for (let i = 0; i < tokens.length; i++) {
1034
299
  const token = tokens[i];
1035
300
  if (token === void 0) {
1036
- return `token ${i} in ${path3} is undefined, this shouldn't happen`;
301
+ return `token ${i} in ${path} is undefined, this shouldn't happen`;
1037
302
  }
1038
303
  if (typeof token !== "string") {
1039
304
  if (token.pattern !== PATH_DEFAULT_PATTERN) {
1040
- return `Path ${path3} cannot use a regular expression wildcard`;
305
+ return `Path ${path} cannot use a regular expression wildcard`;
1041
306
  }
1042
307
  if (token.prefix !== "/") {
1043
- return `Wildcard :${token.name} must be immediately after a / in ${path3}`;
308
+ return `Wildcard :${token.name} must be immediately after a / in ${path}`;
1044
309
  }
1045
310
  if (token.suffix) {
1046
311
  return `Wildcard suffix on :${token.name} is not allowed. Suffixes are not supported`;
1047
312
  }
1048
313
  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`;
314
+ return `Modifier ${token.modifier} is not allowed on wildcard :${token.name} in ${path}. Modifiers are only allowed in the last path component`;
1050
315
  }
1051
316
  }
1052
317
  }
1053
318
  return void 0;
1054
319
  }
1055
- var validateDefaults = (applicationConfigsById) => {
1056
- const defaultApplicationIds = Object.entries(applicationConfigsById).reduce((acc, [id, app]) => app.default ? [...acc, id] : acc, []);
1057
- if (defaultApplicationIds.length === 0) {
320
+ var validateAppPaths = (name, app) => {
321
+ for (const group of app.routing) {
322
+ for (const p of group.paths) {
323
+ if (p === "/") {
324
+ continue;
325
+ }
326
+ if (p.endsWith("/")) {
327
+ throw new MicrofrontendError(
328
+ `Invalid path for application "${name}". ${p} must not end with a slash.`,
329
+ { type: "application", subtype: "invalid_path" }
330
+ );
331
+ }
332
+ if (!p.startsWith("/")) {
333
+ throw new MicrofrontendError(
334
+ `Invalid path for application "${name}". ${p} must start with a slash.`,
335
+ { type: "application", subtype: "invalid_path" }
336
+ );
337
+ }
338
+ }
339
+ }
340
+ };
341
+ var validateConfigDefaultApplication = (applicationConfigsById) => {
342
+ if (!applicationConfigsById) {
343
+ return;
344
+ }
345
+ const applicationsWithRouting = Object.entries(applicationConfigsById).filter(
346
+ ([, app]) => !isDefaultApp(app)
347
+ );
348
+ const applicationsWithRoutingNames = applicationsWithRouting.map(
349
+ ([key]) => key
350
+ );
351
+ const numApplications = Object.keys(applicationConfigsById).length;
352
+ const numApplicationsWithRouting = applicationsWithRoutingNames.length;
353
+ const numApplicationsWithoutRouting = numApplications - numApplicationsWithRouting;
354
+ if (numApplicationsWithoutRouting === 0) {
1058
355
  throw new MicrofrontendError(
1059
- `No default application found. At least one application must be marked as default.`,
356
+ `No default application found. At least one application needs to be the default by omitting routing.`,
1060
357
  { type: "config", subtype: "no_default_application" }
1061
358
  );
1062
359
  }
1063
- if (defaultApplicationIds.length > 1) {
360
+ if (numApplicationsWithoutRouting > 1) {
1064
361
  throw new MicrofrontendError(
1065
- `Only one default application is allowed. Found ${defaultApplicationIds.join(", ")}.`,
362
+ `Only one application can omit "routing". Found ${applicationsWithRoutingNames.length - Object.keys(applicationConfigsById).length > 1}.`,
1066
363
  { type: "config", subtype: "multiple_default_applications" }
1067
364
  );
1068
365
  }
1069
366
  };
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
- );
367
+
368
+ // src/config/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
369
+ var PREFIX = "vc-ap";
370
+ function generateAssetPrefixFromName({
371
+ name
372
+ }) {
373
+ if (!name) {
374
+ throw new Error("Name is required to generate an asset prefix");
375
+ }
376
+ return `${PREFIX}-${name}`;
377
+ }
378
+
379
+ // src/config/microfrontends-config/isomorphic/utils/generate-port.ts
380
+ function generatePortFromName({
381
+ name,
382
+ minPort = 3e3,
383
+ maxPort = 8e3
384
+ }) {
385
+ if (!name) {
386
+ throw new Error("Name is required to generate a port");
387
+ }
388
+ let hash = 0;
389
+ for (let i = 0; i < name.length; i++) {
390
+ hash = (hash << 5) - hash + name.charCodeAt(i);
391
+ hash |= 0;
392
+ }
393
+ hash = Math.abs(hash);
394
+ const range = maxPort - minPort;
395
+ const port = minPort + hash % range;
396
+ return port;
397
+ }
398
+
399
+ // src/config/microfrontends-config/isomorphic/host.ts
400
+ var Host = class {
401
+ constructor(hostConfig, options) {
402
+ const { protocol = "https", host, port } = hostConfig;
403
+ this.protocol = protocol;
404
+ this.host = host;
405
+ this.port = Host.getPort({ port, protocol: this.protocol });
406
+ this.local = options == null ? void 0 : options.isLocal;
407
+ }
408
+ isLocal() {
409
+ return this.local || this.host === "localhost" || this.host === "127.0.0.1";
410
+ }
411
+ static getPort({
412
+ protocol,
413
+ port
414
+ }) {
415
+ if (!port) {
416
+ if (protocol === "http") {
417
+ return 80;
418
+ }
419
+ return 443;
1080
420
  }
421
+ return port;
422
+ }
423
+ isDefaultPort() {
424
+ return this.port === Host.getPort({ protocol: this.protocol });
425
+ }
426
+ toString(opts = {}) {
427
+ const url = this.toUrl(opts);
428
+ return url.toString().replace(/\/$/, "");
429
+ }
430
+ toUrl(opts = {}) {
431
+ const { includeDefaultPort } = opts;
432
+ const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
433
+ return new URL(url);
434
+ }
435
+ };
436
+ var LocalHost = class extends Host {
437
+ constructor({
438
+ appName,
439
+ ...hostConfig
440
+ }) {
441
+ const host = hostConfig.host ?? "localhost";
442
+ const port = hostConfig.port ?? generatePortFromName({ name: appName });
443
+ const protocol = hostConfig.protocol ?? "http";
444
+ super({ protocol, host, port });
1081
445
  }
1082
446
  };
1083
447
 
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
448
+ // src/config/microfrontends-config/isomorphic/application.ts
449
+ var Application = class {
450
+ constructor(name, {
451
+ app,
452
+ overrides,
453
+ isDefault
454
+ }) {
455
+ var _a, _b;
456
+ this.name = name;
457
+ this.development = {
458
+ local: new LocalHost({
459
+ appName: name,
460
+ ...(_a = app.development) == null ? void 0 : _a.local
461
+ }),
462
+ fallback: ((_b = app.development) == null ? void 0 : _b.fallback) ? new Host(app.development.fallback) : void 0
1092
463
  };
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;
464
+ this.production = app.production ? new Host(app.production) : void 0;
465
+ this.vercel = app.vercel;
466
+ this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
467
+ environment: new Host(overrides.environment)
468
+ } : void 0;
469
+ this.default = isDefault ?? false;
470
+ this.serialized = app;
1103
471
  }
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`);
472
+ isDefault() {
473
+ return this.default;
1112
474
  }
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
- };
475
+ getAssetPrefix() {
476
+ return generateAssetPrefixFromName({ name: this.name });
1127
477
  }
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");
478
+ serialize() {
479
+ return this.serialized;
1133
480
  }
1134
- return {
1135
- ...common,
1136
- partOf: defaultApplication[0]
1137
- };
1138
- }
481
+ };
482
+ var DefaultApplication = class extends Application {
483
+ constructor(name, {
484
+ app,
485
+ overrides
486
+ }) {
487
+ super(name, {
488
+ app,
489
+ overrides,
490
+ isDefault: true
491
+ });
492
+ this.default = true;
493
+ this.production = new Host(app.production);
494
+ }
495
+ getAssetPrefix() {
496
+ return "";
497
+ }
498
+ };
499
+ var ChildApplication = class extends Application {
500
+ constructor(name, {
501
+ app,
502
+ overrides
503
+ }) {
504
+ ChildApplication.validate(name, app);
505
+ super(name, {
506
+ app,
507
+ overrides,
508
+ isDefault: false
509
+ });
510
+ this.default = false;
511
+ this.routing = app.routing;
512
+ }
513
+ static validate(name, app) {
514
+ validateAppPaths(name, app);
515
+ }
516
+ };
1139
517
 
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
- }
518
+ // src/config/microfrontends-config/isomorphic/constants.ts
519
+ var DEFAULT_LOCAL_PROXY_PORT = 3024;
1150
520
 
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;
521
+ // src/config/microfrontends-config/isomorphic/index.ts
522
+ var MicrofrontendConfigIsomorphic = class {
523
+ constructor({
524
+ config,
525
+ overrides,
526
+ meta
527
+ }) {
528
+ this.childApplications = {};
529
+ var _a, _b, _c, _d;
530
+ MicrofrontendConfigIsomorphic.validate(config);
531
+ const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
532
+ this.overrides = overrides && !disableOverrides ? overrides : void 0;
533
+ this.isMainConfig = isMainConfig(config);
534
+ if (isMainConfig(config)) {
535
+ for (const [appId, appConfig] of Object.entries(config.applications)) {
536
+ const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
537
+ if (isDefaultApp(appConfig)) {
538
+ this.defaultApplication = new DefaultApplication(appId, {
539
+ app: appConfig,
540
+ overrides: appOverrides
541
+ });
542
+ } else {
543
+ this.childApplications[appId] = new ChildApplication(appId, {
544
+ app: appConfig,
545
+ overrides: appOverrides
546
+ });
547
+ }
548
+ }
549
+ } else {
550
+ this.partOf = config.partOf;
551
+ const appOverrides = !disableOverrides ? (_d = this.overrides) == null ? void 0 : _d.applications[meta.fromApp] : void 0;
552
+ this.childApplications[meta.fromApp] = new ChildApplication(
553
+ meta.fromApp,
554
+ {
555
+ // we don't know routing because we're not in the main config
556
+ app: { routing: [] },
557
+ overrides: appOverrides
558
+ }
559
+ );
560
+ }
561
+ if (isMainConfig(config) && !this.defaultApplication) {
562
+ throw new MicrofrontendError(
563
+ `Could not find default application in microfrontends configuration`,
564
+ {
565
+ type: "application",
566
+ subtype: "not_found"
567
+ }
568
+ );
569
+ }
570
+ this.config = config;
571
+ this.options = config.options;
572
+ this.serialized = {
573
+ config,
574
+ overrides,
575
+ meta
576
+ };
577
+ }
578
+ static validate(config) {
579
+ const c = typeof config === "string" ? parse(config) : config;
580
+ if (isMainConfig(c)) {
581
+ validateConfigVersion(c.version);
582
+ validateConfigPaths(c.applications);
583
+ validateConfigDefaultApplication(c.applications);
584
+ }
585
+ return c;
1161
586
  }
1162
587
  static fromEnv({
588
+ meta,
1163
589
  cookies
1164
590
  }) {
1165
- return new MicrofrontendConfigCommon({
1166
- config: MicrofrontendConfig.validate(
1167
- MicrofrontendConfigCommon.getConfigFromEnv()
1168
- ),
1169
- overrides: Overrides.parseOverrides(cookies)
591
+ return new MicrofrontendConfigIsomorphic({
592
+ config: parse(getConfigStringFromEnv()),
593
+ overrides: parseOverrides(cookies ?? []),
594
+ meta
1170
595
  });
1171
596
  }
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
- });
597
+ isOverridesDisabled() {
598
+ var _a, _b;
599
+ return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
600
+ }
601
+ getConfig() {
602
+ return this.config;
603
+ }
604
+ getApplicationsByType() {
605
+ return {
606
+ defaultApplication: this.defaultApplication,
607
+ applications: Object.values(this.childApplications)
608
+ };
609
+ }
610
+ getChildApplications() {
611
+ return Object.values(this.childApplications);
612
+ }
613
+ getAllApplications() {
614
+ return [
615
+ this.defaultApplication,
616
+ ...Object.values(this.childApplications)
617
+ ].filter(Boolean);
618
+ }
619
+ getApplication(name) {
620
+ var _a;
621
+ if (((_a = this.defaultApplication) == null ? void 0 : _a.name) === name) {
622
+ return this.defaultApplication;
623
+ }
624
+ const app = this.childApplications[name];
625
+ if (!app) {
626
+ throw new MicrofrontendError(
627
+ `Could not find microfrontends configuration for application "${name}"`,
628
+ {
629
+ type: "application",
630
+ subtype: "not_found"
631
+ }
632
+ );
1184
633
  }
634
+ return app;
635
+ }
636
+ getApplicationByProjectId(projectId) {
637
+ var _a, _b;
638
+ if (((_b = (_a = this.defaultApplication) == null ? void 0 : _a.vercel) == null ? void 0 : _b.projectId) === projectId) {
639
+ return this.defaultApplication;
640
+ }
641
+ return Object.values(this.childApplications).find(
642
+ (app) => {
643
+ var _a2;
644
+ return ((_a2 = app.vercel) == null ? void 0 : _a2.projectId) === projectId;
645
+ }
646
+ );
1185
647
  }
1186
648
  /**
1187
- * Writes the configuration to a file.
649
+ * Returns the default application. This can throw if the default application
650
+ * is undefined ( )
1188
651
  */
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);
652
+ getDefaultApplication() {
653
+ if (!this.defaultApplication) {
654
+ throw new MicrofrontendError(
655
+ `Could not find default application in microfrontends configuration`,
656
+ {
657
+ type: "application",
658
+ subtype: "not_found"
659
+ }
660
+ );
1195
661
  }
1196
- if (versions.includes("v2")) {
1197
- const outputPath = getOutputFilePath();
1198
- const v2Config = convertV1ConfigToV2Config(config, fromApp);
1199
- writeFile(outputPath, v2Config, pretty);
662
+ return this.defaultApplication;
663
+ }
664
+ /**
665
+ * Returns the configured port for the local proxy
666
+ */
667
+ getLocalProxyPort() {
668
+ var _a, _b;
669
+ return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
670
+ }
671
+ /**
672
+ * Serializes the class back to the Schema type.
673
+ *
674
+ * NOTE: This is used when writing the config to disk and must always match the input Schema
675
+ */
676
+ toSchemaJson() {
677
+ return this.serialized.config;
678
+ }
679
+ toClientConfig() {
680
+ const applications = Object.fromEntries(
681
+ Object.entries(this.childApplications).map(([name, application]) => [
682
+ name,
683
+ {
684
+ default: false,
685
+ routing: application.routing
686
+ }
687
+ ])
688
+ );
689
+ if (this.defaultApplication) {
690
+ applications[this.defaultApplication.name] = {
691
+ default: true
692
+ };
1200
693
  }
694
+ return new MicrofrontendConfigClient({
695
+ applications
696
+ });
697
+ }
698
+ serialize() {
699
+ return this.serialized;
1201
700
  }
1202
701
  };
1203
702
  export {
1204
- MicrofrontendConfig,
1205
- getClientConfigFromEnv,
1206
- getConfigForClient,
1207
- getWellKnownClientData,
1208
- isCommonApplicationConfig,
1209
- isDefaultApplicationConfig
703
+ MicrofrontendConfigIsomorphic
1210
704
  };
1211
705
  //# sourceMappingURL=config.js.map