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