@vercel/microfrontends 0.13.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 (120) hide show
  1. package/README.md +3 -3
  2. package/dist/bin/cli.cjs +403 -1627
  3. package/dist/config.cjs +460 -1004
  4. package/dist/config.cjs.map +1 -1
  5. package/dist/config.d.ts +4 -25
  6. package/dist/config.js +456 -983
  7. package/dist/config.js.map +1 -1
  8. package/dist/{index-83133f2d.d.ts → index-bf67a461.d.ts} +3 -10
  9. package/dist/{v2/microfrontends → microfrontends}/server.cjs +128 -194
  10. package/dist/microfrontends/server.cjs.map +1 -0
  11. package/dist/{v2/microfrontends → microfrontends}/server.d.ts +6 -5
  12. package/dist/{v2/microfrontends → microfrontends}/server.js +125 -191
  13. package/dist/microfrontends/server.js.map +1 -0
  14. package/dist/{v2/microfrontends.cjs → microfrontends.cjs} +21 -35
  15. package/dist/microfrontends.cjs.map +1 -0
  16. package/dist/{v2/microfrontends.d.ts → microfrontends.d.ts} +4 -3
  17. package/dist/{v2/microfrontends.js → microfrontends.js} +20 -34
  18. package/dist/microfrontends.js.map +1 -0
  19. package/dist/next/client.cjs +1 -1
  20. package/dist/next/client.cjs.map +1 -1
  21. package/dist/next/client.js +1 -1
  22. package/dist/next/client.js.map +1 -1
  23. package/dist/next/config.cjs +1323 -1024
  24. package/dist/next/config.cjs.map +1 -1
  25. package/dist/next/config.d.ts +1 -1
  26. package/dist/next/config.js +1327 -1028
  27. package/dist/next/config.js.map +1 -1
  28. package/dist/next/endpoints.cjs +77 -18
  29. package/dist/next/endpoints.cjs.map +1 -1
  30. package/dist/next/endpoints.d.ts +14 -3
  31. package/dist/next/endpoints.js +77 -18
  32. package/dist/next/endpoints.js.map +1 -1
  33. package/dist/next/middleware.cjs +745 -425
  34. package/dist/next/middleware.cjs.map +1 -1
  35. package/dist/next/middleware.d.ts +10 -5
  36. package/dist/next/middleware.js +745 -425
  37. package/dist/next/middleware.js.map +1 -1
  38. package/dist/next/testing.cjs +595 -1032
  39. package/dist/next/testing.cjs.map +1 -1
  40. package/dist/next/testing.d.ts +14 -12
  41. package/dist/next/testing.js +589 -1016
  42. package/dist/next/testing.js.map +1 -1
  43. package/dist/overrides.cjs +40 -108
  44. package/dist/overrides.cjs.map +1 -1
  45. package/dist/overrides.d.ts +24 -2
  46. package/dist/overrides.js +36 -106
  47. package/dist/overrides.js.map +1 -1
  48. package/dist/routing.cjs +19 -0
  49. package/dist/routing.cjs.map +1 -0
  50. package/dist/routing.d.ts +26 -0
  51. package/dist/routing.js +1 -0
  52. package/dist/{v2/schema.cjs → schema.cjs} +1 -1
  53. package/dist/schema.cjs.map +1 -0
  54. package/dist/schema.d.ts +1 -0
  55. package/dist/schema.js.map +1 -0
  56. package/dist/{types-a995174e.d.ts → types-a29d224a.d.ts} +1 -7
  57. package/dist/{types-15b7f215.d.ts → types-cfe3308b.d.ts} +1 -1
  58. package/dist/types-fc30696d.d.ts +11 -0
  59. package/dist/utils/mfe-port.cjs +254 -1362
  60. package/dist/utils/mfe-port.cjs.map +1 -1
  61. package/dist/utils/mfe-port.js +250 -1358
  62. package/dist/utils/mfe-port.js.map +1 -1
  63. package/dist/validation.cjs +31 -365
  64. package/dist/validation.cjs.map +1 -1
  65. package/dist/validation.d.ts +3 -152
  66. package/dist/validation.js +30 -363
  67. package/dist/validation.js.map +1 -1
  68. package/package.json +32 -80
  69. package/schema/schema.json +174 -244
  70. package/dist/config/client.cjs +0 -54
  71. package/dist/config/client.cjs.map +0 -1
  72. package/dist/config/client.d.ts +0 -23
  73. package/dist/config/client.js +0 -28
  74. package/dist/config/client.js.map +0 -1
  75. package/dist/config/edge.cjs +0 -508
  76. package/dist/config/edge.cjs.map +0 -1
  77. package/dist/config/edge.d.ts +0 -20
  78. package/dist/config/edge.js +0 -481
  79. package/dist/config/edge.js.map +0 -1
  80. package/dist/microfrontend-config-983a5139.d.ts +0 -154
  81. package/dist/schema-2922d49e.d.ts +0 -182
  82. package/dist/v2/config.cjs +0 -723
  83. package/dist/v2/config.cjs.map +0 -1
  84. package/dist/v2/config.d.ts +0 -3
  85. package/dist/v2/config.js +0 -698
  86. package/dist/v2/config.js.map +0 -1
  87. package/dist/v2/microfrontends/server.cjs.map +0 -1
  88. package/dist/v2/microfrontends/server.js.map +0 -1
  89. package/dist/v2/microfrontends.cjs.map +0 -1
  90. package/dist/v2/microfrontends.js.map +0 -1
  91. package/dist/v2/next/client.cjs +0 -3
  92. package/dist/v2/next/client.cjs.map +0 -1
  93. package/dist/v2/next/client.d.ts +0 -45
  94. package/dist/v2/next/client.js +0 -3
  95. package/dist/v2/next/client.js.map +0 -1
  96. package/dist/v2/next/config.cjs +0 -2132
  97. package/dist/v2/next/config.cjs.map +0 -1
  98. package/dist/v2/next/config.d.ts +0 -22
  99. package/dist/v2/next/config.js +0 -2097
  100. package/dist/v2/next/config.js.map +0 -1
  101. package/dist/v2/next/endpoints.cjs +0 -141
  102. package/dist/v2/next/endpoints.cjs.map +0 -1
  103. package/dist/v2/next/endpoints.d.ts +0 -15
  104. package/dist/v2/next/endpoints.js +0 -116
  105. package/dist/v2/next/endpoints.js.map +0 -1
  106. package/dist/v2/next/middleware.cjs +0 -1143
  107. package/dist/v2/next/middleware.cjs.map +0 -1
  108. package/dist/v2/next/middleware.d.ts +0 -29
  109. package/dist/v2/next/middleware.js +0 -1115
  110. package/dist/v2/next/middleware.js.map +0 -1
  111. package/dist/v2/overrides.cjs +0 -75
  112. package/dist/v2/overrides.cjs.map +0 -1
  113. package/dist/v2/overrides.d.ts +0 -24
  114. package/dist/v2/overrides.js +0 -45
  115. package/dist/v2/overrides.js.map +0 -1
  116. package/dist/v2/schema.cjs.map +0 -1
  117. package/dist/v2/schema.d.ts +0 -1
  118. package/schema/schema-v2.json +0 -270
  119. /package/dist/{v2/schema.js.map → routing.js.map} +0 -0
  120. /package/dist/{v2/schema.js → schema.js} +0 -0
