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