@vercel/microfrontends 1.1.1-canary.3 → 1.1.1-canary.4

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 (60) hide show
  1. package/dist/bin/cli.cjs +225 -493
  2. package/dist/config.cjs +27 -62
  3. package/dist/config.cjs.map +1 -1
  4. package/dist/config.d.ts +153 -4
  5. package/dist/config.js +27 -62
  6. package/dist/config.js.map +1 -1
  7. package/dist/experimental/sveltekit.cjs +228 -480
  8. package/dist/experimental/sveltekit.cjs.map +1 -1
  9. package/dist/experimental/sveltekit.js +218 -470
  10. package/dist/experimental/sveltekit.js.map +1 -1
  11. package/dist/experimental/vite.cjs +258 -502
  12. package/dist/experimental/vite.cjs.map +1 -1
  13. package/dist/experimental/vite.js +244 -488
  14. package/dist/experimental/vite.js.map +1 -1
  15. package/dist/microfrontends/server.cjs +227 -476
  16. package/dist/microfrontends/server.cjs.map +1 -1
  17. package/dist/microfrontends/server.d.ts +14 -20
  18. package/dist/microfrontends/server.js +217 -466
  19. package/dist/microfrontends/server.js.map +1 -1
  20. package/dist/next/config.cjs +229 -489
  21. package/dist/next/config.cjs.map +1 -1
  22. package/dist/next/config.js +219 -479
  23. package/dist/next/config.js.map +1 -1
  24. package/dist/next/endpoints.d.ts +2 -2
  25. package/dist/next/middleware.cjs +42 -162
  26. package/dist/next/middleware.cjs.map +1 -1
  27. package/dist/next/middleware.d.ts +2 -4
  28. package/dist/next/middleware.js +42 -162
  29. package/dist/next/middleware.js.map +1 -1
  30. package/dist/next/testing.cjs +28 -64
  31. package/dist/next/testing.cjs.map +1 -1
  32. package/dist/next/testing.d.ts +4 -4
  33. package/dist/next/testing.js +28 -64
  34. package/dist/next/testing.js.map +1 -1
  35. package/dist/overrides.d.ts +3 -3
  36. package/dist/schema.cjs +2 -9
  37. package/dist/schema.cjs.map +1 -1
  38. package/dist/schema.d.ts +3 -4
  39. package/dist/schema.js +1 -7
  40. package/dist/schema.js.map +1 -1
  41. package/dist/{types-6ee19ccc.d.ts → types-54064641.d.ts} +2 -13
  42. package/dist/{types-73527280.d.ts → types-a4add5ab.d.ts} +1 -1
  43. package/dist/{types-74e3336c.d.ts → types-f1260e44.d.ts} +1 -1
  44. package/dist/utils/mfe-port.cjs +232 -483
  45. package/dist/utils/mfe-port.cjs.map +1 -1
  46. package/dist/utils/mfe-port.js +218 -469
  47. package/dist/utils/mfe-port.js.map +1 -1
  48. package/dist/validation.cjs +0 -31
  49. package/dist/validation.cjs.map +1 -1
  50. package/dist/validation.d.ts +1 -1
  51. package/dist/validation.js +0 -31
  52. package/dist/validation.js.map +1 -1
  53. package/package.json +1 -8
  54. package/schema/schema.json +0 -33
  55. package/dist/index-7e69650e.d.ts +0 -165
  56. package/dist/microfrontends.cjs +0 -969
  57. package/dist/microfrontends.cjs.map +0 -1
  58. package/dist/microfrontends.d.ts +0 -45
  59. package/dist/microfrontends.js +0 -942
  60. package/dist/microfrontends.js.map +0 -1