package/dist/v2/config.js DELETED
@@ -1,698 +0,0 @@
1
- // src/config-v2/microfrontends-config/isomorphic/index.ts
2
- import { parse } from "jsonc-parser";
3
-
4
- // src/config-v2/errors.ts
5
- var MicrofrontendError = class extends Error {
6
- constructor(message, opts) {
7
- super(message);
8
- this.name = "MicrofrontendsError";
9
- this.source = (opts == null ? void 0 : opts.source) ?? "@vercel/microfrontends";
10
- this.type = (opts == null ? void 0 : opts.type) ?? "unknown";
11
- this.subtype = opts == null ? void 0 : opts.subtype;
12
- Error.captureStackTrace(this, MicrofrontendError);
13
- }
14
- isKnown() {
15
- return this.type !== "unknown";
16
- }
17
- isUnknown() {
18
- return !this.isKnown();
19
- }
20
- /**
21
- * Converts an error to a MicrofrontendsError.
22
- * @param original - The original error to convert.
23
- * @returns The converted MicrofrontendsError.
24
- */
25
- static convert(original, opts) {
26
- if (opts == null ? void 0 : opts.fileName) {
27
- const err = MicrofrontendError.convertFSError(original, opts.fileName);
28
- if (err) {
29
- return err;
30
- }
31
- }
32
- if (original.message.includes(
33
- "Code generation from strings disallowed for this context"
34
- )) {
35
- return new MicrofrontendError(original.message, {
36
- type: "config",
37
- subtype: "unsupported_validation_env",
38
- source: "ajv"
39
- });
40
- }
41
- return new MicrofrontendError(original.message);
42
- }
43
- static convertFSError(original, fileName) {
44
- if (original instanceof Error && "code" in original) {
45
- if (original.code === "ENOENT") {
46
- return new MicrofrontendError(`Could not find "${fileName}"`, {
47
- type: "config",
48
- subtype: "unable_to_read_file",
49
- source: "fs"
50
- });
51
- }
52
- if (original.code === "EACCES") {
53
- return new MicrofrontendError(
54
- `Permission denied while accessing "${fileName}"`,
55
- {
56
- type: "config",
57
- subtype: "invalid_permissions",
58
- source: "fs"
59
- }
60
- );
61
- }
62
- }
63
- if (original instanceof SyntaxError) {
64
- return new MicrofrontendError(
65
- `Failed to parse "${fileName}": Invalid JSON format.`,
66
- {
67
- type: "config",
68
- subtype: "invalid_syntax",
69
- source: "fs"
70
- }
71
- );
72
- }
73
- return null;
74
- }
75
- /**
76
- * Handles an unknown error and returns a MicrofrontendsError instance.
77
- * @param err - The error to handle.
78
- * @returns A MicrofrontendsError instance.
79
- */
80
- static handle(err, opts) {
81
- if (err instanceof MicrofrontendError) {
82
- return err;
83
- }
84
- if (err instanceof Error) {
85
- return MicrofrontendError.convert(err, opts);
86
- }
87
- if (typeof err === "object" && err !== null) {
88
- if ("message" in err && typeof err.message === "string") {
89
- return MicrofrontendError.convert(new Error(err.message), opts);
90
- }
91
- }
92
- return new MicrofrontendError("An unknown error occurred");
93
- }
94
- };
95
-
96
- // src/config-v2/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-v2/schema/utils/is-main-config.ts
109
- function isMainConfig(c) {
110
- return !("partOf" in c);
111
- }
112
-
113
- // src/config-v2/schema/utils/is-default-app.ts
114
- function isDefaultApp(a) {
115
- return !("routing" in a);
116
- }
117
-
118
- // src/config-v2/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
- }
129
- }
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
- }
167
- }
168
- }
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-v2/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-v2/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-v2/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
- }
203
-
204
- // src/config-v2/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
- }
217
-
218
- // src/config-v2/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
- );
229
- }
230
- };
231
- var validateConfigPaths = (applicationConfigsById) => {
232
- if (!applicationConfigsById) {
233
- return;
234
- }
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
- }
263
- }
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"
296
- });
297
- }
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
- }
317
- }
318
- }
319
- };
320
- var validateConfigDefaultApplication = (applicationConfigsById) => {
321
- if (!applicationConfigsById) {
322
- return;
323
- }
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
- );
344
- }
345
- };
346
- var validateConfigOptions = (options) => {
347
- var _a;
348
- if ((_a = options == null ? void 0 : options.vercel) == null ? void 0 : _a.previewDeploymentSuffix) {
349
- if (!/^[a-zA-Z]{2,}\.[a-zA-Z]{2,}$/.test(
350
- options.vercel.previewDeploymentSuffix
351
- )) {
352
- throw new MicrofrontendError(
353
- `Invalid preview deployment suffix: ${options.vercel.previewDeploymentSuffix}. Should have be formatted like "vercel.app".`,
354
- { type: "config", subtype: "invalid_preview_deployment_suffix" }
355
- );
356
- }
357
- }
358
- };
359
-
360
- // src/config-v2/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
361
- var PREFIX = "vc-ap";
362
- function generateAssetPrefixFromName({
363
- name
364
- }) {
365
- if (!name) {
366
- throw new Error("Name is required to generate an asset prefix");
367
- }
368
- return `${PREFIX}-${name}`;
369
- }
370
-
371
- // src/config-v2/microfrontends-config/isomorphic/utils/generate-port.ts
372
- function generatePortFromName({
373
- name,
374
- minPort = 3e3,
375
- maxPort = 8e3
376
- }) {
377
- if (!name) {
378
- throw new Error("Name is required to generate a port");
379
- }
380
- let hash = 0;
381
- for (let i = 0; i < name.length; i++) {
382
- hash = (hash << 5) - hash + name.charCodeAt(i);
383
- hash |= 0;
384
- }
385
- hash = Math.abs(hash);
386
- const range = maxPort - minPort;
387
- const port = minPort + hash % range;
388
- return port;
389
- }
390
-
391
- // src/config-v2/microfrontends-config/isomorphic/host.ts
392
- var Host = class {
393
- constructor(hostConfig, options) {
394
- const { protocol = "https", host, port } = hostConfig;
395
- this.protocol = protocol;
396
- this.host = host;
397
- this.port = Host.getPort({ port, protocol: this.protocol });
398
- this.local = options == null ? void 0 : options.isLocal;
399
- }
400
- isLocal() {
401
- return this.local || this.host === "localhost" || this.host === "127.0.0.1";
402
- }
403
- static getPort({
404
- protocol,
405
- port
406
- }) {
407
- if (!port) {
408
- if (protocol === "http") {
409
- return 80;
410
- }
411
- return 443;
412
- }
413
- return port;
414
- }
415
- isDefaultPort() {
416
- return this.port === Host.getPort({ protocol: this.protocol });
417
- }
418
- toString(opts = {}) {
419
- const url = this.toUrl(opts);
420
- return url.toString().replace(/\/$/, "");
421
- }
422
- toUrl(opts = {}) {
423
- const { includeDefaultPort } = opts;
424
- const url = `${this.protocol}://${this.host}${this.isDefaultPort() && !includeDefaultPort ? "" : `:${this.port}`}`;
425
- return new URL(url);
426
- }
427
- };
428
- var LocalHost = class extends Host {
429
- constructor({
430
- appName,
431
- ...hostConfig
432
- }) {
433
- const host = hostConfig.host ?? "localhost";
434
- const port = hostConfig.port ?? generatePortFromName({ name: appName });
435
- const protocol = hostConfig.protocol ?? "http";
436
- super({ protocol, host, port });
437
- }
438
- };
439
-
440
- // src/config-v2/microfrontends-config/isomorphic/application.ts
441
- var Application = class {
442
- constructor(name, {
443
- app,
444
- overrides,
445
- isDefault
446
- }) {
447
- var _a, _b;
448
- this.name = name;
449
- this.development = {
450
- local: new LocalHost({
451
- appName: name,
452
- ...(_a = app.development) == null ? void 0 : _a.local
453
- }),
454
- fallback: ((_b = app.development) == null ? void 0 : _b.fallback) ? new Host(app.development.fallback) : void 0
455
- };
456
- this.production = app.production ? new Host(app.production) : void 0;
457
- this.vercel = app.vercel;
458
- this.overrides = (overrides == null ? void 0 : overrides.environment) ? {
459
- environment: new Host(overrides.environment)
460
- } : void 0;
461
- this.default = isDefault ?? false;
462
- this.serialized = app;
463
- }
464
- isDefault() {
465
- return this.default;
466
- }
467
- getAssetPrefix() {
468
- return generateAssetPrefixFromName({ name: this.name });
469
- }
470
- serialize() {
471
- return this.serialized;
472
- }
473
- };
474
- var DefaultApplication = class extends Application {
475
- constructor(name, {
476
- app,
477
- overrides
478
- }) {
479
- super(name, {
480
- app,
481
- overrides,
482
- isDefault: true
483
- });
484
- this.default = true;
485
- this.production = new Host(app.production);
486
- }
487
- getAssetPrefix() {
488
- return "";
489
- }
490
- };
491
- var ChildApplication = class extends Application {
492
- constructor(name, {
493
- app,
494
- overrides
495
- }) {
496
- ChildApplication.validate(name, app);
497
- super(name, {
498
- app,
499
- overrides,
500
- isDefault: false
501
- });
502
- this.default = false;
503
- this.routing = app.routing;
504
- }
505
- static validate(name, app) {
506
- validateAppPaths(name, app);
507
- }
508
- };
509
-
510
- // src/config-v2/microfrontends-config/isomorphic/constants.ts
511
- var DEFAULT_LOCAL_PROXY_PORT = 3024;
512
-
513
- // src/config-v2/microfrontends-config/isomorphic/index.ts
514
- var MicrofrontendConfigIsomorphic = class {
515
- constructor({
516
- config,
517
- overrides,
518
- meta
519
- }) {
520
- this.childApplications = {};
521
- var _a, _b, _c, _d;
522
- MicrofrontendConfigIsomorphic.validate(config);
523
- const disableOverrides = ((_b = (_a = config.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
524
- this.overrides = overrides && !disableOverrides ? overrides : void 0;
525
- this.isMainConfig = isMainConfig(config);
526
- if (isMainConfig(config)) {
527
- for (const [appId, appConfig] of Object.entries(config.applications)) {
528
- const appOverrides = !disableOverrides ? (_c = this.overrides) == null ? void 0 : _c.applications[appId] : void 0;
529
- if (isDefaultApp(appConfig)) {
530
- this.defaultApplication = new DefaultApplication(appId, {
531
- app: appConfig,
532
- overrides: appOverrides
533
- });
534
- } else {
535
- this.childApplications[appId] = new ChildApplication(appId, {
536
- app: appConfig,
537
- overrides: appOverrides
538
- });
539
- }
540
- }
541
- } else {
542
- this.partOf = config.partOf;
543
- const appOverrides = !disableOverrides ? (_d = this.overrides) == null ? void 0 : _d.applications[meta.fromApp] : void 0;
544
- this.childApplications[meta.fromApp] = new ChildApplication(
545
- meta.fromApp,
546
- {
547
- // we don't know routing because we're not in the main config
548
- app: { routing: [] },
549
- overrides: appOverrides
550
- }
551
- );
552
- }
553
- if (isMainConfig(config) && !this.defaultApplication) {
554
- throw new MicrofrontendError(
555
- `Could not find default application in microfrontends configuration`,
556
- {
557
- type: "application",
558
- subtype: "not_found"
559
- }
560
- );
561
- }
562
- this.config = config;
563
- this.options = config.options;
564
- this.serialized = {
565
- config,
566
- overrides,
567
- meta
568
- };
569
- }
570
- static validate(config) {
571
- const c = typeof config === "string" ? parse(config) : config;
572
- if (isMainConfig(c)) {
573
- validateConfigVersion(c.version);
574
- validateConfigPaths(c.applications);
575
- validateConfigDefaultApplication(c.applications);
576
- }
577
- validateConfigOptions(c.options);
578
- return c;
579
- }
580
- static fromEnv({
581
- meta,
582
- cookies
583
- }) {
584
- return new MicrofrontendConfigIsomorphic({
585
- config: parse(getConfigStringFromEnv()),
586
- overrides: parseOverrides(cookies ?? []),
587
- meta
588
- });
589
- }
590
- isOverridesDisabled() {
591
- var _a, _b;
592
- return ((_b = (_a = this.options) == null ? void 0 : _a.vercel) == null ? void 0 : _b.disableOverrides) ?? false;
593
- }
594
- getConfig() {
595
- return this.config;
596
- }
597
- getApplicationsByType() {
598
- return {
599
- defaultApplication: this.defaultApplication,
600
- applications: Object.values(this.childApplications)
601
- };
602
- }
603
- getChildApplications() {
604
- return Object.values(this.childApplications);
605
- }
606
- getAllApplications() {
607
- return [
608
- this.defaultApplication,
609
- ...Object.values(this.childApplications)
610
- ].filter(Boolean);
611
- }
612
- getApplication(name) {
613
- var _a;
614
- if (((_a = this.defaultApplication) == null ? void 0 : _a.name) === name) {
615
- return this.defaultApplication;
616
- }
617
- const app = this.childApplications[name];
618
- if (!app) {
619
- throw new MicrofrontendError(
620
- `Could not find microfrontends configuration for application "${name}"`,
621
- {
622
- type: "application",
623
- subtype: "not_found"
624
- }
625
- );
626
- }
627
- return app;
628
- }
629
- getApplicationByProjectId(projectId) {
630
- var _a, _b;
631
- if (((_b = (_a = this.defaultApplication) == null ? void 0 : _a.vercel) == null ? void 0 : _b.projectId) === projectId) {
632
- return this.defaultApplication;
633
- }
634
- return Object.values(this.childApplications).find(
635
- (app) => {
636
- var _a2;
637
- return ((_a2 = app.vercel) == null ? void 0 : _a2.projectId) === projectId;
638
- }
639
- );
640
- }
641
- /**
642
- * Returns the default application. This can throw if the default application
643
- * is undefined ( )
644
- */
645
- getDefaultApplication() {
646
- if (!this.defaultApplication) {
647
- throw new MicrofrontendError(
648
- `Could not find default application in microfrontends configuration`,
649
- {
650
- type: "application",
651
- subtype: "not_found"
652
- }
653
- );
654
- }
655
- return this.defaultApplication;
656
- }
657
- /**
658
- * Returns the configured port for the local proxy
659
- */
660
- getLocalProxyPort() {
661
- var _a, _b;
662
- return ((_b = (_a = this.config.options) == null ? void 0 : _a.localProxy) == null ? void 0 : _b.port) ?? DEFAULT_LOCAL_PROXY_PORT;
663
- }
664
- /**
665
- * Serializes the class back to the Schema type.
666
- *
667
- * NOTE: This is used when writing the config to disk and must always match the input Schema
668
- */
669
- toSchemaJson() {
670
- return this.serialized.config;
671
- }
672
- toClientConfig() {
673
- const applications = Object.fromEntries(
674
- Object.entries(this.childApplications).map(([name, application]) => [
675
- name,
676
- {
677
- default: false,
678
- routing: application.routing
679
- }
680
- ])
681
- );
682
- if (this.defaultApplication) {
683
- applications[this.defaultApplication.name] = {
684
- default: true
685
- };
686
- }
687
- return new MicrofrontendConfigClient({
688
- applications
689
- });
690
- }
691
- serialize() {
692
- return this.serialized;
693
- }
694
- };
695
- export {
696
- MicrofrontendConfigIsomorphic
697
- };
698
- //# sourceMappingURL=config.js.map