@@ -1,942 +0,0 @@
1
- // src/config/microfrontends-config/isomorphic/index.ts
2
- import { parse } from "jsonc-parser";
3
-
4
- // src/config/errors.ts
5
- var MicrofrontendError = class extends Error {
6
- constructor(message, opts) {
7
- super(message, { cause: opts?.cause });
8
- this.name = "MicrofrontendsError";
9
- this.source = opts?.source ?? "@vercel/microfrontends";
10
- this.type = opts?.type ?? "unknown";
11
- this.subtype = 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?.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/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?.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/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
- return Boolean(cookie.name?.startsWith(OVERRIDES_COOKIE_PREFIX));
191
- }
192
-
193
- // src/config/overrides/get-override-from-cookie.ts
194
- function getOverrideFromCookie(cookie) {
195
- if (!isOverrideCookie(cookie) || !cookie.value)
196
- return;
197
- return {
198
- application: cookie.name.replace(OVERRIDES_ENV_COOKIE_PREFIX, ""),
199
- host: cookie.value
200
- };
201
- }
202
-
203
- // src/config/overrides/parse-overrides.ts
204
- function parseOverrides(cookies) {
205
- const overridesConfig = { applications: {} };
206
- cookies.forEach((cookie) => {
207
- const override = getOverrideFromCookie(cookie);
208
- if (!override)
209
- return;
210
- overridesConfig.applications[override.application] = {
211
- environment: { host: override.host }
212
- };
213
- });
214
- return overridesConfig;
215
- }
216
-
217
- // src/config/microfrontends-config/isomorphic/validation.ts
218
- import { pathToRegexp as pathToRegexp2, parse as parsePathRegexp } from "path-to-regexp";
219
- var LIST_FORMATTER = new Intl.ListFormat("en", {
220
- style: "long",
221
- type: "conjunction"
222
- });
223
- var validateConfigPaths = (applicationConfigsById) => {
224
- if (!applicationConfigsById) {
225
- return;
226
- }
227
- const pathsByApplicationId = /* @__PURE__ */ new Map();
228
- const errors = [];
229
- for (const [id, app] of Object.entries(applicationConfigsById)) {
230
- if (isDefaultApp(app)) {
231
- continue;
232
- }
233
- const childApp = app;
234
- for (const pathMatch of childApp.routing) {
235
- for (const path of pathMatch.paths) {
236
- const maybeError = validatePathExpression(path);
237
- if (maybeError) {
238
- errors.push(maybeError);
239
- } else {
240
- const existing = pathsByApplicationId.get(path);
241
- if (existing) {
242
- existing.applications.push(id);
243
- } else {
244
- pathsByApplicationId.set(path, {
245
- applications: [id],
246
- matcher: pathToRegexp2(path),
247
- applicationId: id
248
- });
249
- }
250
- }
251
- }
252
- }
253
- }
254
- const entries = Array.from(pathsByApplicationId.entries());
255
- for (const [path, { applications: ids, matcher, applicationId }] of entries) {
256
- if (ids.length > 1) {
257
- errors.push(
258
- `Duplicate path "${path}" for applications "${ids.join(", ")}"`
259
- );
260
- }
261
- for (const [
262
- matchPath,
263
- { applications: matchIds, applicationId: matchApplicationId }
264
- ] of entries) {
265
- if (path === matchPath) {
266
- continue;
267
- }
268
- if (applicationId === matchApplicationId) {
269
- continue;
270
- }
271
- if (matcher.test(matchPath)) {
272
- const source = `"${path}" of application${ids.length > 0 ? "s" : ""} ${ids.join(", ")}`;
273
- const destination = `"${matchPath}" of application${matchIds.length > 0 ? "s" : ""} ${matchIds.join(", ")}`;
274
- errors.push(
275
- `Overlapping path detected between ${source} and ${destination}`
276
- );
277
- }
278
- }
279
- }
280
- if (errors.length) {
281
- throw new MicrofrontendError(
282
- `Invalid paths: ${errors.join(", ")}. See supported paths in the documentation https://vercel.com/docs/microfrontends/path-routing#supported-path-expressions.`,
283
- {
284
- type: "config",
285
- subtype: "conflicting_paths"
286
- }
287
- );
288
- }
289
- };
290
- var PATH_DEFAULT_PATTERN = "[^\\/#\\?]+?";
291
- function validatePathExpression(path) {
292
- try {
293
- const tokens = parsePathRegexp(path);
294
- if (/(?<!\\)\{/.test(path)) {
295
- return `Optional paths are not supported: ${path}`;
296
- }
297
- if (/(?<!\\|\()\?/.test(path)) {
298
- return `Optional paths are not supported: ${path}`;
299
- }
300
- if (/\/[^/]*(?<!\\):[^/]*(?<!\\):[^/]*/.test(path)) {
301
- return `Only one wildcard is allowed per path segment: ${path}`;
302
- }
303
- for (let i = 0; i < tokens.length; i++) {
304
- const token = tokens[i];
305
- if (token === void 0) {
306
- return `token ${i} in ${path} is undefined, this shouldn't happen`;
307
- }
308
- if (typeof token !== "string") {
309
- if (!token.name) {
310
- return `Only named wildcards are allowed: ${path} (hint: add ":path" to the wildcard)`;
311
- }
312
- if (token.pattern !== PATH_DEFAULT_PATTERN && // Allows (a|b|c) and ((?!a|b|c).*) regex
313
- // Only limited regex is supported for now, due to performance considerations
314
- !/^(?<allowed>[\w]+(?:\|[^|()]+)+)$|^\(\?!(?<disallowed>[\w]+(?:\|[^|()]+)+)\)\.\*$/.test(
315
- token.pattern
316
- )) {
317
- return `Path ${path} cannot use unsupported regular expression wildcard`;
318
- }
319
- if (token.modifier && i !== tokens.length - 1) {
320
- return `Modifier ${token.modifier} is not allowed on wildcard :${token.name} in ${path}. Modifiers are only allowed in the last path component`;
321
- }
322
- }
323
- }
324
- } catch (e) {
325
- const message = e instanceof Error ? e.message : String(e);
326
- return `Path ${path} could not be parsed into regexp: ${message}`;
327
- }
328
- return void 0;
329
- }
330
- var validateAppPaths = (name, app) => {
331
- for (const group of app.routing) {
332
- for (const p of group.paths) {
333
- if (p === "/") {
334
- continue;
335
- }
336
- if (p.endsWith("/")) {
337
- throw new MicrofrontendError(
338
- `Invalid path for application "${name}". ${p} must not end with a slash.`,
339
- { type: "application", subtype: "invalid_path" }
340
- );
341
- }
342
- if (!p.startsWith("/")) {
343
- throw new MicrofrontendError(
344
- `Invalid path for application "${name}". ${p} must start with a slash.`,
345
- { type: "application", subtype: "invalid_path" }
346
- );
347
- }
348
- }
349
- }
350
- };
351
- var validateConfigDefaultApplication = (applicationConfigsById) => {
352
- if (!applicationConfigsById) {
353
- return;
354
- }
355
- const applicationsWithoutRouting = Object.entries(
356
- applicationConfigsById
357
- ).filter(([, app]) => isDefaultApp(app));
358
- const numApplicationsWithoutRouting = applicationsWithoutRouting.reduce(
359
- (acc) => {
360
- return acc + 1;
361
- },
362
- 0
363
- );
364
- if (numApplicationsWithoutRouting === 0) {
365
- throw new MicrofrontendError(
366
- "No default application found. At least one application needs to be the default by omitting routing.",
367
- { type: "config", subtype: "no_default_application" }
368
- );
369
- }
370
- if (numApplicationsWithoutRouting > 1) {
371
- const applicationNamesMissingRouting = applicationsWithoutRouting.map(
372
- ([name]) => name
373
- );
374
- throw new MicrofrontendError(
375
- `All applications except for the default app must contain the "routing" field. Applications that are missing routing: ${LIST_FORMATTER.format(applicationNamesMissingRouting)}.`,
376
- { type: "config", subtype: "multiple_default_applications" }
377
- );
378
- }
379
- };
380
- var validateDeprecatedFields = (config) => {
381
- const errors = [];
382
- if (config.options?.vercel) {
383
- errors.push(
384
- `Configuration cannot contain deprecated field 'options.vercel'. Use 'options.disableOverrides' instead.`
385
- );
386
- }
387
- if (config.options?.localProxy) {
388
- errors.push(
389
- `Configuration cannot contain deprecated field 'options.localProxy'. Use 'options.localProxyPort' instead.`
390
- );
391
- }
392
- for (const [applicationId, application] of Object.entries(
393
- config.applications
394
- )) {
395
- if (application.vercel) {
396
- errors.push(
397
- `Application '${applicationId}' cannot contain deprecated field 'vercel'. Use 'projectId' instead.`
398
- );
399
- }
400
- if (application.production) {
401
- errors.push(
402
- `Application '${applicationId}' cannot contain deprecated field 'production'. Use 'development.fallback' instead.`
403
- );
404
- }
405
- if (application.development?.localPort) {
406
- errors.push(
407
- `Application '${applicationId}' cannot contain deprecated field 'development.localPort'. Use 'developement.local' instead.`
408
- );
409
- }
410
- if (application.development?.fallback && typeof application.development.fallback !== "string") {
411
- const fallback = application.development.fallback;
412
- let asString = fallback.host;
413
- if (fallback.protocol) {
414
- asString = `${fallback.protocol}://${asString}`;
415
- }
416
- if (fallback.port) {
417
- asString = `${asString}:${fallback.port}`;
418
- }
419
- errors.push(
420
- `Application '${applicationId}' requires a string (not an object) for the 'development.fallback' field. Please set 'development.fallback' to '${asString}'.`
421
- );
422
- }
423
- if (application.development?.local && typeof application.development.local !== "string" && typeof application.development.local !== "number") {
424
- const local = application.development.local;
425
- let asString;
426
- if (local.port && !local.protocol && !local.host) {
427
- asString = String(local.port);
428
- } else {
429
- asString = local.host ?? "localhost";
430
- if (local.protocol) {
431
- asString = `${local.protocol}://${asString}`;
432
- }
433
- if (local.port) {
434
- asString = `${asString}:${local.port}`;
435
- }
436
- }
437
- errors.push(
438
- `Application '${applicationId}' requires a string or number (not an object) for the 'development.local' field. Please set 'development.local' to '${asString}'.`
439
- );
440
- }
441
- }
442
- if (errors.length) {
443
- throw new MicrofrontendError(
444
- `Microfrontends configuration file errors:
445
- - ${errors.join("\n- ")}`,
446
- {
447
- type: "config",
448
- subtype: "depcrecated_field"
449
- }
450
- );
451
- }
452
- };
453
-
454
- // src/config/microfrontends-config/isomorphic/utils/generate-asset-prefix.ts
455
- var PREFIX = "vc-ap";
456
- function generateAssetPrefixFromName({
457
- name
458
- }) {
459
- if (!name) {
460
- throw new Error("Name is required to generate an asset prefix");
461
- }
462
- return `${PREFIX}-${name}`;
463
- }
464
-
465
- // src/config/microfrontends-config/isomorphic/utils/generate-port.ts
466
- function generatePortFromName({
467
- name,
468
- minPort = 3e3,
469
- maxPort = 8e3
470
- }) {
471
- if (!name) {
472
- throw new Error("Name is required to generate a port");
473
- }
474
- let hash = 0;
475
- for (let i = 0; i < name.length; i++) {
476
- hash = (hash << 5) - hash + name.charCodeAt(i);
477
- hash |= 0;
478
- }
479
- hash = Math.abs(hash);
480
- const range = maxPort - minPort;
481
- const port = minPort + hash % range;
482
- return port;
483
- }
484
-
485
- // src/config/microfrontends-config/isomorphic/host.ts
486
- var Host = class {
487
- constructor(hostConfig, options) {
488
- if (typeof hostConfig === "string") {
489
- ({
490
- protocol: this.protocol,
491
- host: this.host,
492
- port: this.port
493
- } = Host.parseUrl(hostConfig));
494
- } else {
495
- const { protocol = "https", host, port } = hostConfig;
496
- this.protocol = protocol;
497
- this.host = host;
498
- this.port = port;
499
- }
500
- this.local = options?.isLocal;
501
- }
502
- static parseUrl(url, defaultProtocol = "https") {
503
- let hostToParse = url;
504
- if (!/^https?:\/\//.exec(hostToParse)) {
505
- hostToParse = `${defaultProtocol}://${hostToParse}`;
506
- }
507
- const parsed = new URL(hostToParse);
508
- if (!parsed.hostname) {
509
- throw new Error(Host.getMicrofrontendsError(url, "requires a host"));
510
- }
511
- if (parsed.hash) {
512
- throw new Error(
513
- Host.getMicrofrontendsError(url, "cannot have a fragment")
514
- );
515
- }
516
- if (parsed.username || parsed.password) {
517
- throw new Error(
518
- Host.getMicrofrontendsError(
519
- url,
520
- "cannot have authentication credentials (username and/or password)"
521
- )
522
- );
523
- }
524
- if (parsed.pathname !== "/") {
525
- throw new Error(Host.getMicrofrontendsError(url, "cannot have a path"));
526
- }
527
- if (parsed.search) {
528
- throw new Error(
529
- Host.getMicrofrontendsError(url, "cannot have query parameters")
530
- );
531
- }
532
- const protocol = parsed.protocol.slice(0, -1);
533
- return {
534
- protocol,
535
- host: parsed.hostname,
536
- port: parsed.port ? Number.parseInt(parsed.port) : void 0
537
- };
538
- }
539
- static getMicrofrontendsError(url, message) {
540
- return `Microfrontends configuration error: the URL ${url} in your microfrontends.json ${message}.`;
541
- }
542
- isLocal() {
543
- return this.local || this.host === "localhost" || this.host === "127.0.0.1";
544
- }
545
- toString() {
546
- const url = this.toUrl();
547
- return url.toString().replace(/\/$/, "");
548
- }
549
- toUrl() {
550
- const url = `${this.protocol}://${this.host}${this.port ? `:${this.port}` : ""}`;
551
- return new URL(url);
552
- }
553
- };
554
- var LocalHost = class extends Host {
555
- constructor({
556
- appName,
557
- localPort,
558
- local
559
- }) {
560
- if (localPort && local) {
561
- throw new Error(
562
- `Microfrontends configuration error: '${appName}' has both the 'development.local' and 'development.localPort' fields set. Please remove the 'development.localPort' field and ensure the 'development.local' field has the correct port.`
563
- );
564
- }
565
- let protocol;
566
- let host;
567
- let port;
568
- if (localPort) {
569
- port = localPort;
570
- } else if (typeof local === "number") {
571
- port = local;
572
- } else if (typeof local === "string") {
573
- if (/^\d+$/.test(local)) {
574
- port = Number.parseInt(local);
575
- } else {
576
- const parsed = Host.parseUrl(local, "http");
577
- protocol = parsed.protocol;
578
- host = parsed.host;
579
- port = parsed.port;
580
- }
581
- } else if (local) {
582
- protocol = local.protocol;
583
- host = local.host;
584
- port = local.port;
585
- }
586
- super({
587
- protocol: protocol ?? "http",
588
- host: host ?? "localhost",
589
- port: port ?? generatePortFromName({ name: appName })
590
- });
591
- }
592
- };
593
-
594
- // src/config/microfrontends-config/isomorphic/application.ts
595
- var Application = class {
596
- constructor(name, {
597
- app,
598
- overrides,
599
- isDefault
600
- }) {
601
- this.name = name;
602
- this.development = {
603
- local: new LocalHost({
604
- appName: name,
605
- localPort: app.development?.localPort,
606
- local: app.development?.local
607
- }),
608
- fallback: app.development?.fallback ? new Host(app.development.fallback) : void 0
609
- };
610
- if (app.development?.fallback) {
611
- this.fallback = new Host(app.development.fallback);
612
- } else if (app.production) {
613
- this.fallback = new Host(app.production);
614
- }
615
- this.projectId = app.projectId ?? app.vercel?.projectId;
616
- this.packageName = app.packageName;
617
- this.overrides = overrides?.environment ? {
618
- environment: new Host(overrides.environment)
619
- } : void 0;
620
- this.default = isDefault ?? false;
621
- this.serialized = app;
622
- }
623
- isDefault() {
624
- return this.default;
625
- }
626
- getAssetPrefix() {
627
- return generateAssetPrefixFromName({ name: this.name });
628
- }
629
- serialize() {
630
- return this.serialized;
631
- }
632
- };
633
- var DefaultApplication = class extends Application {
634
- constructor(name, {
635
- app,
636
- overrides
637
- }) {
638
- super(name, {
639
- app,
640
- overrides,
641
- isDefault: true
642
- });
643
- this.default = true;
644
- const fallbackHost = app.development?.fallback ?? app.production;
645
- if (fallbackHost === void 0) {
646
- throw new Error(
647
- "`app.production` or `app.development.fallback` must be set in the default application in microfrontends.json."
648
- );
649
- }
650
- this.fallback = new Host(fallbackHost);
651
- if (app.production) {
652
- this.production = new Host(app.production);
653
- }
654
- }
655
- getAssetPrefix() {
656
- return "";
657
- }
658
- };
659
- var ChildApplication = class extends Application {
660
- constructor(name, {
661
- app,
662
- overrides
663
- }) {
664
- ChildApplication.validate(name, app);
665
- super(name, {
666
- app,
667
- overrides,
668
- isDefault: false
669
- });
670
- this.default = false;
671
- this.routing = app.routing;
672
- }
673
- static validate(name, app) {
674
- validateAppPaths(name, app);
675
- }
676
- };
677
-
678
- // src/config/microfrontends-config/isomorphic/constants.ts
679
- var DEFAULT_LOCAL_PROXY_PORT = 3024;
680
-
681
- // src/config/microfrontends-config/isomorphic/index.ts
682
- var MicrofrontendConfigIsomorphic = class {
683
- constructor({
684
- config,
685
- overrides,
686
- meta,
687
- opts
688
- }) {
689
- this.childApplications = {};
690
- MicrofrontendConfigIsomorphic.validate(config, opts);
691
- const disableOverrides = config.options?.disableOverrides ?? config.options?.vercel?.disableOverrides ?? false;
692
- this.overrides = overrides && !disableOverrides ? overrides : void 0;
693
- this.isMainConfig = isMainConfig(config);
694
- if (isMainConfig(config)) {
695
- for (const [appId, appConfig] of Object.entries(config.applications)) {
696
- const appOverrides = !disableOverrides ? this.overrides?.applications[appId] : void 0;
697
- if (isDefaultApp(appConfig)) {
698
- this.defaultApplication = new DefaultApplication(appId, {
699
- app: appConfig,
700
- overrides: appOverrides
701
- });
702
- } else {
703
- this.childApplications[appId] = new ChildApplication(appId, {
704
- app: appConfig,
705
- overrides: appOverrides
706
- });
707
- }
708
- }
709
- } else {
710
- this.partOf = config.partOf;
711
- const appOverrides = !disableOverrides ? this.overrides?.applications[meta.fromApp] : void 0;
712
- this.childApplications[meta.fromApp] = new ChildApplication(
713
- meta.fromApp,
714
- {
715
- // we don't know routing because we're not in the main config
716
- app: { routing: [] },
717
- overrides: appOverrides
718
- }
719
- );
720
- }
721
- if (isMainConfig(config) && !this.defaultApplication) {
722
- throw new MicrofrontendError(
723
- "Could not find default application in microfrontends configuration",
724
- {
725
- type: "application",
726
- subtype: "not_found"
727
- }
728
- );
729
- }
730
- this.config = config;
731
- this.options = config.options;
732
- this.serialized = {
733
- config,
734
- overrides,
735
- meta
736
- };
737
- }
738
- static validate(config, opts) {
739
- const skipValidation = opts?.skipValidation ?? [];
740
- const c = typeof config === "string" ? parse(config) : config;
741
- if (isMainConfig(c)) {
742
- validateConfigPaths(c.applications);
743
- validateConfigDefaultApplication(c.applications);
744
- if (!skipValidation.includes("deprecatedFields")) {
745
- validateDeprecatedFields(c);
746
- }
747
- }
748
- return c;
749
- }
750
- static fromEnv({
751
- meta,
752
- cookies
753
- }) {
754
- return new MicrofrontendConfigIsomorphic({
755
- config: parse(getConfigStringFromEnv()),
756
- overrides: parseOverrides(cookies ?? []),
757
- meta
758
- });
759
- }
760
- isOverridesDisabled() {
761
- return this.options?.vercel?.disableOverrides ?? false;
762
- }
763
- getConfig() {
764
- return this.config;
765
- }
766
- getApplicationsByType() {
767
- return {
768
- defaultApplication: this.defaultApplication,
769
- applications: Object.values(this.childApplications)
770
- };
771
- }
772
- getChildApplications() {
773
- return Object.values(this.childApplications);
774
- }
775
- getAllApplications() {
776
- return [
777
- this.defaultApplication,
778
- ...Object.values(this.childApplications)
779
- ].filter(Boolean);
780
- }
781
- getApplication(name) {
782
- if (this.defaultApplication?.name === name || this.defaultApplication?.packageName === name) {
783
- return this.defaultApplication;
784
- }
785
- const app = this.childApplications[name] || Object.values(this.childApplications).find(
786
- (child) => child.packageName === name
787
- );
788
- if (!app) {
789
- throw new MicrofrontendError(
790
- `Could not find microfrontends configuration for application "${name}"`,
791
- {
792
- type: "application",
793
- subtype: "not_found"
794
- }
795
- );
796
- }
797
- return app;
798
- }
799
- getApplicationByProjectId(projectId) {
800
- if (this.defaultApplication?.projectId === projectId) {
801
- return this.defaultApplication;
802
- }
803
- return Object.values(this.childApplications).find(
804
- (app) => app.projectId === projectId
805
- );
806
- }
807
- /**
808
- * Returns the default application. This can throw if the default application
809
- * is undefined ( )
810
- */
811
- getDefaultApplication() {
812
- if (!this.defaultApplication) {
813
- throw new MicrofrontendError(
814
- "Could not find default application in microfrontends configuration",
815
- {
816
- type: "application",
817
- subtype: "not_found"
818
- }
819
- );
820
- }
821
- return this.defaultApplication;
822
- }
823
- /**
824
- * Returns the configured port for the local proxy
825
- */
826
- getLocalProxyPort() {
827
- return this.config.options?.localProxyPort ?? this.config.options?.localProxy?.port ?? DEFAULT_LOCAL_PROXY_PORT;
828
- }
829
- /**
830
- * Serializes the class back to the Schema type.
831
- *
832
- * NOTE: This is used when writing the config to disk and must always match the input Schema
833
- */
834
- toSchemaJson() {
835
- return this.serialized.config;
836
- }
837
- toClientConfig() {
838
- const applications = Object.fromEntries(
839
- Object.entries(this.childApplications).map(([name, application]) => [
840
- name,
841
- {
842
- default: false,
843
- routing: application.routing
844
- }
845
- ])
846
- );
847
- if (this.defaultApplication) {
848
- applications[this.defaultApplication.name] = {
849
- default: true
850
- };
851
- }
852
- return new MicrofrontendConfigClient({
853
- applications
854
- });
855
- }
856
- serialize() {
857
- return this.serialized;
858
- }
859
- };
860
-
861
- // src/config/microfrontends-config/isomorphic/child.ts
862
- var MicrofrontendChildConfig = class extends MicrofrontendConfigIsomorphic {
863
- constructor({
864
- config,
865
- overrides,
866
- meta
867
- }) {
868
- super({ config, overrides, meta });
869
- this.isMainConfig = false;
870
- this.partOf = config.partOf;
871
- }
872
- };
873
-
874
- // src/config/microfrontends-config/isomorphic/main.ts
875
- var MicrofrontendMainConfig = class extends MicrofrontendConfigIsomorphic {
876
- constructor({
877
- config,
878
- overrides,
879
- meta
880
- }) {
881
- super({ config, overrides, meta });
882
- this.isMainConfig = true;
883
- const disableOverrides = config.options?.disableOverrides ?? config.options?.vercel?.disableOverrides ?? false;
884
- let defaultApplication;
885
- for (const [appId, appConfig] of Object.entries(config.applications)) {
886
- const appOverrides = !disableOverrides ? this.overrides?.applications[appId] : void 0;
887
- if (isDefaultApp(appConfig)) {
888
- defaultApplication = new DefaultApplication(appId, {
889
- app: appConfig,
890
- overrides: appOverrides
891
- });
892
- } else {
893
- this.childApplications[appId] = new ChildApplication(appId, {
894
- app: appConfig,
895
- overrides: appOverrides
896
- });
897
- }
898
- }
899
- if (!defaultApplication) {
900
- throw new MicrofrontendError(
901
- "Could not find default application in microfrontends configuration",
902
- {
903
- type: "application",
904
- subtype: "not_found"
905
- }
906
- );
907
- }
908
- this.defaultApplication = defaultApplication;
909
- }
910
- };
911
-
912
- // src/config/microfrontends/isomorphic/index.ts
913
- var Microfrontends = class {
914
- constructor({
915
- config,
916
- overrides,
917
- meta
918
- }) {
919
- if (isMainConfig(config)) {
920
- this.config = new MicrofrontendMainConfig({ config, overrides, meta });
921
- } else {
922
- this.config = new MicrofrontendChildConfig({ config, overrides, meta });
923
- }
924
- }
925
- isChildConfig() {
926
- return this.config instanceof MicrofrontendChildConfig;
927
- }
928
- static fromEnv({
929
- cookies,
930
- meta
931
- }) {
932
- const config = MicrofrontendConfigIsomorphic.fromEnv({
933
- cookies,
934
- meta
935
- });
936
- return new Microfrontends(config.serialize());
937
- }
938
- };
939
- export {
940
- Microfrontends
941
- };
942
- //# sourceMappingURL=microfrontends.js.map